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

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

$user_id = $_SESSION['user_id'];
$project_id = (int)($_GET['project_id'] ?? 0);
if ($project_id <= 0) die("Invalid project");

$stmt = $conn->prepare("SELECT project_name FROM project_master 
LEFT JOIN project_managers ON project_master.id = project_managers.project_id 
WHERE  project_managers.project_id = ? AND project_managers.user_id = ?");
$stmt->bind_param("ii", $project_id, $user_id);
$stmt->execute();
$project = $stmt->get_result()->fetch_assoc();
if (!$project) die("Project not found or access denied");

$success = $error = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $report_date = $_POST['report_date'] ?? date('Y-m-d');
    $weather = trim($_POST['weather'] ?? '');
    $manpower = (int)($_POST['manpower'] ?? 0);
    $work_done = trim($_POST['work_done'] ?? '');
    $material_used = trim($_POST['material_used'] ?? '');
    $remarks = trim($_POST['remarks'] ?? '');

    if (empty($work_done)) {
        $error = "Work description is required.";
    } else {
        $q = "INSERT INTO daily_progress_report 
              (project_id, project_manager_id, report_date, weather_condition, manpower_count, 
               work_description, material_consumed_summary, remarks, status, created_at) 
              VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'Pending', NOW())";
        $stmt = $conn->prepare($q);
        $stmt->bind_param("iississs", $project_id, $user_id, $report_date, $weather, $manpower, $work_done, $material_used, $remarks);
        
        if ($stmt->execute()) {
            $success = "DPR added successfully!";
        } else {
            $error = "Error: " . $stmt->error;
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add DPR - <?= htmlspecialchars($project['project_name']) ?></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="project_dashboard.php?id=<?= $project_id ?>" class="back">
            <i class="fas fa-arrow-left"></i> Back to Project Dashboard
        </a>

        <h1>Add Daily Progress Report</h1>
        <p>Project: <?= htmlspecialchars($project['project_name']) ?></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="<?= date('Y-m-d') ?>" required>
                </div>

                <div class="form-group">
                    <label>Weather Condition</label>
                    <select name="weather">
                        <option value="">Select</option>
                        <option value="Sunny">Sunny</option>
                        <option value="Cloudy">Cloudy</option>
                        <option value="Rainy">Rainy</option>
                        <option value="Stormy">Stormy</option>
                    </select>
                </div>

                <div class="form-group">
                    <label>Manpower Deployed</label>
                    <input type="number" name="manpower" min="0" value="0">
                </div>

                <div class="form-group">
                    <label>Work Done / Progress Today *</label>
                    <textarea name="work_done" required placeholder="Describe today's work..."></textarea>
                </div>

                <div class="form-group">
                    <label>Materials Used Today</label>
                    <textarea name="material_used" placeholder="Cement: 20 bags, Sand: 5 brass..."></textarea>
                </div>

                <div class="form-group">
                    <label>Remarks / Issues</label>
                    <textarea name="remarks" placeholder="Any delays, problems..."></textarea>
                </div>

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