<?php
$pageTitle = 'Demo Data';
require_once __DIR__ . '/includes/header.php';
$db = Database::getInstance();

$demoRestaurants = $db->query("SELECT COUNT(*) as c FROM restaurants WHERE is_demo = 1")->fetch()['c'];
$demoCategories = $db->query("SELECT COUNT(*) as c FROM categories WHERE is_demo = 1")->fetch()['c'];
$demoItems = $db->query("SELECT COUNT(*) as c FROM menu_items WHERE is_demo = 1")->fetch()['c'];
$demoOrders = $db->query("SELECT COUNT(*) as c FROM orders WHERE is_demo = 1")->fetch()['c'];
$demoTables = $db->query("SELECT COUNT(*) as c FROM tables WHERE is_demo = 1")->fetch()['c'];
?>

<div class="row">
    <div class="col-md-6">
        <div class="form-card">
            <h6 class="text-success mb-3"><i class="fas fa-download me-2"></i>Install Demo Data</h6>
            <p class="text-muted">This will install demo restaurants, categories, menu items, tables, and sample orders to help you explore the system.</p>
            <div class="mb-3">
                <h6>Demo restaurants will be created:</h6>
                <ul>
                    <li><strong>Ocean Spice</strong> - View Only mode (mode 1)</li>
                    <li><strong>Urban Taste</strong> - Ordering mode (mode 2)</li>
                </ul>
                <p class="text-muted small">Each restaurant will include categories, menu items, tables, offers, and sample orders.</p>
            </div>
            <button type="button" class="btn btn-success" id="installDemoBtn"><i class="fas fa-download me-1"></i>Install Demo Data</button>
        </div>
    </div>
    <div class="col-md-6">
        <div class="form-card">
            <h6 class="text-danger mb-3"><i class="fas fa-trash me-2"></i>Remove Demo Data</h6>
            <p class="text-muted">This will remove all demo data (records with is_demo = 1). This action cannot be undone.</p>
            <?php if ($demoRestaurants > 0): ?>
            <div class="alert alert-warning">
                <strong>Current demo data:</strong>
                <ul class="mb-0 mt-1">
                    <li><?php echo $demoRestaurants; ?> restaurant(s)</li>
                    <li><?php echo $demoCategories; ?> categories</li>
                    <li><?php echo $demoItems; ?> menu items</li>
                    <li><?php echo $demoTables; ?> tables</li>
                    <li><?php echo $demoOrders; ?> orders</li>
                </ul>
            </div>
            <?php else: ?>
            <div class="alert alert-info">No demo data found.</div>
            <?php endif; ?>
            <div class="mb-3">
                <label class="form-label">Enter admin password to confirm removal:</label>
                <input type="password" class="form-control" id="confirmPassword" placeholder="Admin password">
            </div>
            <button type="button" class="btn btn-danger" id="removeDemoBtn" <?php echo $demoRestaurants === 0 ? 'disabled' : ''; ?>><i class="fas fa-trash me-1"></i>Remove All Demo Data</button>
        </div>
    </div>
</div>

<script>
document.getElementById('installDemoBtn').addEventListener('click', function() {
    if (confirm('Install demo data? This may take a moment.')) {
        this.disabled = true;
        this.innerHTML = '<i class="fas fa-spinner fa-spin me-1"></i>Installing...';
        fetch(BASE_URL + '/admin/demo_install.php', {
            method: 'POST',
            headers: {'Content-Type': 'application/x-www-form-urlencoded', 'X-Requested-With': 'XMLHttpRequest'},
            body: 'csrf_token=' + encodeURIComponent(CSRF_TOKEN)
        })
        .then(function(r) { return r.json(); })
        .then(function(data) {
            if (data.success) {
                showAlert('success', data.message);
                setTimeout(function() { location.reload(); }, 1500);
            } else {
                showAlert('danger', data.message);
                document.getElementById('installDemoBtn').disabled = false;
                document.getElementById('installDemoBtn').innerHTML = '<i class="fas fa-download me-1"></i>Install Demo Data';
            }
        })
        .catch(function() {
            showAlert('danger', 'Network error.');
            document.getElementById('installDemoBtn').disabled = false;
            document.getElementById('installDemoBtn').innerHTML = '<i class="fas fa-download me-1"></i>Install Demo Data';
        });
    }
});

document.getElementById('removeDemoBtn').addEventListener('click', function() {
    var pwd = document.getElementById('confirmPassword').value;
    if (!pwd) { alert('Please enter admin password.'); return; }
    if (confirm('WARNING: This will permanently delete ALL demo data. Continue?')) {
        this.disabled = true;
        this.innerHTML = '<i class="fas fa-spinner fa-spin me-1"></i>Removing...';
        fetch(BASE_URL + '/admin/demo_remove.php', {
            method: 'POST',
            headers: {'Content-Type': 'application/x-www-form-urlencoded', 'X-Requested-With': 'XMLHttpRequest'},
            body: 'csrf_token=' + encodeURIComponent(CSRF_TOKEN) + '&password=' + encodeURIComponent(pwd)
        })
        .then(function(r) { return r.json(); })
        .then(function(data) {
            if (data.success) {
                showAlert('success', data.message);
                setTimeout(function() { location.reload(); }, 1500);
            } else {
                showAlert('danger', data.message);
                document.getElementById('removeDemoBtn').disabled = false;
                document.getElementById('removeDemoBtn').innerHTML = '<i class="fas fa-trash me-1"></i>Remove All Demo Data';
            }
        })
        .catch(function() {
            showAlert('danger', 'Network error.');
            document.getElementById('removeDemoBtn').disabled = false;
            document.getElementById('removeDemoBtn').innerHTML = '<i class="fas fa-trash me-1"></i>Remove All Demo Data';
        });
    }
});
</script>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
