<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="container mt-4">
    <h3 class="mb-3">Ringkasan Transaksi</h3>

    <form method="get" class="row g-3 mb-4">
        <div class="col-md-3">
            <label class="form-label">Dari Tanggal</label>
            <input type="date" name="start_date" class="form-control" value="<?= esc($start_date) ?>">
        </div>
        <div class="col-md-3">
            <label class="form-label">Sampai Tanggal</label>
            <input type="date" name="end_date" class="form-control" value="<?= esc($end_date) ?>">
        </div>
        <div class="col-md-6 d-flex align-items-end">
            <button type="submit" class="btn btn-primary me-2">Filter</button>
            <a href="<?= base_url('admin/report/summary') ?>" class="btn btn-secondary">Reset</a>
        </div>
    </form>

    <?php
        $total_takeaway_cash = $total_takeaway_transfer = $total_dinein_cash = $total_dinein_transfer = 0;
        $qty_takeaway_cash = $qty_takeaway_transfer = $qty_dinein_cash = $qty_dinein_transfer = 0;

        foreach ($summary as $row) {
            $total_takeaway_cash    += $row['takeaway_cash_total'];
            $total_takeaway_transfer+= $row['takeaway_transfer_total'];
            $total_dinein_cash      += $row['dinein_cash_total'];
            $total_dinein_transfer  += $row['dinein_transfer_total'];

            $qty_takeaway_cash      += $row['takeaway_cash_qty'];
            $qty_takeaway_transfer  += $row['takeaway_transfer_qty'];
            $qty_dinein_cash        += $row['dinein_cash_qty'];
            $qty_dinein_transfer    += $row['dinein_transfer_qty'];
        }

        $grand_total = $total_takeaway_cash + $total_takeaway_transfer + $total_dinein_cash + $total_dinein_transfer;
        $grand_qty = $qty_takeaway_cash + $qty_takeaway_transfer + $qty_dinein_cash + $qty_dinein_transfer;
    ?>

    <div class="row g-3 mb-4">
        <div class="col-12">
            <div class="alert alert-dark text-center">
                <strong>Grand Total</strong><br>
                Total Pembelian: <span class="fw-bold text-success">Rp<?= number_format($grand_total, 0, ',', '.') ?></span><br>
                Total Qty: <span class="fw-bold"><?= number_format($grand_qty) ?> unit</span>
            </div>
        </div>
    </div>

    <div class="row g-3">
        <div class="col-md-6 col-lg-3">
            <div class="alert alert-primary">
                <strong>Take Away - Cash</strong><br>
                Total: Rp<?= number_format($total_takeaway_cash, 0, ',', '.') ?><br>
                Qty: <?= number_format($qty_takeaway_cash) ?> unit
            </div>
        </div>
        <div class="col-md-6 col-lg-3">
            <div class="alert alert-success">
                <strong>Take Away - Transfer</strong><br>
                Total: Rp<?= number_format($total_takeaway_transfer, 0, ',', '.') ?><br>
                Qty: <?= number_format($qty_takeaway_transfer) ?> unit
            </div>
        </div>
        <div class="col-md-6 col-lg-3">
            <div class="alert alert-warning">
                <strong>Dine In - Cash</strong><br>
                Total: Rp<?= number_format($total_dinein_cash, 0, ',', '.') ?><br>
                Qty: <?= number_format($qty_dinein_cash) ?> unit
            </div>
        </div>
        <div class="col-md-6 col-lg-3">
            <div class="alert alert-danger">
                <strong>Dine In - Transfer</strong><br>
                Total: Rp<?= number_format($total_dinein_transfer, 0, ',', '.') ?><br>
                Qty: <?= number_format($qty_dinein_transfer) ?> unit
            </div>
        </div>
    </div>

    <div class="table-responsive mt-4">
    <div class="table-responsive mt-4">
    <table class="table table-bordered table-hover table-sm align-middle">
        <thead class="text-center">
            <tr class="bg-info-subtle text-info-emphasis">
                <th rowspan="2" class="align-middle">No</th>
                <th rowspan="2" class="align-middle">Pelanggan</th>
                <th colspan="2" class="bg-light text-dark">Take Away</th>
                <th colspan="2" class="bg-light text-dark">Dine In</th>
                <th rowspan="2" class="align-middle">Total Pembelian</th>
            </tr>
            <tr class="bg-light text-dark">
                <th>Cash</th>
                <th>Transfer</th>
                <th>Cash</th>
                <th>Transfer</th>
            </tr>
        </thead>
        <tbody>
            <?php $no = 1; foreach ($summary as $row): ?>
                <?php
                    $total = $row['takeaway_cash_total'] + $row['takeaway_transfer_total'] +
                             $row['dinein_cash_total'] + $row['dinein_transfer_total'];
                ?>
                <tr>
                    <td class="text-center"><?= $no++ ?></td>
                    <td><?= esc($row['customer_name']) ?></td>
                    <td class="text-end">
                        Rp<?= number_format($row['takeaway_cash_total'], 0, ',', '.') ?><br>
                        <small class="text-muted"><?= $row['takeaway_cash_qty'] ?> unit</small>
                    </td>
                    <td class="text-end">
                        Rp<?= number_format($row['takeaway_transfer_total'], 0, ',', '.') ?><br>
                        <small class="text-muted"><?= $row['takeaway_transfer_qty'] ?> unit</small>
                    </td>
                    <td class="text-end">
                        Rp<?= number_format($row['dinein_cash_total'], 0, ',', '.') ?><br>
                        <small class="text-muted"><?= $row['dinein_cash_qty'] ?> unit</small>
                    </td>
                    <td class="text-end">
                        Rp<?= number_format($row['dinein_transfer_total'], 0, ',', '.') ?><br>
                        <small class="text-muted"><?= $row['dinein_transfer_qty'] ?> unit</small>
                    </td>
                    <td class="text-end fw-semibold bg-success-subtle text-success-emphasis">
                        Rp<?= number_format($total, 0, ',', '.') ?>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>


</div>

<?= $this->endSection() ?>

