<?php
include "koneksi.php";

// ====== Pagination ======
$limit = 10;
$page  = isset($_GET['page_num']) ? (int)$_GET['page_num'] : 1;
$start = ($page - 1) * $limit;

// ====== Filter Search ======
$where = "WHERE 1=1";
if (!empty($_GET['bulan'])) {
    $bulan = $conn->real_escape_string($_GET['bulan']);
    $where .= " AND DATE_FORMAT(tanggal, '%Y-%m') = '$bulan'";
}
if (!empty($_GET['customer'])) {
    $customer = $conn->real_escape_string($_GET['customer']);
    $where .= " AND customer LIKE '%$customer%'";
}

// ====== Hitung total data ======
$result_count = $conn->query("SELECT COUNT(*) as total FROM tabel_transaksi $where");
$total_data = $result_count->fetch_assoc()['total'];
$total_page = ceil($total_data / $limit);

// ====== Ambil data transaksi ======
$sql = "
    SELECT id, no_transaksi, tanggal, customer, total, grand_total
    FROM tabel_transaksi
    $where
    ORDER BY tanggal DESC
    LIMIT $start, $limit
";
$data = $conn->query($sql);
?>

<div class="container mt-4">
    <h3>Daftar Transaksi</h3>
    <div class="mb-3">
        <a href="index.php?page=transaksi_tambah" class="btn btn-primary btn-sm">
            <i class="bi bi-plus-circle"></i> Tambah Transaksi
        </a>
    </div>

    <!-- Form Search -->
    <form method="get" class="row g-2 mb-3">
        <input type="hidden" name="page" value="transaksi">
        <div class="col-md-3">
            <input type="month" name="bulan" class="form-control" value="<?= $_GET['bulan'] ?? '' ?>">
        </div>
        <div class="col-md-3">
            <input type="text" name="customer" class="form-control" placeholder="Cari customer..." value="<?= $_GET['customer'] ?? '' ?>">
        </div>
        <div class="col-md-2">
            <button type="submit" class="btn btn-success"><i class="bi bi-search"></i> Cari</button>
        </div>
    </form>
<div class="mb-2">
    <a href="pages/transaksi_export.php?bulan=<?= $_GET['bulan'] ?? '' ?>&customer=<?= $_GET['customer'] ?? '' ?>" 
       class="btn btn-success btn-sm" target="_blank">
        <i class="bi bi-file-earmark-excel"></i> Export Excel
    </a>
</div>

    <!-- Tabel Transaksi -->
    <table class="table table-bordered table-striped">
        <thead class="table-dark">
            <tr>
                <th>No</th>
                <th>No Transaksi</th>
                <th>Tanggal</th>
                <th>Customer</th>
                <th>Total</th>
                <th>Grand Total</th>
                <th>Aksi</th>
            </tr>
        </thead>
        <tbody>
        <?php if ($data->num_rows > 0): ?>
            <?php $no = $start + 1; while ($row = $data->fetch_assoc()): ?>
                <tr>
                    <td><?= $no++ ?></td>
                    <td><?= $row['no_transaksi'] ?></td>
                    <td><?= date('d-m-Y', strtotime($row['tanggal'])) ?></td>
                    <td><?= $row['customer'] ?></td>
                    <td><?= number_format($row['total'], 0, ',', '.') ?></td>
                    <td><?= number_format($row['grand_total'], 0, ',', '.') ?></td>
                    <td>
                        <a href="index.php?page=transaksi_detail&id=<?= $row['id'] ?>" class="btn btn-info btn-sm" title="Detail">
                            <i class="fas fa-eye"></i>
                        </a>
                        <a href="index.php?page=transaksi_edit&id=<?= $row['id'] ?>" class="btn btn-warning btn-sm" title="Edit">
                            <i class="fas fa-pen"></i>
                        </a>
                        <a href="pages/transaksi_hapus.php?id=<?= $row['id'] ?>" class="btn btn-danger btn-sm" title="Hapus" onclick="return confirm('Yakin hapus transaksi ini?')">
                            <i class="fas fa-trash"></i>
                        </a>
                    </td>
                </tr>
            <?php endwhile; ?>
        <?php else: ?>
            <tr><td colspan="7" class="text-center">Tidak ada data</td></tr>
        <?php endif; ?>
        </tbody>
    </table>

    <!-- Pagination -->
    <nav>
        <ul class="pagination">
            <?php for ($i=1; $i <= $total_page; $i++): ?>
                <li class="page-item <?= ($i == $page) ? 'active' : '' ?>">
                    <a class="page-link" href="?page=transaksi&page_num=<?= $i ?>&bulan=<?= $_GET['bulan'] ?? '' ?>&customer=<?= $_GET['customer'] ?? '' ?>">
                        <?= $i ?>
                    </a>
                </li>
            <?php endfor; ?>
        </ul>
    </nav>
</div>
