<?php include 'layout.php'; ?>

<div class="page-wrapper">
    <div class="content">
        <div class="page-header">
            <h4>Clear Data Panel</h4>
            <h6>Reset specific modules (Admin Only)</h6>
        </div>

        <div class="row g-3">
            <div class="col-md-4">
                <div class="card bg-light border">
                    <div class="card-body text-center">
                        <h5>Clear Sales Data</h5>
                        <p class="text-muted">sales, sale_items, shifts, hold_orders, hold_items, cus_outstanding, cus_payments, delivery_orders, fbr_invoices, fbr_logs, sale_returns, sale_return_items</p>
                        <button class="btn btn-danger" onclick="confirmClear('sales')">Clear Sales</button>
                    </div>
                </div>
            </div>

            <div class="col-md-4">
                <div class="card bg-light border">
                    <div class="card-body text-center">
                        <h5>Clear Inventory Data</h5>
                        <p class="text-muted">purchases, purchase_items, inventory_movements, purchase_returns, purchase_return_items, sup_outstanding, sup_payments</p>
                        <button class="btn btn-danger" onclick="confirmClear('inventory')">Clear Inventory</button>
                    </div>
                </div>
            </div>

            <div class="col-md-4">
                <div class="card bg-light border">
                    <div class="card-body text-center">
                        <h5>Clear Expenses</h5>
                        <p class="text-muted">expenses, expense_accounts, expense_categories, expense_subcategories</p>
                        <button class="btn btn-danger" onclick="confirmClear('expenses')">Clear Expenses</button>
                    </div>
                </div>
            </div>
        </div>
        <div class="row g-3 mt-3">
            <div class="col-md-6">
                <div class="card bg-light border">
                    <div class="card-body text-center">
                        <h5>Clear Customer Ledgers</h5>
                        <p class="text-muted">cus_outstanding, cus_payments</p>
                        <button class="btn btn-danger" onclick="confirmClear('customer_ledgers')">Clear Customer Ledgers</button>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card bg-light border">
                    <div class="card-body text-center">
                        <h5>Clear Supplier Ledgers</h5>
                        <p class="text-muted">sup_outstanding, sup_payments</p>
                        <button class="btn btn-danger" onclick="confirmClear('supplier_ledgers')">Clear Supplier Ledgers</button>
                    </div>
                </div>
            </div>
        </div>
        <div class="row g-3 mt-3">
            <div class="col-md-12">
                <div class="card bg-light border">
                    <div class="card-body text-center">
                        <h5>Clear Items & Categories</h5>
                        <p class="text-muted">items, categories, sub_categories, bom_master, bom_components, price_history, itemwise_sales_report, etc.</p>
                        <button class="btn btn-danger" onclick="confirmClear('items')">Clear Items & Categories</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="container mt-5">
    <div class="card shadow">
        <div class="card-body text-center">
            <h4 class="mb-4">🗄️ Backup Your Database</h4>
            <p class="text-muted mb-4">Download a full .sql backup of your database instantly.</p>
            <a href="backup_database.php" class="btn btn-lg btn-primary">
                <i class="fa fa-database me-2"></i> Download Backup
            </a>
        </div>
    </div>
</div>



<script>
function confirmClear(type) {
    Swal.fire({
        title: 'Are you sure?',
        text: `This will permanently delete all ${type} records.`,
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#d33',
        cancelButtonColor: '#6c757d',
        confirmButtonText: 'Yes, clear it!'
    }).then((result) => {
        if (result.isConfirmed) {
            window.location.href = `clear_data.php?type=${type}`;
        }
    });
}
</script>

<?php if (!empty($_GET['cleared'])): ?>
<script>
Swal.fire({
    icon: 'success',
    title: 'Data Cleared',
    text: '<?= ucfirst($_GET['cleared']) ?> data has been cleared.',
    timer: 2000,
    showConfirmButton: false
});
</script>
<?php endif; ?>
