<?php include __DIR__ . '/../../layouts/header.php'; ?>
<?php include __DIR__ . '/../../layouts/sidebar.php'; ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2 class="mb-0"><?= __('settings') ?></h2>
</div>

<?php if (isset($_GET['success'])): ?>
    <div class="alert alert-success border-0 shadow-sm mb-4 rounded-4" role="alert">
        <i class="fas fa-check-circle me-2"></i> <?= htmlspecialchars($_GET['success']) ?>
    </div>
<?php endif; ?>

<?php if (isset($_GET['error'])): ?>
    <div class="alert alert-danger border-0 shadow-sm mb-4 rounded-4" role="alert">
        <i class="fas fa-exclamation-circle me-2"></i> <?= htmlspecialchars($_GET['error']) ?>
    </div>
<?php endif; ?>

<!-- Tabs Navigation -->
<ul class="nav nav-pills mb-4 gap-2" id="settingsTabs" role="tablist">
    <li class="nav-item" role="presentation">
        <button class="btn <?= $activeTab === 'global' ? 'btn-primary' : 'btn-outline-primary' ?> rounded-pill px-4" onclick="window.location.href='?page=settings&tab=global'">
            <i class="fas fa-sliders-h me-2"></i> <?= __('global_settings') ?>
        </button>
    </li>
    <li class="nav-item" role="presentation">
        <button class="btn <?= $activeTab === 'profile' ? 'btn-primary' : 'btn-outline-primary' ?> rounded-pill px-4" onclick="window.location.href='?page=settings&tab=profile'">
            <i class="fas fa-user-circle me-2"></i> <?= __('admin_profile') ?>
        </button>
    </li>
    <li class="nav-item" role="presentation">
        <button class="btn <?= $activeTab === 'security' ? 'btn-primary' : 'btn-outline-primary' ?> rounded-pill px-4" onclick="window.location.href='?page=settings&tab=security'">
            <i class="fas fa-key me-2"></i> <?= __('change_password') ?>
        </button>
    </li>
    <li class="nav-item" role="presentation">
        <button class="btn <?= $activeTab === 'system' ? 'btn-primary' : 'btn-outline-primary' ?> rounded-pill px-4" onclick="window.location.href='?page=settings&tab=system'">
            <i class="fas fa-info-circle me-2"></i> <?= __('system_info') ?>
        </button>
    </li>
</ul>

<div class="row">
    <div class="col-lg-8">
        <?php if ($activeTab === 'global'): ?>
            <!-- Global Settings Tab -->
            <div class="card border-0 shadow-sm rounded-4 mb-4">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-4"><?= __('global_settings') ?></h5>
                    
                    <?php if (empty($settings)): ?>
                        <div class="text-center py-5 text-muted"><?= __('no_data_found') ?></div>
                    <?php else: ?>
                        <form action="?page=settings&action=update_settings" method="POST">
                            <?php foreach ($settings as $setting): ?>
                                <div class="mb-4">
                                    <label class="form-label text-secondary small fw-bold">
                                        <?= htmlspecialchars(ucwords(str_replace('_', ' ', $setting['setting_key']))) ?>
                                    </label>
                                    <?php if ($setting['setting_type'] === 'boolean'): ?>
                                        <div class="form-check form-switch">
                                            <input type="hidden" name="settings[<?= htmlspecialchars($setting['setting_key']) ?>]" value="0">
                                            <input class="form-check-input" type="checkbox" role="switch" name="settings[<?= htmlspecialchars($setting['setting_key']) ?>]" value="1" <?= $setting['setting_value'] == '1' ? 'checked' : '' ?>>
                                        </div>
                                    <?php elseif ($setting['setting_type'] === 'json'): ?>
                                        <textarea class="form-control" name="settings[<?= htmlspecialchars($setting['setting_key']) ?>]" rows="3" style="border-radius: 12px;"><?= htmlspecialchars($setting['setting_value']) ?></textarea>
                                    <?php elseif ($setting['setting_type'] === 'number'): ?>
                                        <input type="number" class="form-control" name="settings[<?= htmlspecialchars($setting['setting_key']) ?>]" value="<?= htmlspecialchars($setting['setting_value']) ?>" style="border-radius: 12px;">
                                    <?php else: ?>
                                        <input type="text" class="form-control" name="settings[<?= htmlspecialchars($setting['setting_key']) ?>]" value="<?= htmlspecialchars($setting['setting_value']) ?>" style="border-radius: 12px;">
                                    <?php endif; ?>
                                </div>
                            <?php endforeach; ?>
                            <div class="mt-4">
                                <button type="submit" class="btn btn-primary rounded-pill px-5 py-2.5 fw-bold shadow-sm">
                                    <i class="fas fa-save me-1"></i> <?= __('save_changes') ?>
                                </button>
                            </div>
                        </form>
                    <?php endif; ?>
                </div>
            </div>

        <?php elseif ($activeTab === 'profile'): ?>
            <!-- Admin Profile Tab -->
            <div class="card border-0 shadow-sm rounded-4 mb-4">
                <div class="card-body p-4">
                    <div class="text-center mb-5 mt-3">
                        <div class="bg-primary text-white rounded-circle d-inline-flex align-items-center justify-content-center mb-3 shadow-sm" style="width: 80px; height: 80px; font-size: 30px; font-weight: bold;">
                            <?= strtoupper(substr($admin['name'] ?? 'A', 0, 1)) ?>
                        </div>
                        <h4 class="fw-bold mb-1"><?= htmlspecialchars($admin['name'] ?? '') ?></h4>
                        <p class="text-secondary small"><?= htmlspecialchars($admin['email'] ?? '') ?></p>
                    </div>

                    <form action="?page=settings&action=update_profile" method="POST">
                        <div class="row g-4">
                            <div class="col-md-6">
                                <label class="form-label text-secondary small fw-bold"><?= __('full_name') ?></label>
                                <input type="text" name="name" class="form-control form-control-lg bg-light border-0 px-4" value="<?= htmlspecialchars($admin['name'] ?? '') ?>" required style="border-radius: 12px; font-size: 14.5px;">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label text-secondary small fw-bold"><?= __('email_address') ?></label>
                                <input type="email" name="email" class="form-control form-control-lg bg-light border-0 px-4" value="<?= htmlspecialchars($admin['email'] ?? '') ?>" required style="border-radius: 12px; font-size: 14.5px;">
                            </div>
                            <div class="col-12 mt-5">
                                <button type="submit" class="btn btn-primary rounded-pill px-5 py-2.5 fw-bold shadow-sm w-100">
                                    <i class="fas fa-user-edit me-1"></i> <?= __('update_profile') ?>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

        <?php elseif ($activeTab === 'security'): ?>
            <!-- Change Password Tab -->
            <div class="card border-0 shadow-sm rounded-4 mb-4">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-1"><?= __('change_password') ?></h5>
                    <p class="text-secondary small mb-4"><?= __('ensure_secure_password') ?></p>

                    <form action="?page=settings&action=update_password" method="POST">
                        <div class="mb-4">
                            <label class="form-label text-secondary small fw-bold"><?= __('current_password') ?></label>
                            <div class="input-group">
                                <span class="input-group-text bg-light border-0 px-3" style="border-radius: 12px 0 0 12px;"><i class="fas fa-lock text-muted"></i></span>
                                <input type="password" name="old_password" class="form-control form-control-lg bg-light border-0 px-3" placeholder="<?= __('enter_current_password') ?>" required style="border-radius: 0 12px 12px 0; font-size: 14.5px;">
                            </div>
                        </div>

                        <hr class="my-4 opacity-25">

                        <div class="mb-4">
                            <label class="form-label text-secondary small fw-bold"><?= __('new_password') ?></label>
                            <div class="input-group">
                                <span class="input-group-text bg-light border-0 px-3" style="border-radius: 12px 0 0 12px;"><i class="fas fa-key text-muted"></i></span>
                                <input type="password" name="new_password" class="form-control form-control-lg bg-light border-0 px-3" placeholder="<?= __('create_new_password') ?>" required style="border-radius: 0 12px 12px 0; font-size: 14.5px;">
                            </div>
                        </div>

                        <div class="mb-4">
                            <label class="form-label text-secondary small fw-bold"><?= __('confirm_password') ?></label>
                            <div class="input-group">
                                <span class="input-group-text bg-light border-0 px-3" style="border-radius: 12px 0 0 12px;"><i class="fas fa-shield-alt text-muted"></i></span>
                                <input type="password" name="confirm_password" class="form-control form-control-lg bg-light border-0 px-3" placeholder="<?= __('confirm_new_password') ?>" required style="border-radius: 0 12px 12px 0; font-size: 14.5px;">
                            </div>
                        </div>

                        <div class="mt-5">
                            <button type="submit" class="btn btn-primary rounded-pill px-5 py-2.5 fw-bold shadow-sm w-100">
                                <i class="fas fa-shield-alt me-1"></i> <?= __('securely_update_password') ?>
                            </button>
                        </div>
                    </form>
                </div>
            </div>

        <?php elseif ($activeTab === 'system'): ?>
            <!-- System Information Tab -->
            <div class="card border-0 shadow-sm rounded-4 mb-4">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-4"><?= __('system_info') ?></h5>
                    <ul class="list-group list-group-flush">
                        <li class="list-group-item px-0 d-flex justify-content-between py-3">
                            <span class="text-secondary"><?= __('app_version') ?></span>
                            <span class="fw-bold text-dark">v1.0.0</span>
                        </li>
                        <li class="list-group-item px-0 d-flex justify-content-between py-3">
                            <span class="text-secondary"><?= __('environment') ?></span>
                            <span class="badge bg-primary px-3 py-2 rounded-pill">Production</span>
                        </li>
                        <li class="list-group-item px-0 d-flex justify-content-between py-3">
                            <span class="text-secondary"><?= __('php_version') ?></span>
                            <span class="fw-bold text-dark"><?= phpversion() ?></span>
                        </li>
                        <li class="list-group-item px-0 d-flex justify-content-between py-3">
                            <span class="text-secondary"><?= __('timezone') ?></span>
                            <span class="fw-bold text-dark"><?= date_default_timezone_get() ?></span>
                        </li>
                    </ul>
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php include __DIR__ . '/../../layouts/footer.php'; ?>
