<?php
/**
 * @var array $settings
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-cog me-2 text-gold"></i>Settings</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-sliders-h card-icon"></i>
            <h6 class="card-title">System Settings</h6>
        </div>
    </div>
    <div class="card-body">
        <ul class="nav nav-tabs nav-tabs-gold mb-4" id="settingsTabs" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link active" id="general-tab" data-bs-toggle="tab" data-bs-target="#general" type="button" role="tab">
                    <i class="fas fa-building me-1"></i>General
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="hours-tab" data-bs-toggle="tab" data-bs-target="#hours" type="button" role="tab">
                    <i class="fas fa-clock me-1"></i>Business Hours
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="loyalty-tab" data-bs-toggle="tab" data-bs-target="#loyalty" type="button" role="tab">
                    <i class="fas fa-star me-1"></i>Loyalty
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="notifications-tab" data-bs-toggle="tab" data-bs-target="#notifications" type="button" role="tab">
                    <i class="fas fa-bell me-1"></i>Notifications
                </button>
            </li>
        </ul>

        <form method="POST" action="<?= url('settings/update') ?>">
            <?= csrf_field() ?>

            <div class="tab-content">
                <div class="tab-pane fade show active" id="general" role="tabpanel">
                    <h6 class="text-gold mb-3"><i class="fas fa-building me-2"></i>General Details</h6>
                    <div class="row g-3">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Application Name</label>
                                <input type="text" name="app_name" class="form-control bg-transparent"
                                       value="<?= e($settings['app_name'] ?? APP_NAME) ?>" maxlength="100">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Email Address</label>
                                <input type="email" name="app_email" class="form-control bg-transparent"
                                       value="<?= e($settings['app_email'] ?? '') ?>" maxlength="255">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Phone Number</label>
                                <input type="text" name="app_phone" class="form-control bg-transparent"
                                       value="<?= e($settings['app_phone'] ?? '') ?>" maxlength="20">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Address</label>
                                <input type="text" name="app_address" class="form-control bg-transparent"
                                       value="<?= e($settings['app_address'] ?? '') ?>" maxlength="500">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label class="form-label">Timezone</label>
                                <select name="timezone" class="form-select bg-transparent">
                                    <?php $tz = $settings['timezone'] ?? APP_TIMEZONE; ?>
                                    <option value="Asia/Colombo" <?= $tz === 'Asia/Colombo' ? 'selected' : '' ?>>Asia/Colombo</option>
                                    <option value="Asia/Kolkata" <?= $tz === 'Asia/Kolkata' ? 'selected' : '' ?>>Asia/Kolkata</option>
                                    <option value="Asia/Dubai" <?= $tz === 'Asia/Dubai' ? 'selected' : '' ?>>Asia/Dubai</option>
                                    <option value="America/New_York" <?= $tz === 'America/New_York' ? 'selected' : '' ?>>America/New_York</option>
                                    <option value="Europe/London" <?= $tz === 'Europe/London' ? 'selected' : '' ?>>Europe/London</option>
                                    <option value="UTC" <?= $tz === 'UTC' ? 'selected' : '' ?>>UTC</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label class="form-label">Currency</label>
                                <select name="currency" class="form-select bg-transparent">
                                    <?php $cur = $settings['currency'] ?? APP_CURRENCY; ?>
                                    <option value="LKR" <?= $cur === 'LKR' ? 'selected' : '' ?>>LKR - Sri Lankan Rupee</option>
                                    <option value="USD" <?= $cur === 'USD' ? 'selected' : '' ?>>USD - US Dollar</option>
                                    <option value="EUR" <?= $cur === 'EUR' ? 'selected' : '' ?>>EUR - Euro</option>
                                    <option value="GBP" <?= $cur === 'GBP' ? 'selected' : '' ?>>GBP - British Pound</option>
                                    <option value="AED" <?= $cur === 'AED' ? 'selected' : '' ?>>AED - UAE Dirham</option>
                                    <option value="INR" <?= $cur === 'INR' ? 'selected' : '' ?>>INR - Indian Rupee</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label class="form-label">Currency Symbol</label>
                                <input type="text" name="currency_symbol" class="form-control bg-transparent"
                                       value="<?= e($settings['currency_symbol'] ?? APP_CURRENCY_SYMBOL) ?>" maxlength="10">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label class="form-label">Tax Label</label>
                                <input type="text" name="tax_label" class="form-control bg-transparent"
                                       value="<?= e($settings['tax_label'] ?? APP_TAX_LABEL) ?>" maxlength="50">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label class="form-label">Tax Rate (%)</label>
                                <input type="number" step="0.01" name="tax_rate" class="form-control bg-transparent"
                                       value="<?= e($settings['tax_rate'] ?? APP_TAX_RATE) ?>" min="0" max="100">
                            </div>
                        </div>
                    </div>
                </div>

                <div class="tab-pane fade" id="hours" role="tabpanel">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Opening Time</label>
                                <input type="time" name="start_time" class="form-control bg-transparent"
                                       value="<?= e($settings['start_time'] ?? '09:00') ?>">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Closing Time</label>
                                <input type="time" name="end_time" class="form-control bg-transparent"
                                       value="<?= e($settings['end_time'] ?? '19:00') ?>">
                            </div>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label mb-2">Working Days</label>
                        <div class="d-flex flex-wrap gap-3">
                            <?php $workingDays = isset($settings['working_days']) ? explode(',', $settings['working_days']) : ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']; ?>
                            <?php foreach (daysOfWeek() as $day): ?>
                                <div class="form-check">
                                    <input type="checkbox" name="working_days[]" class="form-check-input"
                                           value="<?= $day ?>" id="day_<?= $day ?>"
                                           <?= in_array($day, $workingDays) ? 'checked' : '' ?>>
                                    <label class="form-check-label" for="day_<?= $day ?>"><?= $day ?></label>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>

                <div class="tab-pane fade" id="loyalty" role="tabpanel">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <div class="form-check form-switch">
                                    <input type="hidden" name="loyalty_enabled" value="0">
                                    <input type="checkbox" name="loyalty_enabled" id="loyalty_enabled"
                                           class="form-check-input" value="1"
                                           <?= ($settings['loyalty_enabled'] ?? '1') == '1' ? 'checked' : '' ?>>
                                    <label class="form-check-label" for="loyalty_enabled">Enable Loyalty Points</label>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Points per <?= APP_CURRENCY_SYMBOL ?></label>
                                <input type="number" step="0.01" name="points_per_amount" class="form-control bg-transparent"
                                       value="<?= e($settings['points_per_amount'] ?? '1') ?>" min="0">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label"><?= APP_CURRENCY_SYMBOL ?> required per point</label>
                                <input type="number" step="0.01" name="amount_per_point" class="form-control bg-transparent"
                                       value="<?= e($settings['amount_per_point'] ?? '100') ?>" min="0">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Points expiry (days)</label>
                                <input type="number" name="points_expiry_days" class="form-control bg-transparent"
                                       value="<?= e($settings['points_expiry_days'] ?? '365') ?>" min="0">
                            </div>
                        </div>
                    </div>
                </div>

                <div class="tab-pane fade" id="notifications" role="tabpanel">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <div class="form-check form-switch">
                                    <input type="hidden" name="notifications_enabled" value="0">
                                    <input type="checkbox" name="notifications_enabled" id="notifications_enabled"
                                           class="form-check-input" value="1"
                                           <?= ($settings['notifications_enabled'] ?? '1') == '1' ? 'checked' : '' ?>>
                                    <label class="form-check-label" for="notifications_enabled">Enable Notifications</label>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Reminder Hours</label>
                                <input type="number" name="reminder_hours" class="form-control bg-transparent"
                                       value="<?= e($settings['reminder_hours'] ?? '24') ?>" min="0">
                                <small class="text-muted">Hours before appointment to send reminder</small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="mt-4 d-flex gap-2">
                <button type="submit" class="btn btn-gold">
                    <i class="fas fa-save me-1"></i>Save Settings
                </button>
                <a href="<?= url('settings/backup') ?>" class="btn btn-outline-gold">
                    <i class="fas fa-database me-1"></i>Backup Management
                </a>
            </div>
        </form>
    </div>
</div>
