<?php if (auth()->user()->role == 'customer'): ?>
    <?= $this->extend('layouts/topbar') ?>
<?php else: ?>
    <?= $this->extend('layouts/sidebar') ?>
<?php endif; ?>

<?= $this->section('content') ?>
<section class="content">
    <div class="container-fluid">
        <!-- Report Header -->
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">
                    <i class="fas fa-file-alt"></i> Laporan Pemesanan
                    <small class="ml-2"><?= date('d/m/Y', strtotime($tanggal)) ?></small>
                </h3>
                <div class="card-tools">
                    <a href="<?= base_url('laporan/exportPDF?tanggal=' . $tanggal) ?>" target="_blank" class="btn btn-danger btn-sm">
                        <i class="fas fa-file-pdf"></i> Export PDF
                    </a>
                </div>
            </div>
        </div>

        <!-- Summary Cards -->
        <div class="row">
            <div class="col-md-3">
                <div class="small-box bg-info">
                    <div class="inner">
                        <h3><?= $summary['total_orders'] ?></h3>
                        <p>Total Pesanan</p>
                    </div>
                    <div class="icon">
                        <i class="fas fa-box"></i>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="small-box bg-success">
                    <div class="inner">
                        <h3>Rp. <?= number_format($summary['total_full_payment'], 0, ",", ".") ?></h3>
                        <p>Total Lunas</p>
                    </div>
                    <div class="icon">
                        <i class="fas fa-check-circle"></i>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="small-box bg-warning">
                    <div class="inner">
                        <h3>Rp. <?= number_format($summary['total_dp_collected'], 0, ",", ".") ?></h3>
                        <p>Total DP Terkumpul</p>
                    </div>
                    <div class="icon">
                        <i class="fas fa-money-bill"></i>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="small-box bg-danger">
                    <div class="inner">
                        <h3>Rp. <?= number_format($summary['remaining_payment'], 0, ",", ".") ?></h3>
                        <p>Sisa Pembayaran</p>
                    </div>
                    <div class="icon">
                        <i class="fas fa-exclamation-triangle"></i>
                    </div>
                </div>
            </div>
        </div>

        <!-- Details Table -->
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Detail Pesanan</h3>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th>No</th>
                                <th>No Order</th>
                                <th>Customer</th>
                                <th>Tanggal Order</th>
                                <th>Nominal Pesanan</th>
                                <th>DP</th>
                                <th>Sisa</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $i = 1 ?>
                            <?php foreach ($items as $item): ?>
                                <tr>
                                    <td><?= $i++ ?></td>
                                    <td><?= $item->no_order_custom ?></td>
                                    <td><?= $item->nama ?></td>
                                    <td><?= date('d/m/Y H:i', strtotime($item->tanggal_order)) ?></td>
                                    <td class="text-right">Rp. <?= number_format($item->nominal_pelunasan, 0, ",", ".") ?></td>
                                    <td class="text-right">Rp. <?= number_format($item->dp_pembayaran, 0, ",", ".") ?></td>
                                    <td class="text-right">Rp. <?= number_format($item->sisa_pembayaran, 0, ",", ".") ?></td>
                                    <td>
                                        <?php if ($item->sisa_pembayaran == 0): ?>
                                            <span class="badge badge-success">Lunas</span>
                                        <?php elseif ($item->dp_pembayaran > 0): ?>
                                            <span class="badge badge-warning">DP</span>
                                        <?php else: ?>
                                            <span class="badge badge-danger">Belum Bayar</span>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                        <tfoot>
                            <tr class="table-active">
                                <th colspan="4">TOTAL</th>
                                <th class="text-right">Rp. <?= number_format($summary['total_revenue'], 0, ",", ".") ?></th>
                                <th class="text-right">Rp. <?= number_format($summary['total_dp_collected'], 0, ",", ".") ?></th>
                                <th class="text-right">Rp. <?= number_format($summary['remaining_payment'], 0, ",", ".") ?></th>
                                <th></th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>

        <!-- Back Button -->
        <div class="mt-3">
            <a href="<?= base_url('laporan') ?>" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Kembali
            </a>
        </div>
    </div>
</section>
<?= $this->endSection() ?>