<?php
require_once '../includes/db.php';
require_once '../includes/auth.php';

requireRole('admin');

$services = $pdo->query("SELECT * FROM services ORDER BY box_number")->fetchAll();

$message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['update_service'])) {
        $id = $_POST['id'];
        $name = $_POST['name'];
        $amount = $_POST['amount'];
        $active = isset($_POST['active']) ? 1 : 0;

        $stmt = $pdo->prepare("UPDATE services SET name = ?, amount = ?, active = ? WHERE id = ?");
        if ($stmt->execute([$name, $amount, $active, $id])) {
            $message = 'Service updated successfully';
            header('Location: services.php');
            exit;
        } else {
            $message = 'Failed to update service';
        }
    } elseif (isset($_POST['delete_service'])) {
        $id = $_POST['service_id'];

        $stmt = $pdo->prepare("DELETE FROM services WHERE id = ?");
        if ($stmt->execute([$id])) {
            $message = 'Service deleted successfully';
            header('Location: services.php');
            exit;
        } else {
            $message = 'Failed to delete service';
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Services Management - Shinda Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <a class="navbar-brand" href="#">Shinda Admin</a>
            <div class="navbar-nav ms-auto">
                <a class="nav-link" href="dashboard.php">Dashboard</a>
                <a class="nav-link" href="logout.php">Logout</a>
            </div>
        </div>
    </nav>

    <div class="container-fluid">
        <div class="row">
            <nav id="sidebar" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
                <div class="position-sticky pt-3">
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link" href="dashboard.php">Dashboard</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="users.php">Users</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link active" href="services.php">Services</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="transactions.php">Transactions</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="results.php">Game Results</a>
                        </li>
                        <?php if (hasPermission('super_admin')): ?>
                        <li class="nav-item">
                            <a class="nav-link" href="settings.php">Settings</a>
                        </li>
                        <?php endif; ?>
                    </ul>
                </div>
            </nav>

            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2">Services Management</h1>
                </div>

                <?php if ($message): ?>
                    <div class="alert alert-info"><?php echo $message; ?></div>
                <?php endif; ?>

                <div class="table-responsive">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>Box Number</th>
                                <th>Name</th>
                                <th>Amount (Ksh)</th>
                                <th>Active</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($services as $service): ?>
                                <tr>
                                    <td><?php echo $service['box_number']; ?></td>
                                    <td><?php echo htmlspecialchars($service['name']); ?></td>
                                    <td><?php echo $service['amount']; ?></td>
                                    <td><?php echo $service['active'] ? 'Yes' : 'No'; ?></td>
                                    <td>
                                        <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#editModal<?php echo $service['id']; ?>">Edit</button>
                                        <form method="POST" style="display: inline;">
                                            <input type="hidden" name="service_id" value="<?php echo $service['id']; ?>">
                                            <button type="submit" name="delete_service" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure you want to delete this service?')">Delete</button>
                                        </form>
                                    </td>
                                </tr>

                                <!-- Edit Modal -->
                                <div class="modal fade" id="editModal<?php echo $service['id']; ?>" tabindex="-1">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h5 class="modal-title">Edit Service</h5>
                                                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                            </div>
                                            <form method="POST">
                                                <div class="modal-body">
                                                    <input type="hidden" name="id" value="<?php echo $service['id']; ?>">
                                                    <div class="mb-3">
                                                        <label for="name<?php echo $service['id']; ?>" class="form-label">Name</label>
                                                        <input type="text" class="form-control" id="name<?php echo $service['id']; ?>" name="name" value="<?php echo htmlspecialchars($service['name']); ?>" required>
                                                    </div>
                                                    <div class="mb-3">
                                                        <label for="amount<?php echo $service['id']; ?>" class="form-label">Amount</label>
                                                        <input type="number" step="0.01" class="form-control" id="amount<?php echo $service['id']; ?>" name="amount" value="<?php echo $service['amount']; ?>" required>
                                                    </div>
                                                    <div class="mb-3 form-check">
                                                        <input type="checkbox" class="form-check-input" id="active<?php echo $service['id']; ?>" name="active" <?php echo $service['active'] ? 'checked' : ''; ?>>
                                                        <label class="form-check-label" for="active<?php echo $service['id']; ?>">Active</label>
                                                    </div>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                                                    <button type="submit" name="update_service" class="btn btn-primary">Update</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </main>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>