<?php
/**
 * @var array $backups
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-database me-2 text-gold"></i>Backup Management</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-history card-icon"></i>
            <h6 class="card-title">Database Backups</h6>
        </div>
        <div class="card-header-right">
            <form method="POST" action="<?= url('settings/backup/create') ?>" class="d-inline">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-sm btn-gold" onclick="return confirm('Create a new database backup?')">
                    <i class="fas fa-plus me-1"></i>Create Backup
                </button>
            </form>
        </div>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Filename</th>
                        <th class="text-end">Size</th>
                        <th>Created By</th>
                        <th>Created At</th>
                        <th width="180" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($backups)): ?>
                        <tr>
                            <td colspan="6" class="text-center text-muted py-4">
                                <i class="fas fa-database fa-2x mb-2"></i>
                                <p class="mb-0">No backups found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = 1; ?>
                        <?php foreach ($backups as $backup): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td><code><?= e($backup->filename) ?></code></td>
                                <td class="text-end"><?= formatNumber($backup->size / 1024, 1) ?> KB</td>
                                <td><?= e($backup->created_by ?? 'System') ?></td>
                                <td><?= formatDateTime($backup->created_at) ?></td>
                                <td class="text-center">
                                    <div class="btn-group btn-group-sm">
                                        <a href="<?= url('settings/backup/download/' . $backup->id) ?>" class="btn btn-outline-info" title="Download">
                                            <i class="fas fa-download"></i>
                                        </a>
                                        <form method="POST" action="<?= url('settings/backup/restore/' . $backup->id) ?>" class="d-inline"
                                              onsubmit="return confirm('Restore will replace current data. Continue?')">
                                            <?= csrf_field() ?>
                                            <button type="submit" class="btn btn-outline-warning" title="Restore">
                                                <i class="fas fa-undo"></i>
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="glass-card p-0 mt-4 border-danger">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-trash-alt card-icon text-danger"></i>
            <h6 class="card-title text-danger">Danger Zone</h6>
        </div>
    </div>
    <div class="card-body">
        <div class="alert alert-warning bg-transparent border-warning mb-3">
            <i class="fas fa-exclamation-triangle me-2"></i>
            This will permanently delete all <strong>transactional data</strong> including appointments, invoices, payments, purchases, stock movements, financial records, and employee attendance/salary records.
            Master data (users, roles, branches, customers, services, products, employees) will be kept but product stock will be reset to 0.
        </div>
        <form method="POST" action="<?= url('settings/clear-data') ?>">
            <?= csrf_field() ?>
            <div class="row align-items-end g-3">
                <div class="col-md-4">
                    <label class="form-label text-danger">Type <strong>CLEAR</strong> to confirm</label>
                    <input type="text" name="confirm" class="form-control bg-transparent" required
                           placeholder="Type CLEAR to confirm" autocomplete="off">
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-outline-danger w-100">
                        <i class="fas fa-eraser me-1"></i>Clear All Data
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<?php $inlineScripts = <<<JS
$('form[action*="clear-data"]').on('submit', function() {
    var val = $('input[name="confirm"]').val();
    if (val !== 'CLEAR') {
        alert('Please type CLEAR to confirm.');
        return false;
    }
    return confirm('This action cannot be undone. All transactional data will be permanently deleted. Continue?');
});
JS;
?>
