<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
session_write_close();
require '../config.php';

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

$card_id = (int)($_GET['id'] ?? 0);
$card_res = $conn->query("SELECT * FROM id_cards WHERE id = {$card_id}");
$card = $card_res ? $card_res->fetch_assoc() : null;

if (!$card) {
    header("Location: index.php");
    exit;
}

$emp_res = $conn->query("SELECT id, employee_no, name FROM employees ORDER BY name ASC");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit ID Card - HR System</title>
    <link href="https://fonts.googleapis.com/css2?family=Mulish:wght@400;600;700;800;900&display=swap" rel="stylesheet">
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="../components/sidebar.css">
</head>
<body class="bg-stone-100/60 min-h-screen font-sans text-slate-800 antialiased">
    <div class="flex">
        <?php include '../components/sidebar.php'; ?>
        <div id="main-content" class="flex-1 md:pl-60 p-6 max-w-2xl mx-auto space-y-4">
            <h2 class="text-base font-black text-slate-900 uppercase">Edit Card Metadata</h2>
            <form action="save.php" method="POST" class="bg-white p-6 rounded-xl border border-slate-300 space-y-4 shadow-sm">
                <input type="hidden" name="card_id" value="<?= $card['id']; ?>">
                
                <div>
                    <label class="block text-[10px] font-bold uppercase text-slate-500 mb-1">Assigned Employee</label>
                    <select name="employee_id" class="w-full border border-slate-300 rounded-lg p-2 text-xs font-bold">
                        <?php while ($e = $emp_res->fetch_assoc()): ?>
                            <option value="<?= $e['id'] ?>" <?= $card['employee_id'] == $e['id'] ? 'selected' : '' ?>>
                                <?= htmlspecialchars($e['name']) ?> (<?= htmlspecialchars($e['employee_no']) ?>)
                            </option>
                        <?php endwhile; ?>
                    </select>
                </div>

                <div>
                    <label class="block text-[10px] font-bold uppercase text-slate-500 mb-1">Card Serial Number</label>
                    <input type="text" name="card_number" value="<?= htmlspecialchars($card['card_number']); ?>" required class="w-full border border-slate-300 rounded-lg p-2 text-xs font-mono font-bold">
                </div>

                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="block text-[10px] font-bold uppercase text-slate-500 mb-1">Issue Date</label>
                        <input type="date" name="issue_date" value="<?= $card['issue_date']; ?>" class="w-full border border-slate-300 rounded-lg p-2 text-xs font-bold">
                    </div>
                    <div>
                        <label class="block text-[10px] font-bold uppercase text-slate-500 mb-1">Expiry Date</label>
                        <input type="date" name="expiry_date" value="<?= $card['expiry_date']; ?>" class="w-full border border-slate-300 rounded-lg p-2 text-xs font-bold">
                    </div>
                </div>

                <div>
                    <label class="block text-[10px] font-bold uppercase text-slate-500 mb-1">Status</label>
                    <select name="status" class="w-full border border-slate-300 rounded-lg p-2 text-xs font-bold">
                        <option value="Active" <?= $card['status'] === 'Active' ? 'selected' : '' ?>>Active</option>
                        <option value="Suspended" <?= $card['status'] === 'Suspended' ? 'selected' : '' ?>>Suspended</option>
                        <option value="Expired" <?= $card['status'] === 'Expired' ? 'selected' : '' ?>>Expired</option>
                    </select>
                </div>

                <div class="flex justify-end gap-2 pt-2">
                    <a href="index.php" class="px-4 py-2 bg-stone-100 text-xs font-bold rounded-lg">Cancel</a>
                    <button type="submit" class="px-4 py-2 bg-indigo-600 text-white text-xs font-bold rounded-lg">Update Card</button>
                </div>
            </form>
        </div>
    </div>
</body>
</html>