<?php 
$pageTitle = 'System Settings';
include __DIR__ . '/../layouts/header.php'; 

// Fetch settings for display (already passed from controller)
$settings = $settings ?? [];
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-gear me-2"></i>System Settings</h2>
</div>

<?php if (isset($_SESSION['success'])): ?>
    <div class="alert alert-success alert-dismissible fade show">
        <?= htmlspecialchars($_SESSION['success']); unset($_SESSION['success']); ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
<?php endif; ?>
<?php if (isset($_SESSION['error'])): ?>
    <div class="alert alert-danger alert-dismissible fade show">
        <?= htmlspecialchars($_SESSION['error']); unset($_SESSION['error']); ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
<?php endif; ?>

<ul class="nav nav-tabs mb-4">
    <li class="nav-item">
        <a class="nav-link active" data-bs-toggle="tab" href="#general">
            <i class="bi bi-building me-1"></i>General
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" data-bs-toggle="tab" href="#email">
            <i class="bi bi-envelope me-1"></i>Email
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" data-bs-toggle="tab" href="#sms">
            <i class="bi bi-chat-dots me-1"></i>SMS
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" data-bs-toggle="tab" href="#exchange">
            <i class="bi bi-currency-exchange me-1"></i>Exchange Rates
        </a>
    </li>
</ul>

<form method="post" action="<?= $this->baseUrl('/settings/save') ?>" enctype="multipart/form-data">
    <div class="tab-content">
        <!-- General Tab -->
        <div class="tab-pane fade show active" id="general">
            <div class="card">
                <div class="card-body">
                    <!-- Logo Upload -->
                    <div class="mb-3">
                        <label class="form-label">Company Logo</label>
                        <div class="d-flex align-items-center gap-3">
                            <?php if (!empty($settings['logo'])): ?>
                                <img src="<?= $this->baseUrl('/' . $settings['logo']) ?>" 
                                     alt="Logo" style="max-height:60px; max-width:200px;" class="border rounded p-1">
                            <?php endif; ?>
                            <input type="file" name="logo" class="form-control" accept="image/*">
                        </div>
                        <small class="text-muted">Recommended size: 200×60px. Leave empty to keep current logo.</small>
                    </div>

                    <!-- Company Name -->
                    <div class="mb-3">
                        <label class="form-label">Company Name</label>
                        <input type="text" name="company_name" class="form-control" 
                               value="<?= htmlspecialchars($settings['company_name'] ?? 'Finance Manager') ?>">
                    </div>

                    <!-- Base Currency -->
                    <div class="mb-3">
                        <label class="form-label">Base Currency</label>
                        <select name="base_currency" class="form-select">
                            <option value="KES" <?= ($settings['base_currency'] ?? '') === 'KES' ? 'selected' : '' ?>>KES – Kenyan Shilling</option>
                            <option value="USD" <?= ($settings['base_currency'] ?? '') === 'USD' ? 'selected' : '' ?>>USD – US Dollar</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <!-- Email Tab -->
        <div class="tab-pane fade" id="email">
            <div class="card">
                <div class="card-body">
                    <div class="mb-3">
                        <label class="form-label">SMTP Host</label>
                        <input type="text" name="smtp_host" class="form-control" 
                               value="<?= htmlspecialchars($settings['smtp_host'] ?? '') ?>">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">SMTP Port</label>
                        <input type="number" name="smtp_port" class="form-control" 
                               value="<?= htmlspecialchars($settings['smtp_port'] ?? '587') ?>">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Username</label>
                        <input type="text" name="smtp_username" class="form-control" 
                               value="<?= htmlspecialchars($settings['smtp_username'] ?? '') ?>">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Password</label>
                        <input type="password" name="smtp_password" class="form-control" 
                               value="<?= htmlspecialchars($settings['smtp_password'] ?? '') ?>">
                    </div>
                </div>
            </div>
        </div>

        <!-- SMS Tab -->
        <div class="tab-pane fade" id="sms">
            <div class="card">
                <div class="card-body">
                    <div class="mb-3">
                        <label class="form-label">Africa's Talking API Key</label>
                        <input type="text" name="at_api_key" class="form-control" 
                               value="<?= htmlspecialchars($settings['at_api_key'] ?? '') ?>">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Sender ID</label>
                        <input type="text" name="at_sender_id" class="form-control" 
                               value="<?= htmlspecialchars($settings['at_sender_id'] ?? '') ?>">
                    </div>
                </div>
            </div>
        </div>

        <!-- Exchange Rates Tab -->
        <div class="tab-pane fade" id="exchange">
            <div class="card">
                <div class="card-body">
                    <div class="mb-3">
                        <label class="form-label">Exchange Rate API Key</label>
                        <input type="text" name="exchange_rate_api_key" class="form-control" 
                               value="<?= htmlspecialchars($settings['exchange_rate_api_key'] ?? '') ?>">
                        <small class="text-muted">Get a free key from <a href="https://www.exchangerate-api.com" target="_blank">exchangerate-api.com</a></small>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="mt-4">
        <button type="submit" class="btn btn-primary">
            <i class="bi bi-save"></i> Save Settings
        </button>
    </div>
</form>

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