<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';
?>

<div class="container mt-4">
     <div class="welcome-msg">
        <i class="fa fa-user-circle"></i> Welcome, <strong><?= htmlspecialchars($_SESSION['username'] ?? 'Guest') ?></strong>!
    </div>

    <button class="btn btn-secondary btn-custom" onclick="history.back()">
        <i class="fa fa-arrow-left"></i> Back
    </button>

    <h4>🍺 Bar Tabs Management</h4>

    <div class="mb-3">
        <a href="bar_sale_list.php" class="btn btn-sm btn-primary">Bar Sales</a>
        <a href="open_bar_tab.php" class="btn btn-sm btn-primary">Open Tab</a>
        <a href="bar_tab_pos.php" class="btn btn-sm btn-primary">Tabs POS</a>
    </div>

    <hr>

    <a href="open_bar_tab.php" class="btn btn-success mb-3">
        <i class="fa fa-plus"></i> Open New Tab
    </a>

<?php
// ================= OPEN TABS =================
$open_tabs = $conn->query("
    SELECT 
        bt.*,
        g.name AS guest_name,
        CASE
            WHEN g.name IS NOT NULL AND g.name <> '' THEN g.name
            ELSE 'Walk-in'
        END AS person_name
    FROM bar_tabs bt
    LEFT JOIN guests g ON bt.guest_id = g.id
    WHERE bt.status = 'Open'
    ORDER BY bt.created_at ASC
");
?>

<h5>Open Tabs (<?= $open_tabs->num_rows ?>)</h5>

<table class="table table-bordered table-striped">
<thead class="bg-dark text-white">
<tr>
    <th>#</th>
    <th>Tab No</th>
    <th>Bartender</th>
    <th>Guest</th>
    <th>Status</th>
    <th>Created At</th>
    <th>Actions</th>
</tr>
</thead>
<tbody>

<?php
$i = 1;
while($tab = $open_tabs->fetch_assoc()):
?>

<tr>
    <td><?= $i++ ?></td>
    <td><?= htmlspecialchars($tab['tab_no']) ?></td>
    <td><?= htmlspecialchars($tab['waiter']) ?></td>

    <td>
        <span class="fw-bold text-primary">
            <?= htmlspecialchars($tab['person_name']) ?>
        </span>
    </td>

    <td>
        <span class="badge bg-success">
            <?= htmlspecialchars($tab['status']) ?>
        </span>
    </td>

    <td><?= date("d-M-Y H:i", strtotime($tab['created_at'])) ?></td>

    <td>
        <a href="bar_tab_pos_open.php?tab_id=<?= $tab['id'] ?>"
           class="btn btn-sm btn-primary">Open POS</a>

        <a href="bar_tab_details.php?tab_id=<?= $tab['id'] ?>"
           class="btn btn-sm btn-info">View</a>
    </td>
</tr>

<?php endwhile; ?>
</tbody>
</table>

<?php if($open_tabs->num_rows == 0): ?>
    <p class="text-center text-muted">No open tabs at the moment.</p>
<?php endif; ?>


<?php
// ================= CLOSED TABS =================
$closed_tabs = $conn->query("
    SELECT 
        bt.*,
        g.name AS guest_name,
        CASE
            WHEN g.name IS NOT NULL AND g.name <> '' THEN g.name
            ELSE 'Walk-in'
        END AS person_name
    FROM bar_tabs bt
    LEFT JOIN guests g ON bt.guest_id = g.id
    WHERE bt.status = 'Closed'
    ORDER BY bt.closed_at DESC
");
?>

<h5 class="mt-5">Closed Tabs (<?= $closed_tabs->num_rows ?>)</h5>

<table class="table table-bordered table-striped">
<thead class="bg-secondary text-white">
<tr>
    <th>#</th>
    <th>Tab No</th>
    <th>Bartender</th>
    <th>Guest</th>
    <th>Status</th>
    <th>Created At</th>
    <th>Closed At</th>
    <th>Actions</th>
</tr>
</thead>
<tbody>

<?php
$i = 1;
while($tab = $closed_tabs->fetch_assoc()):
?>

<tr>
    <td><?= $i++ ?></td>
    <td><?= htmlspecialchars($tab['tab_no']) ?></td>
    <td><?= htmlspecialchars($tab['waiter']) ?></td>

    <td>
        <span class="fw-bold text-primary">
            <?= htmlspecialchars($tab['person_name']) ?>
        </span>
    </td>

    <td>
        <span class="badge bg-secondary">
            <?= htmlspecialchars($tab['status']) ?>
        </span>
    </td>

    <td><?= date("d-M-Y H:i", strtotime($tab['created_at'])) ?></td>
    <td><?= date("d-M-Y H:i", strtotime($tab['closed_at'])) ?></td>

    <td>
        <a href="bar_tab_details.php?tab_id=<?= $tab['id'] ?>"
           class="btn btn-sm btn-info">View</a>

        <a href="bar_tab_reopen.php?tab_id=<?= $tab['id'] ?>"
           class="btn btn-sm btn-warning"
           onclick="return confirm('Are you sure you want to reopen this tab?');">
           Reopen
        </a>
    </td>
</tr>

<?php endwhile; ?>
</tbody>
</table>

<?php if($closed_tabs->num_rows == 0): ?>
    <p class="text-center text-muted">No closed tabs yet.</p>
<?php endif; ?>

</div>

<?php include 'includes/footer.php'; ?>
