<?php
// config.php फाइल को शामिल करें, यह सेशन शुरू करेगी और डेटाबेस कनेक्शन ($conn) बनाएगी।
require_once 'config.php';

// सुरक्षा जांच (Security Check) - मल्टी-रोल सिस्टम के लिए अपडेट किया गया
// यह फंक्शन जांच करेगा कि यूजर के पास 'Admin' या 'Manager' रोल है या नहीं।
function userHasRole($required_roles) {
    if (!isset($_SESSION['user_roles'])) return false;
    // array_intersect दो ऐरे के बीच समान वैल्यूज को ढूंढता है।
    return count(array_intersect($required_roles, $_SESSION['user_roles'])) > 0;
}

if (!isset($_SESSION['user_id']) || !userHasRole(['Admin', 'Manager'])) {
    header("Location: dashboard.php");
    exit();
}

$report_id = $_GET['id'] ?? null;
if (!$report_id) {
    header("Location: reports.php");
    exit();
}

$message = '';
$message_type = '';

// --- UPDATE LOGIC ---
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $attendance = $_POST['attendance'];
    $total_assigned = $_POST['total_assigned_tasks'];
    $total_done = $_POST['total_done_tasks'];
    $additional_assigned = $_POST['additional_assigned_tasks'];
    $additional_done = $_POST['additional_done_tasks'];
    $pending_yesterday = $_POST['pending_tasks_yesterday'];

    $stmt = $conn->prepare("UPDATE daily_reports SET attendance = ?, total_assigned_tasks = ?, total_done_tasks = ?, additional_assigned_tasks = ?, additional_done_tasks = ?, pending_tasks_yesterday = ? WHERE id = ?");
    $stmt->bind_param("siiiiii", $attendance, $total_assigned, $total_done, $additional_assigned, $additional_done, $pending_yesterday, $report_id);
    
    if ($stmt->execute()) {
        $message = "Report updated successfully!";
        $message_type = 'success';
    } else {
        $message = "Error updating report: " . $stmt->error;
        $message_type = 'error';
    }
    $stmt->close();
}

// --- FETCH REPORT DATA ---
$stmt = $conn->prepare("SELECT dr.*, u.name FROM daily_reports dr JOIN users u ON dr.user_id = u.id WHERE dr.id = ?");
$stmt->bind_param("i", $report_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 0) {
    header("Location: reports.php");
    exit();
}
$report = $result->fetch_assoc();
$stmt->close();
$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Report - Digify OS</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100">
    <div class="flex">
        <div class="w-64 bg-gray-800 text-white min-h-screen p-4">
            <h1 class="text-2xl font-bold mb-10">Digify OS</h1>
            <nav>
                <a href="dashboard.php" class="block py-2.5 px-4 rounded hover:bg-gray-700">Dashboard</a>
                <a href="employees.php" class="block py-2.5 px-4 rounded hover:bg-gray-700">Employees</a>
                <a href="reports.php" class="block py-2.5 px-4 rounded bg-gray-700">Reports</a>
                <a href="logout.php" class="block py-2.5 px-4 rounded hover:bg-gray-700">Logout</a>
            </nav>
        </div>

        <div class="flex-1 p-10">
            <h2 class="text-3xl font-bold mb-4">Edit Daily Report</h2>
            <p class="mb-8">Editing report for <strong><?php echo htmlspecialchars($report['name']); ?></strong> on <strong><?php echo date("F j, Y", strtotime($report['report_date'])); ?></strong></p>

            <div class="max-w-2xl bg-white p-8 rounded-lg shadow">
                 <?php if (!empty($message)): ?>
                    <div class="mb-4 p-4 rounded <?php echo ($message_type == 'success') ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'; ?>">
                        <?php echo $message; ?>
                    </div>
                <?php endif; ?>
                
                <form method="POST" action="edit-report.php?id=<?php echo $report_id; ?>">
                    <div class="mb-4">
                        <label for="attendance" class="block text-sm font-medium text-gray-700">Attendance</label>
                        <select name="attendance" id="attendance" required class="mt-1 block w-full px-3 py-2 border bg-white border-gray-300 rounded-md shadow-sm">
                            <option value="Present" <?php if($report['attendance'] == 'Present') echo 'selected'; ?>>Present</option>
                            <option value="CL" <?php if($report['attendance'] == 'CL') echo 'selected'; ?>>CL</option>
                            <option value="Absent" <?php if($report['attendance'] == 'Absent') echo 'selected'; ?>>Absent</option>
                        </select>
                    </div>
                    
                    <div class="grid grid-cols-2 gap-6">
                        <div>
                            <label class="block text-sm font-medium text-gray-700">Total Assigned Tasks</label>
                            <input type="number" name="total_assigned_tasks" value="<?php echo htmlspecialchars($report['total_assigned_tasks']); ?>" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700">Total Done Tasks</label>
                            <input type="number" name="total_done_tasks" value="<?php echo htmlspecialchars($report['total_done_tasks']); ?>" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700">Additional Assigned</label>
                            <input type="number" name="additional_assigned_tasks" value="<?php echo htmlspecialchars($report['additional_assigned_tasks']); ?>" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700">Additional Done</label>
                            <input type="number" name="additional_done_tasks" value="<?php echo htmlspecialchars($report['additional_done_tasks']); ?>" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700">Pending from Yesterday</label>
                            <input type="number" name="pending_tasks_yesterday" value="<?php echo htmlspecialchars($report['pending_tasks_yesterday']); ?>" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm">
                        </div>
                    </div>

                    <div class="flex justify-end mt-6">
                        <a href="reports.php?date=<?php echo $report['report_date']; ?>" class="px-4 py-2 border border-gray-300 rounded-md text-sm font-medium text-gray-700 hover:bg-gray-50">Cancel</a>
                        <button type="submit" class="ml-3 inline-flex items-center px-4 py-2 bg-blue-600 border border-transparent rounded-md font-semibold text-white hover:bg-blue-700">
                            Update Report
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>