<?php $title = 'Settings - Ghana Anaesthesiologists Society'; ?>
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-white font-weight-bold"><i class="fas fa-cog mr-2"></i>Settings</h1>
</div>

<form method="POST" action="<?= $baseUrl ?>/settings/update" enctype="multipart/form-data">
    <?php foreach ($settingsGroups as $group => $settings): ?>
    <div class="glass-card mb-4">
        <div class="mb-3" style="border-bottom: 1px solid rgba(255,255,255,0.08); padding-bottom: 0.8rem;">
            <h5 class="font-weight-bold mb-0 text-capitalize" style="color: #4e73df;"><?= htmlspecialchars($group) ?> Settings</h5>
        </div>
        <div class="row">
            <?php foreach ($settings as $setting): ?>
            <div class="col-md-6 mb-3">
                <label style="color: rgba(255,255,255,0.7); font-size: 0.9rem;"><?= htmlspecialchars(ucwords(str_replace('_', ' ', $setting->key))) ?></label>
                <?php if (in_array($setting->key, ['company_logo', 'company_favicon'])): ?>
                <div class="custom-file">
                    <input type="file" class="custom-file-input glass-input" name="<?= htmlspecialchars($setting->key) ?>" id="<?= htmlspecialchars($setting->key) ?>" accept="image/*" style="padding: 0.4rem;">
                    <label class="custom-file-label" for="<?= htmlspecialchars($setting->key) ?>" style="background: rgba(255,255,255,0.06); border: 1px solid rgba(255,255,255,0.1); color: rgba(255,255,255,0.5);">Choose file...</label>
                </div>
                <?php if ($setting->value): ?>
                <div class="mt-2">
                    <img src="<?= $baseUrl . '/' . ltrim($setting->value, '/') ?>" alt="<?= htmlspecialchars($setting->key) ?>" style="max-height: 50px; border-radius: 8px;">
                </div>
                <?php endif; ?>
                <?php elseif ($setting->type === 'boolean'): ?>
                <select class="form-control glass-input" name="<?= htmlspecialchars($setting->key) ?>">
                    <option value="true" <?= $setting->value === 'true' ? 'selected' : '' ?>>Enabled</option>
                    <option value="false" <?= $setting->value === 'false' ? 'selected' : '' ?>>Disabled</option>
                </select>
                <?php elseif ($setting->type === 'textarea'): ?>
                <textarea class="form-control glass-input" name="<?= htmlspecialchars($setting->key) ?>" rows="3"><?= htmlspecialchars($setting->value) ?></textarea>
                <?php elseif ($setting->type === 'select'): ?>
                <?php if ($setting->key === 'payment_reminder_frequency'): ?>
                <select class="form-control glass-input" name="<?= htmlspecialchars($setting->key) ?>">
                    <option value="weekly" <?= $setting->value === 'weekly' ? 'selected' : '' ?>>Weekly</option>
                    <option value="monthly" <?= $setting->value === 'monthly' ? 'selected' : '' ?>>Monthly</option>
                </select>
                <?php elseif ($setting->key === 'default_user_role'): ?>
                <select class="form-control glass-input" name="<?= htmlspecialchars($setting->key) ?>">
                    <option value="viewer" <?= $setting->value === 'viewer' ? 'selected' : '' ?>>Viewer (Limited Access)</option>
                    <option value="editor" <?= $setting->value === 'editor' ? 'selected' : '' ?>>Editor (Moderate Access)</option>
                    <option value="admin" <?= $setting->value === 'admin' ? 'selected' : '' ?>>Admin (Full Access)</option>
                </select>
                <?php elseif ($setting->key === 'sms_provider'): ?>
                <select class="form-control glass-input" name="<?= htmlspecialchars($setting->key) ?>">
                    <option value="hubtel" <?= $setting->value === 'hubtel' ? 'selected' : '' ?>>Hubtel</option>
                </select>
                <?php else: ?>
                <input type="text" class="form-control glass-input" name="<?= htmlspecialchars($setting->key) ?>" value="<?= htmlspecialchars($setting->value) ?>">
                <?php endif; ?>
                <?php else: ?>
                <input type="<?= strpos($setting->key, 'password') !== false ? 'password' : $setting->type ?>" class="form-control glass-input" name="<?= htmlspecialchars($setting->key) ?>" value="<?= htmlspecialchars($setting->value) ?>" <?= strpos($setting->key, 'password') !== false ? 'autocomplete="off"' : '' ?>>
                <?php endif; ?>
            </div>
            <?php endforeach; ?>
        </div>

        <?php if ($group === 'mail'): ?>
        <div class="mt-3 pt-3" style="border-top: 1px solid rgba(255,255,255,0.08);">
            <button type="button" class="glass-btn glass-btn-primary" data-toggle="modal" data-target="#testMailModal"><i class="fas fa-envelope mr-1"></i>Send Test Email</button>
            <small style="color: rgba(255,255,255,0.4); margin-left: 1rem;">Verify your SMTP configuration by sending a test message</small>
        </div>
        <div class="mt-2 pt-2" style="border-top: 1px solid rgba(255,255,255,0.08);">
            <div style="color: rgba(255,255,255,0.6); font-size: 0.85rem;">
                <i class="fas fa-info-circle mr-1"></i>
                Configure SMS settings below to enable password reset codes via phone. Hubtel requires an API key and secret from your Hubtel account.
            </div>
        </div>
        <?php elseif ($group === 'payment'): ?>
        <div class="mt-3 pt-3" style="border-top: 1px solid rgba(255,255,255,0.08);">
            <div style="color: rgba(255,255,255,0.6); font-size: 0.85rem;">
                <i class="fas fa-info-circle mr-1"></i>
                Payment reminders are sent via the cron endpoint: <code style="color: #1cc88a;"><?= $baseUrl ?>/cron/payment-reminder</code>
                <br>Set up a weekly/monthly cron job to call this URL automatically.
                <br>The reminder uses the <code>{name}</code>, <code>{month}</code>, <code>{email}</code> placeholders in the message template.
            </div>
        </div>
        <?php elseif ($group === 'general'): ?>
        <div class="mt-3 pt-3" style="border-top: 1px solid rgba(255,255,255,0.08);">
            <div style="color: rgba(255,255,255,0.6); font-size: 0.85rem;">
                <i class="fas fa-info-circle mr-1"></i>
                General settings control registration, default user roles on sign-up, and company contact information.
            </div>
        </div>
        <?php endif; ?>
    </div>
    <?php endforeach; ?>

    <div class="text-right">
        <button type="submit" class="glass-btn glass-btn-primary btn-lg"><i class="fas fa-save mr-1"></i>Save All Settings</button>
    </div>
</form>

<!-- Database Management -->
<div class="glass-card mb-4">
    <div class="mb-3" style="border-bottom: 1px solid rgba(255,255,255,0.08); padding-bottom: 0.8rem;">
        <h5 class="font-weight-bold mb-0" style="color: #4e73df;"><i class="fas fa-database mr-2"></i>Database Management</h5>
    </div>
    <div class="row">
        <div class="col-md-6 mb-3">
            <p style="color: rgba(255,255,255,0.7); font-size: 0.9rem;">Current database size: <strong><?= number_format($dbSize, 2) ?> MB</strong></p>
            <a href="<?= $baseUrl ?>/settings/backup" class="glass-btn glass-btn-primary" download><i class="fas fa-download mr-1"></i>Download Database Backup</a>
            <small class="d-block mt-2" style="color: rgba(255,255,255,0.4);">Downloads a complete SQL backup of all database tables.</small>
        </div>
        <div class="col-md-6 mb-3">
            <form method="POST" action="<?= $baseUrl ?>/settings/restore" enctype="multipart/form-data">
                <label style="color: rgba(255,255,255,0.7); font-size: 0.9rem;">Restore from SQL File</label>
                <div class="custom-file mb-2">
                    <input type="file" class="custom-file-input glass-input" name="sql_file" id="sqlFile" accept=".sql" required>
                    <label class="custom-file-label" for="sqlFile" style="background: rgba(255,255,255,0.06); border: 1px solid rgba(255,255,255,0.1); color: rgba(255,255,255,0.5);">Choose .sql file...</label>
                </div>
                <button type="submit" class="glass-btn" style="border-color: rgba(231,74,59,0.3); color: #e74a3b;" onclick="return confirm('WARNING: This will overwrite your current database. Are you sure?')"><i class="fas fa-upload mr-1"></i>Restore Database</button>
            </form>
        </div>
    </div>
</div>

<!-- Database Tables Info -->
<div class="glass-card mb-4">
    <div class="mb-3" style="border-bottom: 1px solid rgba(255,255,255,0.08); padding-bottom: 0.8rem;">
        <h5 class="font-weight-bold mb-0" style="color: #4e73df;"><i class="fas fa-table mr-2"></i>Database Tables</h5>
    </div>
    <div class="glass-table">
        <table class="table table-sm">
            <thead><tr><th>Table</th><th>Engine</th><th>Rows</th><th>Size (KB)</th><th>Created</th></tr></thead>
            <tbody>
                <?php foreach ($tables as $t): ?>
                <tr>
                    <td><?= htmlspecialchars($t->Name) ?></td>
                    <td><?= htmlspecialchars($t->Engine) ?></td>
                    <td><?= number_format($t->Rows) ?></td>
                    <td><?= number_format(($t->Data_length + $t->Index_length) / 1024, 1) ?></td>
                    <td><?= $t->Create_time ? date('M d, Y', strtotime($t->Create_time)) : '-' ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- Test Email Modal -->
<div class="modal fade" id="testMailModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content glass-modal-content">
            <form method="POST" action="<?= $baseUrl ?>/settings/test-email">
                <div class="modal-header">
                    <h5 class="modal-title">Send Test Email</h5>
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span></button>
                </div>
                <div class="modal-body">
                    <p style="color: rgba(255,255,255,0.7);">Send a test email to verify your SMTP configuration is working correctly.</p>
                    <div class="form-group">
                        <label style="color: rgba(255,255,255,0.7);">Recipient Email</label>
                        <input type="email" name="test_email" class="form-control glass-input" value="<?= htmlspecialchars($_SESSION['user_email'] ?? '') ?>" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="glass-btn" data-dismiss="modal">Cancel</button>
                    <button type="submit" class="glass-btn glass-btn-primary"><i class="fas fa-paper-plane mr-1"></i>Send Test</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
document.querySelectorAll('.custom-file-input').forEach(function(input) {
    input.addEventListener('change', function(e) {
        var fileName = this.files[0] ? this.files[0].name : 'Choose file...';
        var label = this.nextElementSibling;
        label.textContent = fileName;
    });
});
</script>
