<?php
/**
 * IBVTE Admin - Global Settings
 */
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

if (!checkDBConnection()) exit;
Auth()->requireRole(['super_admin', 'admin']);

$db = DB();
$flash = getFlashMessage();

$settingsFile = __DIR__ . '/../includes/settings.json';
$siteSettings = ['show_left_sidebar' => true];
if (file_exists($settingsFile)) {
    $siteSettings = json_decode(file_get_contents($settingsFile), true);
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!Auth()->verifyCSRFToken($_POST['csrf_token'] ?? '')) {
        redirectWithMessage('settings.php', 'Security session expired.', 'danger');
    }
    
    $siteSettings['show_left_sidebar'] = isset($_POST['show_left_sidebar']) ? true : false;
    
    file_put_contents($settingsFile, json_encode($siteSettings, JSON_PRETTY_PRINT));
    
    redirectWithMessage('settings.php', 'Settings updated successfully.', 'success');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Global Settings - <?= APP_SHORT_NAME ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="../assets/css/admin-style.css" rel="stylesheet">
</head>
<body>
    <?php include __DIR__ . '/../includes/admin_header.php'; ?>
    <div class="container-fluid">
        <div class="row">
            <?php include __DIR__ . '/../includes/admin_sidebar.php'; ?>
            
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 py-4">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h2 class="h3 fw-bold"><i class="fas fa-cog me-2 text-primary"></i>Global Settings</h2>
                </div>

                <?php if ($flash): ?>
                    <div class="alert alert-<?= $flash['type'] ?> alert-dismissible fade show rounded-3 shadow-sm">
                        <?= htmlspecialchars($flash['message']) ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                <?php endif; ?>

                <div class="row">
                    <div class="col-md-8">
                        <div class="card border-0 shadow-sm rounded-4 mb-4">
                            <div class="card-body p-4">
                                <h5 class="fw-bold mb-4">General Configuration</h5>
                                <form method="POST">
                                    <input type="hidden" name="csrf_token" value="<?= Auth()->generateCSRFToken() ?>">
                                    <div class="row g-3">
                                        <div class="col-md-12">
                                            <label class="form-label fw-bold small">Site Title</label>
                                            <input type="text" name="site_title" class="form-control" value="<?= APP_NAME ?>">
                                        </div>
                                        <div class="col-md-6">
                                            <label class="form-label fw-bold small">Admin Email</label>
                                            <input type="email" name="admin_email" class="form-control" value="ibvtenewdelhi@gmail.com">
                                        </div>
                                        <div class="col-md-6">
                                            <label class="form-label fw-bold small">Contact Number</label>
                                            <input type="text" name="contact_phone" class="form-control" value="+91 9999999999">
                                        </div>
                                        <div class="col-md-12">
                                            <label class="form-label fw-bold small">Office Address</label>
                                            <textarea name="address" class="form-control" rows="2">New Delhi, India</textarea>
                                        </div>
                                        
                                        <div class="col-12 mt-4">
                                            <h5 class="fw-bold mb-3">Academic Settings</h5>
                                        </div>
                                        <div class="col-md-6">
                                            <label class="form-label fw-bold small">Current Academic Year</label>
                                            <input type="text" name="academic_year" class="form-control" value="2024-25">
                                        </div>
                                        <div class="col-md-6">
                                            <label class="form-label fw-bold small">Default Passing %</label>
                                            <input type="number" name="pass_percent" class="form-control" value="40">
                                        </div>
                                        
                                        <div class="col-12 mt-4">
                                            <h5 class="fw-bold mb-3">Appearance Settings</h5>
                                        </div>
                                        <div class="col-md-12">
                                            <div class="form-check form-switch">
                                                <input class="form-check-input" type="checkbox" id="show_left_sidebar" name="show_left_sidebar" <?= $siteSettings['show_left_sidebar'] ? 'checked' : '' ?> style="transform: scale(1.3); margin-right: 10px;">
                                                <label class="form-check-label fw-bold small" for="show_left_sidebar" style="margin-top: 3px;">Show Left Colorful Sidebar (Quick Links)</label>
                                            </div>
                                        </div>
                                        
                                        <div class="col-12 mt-4">
                                            <button type="submit" class="btn btn-primary px-4 fw-bold">Update Settings</button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="card border-0 shadow-sm rounded-4 bg-light p-3">
                            <h6 class="fw-bold mb-2">System Info</h6>
                            <ul class="list-unstyled small mb-0">
                                <li class="mb-1"><strong>PHP Version:</strong> <?= phpversion() ?></li>
                                <li class="mb-1"><strong>DB Driver:</strong> PDO MySQL</li>
                                <li class="mb-1"><strong>Timezone:</strong> <?= date_default_timezone_get() ?></li>
                                <li><strong>Storage:</strong> <?= formatBytes(disk_free_space(".")) ?> Free</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
