<?php
session_start();

// Configuration
$PASSWORD_HASH = password_hash("Destinie23", PASSWORD_BCRYPT);
$DB_FILE = "vault_database.json";

// Handle Login
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['password'])) {
    if (password_verify($_POST['password'], $PASSWORD_HASH)) {
        $_SESSION['logged_in'] = true;
    } else {
        $error = "Access Denied: Invalid Credentials";
    }
}

// Handle Logout
if (isset($_GET['logout'])) {
    session_destroy();
    header("Location: admin.php");
    exit;
}

// Read Database
$data = ["receipts" => [], "last_update" => "Never"];
if (file_exists($DB_FILE)) {
    $json = file_get_contents($DB_FILE);
    $decoded = json_decode($json, true);
    if ($decoded) {
        $data = $decoded;
    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>STEM SAFE | KEEPER COMMAND</title>
    <style>
        :root {
            --bg: #020005;
            --panel: #0D001A;
            --accent: #4D4DFF;
            --secondary: #B700FF;
            --text: #fff;
        }
        body {
            background-color: var(--bg);
            color: var(--text);
            font-family: 'Segoe UI', sans-serif;
            margin: 0;
            padding: 20px;
        }
        .container {
            max-width: 1000px;
            margin: 0 auto;
        }
        h1 {
            color: var(--secondary);
            text-transform: uppercase;
            letter-spacing: 2px;
            border-bottom: 2px solid var(--accent);
            padding-bottom: 10px;
        }
        .login-box {
            background: var(--panel);
            padding: 40px;
            border-radius: 10px;
            border: 1px solid var(--accent);
            max-width: 400px;
            margin: 100px auto;
            text-align: center;
        }
        input[type="password"] {
            width: 100%;
            padding: 10px;
            margin: 20px 0;
            background: #000;
            border: 1px solid #333;
            color: var(--accent);
            font-size: 1.2rem;
            text-align: center;
        }
        button {
            background: var(--accent);
            color: #fff;
            border: none;
            padding: 10px 30px;
            font-weight: bold;
            cursor: pointer;
            text-transform: uppercase;
        }
        button:hover {
            background: var(--secondary);
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
            background: var(--panel);
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #333;
        }
        th {
            background: rgba(77, 77, 255, 0.2);
            color: var(--accent);
        }
        tr:hover {
            background: rgba(183, 0, 255, 0.1);
        }
        .hash {
            font-family: monospace;
            color: #888;
            font-size: 0.9rem;
        }
        .status-badge {
            background: #00FF00;
            color: black;
            padding: 2px 6px;
            border-radius: 3px;
            font-size: 0.8rem;
            font-weight: bold;
        }
        .logout {
            float: right;
            color: #666;
            text-decoration: none;
            font-size: 0.9rem;
        }
        .logout:hover { color: #fff; }
    </style>
</head>
<body>

<div class="container">
    <?php if (!isset($_SESSION['logged_in'])): ?>
        <div class="login-box">
            <h1>KEEPER LOGIN</h1>
            <?php if (isset($error)) echo "<p style='color:red'>$error</p>"; ?>
            <form method="POST">
                <input type="password" name="password" placeholder="ENTER ACCESS CODE" required autofocus>
                <button type="submit">AUTHENTICATE</button>
            </form>
            <p style="margin-top: 20px; font-size: 0.8rem; color: #666;">AUTHORIZED PERSONNEL ONLY</p>
        </div>
    <?php else: ?>
        <a href="?logout=true" class="logout">[ DISCONNECT ]</a>
        <h1>GLOBAL VAULT REGISTRY</h1>
        <p><strong>Database Status:</strong> ONLINE | <strong>Last Sync:</strong> <?php echo ($data['last_update'] && $data['last_update'] !== 'Never') ? date("F j, Y, g:i a", $data['last_update']) : 'Never'; ?></p>
        <p><strong>Total Records:</strong> <?php echo count($data['receipts']); ?></p>

        <table>
            <thead>
                <tr>
                    <th>TIMESTAMP</th>
                    <th>FILENAME</th>
                    <th>OWNER</th>
                    <th>DNA FINGERPRINT (HASH)</th>
                    <th>STATUS</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($data['receipts'])): ?>
                    <tr>
                        <td colspan="5" style="text-align:center; padding: 30px; color: #666;">NO RECORDS FOUND IN VAULT</td>
                    </tr>
                <?php else: ?>
                    <?php 
                    // Sort by date descending
                    $receipts = $data['receipts'];
                    usort($receipts, function($a, $b) {
                        return strtotime($b['synced_at']) - strtotime($a['synced_at']);
                    });
                    
                    foreach ($receipts as $r): ?>
                    <tr>
                        <td><?php echo date("Y-m-d H:i", strtotime($r['synced_at'])); ?></td>
                        <td style="color: var(--text); font-weight: bold;"><?php echo htmlspecialchars($r['filename']); ?></td>
                        <td style="color: var(--secondary);"><?php echo htmlspecialchars($r['owner']); ?></td>
                        <td class="hash" title="<?php echo $r['file_hash']; ?>"><?php echo substr($r['file_hash'], 0, 12) . "..."; ?></td>
                        <td><span class="status-badge">SECURE</span></td>
                    </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>

</body>
</html>
