<?php
// public/admin/admins.php
session_start();
require_once __DIR__ . '/../../config/admin.php';
require_admin_role('SUPER_ADMIN');
require_once __DIR__ . '/../../config/db.php';

// Handle create, edit, delete actions (basic version)
$action = $_GET['action'] ?? '';
$edit_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

// Fetch location data
$states = $pdo->query('SELECT id, name FROM states ORDER BY name')->fetchAll();
$districts = $pdo->query('SELECT id, name, state_id FROM districts ORDER BY name')->fetchAll();
$blocks = $pdo->query('SELECT id, name, district_id FROM blocks ORDER BY name')->fetchAll();
$panchayats = $pdo->query('SELECT id, name, block_id FROM gps ORDER BY name')->fetchAll();

// Handle create admin
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create_admin'])) {
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $role = $_POST['role'];
    $state_id = $_POST['state_id'] ?: null;
    $district_id = $_POST['district_id'] ?: null;
    $block_id = $_POST['block_id'] ?: null;
    $panchayat_id = $_POST['panchayat_id'] ?: null;
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
    $stmt = $pdo->prepare('INSERT INTO admins (name, email, password, role, state_id, district_id, block_id, panchayat_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
    $stmt->execute([$name, $email, $password, $role, $state_id, $district_id, $block_id, $panchayat_id]);
    header('Location: admins.php'); exit;
}

// Handle delete admin
if ($action === 'delete' && $edit_id) {
    $pdo->prepare('DELETE FROM admins WHERE id=?')->execute([$edit_id]);
    header('Location: admins.php'); exit;
}

// List admins
$admins = $pdo->query('SELECT a.*, s.name as state_name, d.name as district_name, b.name as block_name, g.name as panchayat_name FROM admins a
LEFT JOIN states s ON a.state_id=s.id
LEFT JOIN districts d ON a.district_id=d.id
LEFT JOIN blocks b ON a.block_id=b.id
LEFT JOIN gps g ON a.panchayat_id=g.id
ORDER BY a.role, a.name')->fetchAll();

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin Management</title>
    <link rel="stylesheet" href="/school-crm/public/assets/bootstrap.min.css">
</head>
<body>
<div class="container py-4">
    <h2>Admin Management</h2>
    <form method="post" class="card p-3 mb-4">
        <h5>Create New Admin</h5>
        <div class="row">
            <div class="col-md-3 mb-2">
                <input type="text" name="name" class="form-control" placeholder="Name" required>
            </div>
            <div class="col-md-3 mb-2">
                <input type="email" name="email" class="form-control" placeholder="Email" required>
            </div>
            <div class="col-md-2 mb-2">
                <select name="role" class="form-select" required>
                    <option value="">Role</option>
                    <option value="STATE_ADMIN">State Admin</option>
                    <option value="DISTRICT_ADMIN">District Admin</option>
                    <option value="BLOCK_ADMIN">Block Admin</option>
                    <option value="PANCHAYAT_ADMIN">Panchayat Admin</option>
                </select>
            </div>
            <div class="col-md-2 mb-2">
                <input type="password" name="password" class="form-control" placeholder="Password" required>
            </div>
        </div>
        <div class="row">
            <div class="col-md-3 mb-2">
                <select name="state_id" class="form-select">
                    <option value="">State</option>
                    <?php foreach ($states as $s): ?>
                        <option value="<?= $s['id'] ?>"><?= htmlspecialchars($s['name']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3 mb-2">
                <select name="district_id" class="form-select">
                    <option value="">District</option>
                    <?php foreach ($districts as $d): ?>
                        <option value="<?= $d['id'] ?>"><?= htmlspecialchars($d['name']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3 mb-2">
                <select name="block_id" class="form-select">
                    <option value="">Block</option>
                    <?php foreach ($blocks as $b): ?>
                        <option value="<?= $b['id'] ?>"><?= htmlspecialchars($b['name']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3 mb-2">
                <select name="panchayat_id" class="form-select">
                    <option value="">Panchayat</option>
                    <?php foreach ($panchayats as $g): ?>
                        <option value="<?= $g['id'] ?>"><?= htmlspecialchars($g['name']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <button type="submit" name="create_admin" class="btn btn-success mt-2">Create Admin</button>
    </form>
    <h5>All Admins</h5>
    <table class="table table-bordered table-sm">
        <thead><tr><th>Name</th><th>Email</th><th>Role</th><th>State</th><th>District</th><th>Block</th><th>Panchayat</th><th>Created</th><th>Action</th></tr></thead>
        <tbody>
        <?php foreach ($admins as $a): ?>
            <tr>
                <td><?= htmlspecialchars($a['name']) ?></td>
                <td><?= htmlspecialchars($a['email']) ?></td>
                <td><?= htmlspecialchars($a['role']) ?></td>
                <td><?= htmlspecialchars($a['state_name']) ?></td>
                <td><?= htmlspecialchars($a['district_name']) ?></td>
                <td><?= htmlspecialchars($a['block_name']) ?></td>
                <td><?= htmlspecialchars($a['panchayat_name']) ?></td>
                <td><?= htmlspecialchars($a['created_at']) ?></td>
                <td>
                    <?php if ($a['role'] !== 'SUPER_ADMIN'): ?>
                        <a href="admins.php?action=delete&id=<?= $a['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Delete this admin?')">Delete</a>
                    <?php endif; ?>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <a href="dashboard_super.php" class="btn btn-secondary">Back to Dashboard</a>
</div>
</body>
</html>
