<?php
session_start();

// ✅ Connect to SQLite database
$db = new PDO('sqlite:ndu_seminar_database.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Connect to SQLite
$db = new PDO('sqlite:database.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Validate and get table name
if (!isset($_GET['table']) || empty($_GET['table'])) {
    die("No table specified.");
}
$tableName = $_GET['table'];

// Check table existence
$stmt = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name='$tableName'");
if (!$stmt->fetch()) {
    die("Table '$tableName' does not exist.");
}

// Format table name according to your rules
function formatTableName($name)
{
    // Remove leading PRO if present
    if (substr($name, 0, 3) === 'PRO') {
        $name = substr($name, 3);
    }

    $formatted = '';
    $len = strlen($name);

    for ($i = 0; $i < $len; $i++) {
        $char = $name[$i];
        $prevChar = $i > 0 ? $name[$i - 1] : '';

        if ($i > 0) {
            // Space if lowercase followed by uppercase
            if (ctype_lower($prevChar) && ctype_upper($char)) {
                $formatted .= ' ';
            }
            // Space if consecutive uppercase but next is lowercase (POPBed -> P O P Bed)
            elseif (ctype_upper($prevChar) && ctype_upper($char) && ($i + 1 < $len && ctype_lower($name[$i + 1]))) {
                $formatted .= ' ';
            }
        }
        $formatted .= $char;
    }
    return $formatted;
}

$displayTableName = formatTableName($tableName);
$notification = $_SESSION['notification'] ?? "";
unset($_SESSION['notification']);


// Handle Update
if (isset($_POST['update'])) {
    $id = $_POST['id'];
    $activity = $_POST['activity'];
    $alot_mark = $_POST['alot_mark'];
    $stmt = $db->prepare("UPDATE '$tableName' SET activity = ?, alot_mark = ? WHERE id = ?");
    $stmt->execute([$activity, $alot_mark, $id]);
    $notification = "Row updated successfully!";
}

// Handle Delete
if (isset($_POST['delete'])) {
    $id = $_POST['id'];
    $stmt = $db->prepare("DELETE FROM '$tableName' WHERE id = ?");
    $stmt->execute([$id]);
    $notification = "Row deleted successfully!";
}

// Handle Delete All
if (isset($_POST['delete_all'])) {
    $confirm = $_POST['confirm_delete_all'] ?? '';
    if ($confirm === 'yes') {
        $db->exec("DELETE FROM '$tableName'");
        $notification = "All items deleted successfully!";
    }
}

// Handle Insert
if (isset($_POST['add'])) {
    $activity = $_POST['activity'];
    $alot_mark = $_POST['alot_mark'];
    $position_id = $_POST['position_id'] ?? null;

    // Reorder: shift IDs after position_id
    if ($position_id) {
        $rows = $db->query("SELECT * FROM '$tableName' WHERE id > $position_id ORDER BY id DESC")->fetchAll(PDO::FETCH_ASSOC);
        foreach ($rows as $r) {
            $new_id = $r['id'] + 1;
            $db->exec("UPDATE '$tableName' SET id = $new_id WHERE id = {$r['id']}");
        }
    }

    // Insert new record
    $stmt = $db->prepare("INSERT INTO '$tableName' (activity, alot_mark) VALUES (?, ?)");
    $stmt->execute([$activity, $alot_mark]);
    $notification = " New row added successfully!";
}

// Fetch updated table
$records = $db->query("SELECT * FROM '$tableName' ORDER BY id ASC")->fetchAll(PDO::FETCH_ASSOC);

// Get current page name for active navigation
$current_page = basename($_SERVER['PHP_SELF']);
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CAOSCE · Network</title>
    <link rel="icon" type="image/x-icon" href="CAOSCE Network.ico">
    <!-- Font Awesome 6 (Free) -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        /* ----- RESET & BASE ----- */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f0f4fb;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            color: #10233f;
        }

        /* ----- NAVIGATION (SOLID, NON-TRANSPARENT) ----- */
        .navbar {
            position: sticky;
            top: 0;
            z-index: 60;
            background: #ffffff;
            border-bottom: 2px solid #e2eaf2;
            padding: 0 5vw;
            height: 74px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 2px 12px rgba(16, 35, 63, 0.06);
        }

        .brand {
            display: flex;
            align-items: center;
            gap: 12px;
            font-weight: 800;
            font-size: 20px;
            color: #10233f;
            text-decoration: none;
        }

        .brand-icon {
            width: 38px;
            height: 38px;
            background: linear-gradient(135deg, #1a6cf0, #6d5dfc);
            border-radius: 12px;
            display: grid;
            place-items: center;
            color: white;
            font-size: 18px;
            font-weight: 900;
            box-shadow: 0 8px 18px rgba(26, 108, 240, 0.25);
        }

        .brand-light {
            font-weight: 500;
            color: #4f6580;
        }

        .nav-links {
            display: flex;
            align-items: center;
            gap: 28px;
        }

        .nav-links a {
            font-size: 14px;
            font-weight: 600;
            color: #4f6580;
            text-decoration: none;
            transition: 0.2s;
            position: relative;
            padding: 6px 0;
        }

        .nav-links a i {
            margin-right: 6px;
            font-size: 13px;
        }

        .nav-links a:hover {
            color: #1a6cf0;
        }

        .nav-links a.active {
            color: #1a6cf0;
        }

        .nav-links a.active::after {
            content: '';
            position: absolute;
            bottom: -2px;
            left: 0;
            width: 100%;
            height: 3px;
            background: #1a6cf0;
            border-radius: 4px;
        }

        .nav-logout {
            background: #f0f4fb;
            border: 1px solid #d9e2eb;
            border-radius: 30px;
            padding: 6px 18px;
            font-weight: 600;
            font-size: 13px;
            color: #10233f;
            transition: 0.2s;
        }

        .nav-logout:hover {
            background: #e2eaf2;
            border-color: #b0c4d8;
            color: #1a6cf0;
        }

        /* ----- MAIN CONTAINER ----- */
        main {
            flex: 1;
            padding: 30px 5vw;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        /* ----- PAGE HEADER ----- */
        .page-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding: 20px 28px;
            background: white;
            border-radius: 16px;
            box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
            border: 1px solid #eef4fa;
            flex-wrap: wrap;
            gap: 15px;
        }

        .page-header h1 {
            color: #10233f;
            font-weight: 700;
            font-size: 1.8rem;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .page-header h1 i {
            color: #1a6cf0;
        }

        .page-header .subtitle {
            font-size: 14px;
            color: #6b7a8f;
            margin-top: 4px;
        }

        .header-actions {
            display: flex;
            gap: 12px;
            flex-wrap: wrap;
        }

        .btn {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 10px 22px;
            border: none;
            border-radius: 30px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            font-size: 14px;
        }

        .btn-back {
            background: #e9ecef;
            color: #4f6580;
        }

        .btn-back:hover {
            background: #dde1e6;
            transform: translateY(-2px);
        }

        .btn-danger {
            background: #dc3545;
            color: white;
        }

        .btn-danger:hover {
            background: #c82333;
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(220, 53, 69, 0.25);
        }

        .btn-primary {
            background: linear-gradient(135deg, #1a6cf0, #6d5dfc);
            color: white;
            box-shadow: 0 4px 14px rgba(26, 108, 240, 0.25);
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 24px rgba(26, 108, 240, 0.35);
        }

        .btn-success {
            background: linear-gradient(135deg, #28a745, #20c997);
            color: white;
        }

        .btn-success:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 24px rgba(40, 167, 69, 0.3);
        }

        .btn-sm {
            padding: 6px 14px;
            font-size: 12px;
            border-radius: 20px;
        }

        /* ----- CARD ----- */
        .card {
            background: white;
            border-radius: 16px;
            box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
            padding: 25px 28px;
            margin-bottom: 25px;
            border: 1px solid #eef4fa;
            transition: 0.3s;
        }

        .card:hover {
            box-shadow: 0 8px 28px rgba(0, 0, 0, 0.08);
        }

        .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f4fb;
            flex-wrap: wrap;
            gap: 10px;
        }

        .card-title {
            font-size: 1.3rem;
            font-weight: 600;
            color: #10233f;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .card-title i {
            color: #1a6cf0;
        }

        .badge {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 5px 14px;
            background: #1a6cf0;
            color: white;
            border-radius: 20px;
            font-size: 0.8rem;
            font-weight: 600;
        }

        .badge-warning {
            background: #f39c12;
        }

        .badge-success {
            background: #28a745;
        }

        /* ----- TABLE ----- */
        .table-wrapper {
            overflow-x: auto;
            border-radius: 12px;
            border: 1px solid #eef4fa;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 14px;
        }

        th {
            background: #f8fafc;
            color: #10233f;
            font-weight: 700;
            padding: 13px 18px;
            text-align: left;
            border-bottom: 2px solid #e2eaf2;
            font-size: 12px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        td {
            padding: 12px 18px;
            border-bottom: 1px solid #eef4fa;
            vertical-align: middle;
        }

        tr:last-child td {
            border-bottom: none;
        }

        tr:hover td {
            background: #f8fafc;
        }

        .form-input {
            width: 100%;
            padding: 10px 14px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 14px;
            transition: 0.3s;
            background: white;
        }

        .form-input:focus {
            outline: none;
            border-color: #1a6cf0;
            box-shadow: 0 0 0 4px rgba(26, 108, 240, 0.12);
        }

        .form-select {
            width: 100%;
            padding: 10px 14px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 14px;
            background: white;
            transition: 0.3s;
            cursor: pointer;
        }

        .form-select:focus {
            outline: none;
            border-color: #1a6cf0;
            box-shadow: 0 0 0 4px rgba(26, 108, 240, 0.12);
        }

        .action-buttons {
            display: flex;
            gap: 6px;
            justify-content: center;
            flex-wrap: wrap;
        }

        .btn-icon {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 36px;
            height: 36px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            transition: 0.3s;
            font-size: 14px;
            color: white;
        }

        .btn-icon:hover {
            transform: translateY(-2px);
        }

        .btn-save {
            background: #28a745;
        }

        .btn-save:hover {
            background: #1e7e34;
            box-shadow: 0 4px 12px rgba(40, 167, 69, 0.3);
        }

        .btn-delete {
            background: #dc3545;
        }

        .btn-delete:hover {
            background: #b91c1c;
            box-shadow: 0 4px 12px rgba(220, 53, 69, 0.3);
        }

        .btn-add {
            background: #1a6cf0;
        }

        .btn-add:hover {
            background: #1057c0;
            box-shadow: 0 4px 12px rgba(26, 108, 240, 0.3);
        }

        /* ----- EMPTY STATE ----- */
        .empty-state {
            text-align: center;
            padding: 50px 20px;
            color: #6b7a8f;
        }

        .empty-state i {
            font-size: 48px;
            color: #d9e2eb;
            display: block;
            margin-bottom: 12px;
        }

        .empty-state .btn {
            margin-top: 16px;
        }

        /* ----- NOTIFICATION ----- */
        .notification {
            position: fixed;
            top: 90px;
            right: 30px;
            padding: 14px 24px;
            border-radius: 12px;
            font-weight: 500;
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
            z-index: 1000;
            display: flex;
            align-items: center;
            gap: 12px;
            transform: translateX(120%);
            transition: transform 0.4s ease;
            max-width: 400px;
        }

        .notification.show {
            transform: translateX(0);
        }

        .notification-success {
            background: #d4edda;
            color: #155724;
            border-left: 5px solid #28a745;
        }

        .notification-error {
            background: #f8d7da;
            color: #721c24;
            border-left: 5px solid #dc3545;
        }

        .notification-warning {
            background: #fff3cd;
            color: #856404;
            border-left: 5px solid #ffc107;
        }

        .notification i {
            font-size: 20px;
        }

        /* ----- MODAL (Add Item) ----- */
        .modal {
            display: none;
            position: fixed;
            z-index: 2000;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            justify-content: center;
            align-items: center;
            backdrop-filter: blur(4px);
        }

        .modal-content {
            background: white;
            padding: 35px 40px;
            width: 90%;
            max-width: 480px;
            border-radius: 20px;
            box-shadow: 0 30px 60px rgba(0, 0, 0, 0.2);
            animation: modalIn 0.3s ease;
        }

        @keyframes modalIn {
            from {
                transform: scale(0.9) translateY(20px);
                opacity: 0;
            }

            to {
                transform: scale(1) translateY(0);
                opacity: 1;
            }
        }

        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f4fb;
        }

        .modal-header h2 {
            font-size: 22px;
            color: #10233f;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .modal-header h2 i {
            color: #1a6cf0;
        }

        .modal-header .close {
            font-size: 28px;
            font-weight: 700;
            color: #999;
            cursor: pointer;
            transition: 0.2s;
            background: none;
            border: none;
            padding: 0 8px;
        }

        .modal-header .close:hover {
            color: #333;
        }

        .modal .form-group {
            margin-bottom: 18px;
        }

        .modal .form-group label {
            display: block;
            font-weight: 600;
            margin-bottom: 6px;
            color: #10233f;
            font-size: 14px;
        }

        .modal .form-group label i {
            margin-right: 6px;
            color: #1a6cf0;
        }

        .modal .form-control {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e9ecef;
            border-radius: 10px;
            font-size: 15px;
            transition: 0.3s;
            background: white;
        }

        .modal .form-control:focus {
            outline: none;
            border-color: #1a6cf0;
            box-shadow: 0 0 0 4px rgba(26, 108, 240, 0.12);
        }

        .modal .modal-actions {
            display: flex;
            gap: 12px;
            justify-content: flex-end;
            margin-top: 10px;
        }

        .modal .modal-actions .btn {
            min-width: 100px;
            justify-content: center;
        }

        /* ----- DELETE ALL MODAL ----- */
        .modal-delete .modal-content {
            text-align: center;
        }

        .modal-delete .modal-content h2 {
            font-size: 22px;
            margin-bottom: 12px;
            color: #10233f;
        }

        .modal-delete .modal-content p {
            color: #6b7a8f;
            margin-bottom: 20px;
            font-size: 15px;
        }

        .modal-delete .modal-actions {
            display: flex;
            gap: 12px;
            justify-content: center;
            flex-wrap: wrap;
        }

        .modal-delete .modal-actions .btn {
            min-width: 120px;
            justify-content: center;
        }

        /* ----- FOOTER ----- */
        .footer {
            margin-top: auto;
            padding: 24px 5vw 20px;
            background: #071a31;
            color: #b6c9dd;
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            align-items: center;
            font-size: 13px;
            border-top: 1px solid #1f3550;
        }

        .footer strong {
            color: #dce7f5;
        }

        .footer .meta {
            display: flex;
            gap: 24px;
            align-items: center;
        }

        .footer .meta span {
            color: #7c92ab;
        }

        /* ----- RESPONSIVE ----- */
        @media (max-width: 900px) {
            .navbar {
                padding: 0 4vw;
                height: 68px;
            }

            .nav-links a:not(.nav-logout) {
                display: none;
            }

            .nav-links a.nav-logout {
                display: inline-block;
            }

            .page-header {
                flex-direction: column;
                align-items: flex-start;
            }

            .header-actions {
                width: 100%;
            }

            .header-actions .btn {
                flex: 1;
                justify-content: center;
            }
        }

        @media (max-width: 600px) {
            main {
                padding: 16px 4vw;
            }

            .card {
                padding: 16px 14px;
            }

            .page-header {
                padding: 16px 18px;
            }

            .page-header h1 {
                font-size: 1.4rem;
            }

            th,
            td {
                padding: 10px 12px;
                font-size: 13px;
            }

            .action-buttons {
                flex-direction: row;
                gap: 4px;
            }

            .btn-icon {
                width: 32px;
                height: 32px;
                font-size: 12px;
            }

            .footer {
                flex-direction: column;
                gap: 10px;
                align-items: flex-start;
            }

            .notification {
                top: 80px;
                right: 16px;
                left: 16px;
                max-width: none;
                padding: 12px 18px;
                font-size: 14px;
            }

            .modal-content {
                padding: 25px 20px;
            }

            .modal .modal-actions {
                flex-direction: column;
            }

            .modal .modal-actions .btn {
                width: 100%;
            }
        }

        @media (max-width: 400px) {
            .brand {
                font-size: 16px;
            }

            .brand-icon {
                width: 30px;
                height: 30px;
                font-size: 14px;
            }
        }
    </style>
</head>

<body>

    <!-- ============================================================
         NAVIGATION (SOLID, NON-TRANSPARENT)
         ============================================================ -->
    <header class="navbar">
        <a href="admin.php" class="brand">
            <span class="brand-icon">CN</span>
            <span>CAOSCE <span class="brand-light">Network</span></span>
        </a>
        <div class="nav-links">
            <a href="admin.php" class="<?php echo $current_page == 'admin.php' ? 'active' : ''; ?>">
                <i class="fas fa-th-large"></i> Dashboard
            </a>
            <a href="classes.php" class="<?php echo $current_page == 'classes.php' ? 'active' : ''; ?>">
                <i class="fas fa-user-plus"></i> Class lists
            </a>
            <a href="result.php" class="<?php echo $current_page == 'result.php' ? 'active' : ''; ?>">
                <i class="fas fa-chart-pie"></i>OSCE Result
            </a>
            <a href="exam_manager.php" class="<?php echo $current_page == 'exam_manager.php' ? 'active' : ''; ?>">
                <i class="fas fa-sliders-h"></i> Exams settings
            </a>
            <a href="logout.php" class="nav-logout">
                <i class="fas fa-sign-out-alt"></i> Logout
            </a>
        </div>
    </header>
    <!-- ============================================================
         MAIN CONTENT
         ============================================================ -->
    <main>
        <div class="container">

            <!-- Page Header -->
            <div class="page-header">
                <div>
                    <h1>
                        <i class="fas fa-edit"></i>
                        Edit Checklist
                    </h1>
                    <div class="subtitle">
                        <i class="fas fa-table"></i>
                        <?= htmlspecialchars($displayTableName) ?> · <?= count($records) ?> items
                    </div>
                </div>
                <div class="header-actions">
                    <a href="exam_manager.php" class="btn btn-back">
                        <i class="fas fa-arrow-left"></i> Go Back
                    </a>
                    <button class="btn btn-primary" onclick="openAddModal()">
                        <i class="fas fa-plus-circle"></i> Add Item
                    </button>
                    <?php if (count($records) > 0): ?>
                        <button class="btn btn-danger" onclick="document.getElementById('deleteAllModal').style.display='flex'">
                            <i class="fas fa-trash-alt"></i> Delete All
                        </button>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Main Card -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <i class="fas fa-list-ul"></i>
                        Activity Items
                    </h2>
                    <span class="badge <?= count($records) > 0 ? 'badge-success' : 'badge-warning' ?>">
                        <i class="fas fa-circle"></i>
                        <?= count($records) ?> Records
                    </span>
                </div>

                <div class="table-wrapper">
                    <table>
                        <thead>
                            <tr>
                                <th style="width: 60%;">Activity</th>
                                <th style="width: 20%;">Score</th>
                                <th style="width: 20%; text-align: center;">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (count($records) > 0): ?>
                                <?php foreach ($records as $row): ?>
                                    <tr>
                                        <form method="POST">
                                            <input type="hidden" name="id" value="<?= $row['id'] ?>">
                                            <td>
                                                <input type="text" name="activity" class="form-input"
                                                    value="<?= htmlspecialchars($row['activity']) ?>"
                                                    placeholder="Enter activity description" required>
                                            </td>
                                            <td>
                                                <select name="alot_mark" class="form-select">
                                                    <option value="<?= htmlspecialchars($row['alot_mark']) ?>" selected>
                                                        <?= htmlspecialchars($row['alot_mark']) ?>
                                                    </option>
                                                    <option value="0">0</option>
                                                    <option value="0.25">0.25</option>
                                                    <option value="0.5">0.5</option>
                                                    <option value="1">1</option>
                                                </select>
                                            </td>
                                            <td>
                                                <div class="action-buttons">
                                                    <button type="submit" name="update" class="btn-icon btn-save" title="Save Changes">
                                                        <i class="fas fa-save"></i>
                                                    </button>
                                                    <button type="submit" name="delete" class="btn-icon btn-delete"
                                                        onclick="return confirm('Are you sure you want to delete this row?')" title="Delete Row">
                                                        <i class="fas fa-trash"></i>
                                                    </button>

                                                </div>
                                            </td>
                                        </form>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="3">
                                        <div class="empty-state">
                                            <i class="fas fa-inbox"></i>
                                            <p>No items in this checklist yet.</p>
                                            <button class="btn btn-primary" onclick="openAddModal(0)" style="margin-top: 12px;">
                                                <i class="fas fa-plus"></i> Add First Item
                                            </button>
                                        </div>
                                    </td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- Hidden add-row form -->
            <form id="addForm" method="POST" style="display:none;">
                <input type="hidden" name="add" value="1">
                <input type="hidden" name="position_id" id="position_id">
                <input type="hidden" name="activity" id="activity_value">
                <input type="hidden" name="alot_mark" id="alot_mark_value">
            </form>

        </div>
    </main>

    <!-- ============================================================
         ADD ITEM MODAL
         ============================================================ -->
    <div id="addItemModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h2><i class="fas fa-plus-circle"></i> Add New Item</h2>
                <button class="close" onclick="closeAddModal()">&times;</button>
            </div>
            <form id="addItemForm" onsubmit="submitAddItem(event)">
                <div class="form-group">
                    <label for="modal_activity"><i class="fas fa-tasks"></i> Activity Name</label>
                    <input type="text" id="modal_activity" class="form-control" placeholder="Enter activity description" required>
                </div>
                <div class="form-group">
                    <label for="modal_score"><i class="fas fa-star"></i> Score Value</label>
                    <select id="modal_score" class="form-control" required>
                        <option value="">Select score</option>
                        <option value="0">0</option>
                        <option value="0.25">0.25</option>
                        <option value="0.5">0.5</option>
                        <option value="1">1</option>
                    </select>
                </div>
                <div class="modal-actions">
                    <button type="button" class="btn btn-back" onclick="closeAddModal()">
                        <i class="fas fa-times"></i> Cancel
                    </button>
                    <button type="submit" class="btn btn-success">
                        <i class="fas fa-check"></i> Add Item
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- ============================================================
         DELETE ALL MODAL
         ============================================================ -->
    <div id="deleteAllModal" class="modal modal-delete">
        <div class="modal-content">
            <div class="modal-header">
                <h2><i class="fas fa-exclamation-triangle" style="color:#dc3545;"></i> Delete All Items</h2>
                <button class="close" onclick="document.getElementById('deleteAllModal').style.display='none'">&times;</button>
            </div>
            <p>Are you sure you want to delete <strong>all <?= count($records) ?> items</strong> from this checklist? This action cannot be undone.</p>
            <form method="POST">
                <input type="hidden" name="delete_all" value="1">
                <input type="hidden" name="confirm_delete_all" value="yes">
                <div class="modal-actions">
                    <button type="button" class="btn btn-back" onclick="document.getElementById('deleteAllModal').style.display='none'">
                        <i class="fas fa-times"></i> Cancel
                    </button>
                    <button type="submit" class="btn btn-danger">
                        <i class="fas fa-trash-alt"></i> Yes, Delete All
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- ============================================================
         FOOTER
         ============================================================ -->
    <footer class="footer">
        <div>
            <strong><i class="fas fa-network-wired" style="margin-right:8px;"></i> CAOSCE Network</strong>
            <span style="margin-left:18px;">© <?php echo date('Y'); ?> All rights reserved</span>
        </div>
        <div class="meta">
            <span><i class="fas fa-code"></i> v2.0</span>
            <span>|</span>
            <span><i class="fas fa-user-cog"></i> Admin Panel</span>
            <span>|</span>
        </div>
    </footer>

    <!-- ============================================================
         NOTIFICATION
         ============================================================ -->
    <?php if ($notification): ?>
        <div class="notification <?=
                                    strpos($notification, '✅') !== false ? 'notification-success' : (strpos($notification, '❌') !== false ? 'notification-error' : (strpos($notification, '⚠️') !== false ? 'notification-warning' : 'notification-success'))
                                    ?>" id="notify">
            <i class="fas <?=
                            strpos($notification, '✅') !== false ? 'fa-check-circle' : (strpos($notification, '❌') !== false ? 'fa-exclamation-circle' : (strpos($notification, '⚠️') !== false ? 'fa-exclamation-triangle' : 'fa-info-circle'))
                            ?>"></i>
            <span><?= $notification ?></span>
        </div>
        <script>
            const note = document.getElementById('notify');
            setTimeout(() => note.classList.add('show'), 100);
            setTimeout(() => {
                note.classList.remove('show');
                setTimeout(() => note.remove(), 400);
            }, 4000);
        </script>
    <?php endif; ?>

    <!-- ============================================================
         SCRIPTS
         ============================================================ -->
    <script>
        let addPositionId = 0;

        // Open Add Modal
        function openAddModal(positionId) {
            addPositionId = positionId || 0;
            document.getElementById('modal_activity').value = '';
            document.getElementById('modal_score').value = '';
            document.getElementById('addItemModal').style.display = 'flex';
            // Focus on the first input
            setTimeout(() => document.getElementById('modal_activity').focus(), 100);
        }

        // Close Add Modal
        function closeAddModal() {
            document.getElementById('addItemModal').style.display = 'none';
        }

        // Submit Add Item via modal
        function submitAddItem(e) {
            e.preventDefault();
            const activity = document.getElementById('modal_activity').value.trim();
            const score = document.getElementById('modal_score').value;

            if (!activity) {
                alert('Please enter an activity name.');
                return;
            }
            if (!score) {
                alert('Please select a score value.');
                return;
            }

            document.getElementById('position_id').value = addPositionId;
            document.getElementById('activity_value').value = activity;
            document.getElementById('alot_mark_value').value = score;
            document.getElementById('addForm').submit();
        }

        // Close modal on outside click
        window.onclick = function(event) {
            if (event.target.classList.contains('modal')) {
                event.target.style.display = 'none';
            }
        }

        // Close modal on Escape key
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape') {
                document.querySelectorAll('.modal').forEach(modal => {
                    modal.style.display = 'none';
                });
            }
        });

        // Table row animation
        document.addEventListener('DOMContentLoaded', function() {
            const rows = document.querySelectorAll('tbody tr');
            rows.forEach((row, index) => {
                row.style.opacity = '0';
                row.style.transform = 'translateY(8px)';
                setTimeout(() => {
                    row.style.transition = 'opacity 0.3s ease, transform 0.3s ease';
                    row.style.opacity = '1';
                    row.style.transform = 'translateY(0)';
                }, index * 40);
            });

            // Activity input validation
            document.querySelectorAll('input[name="activity"]').forEach(input => {
                input.addEventListener('input', function() {
                    this.style.borderColor = this.value.trim() === '' ? '#dc3545' : '#e9ecef';
                });
            });
        });
    </script>

</body>

</html>