<?php
$pageTitle = 'QR Code Management';
require_once __DIR__ . '/includes/header.php';

$tables = $db->query(
    "SELECT * FROM tables WHERE restaurant_id = ? ORDER BY table_number ASC",
    [$restaurantId]
)->fetchAll();

$operatingMode = (int) ($settings['operating_mode'] ?? 1);
$scanText = $operatingMode == 2 ? 'Scan to Order' : 'Scan to View Menu';

$qrDir = UPLOAD_PATH . DIRECTORY_SEPARATOR . 'qrcodes';
if (!is_dir($qrDir)) {
    mkdir($qrDir, 0755, true);
}
?>

<div class="row mb-4">
    <div class="col-md-8">
        <h4><i class="fas fa-qrcode me-2"></i>QR Code Management</h4>
    </div>
</div>

<div class="row mb-4">
    <div class="col-md-6">
        <div class="form-card">
            <h6 class="text-primary mb-3"><i class="fas fa-store me-1"></i>Restaurant QR Code</h6>
            <div class="text-center mb-3">
                <div style="background:#fff;padding:16px;border:1px solid #ddd;border-radius:8px;display:inline-block;">
                    <img src="<?php echo $baseUrl; ?>/qrcode.php?restaurant_id=<?php echo $restaurantId; ?>&format=png"
                         alt="Restaurant QR Code" style="width:200px;height:200px;">
                </div>
                <div class="mt-2">
                    <small class="text-muted"><?php echo sanitize($restaurant['name']); ?> - <?php echo $scanText; ?></small>
                </div>
            </div>
            <div class="d-flex justify-content-center gap-2">
                <a href="<?php echo $baseUrl; ?>/qrcode.php?restaurant_id=<?php echo $restaurantId; ?>&format=png"
                   class="btn btn-sm btn-primary" download="qr-<?php echo sanitize($restaurant['slug']); ?>.png">
                    <i class="fas fa-download me-1"></i>Download
                </a>
                <a href="<?php echo $baseUrl; ?>/admin/qr_print.php?restaurant_id=<?php echo $restaurantId; ?>"
                   class="btn btn-sm btn-outline-secondary" target="_blank">
                    <i class="fas fa-print me-1"></i>Print
                </a>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="form-card">
            <h6 class="text-primary mb-3"><i class="fas fa-info-circle me-1"></i>Details</h6>
            <table class="table table-sm">
                <tr><td><strong>Restaurant:</strong></td><td><?php echo sanitize($restaurant['name']); ?></td></tr>
                <tr><td><strong>Mode:</strong></td><td><?php echo $operatingMode == 2 ? 'Ordering' : 'View Only'; ?></td></tr>
                <tr><td><strong>Menu URL:</strong></td><td><small><?php echo sanitize(getBaseUrl() . '/menu.php?r=' . $restaurant['slug']); ?></small></td></tr>
                <?php if (!empty($settings['wifi_name'])): ?>
                <tr><td><strong>Wi-Fi:</strong></td><td><?php echo sanitize($settings['wifi_name']); ?></td></tr>
                <?php endif; ?>
            </table>
        </div>
    </div>
</div>

<div class="form-card mb-4">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h6 class="text-primary mb-0"><i class="fas fa-chair me-1"></i>Table QR Codes</h6>
        <div>
            <button class="btn btn-sm btn-primary" onclick="batchGenerateQR()">
                <i class="fas fa-sync me-1"></i>Generate All
            </button>
            <button class="btn btn-sm btn-outline-secondary" onclick="printAllQR()">
                <i class="fas fa-print me-1"></i>Print All
            </button>
        </div>
    </div>

    <?php if (empty($tables)): ?>
    <div class="text-center text-muted py-4">
        <p>No tables found. Add tables in the Tables management page first.</p>
    </div>
    <?php else: ?>
    <div class="row">
        <?php foreach ($tables as $table): ?>
        <div class="col-lg-3 col-md-4 col-sm-6 mb-3">
            <div class="card text-center" style="border-radius:12px;overflow:hidden;">
                <div class="card-body">
                    <img src="<?php echo $baseUrl; ?>/qrcode.php?restaurant_id=<?php echo $restaurantId; ?>&table_id=<?php echo $table['id']; ?>&format=png"
                         alt="QR - Table <?php echo sanitize($table['table_number']); ?>"
                         style="width:120px;height:120px;border:1px solid #eee;border-radius:8px;cursor:pointer;"
                         onclick="window.open('<?php echo $baseUrl; ?>/admin/qr_print.php?restaurant_id=<?php echo $restaurantId; ?>&table_id=<?php echo $table['id']; ?>', '_blank')">
                    <h6 class="mt-2 mb-0">Table <?php echo sanitize($table['table_number']); ?></h6>
                    <small class="text-muted"><?php echo $table['capacity']; ?> pax | <?php echo sanitize($table['area'] ?? 'Main'); ?></small>
                    <div class="mt-2">
                        <a href="<?php echo $baseUrl; ?>/qrcode.php?restaurant_id=<?php echo $restaurantId; ?>&table_id=<?php echo $table['id']; ?>&format=png"
                           class="btn btn-sm btn-outline-primary" download="qr-table-<?php echo sanitize($table['table_number']); ?>.png">
                            <i class="fas fa-download"></i>
                        </a>
                        <a href="<?php echo $baseUrl; ?>/admin/qr_print.php?restaurant_id=<?php echo $restaurantId; ?>&table_id=<?php echo $table['id']; ?>"
                           class="btn btn-sm btn-outline-secondary" target="_blank">
                            <i class="fas fa-print"></i>
                        </a>
                    </div>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
</div>

<script>
function batchGenerateQR() {
    if (!confirm('Generate QR codes for all tables?')) return;
    var tables = <?php echo json_encode(array_map(function($t) { return ['id' => $t['id'], 'number' => $t['table_number']]; }, $tables)); ?>;
    tables.forEach(function(t) {
        var img = new Image();
        img.src = BASE_URL + '/admin/qr_generate.php?restaurant_id=<?php echo $restaurantId; ?>&table_id=' + t.id;
    });
    showAlert('success', 'QR codes generated for all tables');
}

function printAllQR() {
    window.open(BASE_URL + '/admin/qr_print.php?restaurant_id=<?php echo $restaurantId; ?>&all=1', '_blank');
}
</script>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
