<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>CETAK LAPORAN TRANSAKSI</title>
    <link href="<?= FCPATH; ?>assets/vendor/fontawesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <style>
      @page { margin: 0 0; }
      body {
        margin-top: 0cm;
        margin-left: 0.5cm;
        margin-right: 1cm;
        margin-bottom: 0cm;
        font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;
      }
      /** Define the footer rules **/
      footer {
        position: fixed; 
        bottom: 0cm; 
        left: 0cm; 
        right: 0cm;
        height: 0.5cm;
        padding:5px;
        background-color: <?=$info['warna_lunas'];?>;
        color: white;
        text-align: center;
      }
      .invoice-box {
        width:100%;
        margin: 0 auto;
        padding: 10px;
        font-size: 12px;
        line-height: 18px;
        font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;
        color: #555;
      }
      .invoice-box table { width: 100%; line-height: inherit; text-align: left; }
      .invoice-box table td { padding: 0 5px 0 5px; vertical-align: top; }

      .invoice-box table tr.top table td { padding-bottom: 0; }
      .invoice-box table tr.top table td.title { font-size: 45px; line-height: 45px; color: #333; }

      .invoice-box table tr.heading td { background: <?=$info['warna_lunas'];?>; color: #fff; font-weight: bold; padding: 3px; }
      .invoice-box table tr.heading th { background: #333; color: #fff; font-weight: bold; padding: 3px; }

      .invoice-box table tr.details td { padding-bottom: 20px; }
      .invoice-box table tr.item td { border-bottom: 1px dotted #000; }
      .invoice-box table tr.kepada td { color: #fff; border-bottom:1px dotted #000 }
      .invoice-box table tr.total { font-weight: bold; text-align:right; }
      .invoice-box table tr.hormat { font-weight: bold; text-align:left; }
      .invoice-box table tr.pelanggan { font-weight: bold; text-align:center; }
      .invoice-box table td.total { text-align:right; }
      .invoice-box table tr.text-right { font-weight: bold; text-align:right; }
      .invoice-box table td.text-right { font-weight: bold; text-align:right; border:1px dotted #000; }
      .invoice-box table td.ket { text-align:left; }
      .invoice-box table td.tgl { border-bottom:1px dotted #000; font-weight:bold; }
      .invoice-box table td.tkepada { background:<?=$info['warna_lunas'];?>; width:20%!important }
      .invoice-box table tr.kepada td.bawah { color:#000; width:15%!important }
      .invoice-box table td.total1 { border-left:1px solid #000; border-top:1px solid #000; border-bottom:1px dotted #000; }
      .invoice-box table td.total2 { border-top:1px solid #000; border-right:1px solid #000; border-bottom:1px dotted #000; text-align:right; font-weight:bold; }
      .invoice-box table td.umuka1 { border-left:1px solid #000; border-bottom:1px dotted #000; }
      .invoice-box table td.umuka2 { border-right:1px solid #000; border-bottom:1px dotted #000; text-align:right; font-weight:bold; }
      .invoice-box table td.sisa1 { border-left:1px solid #000; border-bottom:1px solid #000; }
      .invoice-box table td.sisa2 { border-right:1px solid #000; border-bottom:1px solid #000; text-align:right; font-weight:bold; }
      .invoice-box table td.ttd { border-bottom:1px dotted #000; text-align:center; font-weight:bold; }
      .invoice-box table td.border-1 { border:1px dotted #000; }
      .invoice-box table td.border { border-right:1px dotted #000; }
      .invoice-box table td.borderl { border-left:1px dotted #000; }
      .invoice-box table td.borderr { border-right:1px dotted #000; }

      .invoice-box .table img{ position: fixed; z-index:-1000; }
      .font-weight-bold{font-weight:bold;}
      .watermark{ right:4cm; width:4.5cm; height:auto; opacity:0.2; z-index:-1 }
      .text-right{ text-align:right; border-bottom:1px dotted #000; }
      .text-left{ text-align:left; }
      .text-center{ text-align:center; }
      .nowrap{ white-space:nowrap; }
    </style>
  </head>
  <body>
    <?php
      $alamat = base64_decode($info['deskripsi']);
      $alamat = cleanString($alamat);
    ?>
    <footer>
      <?=$info['title'];?> © <?php echo date("Y");?> 
    </footer>

    <div class="invoice-box">
      <table width="100%" cellpadding="0" cellspacing="0">
        <tr>
          <td colspan="4" rowspan="5" style="width:350px;">
            <img src="<?=$logo;?>" style="display:block; width:auto; height:auto; max-width:350px; max-height:80px; margin:0 auto;">
          </td>
          <td colspan="2" class="tgl">LAPORAN TRANSAKSI</td>
        </tr>
        <tr><td colspan="2">TGL. CETAK : <?=tgl_indo(date('Y-m-d'));?></td></tr>
        <tr><td colspan="2"></td></tr>
        <tr class="kepada">
          <td class="tkepada">PENGGUNA</td>
          <td class="bawah"><?=$user;?></td>
        </tr>
        <tr class="kepada">
          <td class="tkepada"><?=$periode_name;?></td>
          <td class="bawah"><?=($periode);?></td>
        </tr>
        <tr><td colspan="2"></td></tr>
        <tr>
          <td colspan="4">
            <span class="alamat"><?=($alamat);?></span><br>
            M:<span class="sosmed"><?=$info['phone'];?></span>&nbsp;E:<span class="sosmed"><?=$info['email'];?></span>&nbsp;FB:<span class="sosmed"><?=$info['fb'];?></span>&nbsp;IG:<span class="sosmed"><?=$info['ig'];?></span>
          <td colspan="2"></td>
        </tr>
      </table>

      <?php if(!empty($result)) { ?>
        <!-- ====== TABEL IDENTIK DENGAN laporan-bulanan.php ====== -->
        <table style="width:100%;margin-top:5px" cellpadding="0" cellspacing="0">
          <thead>
            <tr class="heading">
              <th style="width:1%;" class="text-center">NO</th>
              <th class="nowrap">TANGGAL</th>
              <th class="text-center">TUNAI</th>
              <th class="text-center">TRANSFER</th>
              <th class="text-center">SUB TOTAL</th>
              <th class="text-center">PEMBELIAN</th>
              <th class="text-center">PENGELUARAN</th>
              <th class="text-center">TOTAL PENDAPATAN</th>
            </tr>
          </thead>
          <tbody>
            <?php 
              $no = 1;

              // Akumulator selaras dengan laporan-bulanan.php
              $total_tunai       = 0;
              $total_transfer    = 0;
              $total_omset       = 0; // SUB TOTAL (TOTAL penjualan kotor per hari)
              $total_pembelian   = 0;
              $total_pengeluaran = 0;
              $total_bersih      = 0; // pendapatan bersih = SUB TOTAL - PEMBELIAN - PENGELUARAN

              // Tracking tertinggi/terendah BERDASARKAN SUB TOTAL (sesuai permintaan)
              $subtotal_tertinggi = null;
              $subtotal_terendah  = null;

              foreach($result as $row):
                // Normalisasi nilai numerik (antisipasi null)
                $tunai       = (float) ($row->TUNAI ?? 0);
                $transfer    = (float) ($row->TRANSFER ?? 0);
                $subtotal    = (float) ($row->TOTAL ?? 0);        // penjualan kotor per hari (SUB TOTAL)
                $pembelian   = (float) ($row->PEMBELIAN ?? 0);
                $pengeluaran = (float) ($row->PENGELUARAN ?? 0);

                // Hitung pendapatan bersih harian
                $bersih = $subtotal - $pembelian - $pengeluaran;

                // Akumulasi
                $total_tunai       += $tunai;
                $total_transfer    += $transfer;
                $total_omset       += $subtotal;
                $total_pembelian   += $pembelian;
                $total_pengeluaran += $pengeluaran;
                $total_bersih      += $bersih;

                // Track SUB TOTAL tertinggi & terendah
                if ($subtotal_tertinggi === null || $subtotal > $subtotal_tertinggi) { $subtotal_tertinggi = $subtotal; }
                if ($subtotal_terendah  === null || $subtotal < $subtotal_terendah ) { $subtotal_terendah  = $subtotal; }
            ?>
            <tr class="item">
              <td class="borderl borderr text-center"><?= $no; ?></td>
              <td class="borderr nowrap"><?= dtime($row->tanggal) ?></td>
              <td class="borderr total"><?= rp($tunai); ?></td>
              <td class="borderr total"><?= rp($transfer); ?></td>
              <td class="borderr total"><?= rp($subtotal); ?></td>
              <td class="borderr total"><?= rp($pembelian); ?></td>
              <td class="borderr total"><?= rp($pengeluaran); ?></td>
              <td class="borderr total"><?= rp($bersih); ?></td>
            </tr>
            <?php 
                $no++;
              endforeach; 

              // Fallback total_hari bila tidak disuplai controller
              $total_hari = isset($total_hari) ? (int)$total_hari : count($result);
              $total_hari = max(1, $total_hari); // guard divide-by-zero

              // === Sesuai laporan-bulanan.php ===
              // PENDAPATAN RATA2 / HARI = rata-rata SUB TOTAL per hari
              $total_rata = $total_omset / $total_hari;

              // PENDAPATAN TERTINGGI/TERENDAH = berdasarkan SUB TOTAL
              if (!isset($harga_tertinggi)) $harga_tertinggi = (float) $subtotal_tertinggi;
              if (!isset($harga_terendah))  $harga_terendah  = (float) $subtotal_terendah;
            ?>
          </tbody>

          <tfoot>
            <tr class="heading">
              <th colspan="2" class="text-left">TOTAL</th>
              <th class="text-right"><?= rp($total_tunai); ?></th>
              <th class="text-right"><?= rp($total_transfer); ?></th>
              <th class="text-right"><?= rp($total_omset); ?></th>
              <th class="text-right"><?= rp($total_pembelian); ?></th>
              <th class="text-right"><?= rp($total_pengeluaran); ?></th>
              <th class="text-right"><?= rp($total_bersih); ?></th>
            </tr>
            <tr>
              <th colspan="7" class="text-right">TOTAL PENDAPATAN BERSIH</th>
              <th class="text-right"><?= rprp($total_bersih); ?></th>
            </tr>
            <tr>
              <th colspan="7" class="text-right">PENDAPATAN RATA2 / HARI</th>
              <th class="text-right"><?= rprp($total_rata); ?></th>
            </tr>
            <tr>
              <th colspan="7" class="text-right">PENDAPATAN TERTINGGI</th>
              <th class="text-right"><?= rprp($harga_tertinggi); ?></th>
            </tr>
            <tr>
              <th colspan="7" class="text-right">PENDAPATAN TERENDAH</th>
              <th class="text-right"><?= rprp($harga_terendah); ?></th>
            </tr>
          </tfoot>
        </table>
      <?php } ?>
    </div>
  </body>
</html>
