<?= $this->extend('layouts/app') ?>

<?= $this->section('content') ?>

<style>
    /* ===== MODERN SAAS DESIGN SYSTEM TOKENS ===== */
    :root {
        --bg-body: #f8fafc;
        --bg-card: #ffffff;
        --bg-accent: #f1f5f9;
        --bg-input: #ffffff;
        --border-color: #e2e8f0;
        --border-hover: #cbd5e1;
        --text-main: #0f172a;
        --text-sub: #64748b;
        --primary: #4f46e5;
        --primary-hover: #4338ca;
        --primary-light: rgba(79, 70, 229, 0.08);
        --success: #10b981;
        --warning: #f59e0b;
        --danger: #ef4444;
        --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
        --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.07), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
        --radius-lg: 16px;
        --radius-md: 12px;
        --radius-sm: 8px;
        --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    }

    @media (prefers-color-scheme: dark) {
        :root {
            --bg-body: #0b0f19;
            --bg-card: #151e2e;
            --bg-accent: #1e293b;
            --bg-input: #0f172a;
            --border-color: #243044;
            --border-hover: #334155;
            --text-main: #f8fafc;
            --text-sub: #94a3b8;
            --primary-light: rgba(79, 70, 229, 0.18);
            --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
            --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
        }
    }

    [data-bs-theme="dark"] {
        --bg-body: #0b0f19;
        --bg-card: #151e2e;
        --bg-accent: #1e293b;
        --bg-input: #0f172a;
        --border-color: #243044;
        --border-hover: #334155;
        --text-main: #f8fafc;
        --text-sub: #94a3b8;
        --primary-light: rgba(79, 70, 229, 0.18);
        --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
        --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
    }

    body, .content-wrapper, .main-content, .content, .page-content, main, .setting-workspace {
        background-color: var(--bg-body) !important;
        color: var(--text-main) !important;
        transition: var(--transition);
    }

    .setting-card-container {
        max-width: 920px;
        margin: 0 auto;
    }

    /* Card Premium Styling */
    .card-modern {
        background: var(--bg-card);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-lg);
        padding: 1.75rem;
        box-shadow: var(--shadow-sm);
        transition: var(--transition);
        margin-bottom: 1.5rem;
    }

    .card-header-custom {
        display: flex;
        align-items: center;
        gap: 0.75rem;
        padding-bottom: 1.25rem;
        margin-bottom: 1.5rem;
        border-bottom: 1px solid var(--border-color);
    }

    .card-header-icon {
        width: 38px;
        height: 38px;
        border-radius: var(--radius-md);
        background: var(--primary-light);
        color: var(--primary);
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.2rem;
        flex-shrink: 0;
    }

    /* Form Modern Inputs */
    .form-control-modern, .form-select-modern {
        background-color: var(--bg-input) !important;
        border: 1px solid var(--border-color) !important;
        color: var(--text-main) !important;
        border-radius: var(--radius-md) !important;
        padding: 0.6rem 0.9rem !important;
        font-size: 0.9rem !important;
        transition: var(--transition) !important;
    }

    .form-control-modern:focus, .form-select-modern:focus {
        border-color: var(--primary) !important;
        box-shadow: 0 0 0 3px var(--primary-light) !important;
        outline: none !important;
    }

    .custom-input-addon {
        background-color: var(--bg-accent) !important;
        border: 1px solid var(--border-color) !important;
        color: var(--text-sub) !important;
        border-top-left-radius: var(--radius-md) !important;
        border-bottom-left-radius: var(--radius-md) !important;
    }

    .custom-input-addon-btn {
        background-color: var(--bg-accent) !important;
        border: 1px solid var(--border-color) !important;
        color: var(--text-sub) !important;
        border-top-right-radius: var(--radius-md) !important;
        border-bottom-right-radius: var(--radius-md) !important;
        transition: var(--transition);
    }

    .custom-input-addon-btn:hover {
        background-color: var(--border-color) !important;
        color: var(--text-main) !important;
    }
</style>

<div class="container-fluid py-4 setting-workspace">
    
    <div class="setting-card-container">
        
        <!-- PAGE HEADER -->
        <div class="d-flex flex-column flex-sm-row justify-content-between align-items-sm-center mb-4 gap-3">
            <div>
                <h3 class="fw-bold mb-0 text-main">Pengaturan Sistem</h3>
                <p class="text-sub small mb-0 mt-0.5">Kelola identitas entitas bisnis, preferensi aplikasi, dan konfigurasi server.</p>
            </div>
            <div>
                <button type="submit" form="settingForm" class="btn btn-primary rounded-3 px-4 py-2 fw-semibold d-inline-flex align-items-center gap-2 shadow-sm">
                    <i class="bi bi-floppy-fill"></i> Simpan Pengaturan
                </button>
            </div>
        </div>

        <!-- FLASH SUCCESS -->
        <?php if (session()->getFlashdata('success')) : ?>
            <div class="alert alert-success alert-dismissible fade show rounded-3 shadow-sm border-0 mb-4" role="alert">
                <div class="d-flex align-items-center gap-2">
                    <i class="bi bi-check-circle-fill fs-5 text-success"></i>
                    <div><?= session()->getFlashdata('success') ?></div>
                </div>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        <?php endif; ?>

        <!-- FLASH ERRORS -->
        <?php if (session()->getFlashdata('errors')) : ?>
            <div class="alert alert-danger alert-dismissible fade show rounded-3 shadow-sm border-0 mb-4" role="alert">
                <div class="d-flex align-items-start gap-2.5">
                    <i class="bi bi-exclamation-triangle-fill fs-5 text-danger flex-shrink-0 mt-0.5"></i>
                    <div>
                        <strong class="d-block mb-1">Terjadi kesalahan pada pengaturan:</strong>
                        <ul class="mb-0 ps-3 small">
                            <?php foreach (session()->getFlashdata('errors') as $error) : ?>
                                <li><?= $error ?></li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                </div>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        <?php endif; ?>

        <!-- FORM START -->
        <form id="settingForm" action="<?= base_url('superadmin/setting/save') ?>" method="post">
            <?= csrf_field() ?>

            <!-- SECTION 1: INFORMASI PERUSAHAAN -->
            <div class="card-modern">
                <div class="card-header-custom">
                    <div class="card-header-icon"><i class="bi bi-building"></i></div>
                    <div>
                        <h5 class="fw-bold mb-0 text-main">Informasi Perusahaan</h5>
                        <p class="text-sub small mb-0">Identitas fisik dan kontak utama organisasi/perusahaan.</p>
                    </div>
                </div>

                <div class="row g-3">
                    <!-- Nama Perusahaan -->
                    <div class="col-md-6">
                        <label class="form-label fw-semibold text-main small">Nama Perusahaan</label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-building"></i></span>
                            <input type="text" name="company_name" class="form-control form-control-modern" value="<?= esc($setting['company_name'] ?? '') ?>" placeholder="PT Contoh Maju Bersama">
                        </div>
                    </div>

                    <!-- Pemilik / Penanggung Jawab -->
                    <div class="col-md-6">
                        <label class="form-label fw-semibold text-main small">Pemilik / Penanggung Jawab</label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-person-badge"></i></span>
                            <input type="text" name="company_owner" class="form-control form-control-modern" value="<?= esc($setting['company_owner'] ?? '') ?>" placeholder="Nama Pemilik Perusahaan">
                        </div>
                    </div>

                    <!-- Email -->
                    <div class="col-md-4">
                        <label class="form-label fw-semibold text-main small">Email Resmi</label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-envelope"></i></span>
                            <input type="email" name="email" class="form-control form-control-modern" value="<?= esc($setting['email'] ?? '') ?>" placeholder="info@perusahaan.com">
                        </div>
                    </div>

                    <!-- Telepon -->
                    <div class="col-md-4">
                        <label class="form-label fw-semibold text-main small">No. Telepon / Whatsapp</label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-telephone"></i></span>
                            <input type="text" name="phone" class="form-control form-control-modern" value="<?= esc($setting['phone'] ?? '') ?>" placeholder="+62 812-3456-7890">
                        </div>
                    </div>

                    <!-- Website -->
                    <div class="col-md-4">
                        <label class="form-label fw-semibold text-main small">Situs Web</label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-globe"></i></span>
                            <input type="text" name="website" class="form-control form-control-modern" value="<?= esc($setting['website'] ?? '') ?>" placeholder="https://perusahaan.com">
                        </div>
                    </div>

                    <!-- Alamat -->
                    <div class="col-12">
                        <label class="form-label fw-semibold text-main small">Alamat Lengkap</label>
                        <textarea name="address" class="form-control form-control-modern" rows="2" placeholder="Jl. Utama No. 123, Komplek Bisnis"><?= esc($setting['address'] ?? '') ?></textarea>
                    </div>

                    <!-- Kota -->
                    <div class="col-md-4">
                        <label class="form-label fw-semibold text-main small">Kota / Kabupaten</label>
                        <input type="text" name="city" class="form-control form-control-modern" value="<?= esc($setting['city'] ?? '') ?>" placeholder="Jakarta Selatan">
                    </div>

                    <!-- Provinsi -->
                    <div class="col-md-4">
                        <label class="form-label fw-semibold text-main small">Provinsi</label>
                        <input type="text" name="province" class="form-control form-control-modern" value="<?= esc($setting['province'] ?? '') ?>" placeholder="DKI Jakarta">
                    </div>

                    <!-- Kode Pos -->
                    <div class="col-md-4">
                        <label class="form-label fw-semibold text-main small">Kode Pos</label>
                        <input type="text" name="postal_code" class="form-control form-control-modern" value="<?= esc($setting['postal_code'] ?? '') ?>" placeholder="12340">
                    </div>
                </div>
            </div>

            <!-- SECTION 2: PENGATURAN SISTEM & LOKALISASI -->
            <div class="card-modern">
                <div class="card-header-custom">
                    <div class="card-header-icon"><i class="bi bi-sliders2"></i></div>
                    <div>
                        <h5 class="fw-bold mb-0 text-main">Konfigurasi Sistem & Tagihan</h5>
                        <p class="text-sub small mb-0">Pengaturan zona waktu, transaksi mata uang, serta otomasi backup.</p>
                    </div>
                </div>

                <div class="row g-3">
                    <!-- Timezone -->
                    <div class="col-md-4">
                        <label class="form-label fw-semibold text-main small">Zona Waktu (Timezone)</label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-clock-history"></i></span>
                            <input type="text" name="timezone" class="form-control form-control-modern" value="<?= esc($setting['timezone'] ?? 'Asia/Jakarta') ?>" placeholder="Asia/Jakarta">
                        </div>
                    </div>

                    <!-- Mata Uang -->
                    <div class="col-md-4">
                        <label class="form-label fw-semibold text-main small">Mata Uang (Code)</label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-cash-stack"></i></span>
                            <input type="text" name="currency" class="form-control form-control-modern" value="<?= esc($setting['currency'] ?? 'IDR') ?>" placeholder="IDR">
                        </div>
                    </div>

                    <!-- Simbol Mata Uang -->
                    <div class="col-md-4">
                        <label class="form-label fw-semibold text-main small">Simbol Mata Uang</label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-currency-dollar"></i></span>
                            <input type="text" name="currency_symbol" class="form-control form-control-modern" value="<?= esc($setting['currency_symbol'] ?? 'Rp') ?>" placeholder="Rp">
                        </div>
                    </div>

                    <!-- Prefix Invoice -->
                    <div class="col-md-6">
                        <label class="form-label fw-semibold text-main small">Prefix Nomor Invoice</label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-receipt"></i></span>
                            <input type="text" name="invoice_prefix" class="form-control form-control-modern" value="<?= esc($setting['invoice_prefix'] ?? 'INV') ?>" placeholder="INV">
                        </div>
                    </div>

                    <!-- Backup Otomatis -->
                    <div class="col-md-6">
                        <label class="form-label fw-semibold text-main small">Backup Basis Data Otomatis</label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-database-fill-gear"></i></span>
                            <select name="backup_auto" class="form-select form-select-modern">
                                <option value="0" <?= (($setting['backup_auto'] ?? 0) == 0) ? 'selected' : '' ?>>Nonaktif (Tidak)</option>
                                <option value="1" <?= (($setting['backup_auto'] ?? 0) == 1) ? 'selected' : '' ?>>Aktif (Ya)</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>

            <!-- SECTION 3: INTEGRASI EMAIL (SMTP) -->
            <div class="card-modern">
                <div class="card-header-custom">
                    <div class="card-header-icon"><i class="bi bi-envelope-at"></i></div>
                    <div>
                        <h5 class="fw-bold mb-0 text-main">Konfigurasi Email (SMTP)</h5>
                        <p class="text-sub small mb-0">Pengaturan kredensial mail server untuk pengiriman notifikasi otomatis.</p>
                    </div>
                </div>

                <div class="row g-3">
                    <!-- SMTP Host -->
                    <div class="col-md-8">
                        <label class="form-label fw-semibold text-main small">SMTP Host</label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-server"></i></span>
                            <input type="text" name="smtp_host" class="form-control form-control-modern" value="<?= esc($setting['smtp_host'] ?? '') ?>" placeholder="smtp.gmail.com atau mail.domain.com">
                        </div>
                    </div>

                    <!-- SMTP Port -->
                    <div class="col-md-4">
                        <label class="form-label fw-semibold text-main small">SMTP Port</label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-ethernet"></i></span>
                            <input type="text" name="smtp_port" class="form-control form-control-modern" value="<?= esc($setting['smtp_port'] ?? '') ?>" placeholder="587 / 465">
                        </div>
                    </div>

                    <!-- SMTP User -->
                    <div class="col-md-6">
                        <label class="form-label fw-semibold text-main small">SMTP Username</label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-person-lock"></i></span>
                            <input type="text" name="smtp_user" class="form-control form-control-modern" value="<?= esc($setting['smtp_user'] ?? '') ?>" placeholder="notifikasi@domain.com">
                        </div>
                    </div>

                    <!-- SMTP Password -->
                    <div class="col-md-6">
                        <label class="form-label fw-semibold text-main small">SMTP Password</label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-key"></i></span>
                            <input type="password" name="smtp_pass" id="smtpPassInput" class="form-control form-control-modern" value="<?= esc($setting['smtp_pass'] ?? '') ?>" placeholder="••••••••••••">
                            <button class="btn custom-input-addon-btn" type="button" id="toggleSmtpPass">
                                <i class="bi bi-eye" id="toggleSmtpIcon"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>

            <!-- ACTION BAR BOTTOM -->
            <div class="d-flex justify-content-end align-items-center gap-2 mb-4">
                <button type="submit" class="btn btn-primary rounded-3 px-4 py-2.5 fw-semibold shadow-sm">
                    <i class="bi bi-floppy-fill me-1"></i> Simpan Semua Pengaturan
                </button>
            </div>

        </form>

    </div>

</div>

<!-- TOGGLE PASSWORD VISIBILITY JS -->
<script>
    document.addEventListener('DOMContentLoaded', function () {
        const toggleSmtpPass = document.getElementById('toggleSmtpPass');
        const smtpPassInput = document.getElementById('smtpPassInput');
        const toggleSmtpIcon = document.getElementById('toggleSmtpIcon');

        if (toggleSmtpPass && smtpPassInput) {
            toggleSmtpPass.addEventListener('click', function () {
                const type = smtpPassInput.getAttribute('type') === 'password' ? 'text' : 'password';
                smtpPassInput.setAttribute('type', type);
                
                if (type === 'text') {
                    toggleSmtpIcon.classList.remove('bi-eye');
                    toggleSmtpIcon.classList.add('bi-eye-slash');
                } else {
                    toggleSmtpIcon.classList.remove('bi-eye-slash');
                    toggleSmtpIcon.classList.add('bi-eye');
                }
            });
        }
    });
</script>

<?= $this->endSection() ?>