<?php
session_start();

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

// Get table name from URL
if (!isset($_GET['table']) || empty($_GET['table'])) {
    die("No procedure table specified.");
}

$tableName = $_GET['table'];

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

// Fetch all rows from the procedure table
$records = $db->query("SELECT * FROM '$tableName' ORDER BY id")->fetchAll(PDO::FETCH_ASSOC);

// Format table name: remove PRO prefix and add spaces between words
function formatDisplayName($name)
{
    // Remove leading PRO if present
    $name = preg_replace('/^PRO/', '', $name);

    // Add spaces between words (uppercase transitions)
    $name = preg_replace('/(?<!^)([A-Z])/', ' $1', $name);

    // Replace underscores with spaces
    $name = str_replace('_', ' ', $name);

    return trim($name);
}

$displayTableName = formatDisplayName($tableName);

// 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', 'Roboto', 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: 1100px;
            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 .title-group {
            display: flex;
            align-items: center;
            gap: 14px;
        }

        .page-header .title-group .icon-large {
            font-size: 32px;
            color: #1a6cf0;
        }

        .page-header h1 {
            color: #10233f;
            font-weight: 700;
            font-size: 1.8rem;
            margin: 0;
        }

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

        .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-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);
        }

        /* ----- CARD ----- */
        .card {
            background: white;
            border-radius: 16px;
            box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
            padding: 25px 28px;
            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.2rem;
            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;
        }

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

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

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

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

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

        tr:hover td {
            background: #f8fafc;
        }

        .activity-cell {
            font-weight: 600;
            font-size: 18px;
            color: #10233f;
        }

        .mark-cell {
            font-weight: 600;
            font-size: 18px;
            color: #1a6cf0;
            text-align: center;
        }

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

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

        /* ----- 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 14px;
                font-size: 14px;
            }

            .activity-cell {
                font-size: 16px;
            }

            .mark-cell {
                font-size: 16px;
            }

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

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

            .brand-icon {
                width: 30px;
                height: 30px;
                font-size: 14px;
            }

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

            .activity-cell {
                font-size: 14px;
            }

            .mark-cell {
                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 class="title-group">
                    <span class="icon-large"><i class="fas fa-eye"></i></span>
                    <div>
                        <h1><?= htmlspecialchars($displayTableName) ?></h1>
                        <div class="subtitle">
                            <i class="fas fa-list-ul"></i>
                            Procedure Checklist · <?= count($records) ?> items
                        </div>
                    </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>
                    <a href="edit_procedure.php?table=<?= urlencode($tableName) ?>" class="btn btn-primary">
                        <i class="fas fa-edit"></i> Edit Checklist
                    </a>
                </div>
            </div>

            <!-- Table Card -->
            <div class="card">
                <div class="card-header">
                    <h2 class="card-title">
                        <i class="fas fa-clipboard-list"></i>
                        Procedure Items
                    </h2>
                    <span class="badge">
                        <i class="fas fa-circle"></i>
                        <?= count($records) ?> Items
                    </span>
                </div>

                <div class="table-wrapper">
                    <table>
                        <thead>
                            <tr>
                                <th style="width: 75%;">Activity</th>
                                <th style="width: 25%; text-align: center;">Allocated Mark</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (count($records) > 0): ?>
                                <?php foreach ($records as $row): ?>
                                    <tr>
                                        <td class="activity-cell">
                                            <?= htmlspecialchars($row['activity'] ?? '') ?>
                                        </td>
                                        <td class="mark-cell">
                                            <?= htmlspecialchars($row['alot_mark'] ?? '') ?>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="2">
                                        <div class="empty-state">
                                            <i class="fas fa-inbox"></i>
                                            <p>No items found in this checklist.</p>
                                            <a href="edit_procedure.php?table=<?= urlencode($tableName) ?>" class="btn btn-primary" style="margin-top:12px;">
                                                <i class="fas fa-plus"></i> Add Items
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

        </div>
    </main>

    <!-- ============================================================
         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>

    <!-- ============================================================
         SCRIPTS
         ============================================================ -->
    <script>
        // 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 * 50);
            });
        });
    </script>

</body>

</html>