<?php
// This is a developer tool to clear all master and transactional data.
// USE WITH EXTREME CAUTION. This will wipe your data.

session_start();
include_once('includes/config.php');

// Basic security check: ensure a user is logged in.
if (!isset($_SESSION['uid'])) {
    header('location:logout.php');
    exit();
}

$message = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['confirm_delete'])) {
    // List of tables to truncate
    $tables = [
        'tblitems',       // Must be truncated before masters to avoid foreign key issues
        'tblcategories',
        'tblbrands',
        'tblsuppliers',
        'tblunits',
        'tbltaxtypes',
        'tbllocations',
        'tblracks'
    ];

    mysqli_query($con, "SET FOREIGN_KEY_CHECKS=0");

    $all_successful = true;
    foreach ($tables as $table) {
        $query = "TRUNCATE TABLE `$table`";
        if (mysqli_query($con, $query)) {
            // Success
        } else {
            $error .= "Error truncating table `$table`: " . mysqli_error($con) . "<br>";
            $all_successful = false;
        }
    }

    mysqli_query($con, "SET FOREIGN_KEY_CHECKS=1");

    if ($all_successful) {
        $message = "All dropdown data and associated items have been successfully deleted.";
    } else {
        $error = "An error occurred during deletion. Please check the details below:<br>" . $error;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Clear All Master Data</title>
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link href="css/sb-admin-2.min.css" rel="stylesheet">
    <link href="css/modern-messagebox.css" rel="stylesheet">
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f8f9fc;
        }
        .container {
            max-width: 600px;
            text-align: center;
            padding: 40px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
        }
        .btn-danger {
            font-size: 1.2rem;
            padding: 12px 24px;
        }
        .alert {
            text-align: left;
        }
    </style>
</head>
<body>
    <div class="container">
        <i class="fas fa-exclamation-triangle fa-3x text-danger mb-3"></i>
        <h1 class="h3 mb-3">Delete All Dropdown Data</h1>
        
        <?php if ($message): ?>
            <div class="alert alert-success"><?php echo $message; ?></div>
            <a href="items/add-item.php" class="btn btn-primary mt-3">Back to Add Item Page</a>
        <?php elseif ($error): ?>
            <div class="alert alert-danger"><?php echo $error; ?></div>
            <a href="items/add-item.php" class="btn btn-secondary mt-3">Back to Add Item Page</a>
        <?php else: ?>
            <p class="lead">
                Are you sure you want to permanently delete all data from the dropdown boxes?
            </p>
            <p class="text-danger">
                <strong>This action cannot be undone.</strong> It will delete all categories, brands, suppliers, units, taxes, locations, racks, and all items.
            </p>
            <form method="POST" id="deleteForm">
                <button type="submit" name="confirm_delete" class="btn btn-danger mt-3">
                    <i class="fas fa-trash"></i> Yes, Delete Everything
                </button>
                <a href="items/add-item.php" class="btn btn-secondary mt-3">
                    Cancel
                </a>
            </form>
        <?php endif; ?>
    </div>
    <script src="js/modern-messagebox.js"></script>
    <script src="js/messagebox-wrapper.js"></script>
    <script>
        document.getElementById('deleteForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            const result = await MsgBox.confirm('FINAL WARNING: This will delete all data. Are you absolutely sure?', {
                dangerMode: true,
                confirmText: 'Yes, Delete Everything'
            });
            if (result) {
                this.submit();
            }
        });
    </script>
</body>
</html>
