<?php include_once 'fungsi/fungsi.php'; ?>
<?php include 'comp/header.php'; ?>
<div class="content-wrapper">
  <div class="content-header">
    <div class="container-fluid">
      <div class="page-header">
        <div>
          <h1>Laporan Keuangan</h1>
          <ul class="page-breadcrumb">
            <li><a href="index.php">Home</a></li>
            <li><span class="sep">&rsaquo;</span></li>
            <li>Laporan Keuangan</li>
          </ul>
        </div>
      </div>
    </div>
  </div>
  <section class="content">
    <div class="container-fluid">
      <div class="table-container">
        <div class="table-toolbar" style="flex-wrap:wrap;">
          <span class="table-toolbar-title"><i class="fas fa-chart-bar" style="margin-right:8px;color:var(--primary);"></i> Laporan Per Invoice</span>
          <form action="" method="GET" style="display:flex;gap:8px;align-items:center;flex-wrap:wrap;">
            <input type="hidden" name="m" value="laporan">
            <input type="hidden" name="s" value="title">
            <div style="display:flex;align-items:center;gap:6px;">
              <input type="date" name="start_date" class="form-control form-control-sm" value="<?= $_GET['start_date'] ?? '' ?>" style="width:130px;border-radius:8px;">
              <span style="color:var(--text-muted);font-weight:600;font-size:0.8rem;">s/d</span>
              <input type="date" name="end_date" class="form-control form-control-sm" value="<?= $_GET['end_date'] ?? '' ?>" style="width:130px;border-radius:8px;">
              <span style="color:var(--border);width:1px;height:24px;margin:0 2px;"></span>
              <input type="text" class="form-control form-control-sm" name="cari" placeholder="No. invoice..." value="<?= $_GET['cari'] ?? '' ?>" style="width:150px;border-radius:8px;">
              <button type="submit" name="go" class="btn btn-primary btn-sm" style="border-radius:8px;height:31px;display:inline-flex;align-items:center;"><i class="fas fa-search"></i></button>
              <?php if(isset($_GET['go'])): ?>
                <a href="?m=laporan&s=title" class="btn btn-light btn-sm" style="border:1px solid var(--border);border-radius:8px;height:31px;display:inline-flex;align-items:center;">Reset</a>
              <?php endif; ?>
            </div>
          </form>
        </div>
        <div class="table-responsive">
          <table class="table" id="table-laporan">
            <thead>
              <tr>
                <th style="width:30px;"></th>
                <th>No. Invoice</th>
                <th>Tanggal</th>
                <th>Jam</th>
                <th>Kasir</th>
                <th style="text-align:right;">Item</th>
                <th style="text-align:right;">Total</th>
                <th style="text-align:center;">Aksi</th>
              </tr>
            </thead>
            <tbody>
              <?php
              global $koneksi;
              $where = "WHERE no_inv IS NOT NULL AND no_inv != ''";
              if (isset($_GET['start_date']) && $_GET['start_date'] != '') {
                  $start = mysqli_real_escape_string($koneksi, $_GET['start_date']);
                  $where .= " AND tanggal >= '$start'";
              }
              if (isset($_GET['end_date']) && $_GET['end_date'] != '') {
                  $end = mysqli_real_escape_string($koneksi, $_GET['end_date']);
                  $where .= " AND tanggal <= '$end'";
              }
              if (isset($_GET['cari']) && $_GET['cari'] != '') {
                  $cari = mysqli_real_escape_string($koneksi, $_GET['cari']);
                  $where .= " AND no_inv LIKE '%$cari%'";
              }
              $q_inv = mysqli_query($koneksi, "SELECT no_inv, MAX(tanggal) as tanggal, MAX(jam) as jam, MAX(kasir) as kasir, COUNT(*) as jml_item, SUM(total) as total_inv FROM laporan_penjualan $where GROUP BY no_inv ORDER BY MAX(id) DESC LIMIT 100");
              $no = 1;
              while ($inv = mysqli_fetch_array($q_inv)):
                $items = mysqli_query($koneksi, "SELECT * FROM laporan_penjualan WHERE no_inv='" . mysqli_real_escape_string($koneksi, $inv['no_inv']) . "' ORDER BY id ASC");
              ?>
              <tr class="invoice-header" data-inv="<?= $inv['no_inv'] ?>">
                <td>
                  <button class="btn btn-sm btn-light toggle-detail" style="padding:0;width:22px;height:22px;border-radius:4px;border:1px solid var(--border);font-size:0.65rem;cursor:pointer;display:inline-flex;align-items:center;justify-content:center;line-height:1;" onmouseover="this.style.background='var(--surface-2)'" onmouseout="this.style.background=''" onclick="toggleDetail('<?= $inv['no_inv'] ?>')">
                    <i class="fas fa-plus"></i>
                  </button>
                </td>
                <td><code style="font-weight:700;"><?= $inv['no_inv'] ?></code></td>
                <td><?= $inv['tanggal'] ?></td>
                <td><?= $inv['jam'] ?></td>
                <td><?= $inv['kasir'] ?></td>
                <td style="text-align:right;font-weight:600;"><?= $inv['jml_item'] ?></td>
                <td style="text-align:right;font-weight:700;color:var(--primary);">Rp <?= number_format($inv['total_inv'], 0, ',', '.') ?></td>
                <td style="text-align:center;">
                  <a href="../../kasir/fungsi/print.php?no_inv=<?= urlencode($inv['no_inv']) ?>" target="_blank" class="btn btn-sm btn-primary" style="padding:2px 8px;font-size:0.7rem;border-radius:5px;" title="Cetak Ulang"><i class="fas fa-print"></i></a>
                  <a href="?m=retur&s=title&no_inv=<?= urlencode($inv['no_inv']) ?>" class="btn btn-sm btn-warning" style="padding:2px 8px;font-size:0.7rem;border-radius:5px;" title="Retur"><i class="fas fa-undo-alt"></i></a>
                </td>
              </tr>
              <tr class="invoice-detail" id="detail-<?= $inv['no_inv'] ?>" style="display:none;">
                <td colspan="8" style="padding:0;background:var(--surface-2);">
                  <div style="padding:6px 14px 10px;">
                    <table style="width:100%;font-size:0.8rem;">
                      <thead>
                        <tr style="border-bottom:1px solid var(--border);">
                          <th style="text-align:left;padding:4px 6px;font-size:0.7rem;color:var(--text-muted);font-weight:700;text-transform:uppercase;">Kode</th>
                          <th style="text-align:left;padding:4px 6px;font-size:0.7rem;color:var(--text-muted);font-weight:700;text-transform:uppercase;">Nama Barang</th>
                          <th style="text-align:center;padding:4px 6px;font-size:0.7rem;color:var(--text-muted);font-weight:700;text-transform:uppercase;">Qty</th>
                          <th style="text-align:right;padding:4px 6px;font-size:0.7rem;color:var(--text-muted);font-weight:700;text-transform:uppercase;">Harga</th>
                          <th style="text-align:right;padding:4px 6px;font-size:0.7rem;color:var(--text-muted);font-weight:700;text-transform:uppercase;">Subtotal</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php while ($item = mysqli_fetch_array($items)): ?>
                        <tr>
                          <td style="padding:4px 6px;"><code style="font-size:0.7rem;"><?= $item['kdbarang'] ?></code></td>
                          <td style="padding:4px 6px;font-weight:600;"><?= $item['nm_barang'] ?></td>
                          <td style="text-align:center;padding:4px 6px;"><?= $item['jumlah_beli'] ?></td>
                          <td style="text-align:right;padding:4px 6px;">Rp <?= number_format($item['total']/$item['jumlah_beli'], 0, ',', '.') ?></td>
                          <td style="text-align:right;padding:4px 6px;font-weight:600;">Rp <?= number_format($item['total'], 0, ',', '.') ?></td>
                        </tr>
                        <?php endwhile; ?>
                      </tbody>
                    </table>
                  </div>
                </td>
              </tr>
              <?php endwhile; ?>
              <?php if (mysqli_num_rows($q_inv) == 0): ?>
              <tr><td colspan="8" style="text-align:center;padding:40px;color:var(--text-muted);">Belum ada data penjualan.</td></tr>
              <?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </section>
</div>

<script>
function toggleDetail(no_inv) {
    var el = document.getElementById('detail-' + no_inv);
    if (el) {
        var isHidden = el.style.display === 'none' || el.style.display === '';
        el.style.display = isHidden ? 'table-row' : 'none';
        var btn = document.querySelector('.invoice-header[data-inv="' + no_inv + '"] .toggle-detail i');
        if (btn) {
            btn.className = isHidden ? 'fas fa-minus' : 'fas fa-plus';
        }
        var container = el.closest('.table-container');
        if (container) container.style.overflow = isHidden ? 'visible' : 'auto';
    }
}
</script>

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