<?php
session_start();
require_once '../includes/db.php';
require_once '../includes/functions.php';

redirectIfNotAdmin();

$error = '';
$success = '';

// Handle settings update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        foreach ($_POST as $key => $value) {
            if (strpos($key, 'setting_') === 0) {
                $setting_key = substr($key, 8);
                $stmt = $pdo->prepare("INSERT INTO admin_sms_settings (setting_key, setting_value) 
                                       VALUES (?, ?) 
                                       ON DUPLICATE KEY UPDATE setting_value = ?");
                $stmt->execute([$setting_key, $value, $value]);
            }
        }
        $success = "SMS settings updated successfully!";
    } catch (Exception $e) {
        $error = "Failed to update settings: " . $e->getMessage();
    }
}

// Fetch current settings
$settings = [];
$stmt = $pdo->query("SELECT setting_key, setting_value FROM admin_sms_settings");
while ($row = $stmt->fetch()) {
    $settings[$row['setting_key']] = $row['setting_value'];
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SMS Settings - Oakshire Finance Admin</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:opsz,wght@14..32,300;400;500;600;700;800&display=swap"
        rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', sans-serif;
            background: #f8f9fc;
            color: #212529;
        }

        .wrapper {
            display: flex;
            min-height: 100vh;
        }

        .sidebar {
            width: 280px;
            background: white;
            border-right: 1px solid #e9ecef;
            position: fixed;
            height: 100vh;
            overflow-y: auto;
        }

        .sidebar-header {
            padding: 24px;
            text-align: center;
            border-bottom: 1px solid #e9ecef;
        }

        .logo {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            text-decoration: none;
        }

        .logo-icon {
            width: 40px;
            height: 40px;
            background: #0a2540;
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .logo-icon i {
            color: white;
            font-size: 20px;
        }

        .logo-text {
            font-size: 22px;
            font-weight: 700;
            color: #0a2540;
        }

        .nav-menu {
            padding: 24px 16px;
        }

        .nav-link {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px 16px;
            border-radius: 12px;
            text-decoration: none;
            color: #495057;
            transition: all 0.3s;
        }

        .nav-link:hover {
            background: #f1f3f9;
            color: #1a4d8c;
        }

        .nav-link.active {
            background: #0a2540;
            color: white;
        }

        .main-content {
            flex: 1;
            margin-left: 280px;
        }

        .top-header {
            background: white;
            border-bottom: 1px solid #e9ecef;
            padding: 16px 32px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .page-title h1 {
            font-size: 24px;
            font-weight: 700;
            color: #212529;
        }

        .content-area {
            padding: 32px;
        }

        .card {
            background: white;
            border-radius: 20px;
            border: 1px solid #e9ecef;
            padding: 24px;
            margin-bottom: 24px;
        }

        .card h2 {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 20px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            font-size: 13px;
            font-weight: 500;
            color: #495057;
            margin-bottom: 8px;
        }

        .form-group input,
        .form-group select {
            width: 100%;
            padding: 12px 16px;
            border: 1.5px solid #e9ecef;
            border-radius: 12px;
            font-size: 14px;
        }

        .form-group input:focus {
            border-color: #1a4d8c;
            outline: none;
        }

        .btn-primary {
            padding: 12px 24px;
            background: #0a2540;
            color: white;
            border: none;
            border-radius: 12px;
            font-weight: 600;
            cursor: pointer;
        }

        .btn-primary:hover {
            background: #0a1c2f;
        }

        .alert {
            padding: 14px 16px;
            border-radius: 12px;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .alert-success {
            background: rgba(46, 125, 50, 0.1);
            border-left: 3px solid #2e7d32;
            color: #2e7d32;
        }

        .alert-danger {
            background: rgba(220, 53, 69, 0.1);
            border-left: 3px solid #dc3545;
            color: #dc3545;
        }

        .form-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
        }

        @media (max-width: 768px) {
            .sidebar {
                transform: translateX(-100%);
                position: fixed;
                z-index: 1000;
            }

            .sidebar.mobile-open {
                transform: translateX(0);
            }

            .main-content {
                margin-left: 0;
            }

            .form-row {
                grid-template-columns: 1fr;
            }
        }

        .hamburger {
            display: none;
            background: none;
            border: none;
            font-size: 24px;
            cursor: pointer;
        }

        @media (max-width: 1024px) {
            .hamburger {
                display: block;
            }
        }
    </style>
    <link rel="stylesheet" href="../assets/css/nexus-theme.css">
</head>

<body>
    <div class="wrapper">
        <aside class="sidebar" id="sidebar">
            <div class="sidebar-header">
                <a href="dashboard.php" class="logo">
                    <div class="logo-icon"><i class="fas fa-building-columns"></i></div>
                    <span class="logo-text">Oakshire Finance</span>
                </a>
            </div>
            <nav class="nav-menu">
                <div class="nav-link"><a href="dashboard.php">Dashboard</a></div>
                <div class="nav-link"><a href="manage-users.php">Manage Users</a></div>
                <div class="nav-link"><a href="pending-transactions.php" class="active">Pending Transactions</a></div>
                <div class="nav-link"><a href="sms-settings.php">SMS Settings</a></div>
            </nav>
        </aside>

        <main class="main-content">
            <header class="top-header">
                <button class="hamburger" id="hamburger"><i class="fas fa-bars"></i></button>
                <div class="page-title">
                    <h1>SMS Verification Settings</h1>
                </div>
            </header>

            <div class="content-area">
                <?php if ($success): ?>
                    <div class="alert alert-success"><i class="fas fa-check-circle"></i> <?= $success ?></div>
                <?php endif; ?>
                <?php if ($error): ?>
                    <div class="alert alert-danger"><i class="fas fa-exclamation-circle"></i> <?= $error ?></div>
                <?php endif; ?>

                <div class="card">
                    <h2><i class="fas fa-sliders-h"></i> SMS Configuration</h2>
                    <form method="POST">
                        <div class="form-row">
                            <div class="form-group">
                                <label>Enable SMS Verification</label>
                                <select name="setting_sms_enabled">
                                    <option value="1" <?= ($settings['sms_enabled'] ?? '1') == '1' ? 'selected' : '' ?>>
                                        Enabled</option>
                                    <option value="0" <?= ($settings['sms_enabled'] ?? '1') == '0' ? 'selected' : '' ?>>
                                        Disabled</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label>OTP Expiry (Hours)</label>
                                <input type="number" name="setting_otp_expiry_hours"
                                    value="<?= $settings['otp_expiry_hours'] ?? 24 ?>" min="1" max="72">
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group">
                                <label>Max Resend Attempts</label>
                                <input type="number" name="setting_max_resend_attempts"
                                    value="<?= $settings['max_resend_attempts'] ?? 3 ?>" min="1" max="10">
                            </div>
                            <div class="form-group">
                                <label>SMS Sender ID</label>
                                <input type="text" name="setting_sms_sender_id"
                                    value="<?= $settings['sms_sender_id'] ?? 'Oakshire Finance' ?>"
                                    placeholder="Oakshire Finance">
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group">
                                <label>Require Admin Approval</label>
                                <select name="setting_require_admin_approval">
                                    <option value="1" <?= ($settings['require_admin_approval'] ?? '1') == '1' ? 'selected' : '' ?>>Yes</option>
                                    <option value="0" <?= ($settings['require_admin_approval'] ?? '1') == '0' ? 'selected' : '' ?>>No</option>
                                </select>
                            </div>
                        </div>
                        <button type="submit" class="btn-primary"><i class="fas fa-save"></i> Save Settings</button>
                    </form>
                </div>

                <div class="card">
                    <h2><i class="fas fa-info-circle"></i> SMS Verification Flow</h2>
                    <div style="background: #f8f9fc; padding: 20px; border-radius: 12px;">
                        <ol style="margin-left: 20px; line-height: 2;">
                            <li>Admin creates a pending transaction for a user</li>
                            <li>Admin sends SMS #1 (first verification code)</li>
                            <li>User enters code #1 → Verified</li>
                            <li>Admin sends SMS #2 (second verification code)</li>
                            <li>User enters code #2 → Verified</li>
                            <li>Admin sends SMS #3 (third verification code)</li>
                            <li>User enters code #3 → Verified</li>
                            <li>Admin approves the transaction</li>
                            <li>Transaction is processed</li>
                        </ol>
                    </div>
                </div>
            </div>
        </main>
    </div>

    <script>
        const hamburger = document.getElementById('hamburger');
        const sidebar = document.getElementById('sidebar');
        if (hamburger) {
            hamburger.addEventListener('click', () => sidebar.classList.toggle('mobile-open'));
        }
    </script>
</body>

</html>