<?php
// admin/pins/generate.php
ob_start();
$projectRoot = dirname(dirname(__DIR__));
require_once $projectRoot . '/config/database.php';
require_once $projectRoot . '/config/config.php';

// --- NEW RESTRICTION START ---
// Only a 'full' admin can access this page. Sub-admins are sent back to the dashboard.
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../dashboard.php?error=unauthorized");
    exit();
}
// --- NEW RESTRICTION END ---

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['generate'])) {
    $count = (int)$_POST['count'];
    
    for ($i = 0; $i < $count; $i++) {
        // YOUR ORIGINAL PIN GENERATION SEQUENCE
        $pin = "";
        for($j = 0; $j < 10; $j++) {
            $pin .= mt_rand(0, 9);
        }
        
        $serial = "SN" . mt_rand(100000, 999999);
        
        $stmt = $pdo->prepare("INSERT IGNORE INTO pins (pin_code, serial_number, usage_limit, times_used, status) VALUES (?, ?, 3, 0, 'unused')");
        $stmt->execute([$pin, $serial]);
    }
    $success = "$count PINs (10-digits) generated successfully!";
}

$unused_pins = $pdo->query("SELECT COUNT(*) FROM pins WHERE status = 'unused'")->fetchColumn();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Generate 10-Digit PINs</title>
    <style>
        body { font-family: 'Segoe UI', sans-serif; padding: 40px; background: #f4f4f4; display: flex; align-items: center; justify-content: center; min-height: 80vh; margin: 0; }
        .card { background: white; padding: 30px; border-radius: 8px; width: 100%; max-width: 450px; box-shadow: 0 4px 10px rgba(0,0,0,0.1); text-align: center; }
        h2 { margin-top: 0; color: #2c3e50; }
        .stats { background: #fff3cd; color: #856404; padding: 10px; border-radius: 5px; margin: 15px 0; font-weight: bold; border: 1px solid #ffeeba; }
        input, button { width: 100%; padding: 12px; margin: 10px 0; border-radius: 5px; border: 1px solid #ccc; box-sizing: border-box; font-size: 16px; }
        button { background: #e67e22; color: white; border: none; cursor: pointer; font-weight: bold; transition: background 0.3s; }
        button:hover { background: #d35400; }
        .success-msg { color: #155724; background: #d4edda; padding: 10px; border-radius: 5px; border: 1px solid #c3e6cb; margin-bottom: 15px; }
        .back-link { display: inline-block; margin-top: 15px; color: #3498db; text-decoration: none; }
    </style>
</head>
<body>
    <div class="card">
        <h2>10-Digit PIN Generator</h2>
        <p style="color: #e74c3c; font-weight: bold; font-size: 13px;">SUPER ADMIN ACCESS ONLY</p>
        
        <div class="stats">
            Unused PINs in Stock: <?php echo $unused_pins; ?>
        </div>

        <?php if(isset($success)) echo "<div class='success-msg'>$success</div>"; ?>
        
        <form method="POST">
            <label style="display:block; text-align:left; font-size: 14px; color: #666;">Number of PINs to generate:</label>
            <input type="number" name="count" min="1" max="500" value="50">
            <button type="submit" name="generate">Generate PINs Now</button>
        </form>
        
        <a href="../dashboard.php" class="back-link">← Back to Dashboard</a>
    </div>
</body>
</html>