<?php include ADMIN_PATH . '/templates/header.php'; ?>

<div class="content-wrapper">
<section class="content pt-3">

<div class="container-fluid pb-5">

    <div class="d-flex justify-content-between align-items-start gap-3 mb-4"><div><h3 class="mb-1 fw-bold"><i class="bi bi-database-fill text-primary me-2"></i><?= __('system.backup_title', 'System') ?></h3></div></div>

    <?php if (isset($_GET['done'])): ?>
        <div class="alert alert-success">
            <div class="fw-bold">
                <i class="bi bi-check-circle me-1"></i>
                <?= __('system.backup_done', 'Backup completed successfully.') ?>
            </div>

            <?php if (!empty($_GET['file'])): ?>
                <div class="mt-2">
                    <a class="btn btn-sm btn-success"
                       href="/admin/system/backup/download?file=<?= urlencode((string)$_GET['file']) ?>">
                        <i class="bi bi-download me-1"></i>
                        <?= __('system.download_backup', 'Download Backup') ?>
                    </a>
                </div>
            <?php endif; ?>
        </div>
    <?php endif; ?>

    <?php if (isset($_GET['deleted'])): ?>
        <div class="alert alert-success">
            <i class="bi bi-trash3 me-1"></i>
            <?= __('system.backup_deleted', 'Backup deleted successfully.') ?>
        </div>
    <?php endif; ?>

    <?php if (isset($_GET['error'])): ?>
        <div class="alert alert-danger">
            <i class="bi bi-exclamation-triangle me-1"></i>
            <?= __('system.backup_error', 'Operation failed.') ?>
        </div>
    <?php endif; ?>

    <div class="row g-4">

        <!-- ===================== -->
        <!-- CREATE BACKUP -->
        <!-- ===================== -->
        <div class="col-lg-5">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-header fw-bold">
                    <i class="bi bi-plus-circle me-1"></i>
                    <?= __('system.create_backup', 'Create Backup') ?>
                </div>

                <div class="card-body">

                    <form method="POST" action="/admin/system/backup/run" class="d-grid gap-3">
                        <?= \Core\System\Csrf::field(); ?>

                        <!-- Backup Type -->
                        <div>
                            <label class="form-label">
                                <?= __('system.backup_type', 'Backup Type') ?>
                            </label>

                            <select name="backup_type" id="backup_type" class="form-select">
                                <option value="db" selected><?= __('system.backup_db', 'Database Backup (SQL)') ?></option>
                                <option value="script"><?= __('system.backup_script', 'Script Files Backup (ZIP)') ?></option>
                                <option value="full"><?= __('system.backup_full', 'Full Backup (DB + Script)') ?></option>
                            </select>

                            <small class="text-muted d-block mt-1">
                                <?= __('system.backup_type_hint', 'Choose database only, script only, or full backup.') ?>
                            </small>
                        </div>

                        <!-- Compression (DB only or Full only) -->
                        <div id="compress_box">
                            <label class="form-label"><?= __('system.compress', 'Compression') ?></label>
                            <select name="compress" class="form-select">
                                <option value="0"><?= __('system.no', 'No') ?></option>
                                <option value="1" selected><?= __('system.yes', 'Yes (GZIP)') ?></option>
                            </select>
                            <small class="text-muted">
                                <?= __('system.compress_hint', 'Database will be saved as .sql.gz when enabled.') ?>
                            </small>
                        </div>

                        <!-- Tables selection (DB only / Full only) -->
                        <div id="tables_box">
                            <label class="form-label"><?= __('system.tables_mode', 'Tables') ?></label>
                            <select name="tables_mode" id="tables_mode" class="form-select">
                                <option value="all" selected><?= __('system.tables_all', 'All tables') ?></option>
                                <option value="selected"><?= __('system.tables_selected', 'Select specific tables') ?></option>
                            </select>

                            <div class="mt-2" id="tables_list" style="display:none;">
                                <select name="tables[]" class="form-select" multiple size="10">
                                    <?php foreach (($tables ?? []) as $t): ?>
                                        <option value="<?= htmlspecialchars($t) ?>">
                                            <?= htmlspecialchars($t) ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>

                                <small class="text-muted">
                                    <?= __('system.tables_select_hint', 'Hold CTRL to select multiple tables.') ?>
                                </small>
                            </div>
                        </div>

                        <!-- Script options (Script only / Full only) -->
                        <div id="script_box" style="display:none;">
                            <div class="alert alert-info py-2 mb-0">
                                <i class="bi bi-folder2-open me-1"></i>
                                <?= __('system.script_backup_hint',
                                    'Script backup will include all folders and files inside the project automatically.'
                                ) ?>
                                <div class="small text-muted mt-1">
                                    <?= __('system.script_backup_location',
                                        'It will be packed as a ZIP file for download.'
                                    ) ?>
                                </div>
                            </div>
                        </div>

                        <button class="btn btn-primary btn-lg" type="submit"
                                onclick="return confirm('<?= __('system.confirm_backup','Create backup now?') ?>')">
                            <i class="bi bi-play-fill me-1"></i>
                            <?= __('system.start_backup', 'Start Backup') ?>
                        </button>
                    </form>

                </div>
            </div>
        </div>

        <!-- ===================== -->
        <!-- BACKUP MANAGER -->
        <!-- ===================== -->
        <div class="col-lg-7">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-header fw-bold d-flex justify-content-between align-items-center">
                    <div>
                        <i class="bi bi-archive-fill me-1"></i>
                        <?= __('system.backup_manager', 'Backups Manager') ?>
                    </div>

                    <form method="POST" action="/admin/system/backup/clean" class="m-0">
                        <?= \Core\System\Csrf::field(); ?>
                        <button class="btn btn-sm btn-outline-danger"
                                onclick="return confirm('<?= __('system.confirm_cleanup_backups','Delete old backups?') ?>')">
                            <i class="bi bi-broom me-1"></i>
                            <?= __('system.cleanup_backups', 'Cleanup Old') ?>
                        </button>
                    </form>
                </div>

                <div class="card-body">

                    <?php if (empty($backups)): ?>
                        <div class="text-muted">
                            <?= __('system.no_backups', 'No backups found yet.') ?>
                        </div>
                    <?php else: ?>

                        <div class="table-responsive">
                            <table class="table table-striped align-middle">
                                <thead>
                                <tr>
                                    <th><?= __('system.file', 'File') ?></th>
                                    <th><?= __('system.size', 'Size') ?></th>
                                    <th><?= __('system.date', 'Date') ?></th>
                                    <th class="text-end"><?= __('system.actions', 'Actions') ?></th>
                                </tr>
                                </thead>

                                <tbody>
                                <?php foreach ($backups as $b): ?>
                                    <tr>
                                        <td class="fw-bold"><?= htmlspecialchars($b['name']) ?></td>
                                        <td><?= htmlspecialchars($b['size_human']) ?></td>
                                        <td><?= htmlspecialchars($b['date']) ?></td>
                                        <td class="text-end">

                                            <a class="btn btn-sm btn-success"
                                               href="/admin/system/backup/download?file=<?= urlencode($b['name']) ?>">
                                                <i class="bi bi-download"></i>
                                            </a>

                                            <form method="POST"
                                                  action="/admin/system/backup/delete"
                                                  style="display:inline-block">
                                                <?= \Core\System\Csrf::field(); ?>
                                                <input type="hidden" name="file" value="<?= htmlspecialchars($b['name']) ?>">
                                                <button class="btn btn-sm btn-danger"
                                                        onclick="return confirm('<?= __('system.confirm_delete_backup','Delete this backup?') ?>')">
                                                    <i class="bi bi-trash3"></i>
                                                </button>
                                            </form>

                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                                </tbody>

                            </table>
                        </div>

                        <small class="text-muted d-block mt-2">
                            <?= __('system.backup_notice', 'Keep backups safe. You can download and store them externally.') ?>
                        </small>

                    <?php endif; ?>

                </div>
            </div>
        </div>

    </div>

</div>
</section>
</div>

<?php include ADMIN_PATH . '/templates/footer.php'; ?>

<script>
document.addEventListener('DOMContentLoaded', function () {

    const backupType   = document.getElementById('backup_type');
    const compressBox  = document.getElementById('compress_box');
    const tablesBox    = document.getElementById('tables_box');
    const tablesMode   = document.getElementById('tables_mode');
    const tablesList   = document.getElementById('tables_list');
    const scriptBox    = document.getElementById('script_box');

    function toggleTablesList() {
        if (!tablesMode || !tablesList) return;
        tablesList.style.display = (tablesMode.value === 'selected') ? 'block' : 'none';
    }

    function toggleByType() {
        const type = backupType ? backupType.value : 'db';

        // DB only
        if (type === 'db') {
            compressBox.style.display = 'block';
            tablesBox.style.display   = 'block';
            scriptBox.style.display   = 'none';
        }

        // Script only
        if (type === 'script') {
            compressBox.style.display = 'none';
            tablesBox.style.display   = 'none';
            scriptBox.style.display   = 'block';
        }

        // Full
        if (type === 'full') {
            compressBox.style.display = 'block';
            tablesBox.style.display   = 'block';
            scriptBox.style.display   = 'block';
        }

        toggleTablesList();
    }

    if (tablesMode) {
        tablesMode.addEventListener('change', toggleTablesList);
    }

    if (backupType) {
        backupType.addEventListener('change', toggleByType);
    }

    toggleByType();
});
</script>