<?php
// ============================================
// ADMIN CONFIGURATION PANEL
// Complete ready-to-use file
// ============================================

session_start();
require_once 'config.php';

// ============================================
// ADMIN LOGIN
// ============================================
$admin_password = 'admin123'; // Change this password

// Check if logged in
if (!isset($_SESSION['admin_logged_in'])) {
    $_SESSION['admin_logged_in'] = false;
}

// Handle login
if (isset($_POST['login'])) {
    if ($_POST['password'] == $admin_password) {
        $_SESSION['admin_logged_in'] = true;
        $_SESSION['success'] = 'Logged in successfully!';
    } else {
        $_SESSION['error'] = 'Wrong password!';
    }
    header('Location: admin_config.php');
    exit;
}

// Handle logout
if (isset($_GET['logout'])) {
    $_SESSION['admin_logged_in'] = false;
    session_destroy();
    header('Location: admin_config.php');
    exit;
}

// ============================================
// HANDLE FORM SUBMISSIONS
// ============================================
if ($_SESSION['admin_logged_in']) {
    
    // 1. Add/Update Doctor
    if (isset($_POST['save_doctor'])) {
        $id = intval($_POST['doctor_id'] ?? 0);
        $name = mysqli_real_escape_string($conn, $_POST['name']);
        $qualification = mysqli_real_escape_string($conn, $_POST['qualification'] ?? '');
        $specialty = mysqli_real_escape_string($conn, $_POST['specialty'] ?? '');
        $description = mysqli_real_escape_string($conn, $_POST['description'] ?? '');
        $status = $_POST['status'] ?? 'active';
        $display_order = intval($_POST['display_order'] ?? 0);

        $image_path = '';
        if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
            $target_dir = "uploads/doctors/";
            if (!is_dir($target_dir)) mkdir($target_dir, 0777, true);
            $image_name = time() . '_' . basename($_FILES['image']['name']);
            $target_file = $target_dir . $image_name;
            if (move_uploaded_file($_FILES['image']['tmp_name'], $target_file)) {
                $image_path = $target_file;
            }
        }

        if ($id > 0) {
            $query = "UPDATE doctors SET 
                      name='$name', 
                      qualification='$qualification', 
                      specialty='$specialty', 
                      description='$description', 
                      status='$status', 
                      display_order=$display_order";
            if ($image_path) $query .= ", image='$image_path'";
            $query .= " WHERE id=$id";
            mysqli_query($conn, $query);
            $_SESSION['success'] = "Doctor updated successfully!";
        } else {
            $query = "INSERT INTO doctors (name, qualification, specialty, description, image, status, display_order) 
                      VALUES ('$name', '$qualification', '$specialty', '$description', '$image_path', '$status', $display_order)";
            mysqli_query($conn, $query);
            $_SESSION['success'] = "Doctor added successfully!";
        }
        header('Location: admin_config.php#doctors');
        exit;
    }

    // 2. Delete Doctor
    if (isset($_GET['delete_doctor'])) {
        $id = intval($_GET['delete_doctor']);
        mysqli_query($conn, "DELETE FROM doctors WHERE id=$id");
        $_SESSION['success'] = "Doctor deleted!";
        header('Location: admin_config.php#doctors');
        exit;
    }

    // 3. Add/Update Chamber
    if (isset($_POST['save_chamber'])) {
        $id = intval($_POST['chamber_id'] ?? 0);
        $name = mysqli_real_escape_string($conn, $_POST['name']);
        $address = mysqli_real_escape_string($conn, $_POST['address']);
        $phone = mysqli_real_escape_string($conn, $_POST['phone'] ?? '');
        $status = $_POST['status'] ?? 'active';
        $display_order = intval($_POST['display_order'] ?? 0);

        $image_path = '';
        if (isset($_FILES['chamber_image']) && $_FILES['chamber_image']['error'] == 0) {
            $target_dir = "uploads/chambers/";
            if (!is_dir($target_dir)) mkdir($target_dir, 0777, true);
            $image_name = time() . '_' . basename($_FILES['chamber_image']['name']);
            $target_file = $target_dir . $image_name;
            if (move_uploaded_file($_FILES['chamber_image']['tmp_name'], $target_file)) {
                $image_path = $target_file;
            }
        }

        if ($id > 0) {
            $query = "UPDATE chambers SET 
                      name='$name', 
                      address='$address', 
                      phone='$phone', 
                      status='$status', 
                      display_order=$display_order";
            if ($image_path) $query .= ", image='$image_path'";
            $query .= " WHERE id=$id";
            mysqli_query($conn, $query);
            $_SESSION['success'] = "Chamber updated!";
        } else {
            $query = "INSERT INTO chambers (name, address, phone, image, status, display_order) 
                      VALUES ('$name', '$address', '$phone', '$image_path', '$status', $display_order)";
            mysqli_query($conn, $query);
            $_SESSION['success'] = "Chamber added!";
        }
        header('Location: admin_config.php#chambers');
        exit;
    }

    // 4. Delete Chamber
    if (isset($_GET['delete_chamber'])) {
        $id = intval($_GET['delete_chamber']);
        mysqli_query($conn, "DELETE FROM chambers WHERE id=$id");
        $_SESSION['success'] = "Chamber deleted!";
        header('Location: admin_config.php#chambers');
        exit;
    }

    // 5. Save Schedule
    if (isset($_POST['save_schedule'])) {
        $chamber_id = intval($_POST['schedule_chamber_id']);
        mysqli_query($conn, "DELETE FROM chamber_schedules WHERE chamber_id=$chamber_id");
        
        foreach ($_POST['schedule'] as $day => $data) {
            if (isset($data['active']) && $data['active'] == 1) {
                $start = mysqli_real_escape_string($conn, $data['start']);
                $end = mysqli_real_escape_string($conn, $data['end']);
                $duration = intval($data['duration'] ?? 30);
                $is_free_camp = isset($data['free_camp']) ? 1 : 0;
                $status = $data['status'] ?? 'active';
                
                $query = "INSERT INTO chamber_schedules 
                          (chamber_id, day_of_week, start_time, end_time, slot_duration, is_free_camp, status) 
                          VALUES ($chamber_id, $day, '$start', '$end', $duration, $is_free_camp, '$status')";
                mysqli_query($conn, $query);
            }
        }
        $_SESSION['success'] = "Schedule saved!";
        header('Location: admin_config.php#schedule');
        exit;
    }

    // 6. Save Settings (including SMS)
    if (isset($_POST['save_settings'])) {
        $settings = array(
            'min_booking_days' => isset($_POST['settings']['min_booking_days']) ? $_POST['settings']['min_booking_days'] : '3',
            'max_booking_days' => isset($_POST['settings']['max_booking_days']) ? $_POST['settings']['max_booking_days'] : '30',
            'slot_duration' => isset($_POST['settings']['slot_duration']) ? $_POST['settings']['slot_duration'] : '30',
            'booking_fees' => isset($_POST['settings']['booking_fees']) ? $_POST['settings']['booking_fees'] : '{"regular":200,"special":500,"free_medicine":100}',
            'enable_sms' => isset($_POST['settings']['enable_sms']) ? $_POST['settings']['enable_sms'] : 'false',
            'sms_api_key' => isset($_POST['settings']['sms_api_key']) ? $_POST['settings']['sms_api_key'] : '',
            'sms_sender_id' => isset($_POST['settings']['sms_sender_id']) ? $_POST['settings']['sms_sender_id'] : '8809617610000',
            'sms_username' => isset($_POST['settings']['sms_username']) ? $_POST['settings']['sms_username'] : '',
            'sms_password' => isset($_POST['settings']['sms_password']) ? $_POST['settings']['sms_password'] : '',
            'sms_api_url' => isset($_POST['settings']['sms_api_url']) ? $_POST['settings']['sms_api_url'] : 'https://bulksmsbd.net/api/smsapi',
            'admin_phone' => isset($_POST['settings']['admin_phone']) ? $_POST['settings']['admin_phone'] : '01775121112'
        );
        
        foreach ($settings as $key => $value) {
            $value = mysqli_real_escape_string($conn, $value);
            $check = mysqli_query($conn, "SELECT id FROM admin_settings WHERE setting_key='$key'");
            if (mysqli_num_rows($check) > 0) {
                mysqli_query($conn, "UPDATE admin_settings SET setting_value='$value' WHERE setting_key='$key'");
            } else {
                mysqli_query($conn, "INSERT INTO admin_settings (setting_key, setting_value, setting_type) VALUES ('$key', '$value', 'text')");
            }
        }
        $_SESSION['success'] = "All settings saved successfully!";
        header('Location: admin_config.php#settings');
        exit;
    }

    // 7. Test SMS
    if (isset($_POST['test_sms'])) {
        $test_number = isset($_POST['test_phone']) ? $_POST['test_phone'] : ADMIN_PHONE;
        $test_message = "Test SMS from Doctor Portal. Server time: " . date('Y-m-d H:i:s');
        
        if (function_exists('sendSMS')) {
            $result = sendSMS($test_number, $test_message);
            if ($result) {
                $_SESSION['sms_test'] = "✅ SMS sent successfully to $test_number!";
            } else {
                $_SESSION['sms_test'] = "❌ SMS failed to send. Check API Key and settings.";
            }
        } else {
            $_SESSION['sms_test'] = "❌ SMS function not available. Check config.php";
        }
        header('Location: admin_config.php#settings');
        exit;
    }

    // 8. Add Hero Slide
    if (isset($_POST['save_hero_slide'])) {
        $id = intval($_POST['slide_id'] ?? 0);
        $image = mysqli_real_escape_string($conn, $_POST['image']);
        $title = mysqli_real_escape_string($conn, $_POST['title']);
        $subtitle = mysqli_real_escape_string($conn, $_POST['subtitle']);
        $button_text = mysqli_real_escape_string($conn, $_POST['button_text']);
        $button_link = mysqli_real_escape_string($conn, $_POST['button_link']);
        $button_secondary_text = mysqli_real_escape_string($conn, $_POST['button_secondary_text']);
        $button_secondary_link = mysqli_real_escape_string($conn, $_POST['button_secondary_link']);
        $display_order = intval($_POST['display_order'] ?? 0);
        $is_active = isset($_POST['is_active']) ? 1 : 0;

        if ($id > 0) {
            $query = "UPDATE hero_slides SET 
                      image='$image', 
                      title='$title', 
                      subtitle='$subtitle', 
                      button_text='$button_text', 
                      button_link='$button_link', 
                      button_secondary_text='$button_secondary_text', 
                      button_secondary_link='$button_secondary_link', 
                      display_order=$display_order, 
                      is_active=$is_active 
                      WHERE id=$id";
            mysqli_query($conn, $query);
            $_SESSION['success'] = "Hero slide updated!";
        } else {
            $query = "INSERT INTO hero_slides (image, title, subtitle, button_text, button_link, button_secondary_text, button_secondary_link, display_order, is_active) 
                      VALUES ('$image', '$title', '$subtitle', '$button_text', '$button_link', '$button_secondary_text', '$button_secondary_link', $display_order, $is_active)";
            mysqli_query($conn, $query);
            $_SESSION['success'] = "Hero slide added!";
        }
        header('Location: admin_config.php#hero');
        exit;
    }

    // 9. Delete Hero Slide
    if (isset($_GET['delete_slide'])) {
        $id = intval($_GET['delete_slide']);
        mysqli_query($conn, "DELETE FROM hero_slides WHERE id=$id");
        $_SESSION['success'] = "Hero slide deleted!";
        header('Location: admin_config.php#hero');
        exit;
    }
}

// ============================================
// FETCH DATA
// ============================================
$doctors = mysqli_query($conn, "SELECT * FROM doctors ORDER BY display_order, name");
$chambers = mysqli_query($conn, "SELECT * FROM chambers ORDER BY display_order, name");
$hero_slides = mysqli_query($conn, "SELECT * FROM hero_slides ORDER BY display_order");

// Get settings
$settings_result = mysqli_query($conn, "SELECT * FROM admin_settings");
$settings = array();
if ($settings_result) {
    while ($row = mysqli_fetch_assoc($settings_result)) {
        $settings[$row['setting_key']] = $row;
    }
}

function getSetting($key, $default = '') {
    global $settings;
    return isset($settings[$key]) ? $settings[$key]['setting_value'] : $default;
}

// Get all chambers for schedule dropdown
$all_chambers = mysqli_query($conn, "SELECT * FROM chambers WHERE status='active' ORDER BY name");
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Configuration - ডা. মোঃ ফেরদৌস</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        :root {
            --primary: #1a5276;
            --primary-light: #2e86c1;
            --sidebar-width: 250px;
        }
        
        body {
            background: #f0f2f5;
            font-family: 'SolaimanLipi', 'Hind Siliguri', Arial, sans-serif;
        }
        
        /* Login Page */
        .login-container {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #1a5276, #2e86c1);
        }
        .login-card {
            background: white;
            padding: 40px;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            max-width: 400px;
            width: 100%;
        }
        .login-card .logo {
            text-align: center;
            margin-bottom: 30px;
        }
        .login-card .logo i {
            font-size: 48px;
            color: #1a5276;
            background: #eaf4f9;
            padding: 15px;
            border-radius: 50%;
        }
        .login-card h3 {
            text-align: center;
            color: #1a5276;
            margin-bottom: 25px;
        }
        .login-card .form-control {
            border-radius: 12px;
            padding: 12px 16px;
            border: 2px solid #e8ecf1;
        }
        .login-card .form-control:focus {
            border-color: #1a5276;
            box-shadow: 0 0 0 4px rgba(26,82,118,0.1);
        }
        .login-card .btn-primary {
            background: linear-gradient(135deg, #1a5276, #2e86c1);
            border: none;
            padding: 12px;
            border-radius: 12px;
            font-weight: 600;
            width: 100%;
        }
        .login-card .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 25px rgba(26,82,118,0.3);
        }

        /* Admin Panel */
        .admin-header {
            background: white;
            padding: 15px 25px;
            box-shadow: 0 2px 15px rgba(0,0,0,0.08);
            position: sticky;
            top: 0;
            z-index: 100;
        }
        .admin-header .brand {
            font-size: 20px;
            font-weight: 700;
            color: #1a5276;
        }
        .admin-header .brand i {
            margin-right: 10px;
        }
        
        .admin-container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 20px;
        }
        
        /* Tabs */
        .nav-tabs {
            border: none;
            gap: 5px;
            flex-wrap: nowrap;
            overflow-x: auto;
            padding-bottom: 10px;
        }
        .nav-tabs .nav-link {
            border: none;
            border-radius: 12px;
            padding: 12px 24px;
            font-weight: 600;
            color: #6c757d;
            background: white;
            box-shadow: 0 2px 8px rgba(0,0,0,0.04);
            transition: all 0.3s;
            white-space: nowrap;
        }
        .nav-tabs .nav-link:hover {
            background: #eaf4f9;
            color: #1a5276;
        }
        .nav-tabs .nav-link.active {
            background: #1a5276;
            color: white;
            box-shadow: 0 4px 15px rgba(26,82,118,0.3);
        }
        .nav-tabs .nav-link i {
            margin-right: 8px;
        }
        
        .tab-content {
            background: white;
            border-radius: 16px;
            padding: 25px;
            box-shadow: 0 2px 15px rgba(0,0,0,0.06);
            margin-top: 5px;
        }
        
        /* Cards */
        .card {
            border: none;
            border-radius: 16px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.06);
            margin-bottom: 20px;
        }
        .card-header {
            background: white;
            border-bottom: 2px solid #f0f2f5;
            padding: 15px 20px;
            font-weight: 700;
            border-radius: 16px 16px 0 0 !important;
        }
        .card-header .badge {
            font-size: 12px;
            padding: 5px 12px;
        }
        
        /* Lists */
        .item-list {
            max-height: 500px;
            overflow-y: auto;
        }
        .item-card {
            background: #f8f9fa;
            border-radius: 12px;
            padding: 15px;
            margin-bottom: 10px;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            gap: 15px;
        }
        .item-card:hover {
            background: #eaf4f9;
            transform: translateX(5px);
        }
        .item-card .avatar {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            object-fit: cover;
            border: 3px solid #1a5276;
            flex-shrink: 0;
        }
        .item-card .avatar-placeholder {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: #dee2e6;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #6c757d;
            font-size: 20px;
            flex-shrink: 0;
        }
        .item-card .info {
            flex: 1;
        }
        .item-card .info h6 {
            margin: 0;
            font-weight: 600;
        }
        .item-card .info small {
            color: #6c757d;
        }
        .item-card .actions {
            display: flex;
            gap: 5px;
        }
        .item-card .actions .btn {
            padding: 5px 10px;
            border-radius: 8px;
            font-size: 13px;
        }
        
        /* Forms */
        .form-control, .form-select {
            border-radius: 10px;
            border: 2px solid #e8ecf1;
            padding: 10px 14px;
            transition: all 0.3s;
        }
        .form-control:focus, .form-select:focus {
            border-color: #1a5276;
            box-shadow: 0 0 0 4px rgba(26,82,118,0.1);
        }
        .form-label {
            font-weight: 600;
            color: #2c3e50;
            font-size: 14px;
        }
        
        .btn-primary {
            background: linear-gradient(135deg, #1a5276, #2e86c1);
            border: none;
            padding: 10px 25px;
            border-radius: 10px;
            font-weight: 600;
            transition: all 0.3s;
        }
        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 25px rgba(26,82,118,0.3);
        }
        .btn-success {
            background: #28a745;
            border: none;
            padding: 10px 25px;
            border-radius: 10px;
            font-weight: 600;
        }
        .btn-danger {
            border-radius: 10px;
            padding: 10px 25px;
            font-weight: 600;
        }
        .btn-warning {
            border-radius: 10px;
            padding: 10px 25px;
            font-weight: 600;
        }
        .btn-outline-secondary {
            border-radius: 10px;
            padding: 10px 25px;
            font-weight: 600;
        }
        
        .settings-group {
            padding: 15px 0;
            border-bottom: 1px solid #f0f2f5;
        }
        .settings-group:last-child {
            border-bottom: none;
        }
        .settings-group label {
            font-weight: 600;
            color: #2c3e50;
        }
        .settings-group .help-text {
            color: #6c757d;
            font-size: 13px;
            margin-top: 3px;
        }
        
        .alert {
            border-radius: 12px;
        }
        
        /* Responsive */
        @media (max-width: 768px) {
            .admin-header { padding: 10px 15px; }
            .admin-header .brand { font-size: 16px; }
            .nav-tabs .nav-link { padding: 8px 14px; font-size: 13px; }
            .tab-content { padding: 15px; }
            .item-card { flex-wrap: wrap; }
            .item-card .actions { width: 100%; justify-content: flex-end; }
        }
        
        @media (max-width: 576px) {
            .nav-tabs { flex-wrap: nowrap; }
            .nav-tabs .nav-link { font-size: 12px; padding: 6px 12px; }
            .item-card .avatar { width: 40px; height: 40px; }
        }
    </style>
</head>
<body>

<?php if (!$_SESSION['admin_logged_in']): ?>
    <!-- ============================================
         LOGIN PAGE
         ============================================ -->
    <div class="login-container">
        <div class="login-card">
            <div class="logo">
                <i class="fas fa-user-md"></i>
            </div>
            <h3>🔐 Admin Login</h3>
            
            <?php if (isset($_SESSION['error'])): ?>
                <div class="alert alert-danger"><?php echo $_SESSION['error']; unset($_SESSION['error']); ?></div>
            <?php endif; ?>
            
            <form method="post">
                <div class="mb-3">
                    <label class="form-label">Password</label>
                    <input type="password" name="password" class="form-control" required placeholder="Enter admin password">
                </div>
                <button type="submit" name="login" class="btn btn-primary">
                    <i class="fas fa-lock"></i> Login
                </button>
            </form>
            <div class="text-center mt-3">
                <small class="text-muted">Default password: <strong>admin123</strong></small>
            </div>
        </div>
    </div>

<?php else: ?>
    <!-- ============================================
         ADMIN PANEL
         ============================================ -->
    
    <!-- Header -->
    <div class="admin-header">
        <div class="d-flex justify-content-between align-items-center">
            <div class="brand">
                <i class="fas fa-cog"></i> Admin Configuration
            </div>
            <div>
                <a href="?logout=1" class="btn btn-danger btn-sm">
                    <i class="fas fa-sign-out-alt"></i> Logout
                </a>
            </div>
        </div>
    </div>

    <div class="admin-container">
        
        <!-- Success/Error Messages -->
        <?php if (isset($_SESSION['success'])): ?>
            <div class="alert alert-success alert-dismissible fade show">
                <i class="fas fa-check-circle"></i> <?php echo $_SESSION['success']; unset($_SESSION['success']); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>
        
        <?php if (isset($_SESSION['sms_test'])): ?>
            <div class="alert alert-<?php echo strpos($_SESSION['sms_test'], '✅') !== false ? 'success' : 'danger'; ?> alert-dismissible fade show">
                <?php echo $_SESSION['sms_test']; unset($_SESSION['sms_test']); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>

        <!-- Tabs -->
        <ul class="nav nav-tabs" id="configTabs" role="tablist">
            <li class="nav-item">
                <a class="nav-link active" id="doctors-tab" data-bs-toggle="tab" href="#doctors">
                    <i class="fas fa-user-md"></i> Doctors
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" id="chambers-tab" data-bs-toggle="tab" href="#chambers">
                    <i class="fas fa-hospital"></i> Chambers
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" id="schedule-tab" data-bs-toggle="tab" href="#schedule">
                    <i class="fas fa-clock"></i> Schedules
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" id="hero-tab" data-bs-toggle="tab" href="#hero">
                    <i class="fas fa-image"></i> Hero Slides
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" id="settings-tab" data-bs-toggle="tab" href="#settings">
                    <i class="fas fa-cog"></i> Settings
                </a>
            </li>
        </ul>

        <div class="tab-content">
            
            <!-- ==========================================
                 DOCTORS TAB
                 ========================================== -->
            <div class="tab-pane fade show active" id="doctors">
                <div class="row">
                    <!-- Add/Edit Form -->
                    <div class="col-lg-4">
                        <div class="card">
                            <div class="card-header">
                                <?php echo isset($_GET['edit_doctor']) ? '✏️ Edit Doctor' : '➕ Add Doctor'; ?>
                            </div>
                            <div class="card-body">
                                <form method="post" enctype="multipart/form-data">
                                    <?php 
                                    $edit_doctor = null;
                                    if (isset($_GET['edit_doctor'])) {
                                        $id = intval($_GET['edit_doctor']);
                                        $result = mysqli_query($conn, "SELECT * FROM doctors WHERE id=$id");
                                        $edit_doctor = mysqli_fetch_assoc($result);
                                    }
                                    ?>
                                    <input type="hidden" name="doctor_id" value="<?php echo $edit_doctor['id'] ?? 0; ?>">
                                    
                                    <div class="mb-2">
                                        <label class="form-label">Name *</label>
                                        <input type="text" name="name" class="form-control" required 
                                               value="<?php echo htmlspecialchars($edit_doctor['name'] ?? ''); ?>">
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Qualification</label>
                                        <input type="text" name="qualification" class="form-control" 
                                               value="<?php echo htmlspecialchars($edit_doctor['qualification'] ?? ''); ?>">
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Specialty</label>
                                        <input type="text" name="specialty" class="form-control" 
                                               value="<?php echo htmlspecialchars($edit_doctor['specialty'] ?? ''); ?>">
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Description</label>
                                        <textarea name="description" class="form-control" rows="2"><?php echo htmlspecialchars($edit_doctor['description'] ?? ''); ?></textarea>
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Image</label>
                                        <?php if (!empty($edit_doctor['image']) && file_exists($edit_doctor['image'])): ?>
                                            <div class="mb-1"><img src="<?php echo $edit_doctor['image']; ?>" style="max-width:80px;border-radius:8px;"></div>
                                        <?php endif; ?>
                                        <input type="file" name="image" class="form-control" accept="image/*">
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Status</label>
                                        <select name="status" class="form-select">
                                            <option value="active" <?php echo ($edit_doctor['status'] ?? '') == 'active' ? 'selected' : ''; ?>>Active</option>
                                            <option value="inactive" <?php echo ($edit_doctor['status'] ?? '') == 'inactive' ? 'selected' : ''; ?>>Inactive</option>
                                        </select>
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Display Order</label>
                                        <input type="number" name="display_order" class="form-control" 
                                               value="<?php echo $edit_doctor['display_order'] ?? 0; ?>">
                                    </div>
                                    <button type="submit" name="save_doctor" class="btn btn-primary w-100">
                                        <i class="fas fa-save"></i> <?php echo isset($_GET['edit_doctor']) ? 'Update' : 'Add'; ?> Doctor
                                    </button>
                                    <?php if (isset($_GET['edit_doctor'])): ?>
                                        <a href="admin_config.php#doctors" class="btn btn-outline-secondary w-100 mt-2">Cancel</a>
                                    <?php endif; ?>
                                </form>
                            </div>
                        </div>
                    </div>
                    
                    <!-- List -->
                    <div class="col-lg-8">
                        <div class="card">
                            <div class="card-header">
                                <i class="fas fa-list"></i> Doctor List
                                <span class="badge bg-primary float-end"><?php echo mysqli_num_rows($doctors); ?></span>
                            </div>
                            <div class="card-body item-list">
                                <?php if ($doctors && mysqli_num_rows($doctors) > 0): ?>
                                    <?php while ($doctor = mysqli_fetch_assoc($doctors)): ?>
                                        <div class="item-card">
                                            <?php if (!empty($doctor['image']) && file_exists($doctor['image'])): ?>
                                                <img src="<?php echo $doctor['image']; ?>" class="avatar" alt="<?php echo $doctor['name']; ?>">
                                            <?php else: ?>
                                                <div class="avatar-placeholder"><i class="fas fa-user-md"></i></div>
                                            <?php endif; ?>
                                            <div class="info">
                                                <h6><?php echo htmlspecialchars($doctor['name']); ?></h6>
                                                <small><?php echo htmlspecialchars($doctor['qualification'] ?? ''); ?> - <?php echo htmlspecialchars($doctor['specialty'] ?? ''); ?></small>
                                                <span class="badge bg-<?php echo ($doctor['status'] ?? 'inactive') == 'active' ? 'success' : 'secondary'; ?> ms-2">
                                                    <?php echo $doctor['status'] ?? 'inactive'; ?>
                                                </span>
                                            </div>
                                            <div class="actions">
                                                <a href="?edit_doctor=<?php echo $doctor['id']; ?>" class="btn btn-primary btn-sm">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <a href="?delete_doctor=<?php echo $doctor['id']; ?>" class="btn btn-danger btn-sm" 
                                                   onclick="return confirm('Delete this doctor?')">
                                                    <i class="fas fa-trash"></i>
                                                </a>
                                            </div>
                                        </div>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <div class="text-center text-muted py-4">
                                        <i class="fas fa-user-md fa-3x mb-3 d-block"></i>
                                        <p>No doctors found. Add your first doctor!</p>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- ==========================================
                 CHAMBERS TAB
                 ========================================== -->
            <div class="tab-pane fade" id="chambers">
                <div class="row">
                    <div class="col-lg-4">
                        <div class="card">
                            <div class="card-header">
                                <?php echo isset($_GET['edit_chamber']) ? '✏️ Edit Chamber' : '➕ Add Chamber'; ?>
                            </div>
                            <div class="card-body">
                                <form method="post" enctype="multipart/form-data">
                                    <?php 
                                    $edit_chamber = null;
                                    if (isset($_GET['edit_chamber'])) {
                                        $id = intval($_GET['edit_chamber']);
                                        $result = mysqli_query($conn, "SELECT * FROM chambers WHERE id=$id");
                                        $edit_chamber = mysqli_fetch_assoc($result);
                                    }
                                    ?>
                                    <input type="hidden" name="chamber_id" value="<?php echo $edit_chamber['id'] ?? 0; ?>">
                                    
                                    <div class="mb-2">
                                        <label class="form-label">Name *</label>
                                        <input type="text" name="name" class="form-control" required 
                                               value="<?php echo htmlspecialchars($edit_chamber['name'] ?? ''); ?>">
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Address *</label>
                                        <textarea name="address" class="form-control" required rows="2"><?php echo htmlspecialchars($edit_chamber['address'] ?? ''); ?></textarea>
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Phone</label>
                                        <input type="text" name="phone" class="form-control" 
                                               value="<?php echo htmlspecialchars($edit_chamber['phone'] ?? ''); ?>">
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Image</label>
                                        <?php if (!empty($edit_chamber['image']) && file_exists($edit_chamber['image'])): ?>
                                            <div class="mb-1"><img src="<?php echo $edit_chamber['image']; ?>" style="max-width:80px;border-radius:8px;"></div>
                                        <?php endif; ?>
                                        <input type="file" name="chamber_image" class="form-control" accept="image/*">
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Status</label>
                                        <select name="status" class="form-select">
                                            <option value="active" <?php echo ($edit_chamber['status'] ?? '') == 'active' ? 'selected' : ''; ?>>Active</option>
                                            <option value="inactive" <?php echo ($edit_chamber['status'] ?? '') == 'inactive' ? 'selected' : ''; ?>>Inactive</option>
                                        </select>
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Display Order</label>
                                        <input type="number" name="display_order" class="form-control" 
                                               value="<?php echo $edit_chamber['display_order'] ?? 0; ?>">
                                    </div>
                                    <button type="submit" name="save_chamber" class="btn btn-primary w-100">
                                        <i class="fas fa-save"></i> <?php echo isset($_GET['edit_chamber']) ? 'Update' : 'Add'; ?> Chamber
                                    </button>
                                    <?php if (isset($_GET['edit_chamber'])): ?>
                                        <a href="admin_config.php#chambers" class="btn btn-outline-secondary w-100 mt-2">Cancel</a>
                                    <?php endif; ?>
                                </form>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-lg-8">
                        <div class="card">
                            <div class="card-header">
                                <i class="fas fa-list"></i> Chamber List
                                <span class="badge bg-primary float-end"><?php echo mysqli_num_rows($chambers); ?></span>
                            </div>
                            <div class="card-body item-list">
                                <?php if ($chambers && mysqli_num_rows($chambers) > 0): ?>
                                    <?php while ($chamber = mysqli_fetch_assoc($chambers)): ?>
                                        <div class="item-card">
                                            <?php if (!empty($chamber['image']) && file_exists($chamber['image'])): ?>
                                                <img src="<?php echo $chamber['image']; ?>" class="avatar" alt="<?php echo $chamber['name']; ?>">
                                            <?php else: ?>
                                                <div class="avatar-placeholder"><i class="fas fa-hospital"></i></div>
                                            <?php endif; ?>
                                            <div class="info">
                                                <h6><?php echo htmlspecialchars($chamber['name']); ?></h6>
                                                <small><?php echo htmlspecialchars($chamber['address'] ?? ''); ?></small>
                                                <span class="badge bg-<?php echo ($chamber['status'] ?? 'inactive') == 'active' ? 'success' : 'secondary'; ?> ms-2">
                                                    <?php echo $chamber['status'] ?? 'inactive'; ?>
                                                </span>
                                            </div>
                                            <div class="actions">
                                                <a href="?edit_chamber=<?php echo $chamber['id']; ?>" class="btn btn-primary btn-sm">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <a href="?delete_chamber=<?php echo $chamber['id']; ?>" class="btn btn-danger btn-sm" 
                                                   onclick="return confirm('Delete this chamber?')">
                                                    <i class="fas fa-trash"></i>
                                                </a>
                                            </div>
                                        </div>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <div class="text-center text-muted py-4">
                                        <i class="fas fa-hospital fa-3x mb-3 d-block"></i>
                                        <p>No chambers found. Add your first chamber!</p>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- ==========================================
                 SCHEDULE TAB
                 ========================================== -->
            <div class="tab-pane fade" id="schedule">
                <div class="card">
                    <div class="card-header">📅 Chamber Schedules</div>
                    <div class="card-body">
                        <form method="post">
                            <div class="mb-3">
                                <label class="form-label">Select Chamber</label>
                                <select name="schedule_chamber_id" id="schedule_chamber_id" class="form-select">
                                    <option value="">-- Select Chamber --</option>
                                    <?php if ($all_chambers): ?>
                                        <?php while ($ch = mysqli_fetch_assoc($all_chambers)): ?>
                                            <option value="<?php echo $ch['id']; ?>" 
                                                <?php echo (isset($_GET['schedule_chamber']) && $_GET['schedule_chamber'] == $ch['id']) ? 'selected' : ''; ?>>
                                                <?php echo htmlspecialchars($ch['name']); ?>
                                            </option>
                                        <?php endwhile; ?>
                                    <?php endif; ?>
                                </select>
                            </div>

                            <div id="schedule_form" style="<?php echo isset($_GET['schedule_chamber']) ? 'display:block' : 'display:none'; ?>">
                                <?php 
                                $existing_schedule = array();
                                if (isset($_GET['schedule_chamber'])) {
                                    $ch_id = intval($_GET['schedule_chamber']);
                                    $sch_query = mysqli_query($conn, "SELECT * FROM chamber_schedules WHERE chamber_id=$ch_id");
                                    if ($sch_query) {
                                        while ($sch = mysqli_fetch_assoc($sch_query)) {
                                            $existing_schedule[$sch['day_of_week']] = $sch;
                                        }
                                    }
                                }
                                ?>
                                <div class="table-responsive">
                                    <table class="table table-bordered">
                                        <thead>
                                            <tr>
                                                <th>Day</th>
                                                <th>Active</th>
                                                <th>Start</th>
                                                <th>End</th>
                                                <th>Slot (min)</th>
                                                <th>Free Camp</th>
                                                <th>Status</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php 
                                            $days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
                                            foreach ($days as $index => $day_name): 
                                                $existing = isset($existing_schedule[$index]) ? $existing_schedule[$index] : null;
                                            ?>
                                            <tr>
                                                <td><strong><?php echo $day_name; ?></strong></td>
                                                <td>
                                                    <input type="checkbox" name="schedule[<?php echo $index; ?>][active]" 
                                                           class="day-active" <?php echo $existing ? 'checked' : ''; ?>>
                                                </td>
                                                <td>
                                                    <input type="time" name="schedule[<?php echo $index; ?>][start]" 
                                                           value="<?php echo $existing['start_time'] ?? '09:00'; ?>" 
                                                           <?php echo $existing ? '' : 'disabled'; ?>>
                                                </td>
                                                <td>
                                                    <input type="time" name="schedule[<?php echo $index; ?>][end]" 
                                                           value="<?php echo $existing['end_time'] ?? '17:00'; ?>" 
                                                           <?php echo $existing ? '' : 'disabled'; ?>>
                                                </td>
                                                <td>
                                                    <input type="number" name="schedule[<?php echo $index; ?>][duration]" 
                                                           value="<?php echo $existing['slot_duration'] ?? 30; ?>" 
                                                           <?php echo $existing ? '' : 'disabled'; ?> min="15" step="5">
                                                </td>
                                                <td>
                                                    <input type="checkbox" name="schedule[<?php echo $index; ?>][free_camp]" 
                                                           <?php echo ($existing && $existing['is_free_camp'] == 1) ? 'checked' : ''; ?> 
                                                           <?php echo $existing ? '' : 'disabled'; ?>>
                                                </td>
                                                <td>
                                                    <select name="schedule[<?php echo $index; ?>][status]" <?php echo $existing ? '' : 'disabled'; ?>>
                                                        <option value="active" <?php echo ($existing && $existing['status'] == 'active') ? 'selected' : ''; ?>>Active</option>
                                                        <option value="inactive" <?php echo ($existing && $existing['status'] == 'inactive') ? 'selected' : ''; ?>>Inactive</option>
                                                    </select>
                                                </td>
                                            </tr>
                                            <?php endforeach; ?>
                                        </tbody>
                                    </table>
                                </div>
                                <button type="submit" name="save_schedule" class="btn btn-primary">
                                    <i class="fas fa-save"></i> Save Schedule
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

            <!-- ==========================================
                 HERO SLIDES TAB
                 ========================================== -->
            <div class="tab-pane fade" id="hero">
                <div class="row">
                    <div class="col-lg-4">
                        <div class="card">
                            <div class="card-header">
                                <?php echo isset($_GET['edit_slide']) ? '✏️ Edit Slide' : '➕ Add Hero Slide'; ?>
                            </div>
                            <div class="card-body">
                                <form method="post">
                                    <?php 
                                    $edit_slide = null;
                                    if (isset($_GET['edit_slide'])) {
                                        $id = intval($_GET['edit_slide']);
                                        $result = mysqli_query($conn, "SELECT * FROM hero_slides WHERE id=$id");
                                        $edit_slide = mysqli_fetch_assoc($result);
                                    }
                                    ?>
                                    <input type="hidden" name="slide_id" value="<?php echo $edit_slide['id'] ?? 0; ?>">
                                    
                                    <div class="mb-2">
                                        <label class="form-label">Image URL *</label>
                                        <input type="text" name="image" class="form-control" required 
                                               value="<?php echo htmlspecialchars($edit_slide['image'] ?? ''); ?>" 
                                               placeholder="assets/images/hero-1.jpg">
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Title *</label>
                                        <input type="text" name="title" class="form-control" required 
                                               value="<?php echo htmlspecialchars($edit_slide['title'] ?? ''); ?>">
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Subtitle</label>
                                        <input type="text" name="subtitle" class="form-control" 
                                               value="<?php echo htmlspecialchars($edit_slide['subtitle'] ?? ''); ?>">
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Button Text</label>
                                        <input type="text" name="button_text" class="form-control" 
                                               value="<?php echo htmlspecialchars($edit_slide['button_text'] ?? ''); ?>">
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Button Link</label>
                                        <input type="text" name="button_link" class="form-control" 
                                               value="<?php echo htmlspecialchars($edit_slide['button_link'] ?? ''); ?>">
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Secondary Button Text</label>
                                        <input type="text" name="button_secondary_text" class="form-control" 
                                               value="<?php echo htmlspecialchars($edit_slide['button_secondary_text'] ?? ''); ?>">
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Secondary Button Link</label>
                                        <input type="text" name="button_secondary_link" class="form-control" 
                                               value="<?php echo htmlspecialchars($edit_slide['button_secondary_link'] ?? ''); ?>">
                                    </div>
                                    <div class="mb-2">
                                        <label class="form-label">Display Order</label>
                                        <input type="number" name="display_order" class="form-control" 
                                               value="<?php echo $edit_slide['display_order'] ?? 0; ?>">
                                    </div>
                                    <div class="mb-2">
                                        <div class="form-check">
                                            <input type="checkbox" name="is_active" class="form-check-input" id="is_active" 
                                                   <?php echo ($edit_slide['is_active'] ?? 1) == 1 ? 'checked' : ''; ?>>
                                            <label class="form-check-label" for="is_active">Active</label>
                                        </div>
                                    </div>
                                    <button type="submit" name="save_hero_slide" class="btn btn-primary w-100">
                                        <i class="fas fa-save"></i> <?php echo isset($_GET['edit_slide']) ? 'Update' : 'Add'; ?> Slide
                                    </button>
                                    <?php if (isset($_GET['edit_slide'])): ?>
                                        <a href="admin_config.php#hero" class="btn btn-outline-secondary w-100 mt-2">Cancel</a>
                                    <?php endif; ?>
                                </form>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-lg-8">
                        <div class="card">
                            <div class="card-header">
                                <i class="fas fa-images"></i> Hero Slides
                                <span class="badge bg-primary float-end"><?php echo mysqli_num_rows($hero_slides); ?></span>
                            </div>
                            <div class="card-body item-list">
                                <?php if ($hero_slides && mysqli_num_rows($hero_slides) > 0): ?>
                                    <?php while ($slide = mysqli_fetch_assoc($hero_slides)): ?>
                                        <div class="item-card">
                                            <?php if (!empty($slide['image']) && file_exists($slide['image'])): ?>
                                                <img src="<?php echo $slide['image']; ?>" class="avatar" alt="<?php echo $slide['title']; ?>" style="border-radius:8px;">
                                            <?php else: ?>
                                                <div class="avatar-placeholder"><i class="fas fa-image"></i></div>
                                            <?php endif; ?>
                                            <div class="info">
                                                <h6><?php echo htmlspecialchars($slide['title']); ?></h6>
                                                <small><?php echo htmlspecialchars($slide['subtitle'] ?? ''); ?></small>
                                                <span class="badge bg-<?php echo ($slide['is_active'] ?? 1) == 1 ? 'success' : 'secondary'; ?> ms-2">
                                                    <?php echo ($slide['is_active'] ?? 1) == 1 ? 'Active' : 'Inactive'; ?>
                                                </span>
                                                <span class="badge bg-info ms-1">Order: <?php echo $slide['display_order'] ?? 0; ?></span>
                                            </div>
                                            <div class="actions">
                                                <a href="?edit_slide=<?php echo $slide['id']; ?>" class="btn btn-primary btn-sm">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <a href="?delete_slide=<?php echo $slide['id']; ?>" class="btn btn-danger btn-sm" 
                                                   onclick="return confirm('Delete this slide?')">
                                                    <i class="fas fa-trash"></i>
                                                </a>
                                            </div>
                                        </div>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <div class="text-center text-muted py-4">
                                        <i class="fas fa-image fa-3x mb-3 d-block"></i>
                                        <p>No hero slides found. Add your first slide!</p>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- ==========================================
                 SETTINGS TAB
                 ========================================== -->
            <div class="tab-pane fade" id="settings">
                <form method="post">
                    <!-- General Settings -->
                    <div class="card">
                        <div class="card-header">⚙️ General Settings</div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-4">
                                    <div class="settings-group">
                                        <label>Minimum Booking Days</label>
                                        <input type="number" name="settings[min_booking_days]" class="form-control" 
                                               value="<?php echo htmlspecialchars(getSetting('min_booking_days', '3')); ?>">
                                        <div class="help-text">Days in advance required for booking</div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="settings-group">
                                        <label>Maximum Booking Days</label>
                                        <input type="number" name="settings[max_booking_days]" class="form-control" 
                                               value="<?php echo htmlspecialchars(getSetting('max_booking_days', '30')); ?>">
                                        <div class="help-text">Maximum days in advance allowed</div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="settings-group">
                                        <label>Default Slot Duration</label>
                                        <input type="number" name="settings[slot_duration]" class="form-control" 
                                               value="<?php echo htmlspecialchars(getSetting('slot_duration', '30')); ?>">
                                        <div class="help-text">Minutes per slot</div>
                                    </div>
                                </div>
                            </div>
                            <div class="settings-group">
                                <label>Booking Fees (JSON)</label>
                                <textarea name="settings[booking_fees]" class="form-control" rows="2"><?php echo htmlspecialchars(getSetting('booking_fees', '{"regular":200,"special":500,"free_medicine":100}')); ?></textarea>
                                <div class="help-text">Format: {"regular":200,"special":500,"free_medicine":100}</div>
                            </div>
                        </div>
                    </div>

                    <!-- SMS Configuration -->
                    <div class="card">
                        <div class="card-header" style="background:#e8f5e9;border-bottom:2px solid #4caf50;">
                            <i class="fas fa-sms text-success"></i> SMS Configuration (bulksmsbd.net)
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="settings-group">
                                        <label>Enable SMS</label>
                                        <select name="settings[enable_sms]" class="form-select">
                                            <option value="true" <?php echo getSetting('enable_sms') == 'true' ? 'selected' : ''; ?>>Yes</option>
                                            <option value="false" <?php echo getSetting('enable_sms') == 'false' ? 'selected' : ''; ?>>No</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="settings-group">
                                        <label>Admin Phone Number</label>
                                        <input type="text" name="settings[admin_phone]" class="form-control" 
                                               value="<?php echo htmlspecialchars(getSetting('admin_phone', '01775121112')); ?>">
                                        <div class="help-text">Admin phone for SMS alerts</div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="settings-group">
                                        <label>SMS API Key *</label>
                                        <input type="text" name="settings[sms_api_key]" class="form-control" 
                                               value="<?php echo htmlspecialchars(getSetting('sms_api_key', '')); ?>">
                                        <div class="help-text">Get from <a href="https://bulksmsbd.net" target="_blank">bulksmsbd.net</a> dashboard</div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="settings-group">
                                        <label>Sender ID</label>
                                        <input type="text" name="settings[sms_sender_id]" class="form-control" 
                                               value="<?php echo htmlspecialchars(getSetting('sms_sender_id', '8809617610000')); ?>">
                                        <div class="help-text">Your approved sender ID</div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="settings-group">
                                        <label>Username (Optional)</label>
                                        <input type="text" name="settings[sms_username]" class="form-control" 
                                               value="<?php echo htmlspecialchars(getSetting('sms_username', '')); ?>">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="settings-group">
                                        <label>Password (Optional)</label>
                                        <input type="text" name="settings[sms_password]" class="form-control" 
                                               value="<?php echo htmlspecialchars(getSetting('sms_password', '')); ?>">
                                    </div>
                                </div>
                            </div>
                            <div class="settings-group">
                                <label>API URL</label>
                                <input type="text" name="settings[sms_api_url]" class="form-control" 
                                       value="<?php echo htmlspecialchars(getSetting('sms_api_url', 'https://bulksmsbd.net/api/smsapi')); ?>">
                            </div>
                            
                            <div class="alert alert-info mt-3">
                                <i class="fas fa-info-circle"></i> 
                                <strong>How to get credentials:</strong> 
                                Login to <a href="https://bulksmsbd.net" target="_blank">bulksmsbd.net</a> → Dashboard → API Settings
                            </div>
                            
                            <div class="row mt-3">
                                <div class="col-md-8">
                                    <div class="settings-group">
                                        <label>Test Phone Number</label>
                                        <input type="text" name="test_phone" class="form-control" 
                                               value="<?php echo htmlspecialchars(getSetting('admin_phone', '01775121112')); ?>">
                                    </div>
                                </div>
                                <div class="col-md-4 d-flex align-items-end">
                                    <button type="submit" name="test_sms" class="btn btn-success w-100">
                                        <i class="fas fa-paper-plane"></i> Send Test SMS
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Save Button -->
                    <button type="submit" name="save_settings" class="btn btn-primary btn-lg">
                        <i class="fas fa-save"></i> Save All Settings
                    </button>
                </form>
            </div>

        </div><!-- end tab-content -->
    </div><!-- end admin-container -->

<?php endif; ?>

<!-- ============================================
     SCRIPTS
     ============================================ -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

<script>
// Auto-load schedule when chamber is selected
document.getElementById('schedule_chamber_id')?.addEventListener('change', function() {
    if (this.value) {
        window.location.href = 'admin_config.php?schedule_chamber=' + this.value + '#schedule';
    }
});

// Enable/disable schedule inputs
document.querySelectorAll('.day-active')?.forEach(function(cb) {
    cb.addEventListener('change', function() {
        var row = this.closest('tr');
        var inputs = row.querySelectorAll('input:not(.day-active), select');
        inputs.forEach(function(inp) {
            inp.disabled = !this.checked;
        });
    });
});

// Preserve tab after form submission
document.addEventListener('DOMContentLoaded', function() {
    var hash = window.location.hash;
    if (hash) {
        var tab = document.querySelector('[href="' + hash + '"]');
        if (tab) {
            var tabInstance = new bootstrap.Tab(tab);
            tabInstance.show();
        }
    }
});

// Auto-dismiss alerts
setTimeout(function() {
    document.querySelectorAll('.alert').forEach(function(el) {
        var alert = new bootstrap.Alert(el);
        alert.close();
    });
}, 5000);
</script>

</body>
</html>