<?php
session_start();
require_once("../config/config.php");

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'project manager') {
    header("Location: ../login.php");
    exit();
}

$dpr_id = (int)($_GET['id'] ?? 0);
if ($dpr_id <= 0) die("Invalid DPR");

$user_id = $_SESSION['user_id'];

// Fetch DPR + verify ownership & pending status
$stmt = $conn->prepare("
    SELECT d.*, p.project_name, p.project_code 
    FROM daily_progress_report d
    JOIN project_master p ON d.project_id = p.id
    WHERE d.id = ? AND d.project_manager_id = ? AND d.status = 'Pending'
");
$stmt->bind_param("ii", $dpr_id, $user_id);
$stmt->execute();
$dpr = $stmt->get_result()->fetch_assoc();

if (!$dpr) die("DPR not found, not pending, or access denied");

$success = $error = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $report_date = $_POST['report_date'] ?? $dpr['report_date'];
    $weather = trim($_POST['weather'] ?? $dpr['weather_condition']);
    $manpower = (int)($_POST['manpower'] ?? $dpr['manpower_count']);
    $work_done = trim($_POST['work_done'] ?? $dpr['work_description']);
    $material_used = trim($_POST['material_used'] ?? $dpr['material_consumed_summary']);
    $remarks = trim($_POST['remarks'] ?? $dpr['remarks']);

    if (empty($work_done)) {
        $error = "Work description is required.";
    } else {
        $q = "UPDATE daily_progress_report SET 
              report_date = ?, weather_condition = ?, manpower_count = ?,
              work_description = ?, material_consumed_summary = ?, remarks = ?, updated_at = NOW()
              WHERE id = ? AND project_manager_id = ? AND status = 'Pending'";
        $stmt = $conn->prepare($q);
        $stmt->bind_param("ssisssii", $report_date, $weather, $manpower, $work_done, $material_used, $remarks, $dpr_id, $user_id);
        
        if ($stmt->execute()) {
            $success = "DPR updated successfully!";
            header("Location: dpr_view.php?id=$dpr_id");
            exit();
        } else {
            $error = "Update failed: " . $stmt->error;
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit DPR</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        :root {
            --primary: #8b5cf6;
            --success: #22c55e;
            --danger: #ef4444;
            --glass: rgba(255,255,255,0.08);
            --border: rgba(255,255,255,0.15);
        }
        body {
            font-family: 'Segoe UI', sans-serif;
            background: linear-gradient(rgba(0,0,0,0.8), rgba(0,0,0,0.95)),
                        url('../background.jpeg') center/cover fixed;
            color: white;
            margin: 0;
            padding: 20px;
        }
        .container { max-width: 900px; margin: 0 auto; }
        .back { color: #c4b5fd; text-decoration: none; display: inline-flex; align-items: center; gap: 8px; margin-bottom: 20px; }
        .back:hover { color: white; }
        h1 { margin: 0 0 10px; }
        .form-area {
            background: var(--glass);
            backdrop-filter: blur(12px);
            border: 1px solid var(--border);
            border-radius: 16px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.4);
        }
        .form-group { margin-bottom: 24px; }
        label { display: block; margin-bottom: 8px; font-weight: 600; }
        input, select, textarea {
            width: 100%;
            padding: 14px;
            background: rgba(255,255,255,0.08);
            border: 1px solid rgba(255,255,255,0.2);
            border-radius: 10px;
            color: white;
        }
        textarea { min-height: 140px; }
        .submit-btn {
            background: linear-gradient(135deg, var(--primary), #a78bfa);
            color: white;
            border: none;
            padding: 16px;
            border-radius: 12px;
            font-size: 1.1rem;
            font-weight: 600;
            cursor: pointer;
            width: 100%;
        }
        .message { padding: 16px; border-radius: 10px; margin-bottom: 20px; }
        .success { background: rgba(34,197,94,0.25); color: #86efac; }
        .error { background: rgba(239,68,68,0.25); color: #fca5a5; }
    </style>
</head>
<body>
    <div class="container">
        <a href="dpr_view.php?id=<?= $dpr_id ?>" class="back">
            <i class="fas fa-arrow-left"></i> Back to View
        </a>

        <h1>Edit DPR</h1>
        <p>Project: <?= htmlspecialchars($dpr['project_name']) ?> (<?= htmlspecialchars($dpr['project_code']) ?>)</p>

        <?php if ($success): ?><div class="message success"><?= $success ?></div><?php endif; ?>
        <?php if ($error): ?><div class="message error"><?= $error ?></div><?php endif; ?>

        <div class="form-area">
            <form method="post">
                <div class="form-group">
                    <label>Report Date *</label>
                    <input type="date" name="report_date" value="<?= htmlspecialchars($dpr['report_date']) ?>" required>
                </div>

                <div class="form-group">
                    <label>Weather Condition</label>
                    <select name="weather">
                        <option value="">Select</option>
                        <option value="Sunny" <?= $dpr['weather_condition'] === 'Sunny' ? 'selected' : '' ?>>Sunny</option>
                        <option value="Cloudy" <?= $dpr['weather_condition'] === 'Cloudy' ? 'selected' : '' ?>>Cloudy</option>
                        <option value="Rainy" <?= $dpr['weather_condition'] === 'Rainy' ? 'selected' : '' ?>>Rainy</option>
                        <option value="Stormy" <?= $dpr['weather_condition'] === 'Stormy' ? 'selected' : '' ?>>Stormy</option>
                    </select>
                </div>

                <div class="form-group">
                    <label>Manpower Deployed</label>
                    <input type="number" name="manpower" min="0" value="<?= htmlspecialchars($dpr['manpower_count']) ?>">
                </div>

                <div class="form-group">
                    <label>Work Done / Progress Today *</label>
                    <textarea name="work_done" required><?= htmlspecialchars($dpr['work_description']) ?></textarea>
                </div>

                <div class="form-group">
                    <label>Materials Used Today</label>
                    <textarea name="material_used"><?= htmlspecialchars($dpr['material_consumed_summary']) ?></textarea>
                </div>

                <div class="form-group">
                    <label>Remarks / Issues</label>
                    <textarea name="remarks"><?= htmlspecialchars($dpr['remarks']) ?></textarea>
                </div>

                <button type="submit" class="submit-btn">Update DPR</button>
            </form>
        </div>
    </div>
</body>
</html>