<?= $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;
        --success-hover: #059669;
        --success-light: rgba(16, 185, 129, 0.08);
        --warning: #f59e0b;
        --danger: #ef4444;
        --danger-hover: #dc2626;
        --danger-light: rgba(239, 68, 68, 0.08);
        --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);
            --success-light: rgba(16, 185, 129, 0.18);
            --danger-light: rgba(239, 68, 68, 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);
        --success-light: rgba(16, 185, 129, 0.18);
        --danger-light: rgba(239, 68, 68, 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, .backup-workspace {
        background-color: var(--bg-body) !important;
        color: var(--text-main) !important;
        transition: var(--transition);
    }

    /* Card Premium Styling */
    .card-modern {
        background: var(--bg-card);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow-sm);
        transition: var(--transition);
        overflow: hidden;
    }

    /* Modern Table Styling */
    .table-modern {
        margin-bottom: 0;
        color: var(--text-main);
    }

    .table-modern th {
        background-color: var(--bg-accent);
        color: var(--text-sub);
        font-weight: 600;
        font-size: 0.75rem;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        padding: 1rem 1.25rem;
        border-bottom: 1px solid var(--border-color);
    }

    .table-modern td {
        padding: 1rem 1.25rem;
        border-bottom: 1px solid var(--border-color);
        background-color: transparent;
        vertical-align: middle;
        font-size: 0.875rem;
    }

    .table-modern tbody tr {
        transition: var(--transition);
    }

    .table-modern tbody tr:hover {
        background-color: var(--primary-light) !important;
    }

    .table-modern tbody tr:last-child td {
        border-bottom: none;
    }

    /* User Avatar Initial */
    .user-avatar {
        width: 32px;
        height: 32px;
        border-radius: 50%;
        background-color: var(--primary-light);
        color: var(--primary);
        font-weight: 700;
        font-size: 0.8rem;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
    }

    /* File Icon Container */
    .file-icon-box {
        width: 38px;
        height: 38px;
        border-radius: var(--radius-md);
        background-color: var(--primary-light);
        color: var(--primary);
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.1rem;
        flex-shrink: 0;
    }

    /* Code Pill for Filename */
    .file-pill {
        font-family: var(--bs-font-monospace);
        font-weight: 600;
        color: var(--text-main);
        font-size: 0.875rem;
    }

    /* Action Buttons */
    .btn-action-primary {
        width: 34px;
        height: 34px;
        padding: 0;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border-radius: var(--radius-sm);
        color: var(--primary);
        border: 1px solid var(--border-color);
        background-color: var(--bg-card);
        transition: var(--transition);
    }

    .btn-action-primary:hover {
        background-color: var(--primary);
        color: #ffffff;
        border-color: var(--primary);
    }

    .btn-action-danger {
        width: 34px;
        height: 34px;
        padding: 0;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border-radius: var(--radius-sm);
        color: var(--danger);
        border: 1px solid var(--border-color);
        background-color: var(--bg-card);
        transition: var(--transition);
    }

    .btn-action-danger:hover {
        background-color: var(--danger);
        color: #ffffff;
        border-color: var(--danger);
    }

    /* Modal Modernization */
    .modal-content-modern {
        background-color: var(--bg-card);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-lg);
        color: var(--text-main);
    }
</style>

<div class="container-fluid py-4 backup-workspace">
    
    <!-- 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">Backup Database</h3>
            <p class="text-sub small mb-0 mt-0.5">Kelola salinan cadangan basis data aplikasi untuk pemulihan bencana sistem.</p>
        </div>
        <div class="d-flex align-items-center gap-2">
            <a href="<?= base_url('superadmin/backup/create') ?>" class="btn btn-success rounded-3 px-4 py-2 fw-semibold d-inline-flex align-items-center gap-2 shadow-sm fs-7">
                <i class="bi bi-database-add fs-6"></i> Buat Backup Baru
            </a>
        </div>
    </div>

    <!-- FLASH SUCCESS ALERT -->
    <?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 ERROR ALERT -->
    <?php if (session()->getFlashdata('error')) : ?>
        <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-center gap-2">
                <i class="bi bi-exclamation-triangle-fill fs-5 text-danger"></i>
                <div><?= session()->getFlashdata('error') ?></div>
            </div>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>

    <!-- MAIN TABLE CARD -->
    <div class="card-modern">
        <div class="table-responsive">
            <table class="table table-modern align-middle">
                <thead>
                    <tr>
                        <th style="width: 60px;" class="text-center">ID</th>
                        <th>Nama Berkas Backup</th>
                        <th style="width: 140px;">Ukuran File</th>
                        <th style="width: 200px;">Tanggal Backup</th>
                        <th style="width: 200px;">Dibuat Oleh</th>
                        <th style="width: 110px;" class="text-center">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($backups)) : ?>
                        <tr>
                            <td colspan="6" class="text-center py-5">
                                <div class="py-4">
                                    <i class="bi bi-database-exclamation fs-1 text-sub opacity-50 d-block mb-2"></i>
                                    <h6 class="fw-semibold text-main mb-1">Belum Ada Berkas Backup</h6>
                                    <p class="text-sub small mb-3">Sistem belum memiliki salinan cadangan basis data.</p>
                                    <a href="<?= base_url('superadmin/backup/create') ?>" class="btn btn-sm btn-primary rounded-3 px-3 py-1.5 fw-semibold">
                                        <i class="bi bi-plus-lg me-1"></i> Buat Backup Sekarang
                                    </a>
                                </div>
                            </td>
                        </tr>
                    <?php else : ?>
                        <?php foreach ($backups as $backup) : ?>
                            <?php 
                                $creatorName = $backup['nama_lengkap'] ?? 'System';
                                $initials = strtoupper(substr(trim($creatorName), 0, 1));
                            ?>
                            <tr>
                                <td class="text-center text-sub font-monospace fs-7">#<?= esc($backup['id_backup']) ?></td>
                                <td>
                                    <div class="d-flex align-items-center gap-3">
                                        <div class="file-icon-box">
                                            <i class="bi bi-database-down"></i>
                                        </div>
                                        <div>
                                            <span class="file-pill d-block leading-tight">
                                                <?= esc($backup['filename']) ?>
                                            </span>
                                            <span class="text-sub fs-7">Format File SQL (.sql)</span>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <span class="badge bg-secondary-subtle text-secondary border border-secondary-subtle rounded-pill px-2.5 py-1 fw-medium font-monospace">
                                        <i class="bi bi-hdd-network me-1"></i><?= esc($backup['size_mb']) ?> MB
                                    </span>
                                </td>
                                <td>
                                    <div class="d-flex align-items-center gap-1.5 text-main small font-monospace">
                                        <i class="bi bi-calendar-event text-sub"></i>
                                        <span><?= esc($backup['backup_date'] ?? '-') ?></span>
                                    </div>
                                </td>
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <div class="user-avatar">
                                            <?= $initials ?>
                                        </div>
                                        <span class="fw-medium text-main small">
                                            <?= esc($creatorName) ?>
                                        </span>
                                    </div>
                                </td>
                                <td class="text-center">
                                    <div class="d-flex align-items-center justify-content-center gap-1.5">
                                        <!-- DOWNLOAD BUTTON -->
                                        <a href="<?= base_url('superadmin/backup/download/' . $backup['id_backup']) ?>" 
                                           class="btn-action-primary" 
                                           title="Unduh File Backup"
                                           data-bs-toggle="tooltip" 
                                           data-bs-placement="top">
                                            <i class="bi bi-download"></i>
                                        </a>

                                        <!-- DELETE BUTTON WITH MODAL TRIGGER -->
                                        <button type="button" 
                                                class="btn-action-danger" 
                                                title="Hapus File Backup"
                                                data-bs-toggle="modal" 
                                                data-bs-target="#deleteModal<?= $backup['id_backup'] ?>">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </div>

                                    <!-- DELETE CONFIRMATION MODAL -->
                                    <div class="modal fade" id="deleteModal<?= $backup['id_backup'] ?>" tabindex="-1" aria-hidden="true">
                                        <div class="modal-dialog modal-dialog-centered">
                                            <div class="modal-content modal-content-modern text-start">
                                                <div class="modal-body p-4 text-center">
                                                    <div class="width-50 height-50 rounded-circle bg-danger-subtle text-danger mx-auto d-flex align-items-center justify-content-center mb-3" style="width: 54px; height: 54px;">
                                                        <i class="bi bi-trash3-fill fs-3"></i>
                                                    </div>
                                                    <h5 class="fw-bold text-main mb-2">Hapus Berkas Backup?</h5>
                                                    <p class="text-sub small mb-4">
                                                        Apakah Anda yakin ingin menghapus file <strong class="text-main font-monospace"><?= esc($backup['filename']) ?></strong>? Tindakan ini tidak dapat dibatalkan.
                                                    </p>
                                                    <div class="d-flex justify-content-center gap-2">
                                                        <button type="button" class="btn btn-outline-secondary rounded-3 px-3 py-2 fw-semibold fs-7" data-bs-dismiss="modal">
                                                            Batal
                                                        </button>
                                                        <a href="<?= base_url('superadmin/backup/delete/' . $backup['id_backup']) ?>" class="btn btn-danger rounded-3 px-4 py-2 fw-semibold fs-7">
                                                            Ya, Hapus File
                                                        </a>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <!-- END MODAL -->

                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

</div>

<!-- TOOLTIP INITIALIZATION JS -->
<script>
    document.addEventListener('DOMContentLoaded', function () {
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
            return new bootstrap.Tooltip(tooltipTriggerEl);
        });
    });
</script>

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