<?php
$total_tunai = 0;
$total_transfer = 0;
$total_pembelian = 0;
$total_pengeluaran = 0;
$total_omset = 0;
$total_pendapatan_bersih = 0;

if (!empty($laporan)) {
  foreach ($laporan as $row) {
    $omset_harian = (float)$row->TOTAL;
    $pendapatan_harian = $omset_harian - (float)$row->PEMBELIAN - (float)$row->PENGELUARAN;

    $total_tunai += (float)$row->TUNAI;
    $total_transfer += (float)$row->TRANSFER;
    $total_pembelian += (float)$row->PEMBELIAN;
    $total_pengeluaran += (float)$row->PENGELUARAN;
    $total_omset += $omset_harian;
    $total_pendapatan_bersih += $pendapatan_harian;
  }
}

$total_uang_keluar = $total_pembelian + $total_pengeluaran;
$total_rata = !empty($total_hari) ? $total_omset / $total_hari : 0;
?>

<div class="pg-monthly-report">
  <style>
    .pg-monthly-report{
      color:#344054;
      text-align:left;
    }
    .pg-monthly-kpis{
      display:grid;
      grid-template-columns:repeat(3,minmax(0,1fr));
      margin:0 0 18px;
      padding:20px 24px;
      border:1px solid #e4e9f1;
      border-radius:10px;
      background:#fff;
      box-shadow:0 6px 18px rgba(15,23,42,.07);
    }
    .pg-monthly-kpi{
      min-width:0;
      display:flex;
      align-items:center;
      gap:18px;
      padding:2px 26px;
      border-right:1px solid #e7ebf1;
    }
    .pg-monthly-kpi:first-child{ padding-left:0; }
    .pg-monthly-kpi:last-child{
      padding-right:0;
      border-right:0;
    }
    .pg-monthly-kpi-icon{
      width:52px;
      height:52px;
      flex:0 0 52px;
      display:flex;
      align-items:center;
      justify-content:center;
      border-radius:50%;
      color:#fff;
      font-size:22px;
      box-shadow:0 8px 16px rgba(15,23,42,.12);
    }
    .pg-kpi-blue .pg-monthly-kpi-icon{ background:linear-gradient(135deg,#67b7ff,#348cf3); }
    .pg-kpi-green .pg-monthly-kpi-icon{ background:linear-gradient(135deg,#6bd58b,#35b969); }
    .pg-kpi-orange .pg-monthly-kpi-icon{ background:linear-gradient(135deg,#ffbd3f,#ff970f); }
    .pg-monthly-kpi-copy{
      min-width:0;
    }
    .pg-monthly-kpi-copy small{
      display:block;
      margin-bottom:5px;
      color:#667085;
      font-size:12px;
      line-height:1.2;
      font-weight:600;
      white-space:nowrap;
    }
    .pg-monthly-kpi-copy strong{
      display:block;
      font-size:20px;
      line-height:1.15;
      font-weight:800;
      white-space:nowrap;
      font-variant-numeric:tabular-nums;
    }
    .pg-kpi-blue strong{ color:#287ff0; }
    .pg-kpi-green strong{ color:#20ad59; }
    .pg-kpi-orange strong{ color:#ff9913; }

    .pg-monthly-table-card{
      padding:18px 14px 16px;
      border:1px solid #e4e9f1;
      border-radius:10px;
      background:#fff;
      box-shadow:0 6px 18px rgba(15,23,42,.07);
    }
    .pg-monthly-table-scroll{
      overflow-x:auto;
      border:1px solid #e5eaf2;
      border-radius:7px;
    }
    .pg-monthly-table{
      width:100%;
      min-width:940px;
      margin:0!important;
      border:0!important;
      border-collapse:collapse!important;
      table-layout:fixed;
      background:#fff;
      color:#475467;
      font-size:12.5px;
    }
    .pg-monthly-table th,
    .pg-monthly-table td{
      height:36px;
      padding:7px 12px!important;
      border:1px solid #e7ebf2!important;
      vertical-align:middle!important;
      line-height:1.2;
      font-variant-numeric:tabular-nums;
    }
    .pg-monthly-table thead th{
      position:static;
      background:#f3f6fc!important;
      color:#344054!important;
      border-color:#e4e9f1!important;
      font-size:12px;
      font-weight:800;
      text-transform:none;
    }
    .pg-monthly-table tbody tr:hover{
      background:#f8fafc;
    }
    .pg-monthly-table .pg-col-no{ width:5%; }
    .pg-monthly-table .pg-col-date{ width:14%; }
    .pg-monthly-table .pg-col-number{ width:13.5%; }
    .pg-monthly-table .text-left{ text-align:left!important; }
    .pg-monthly-table .text-center{ text-align:center!important; }
    .pg-monthly-table .text-right{ text-align:right!important; }
    .pg-monthly-table .nowrap{ white-space:nowrap!important; }
    .pg-monthly-table .pg-net-value{
      font-weight:600;
    }
    .pg-monthly-table .is-negative{
      color:#ff4d5f!important;
      font-weight:700;
    }
    .pg-monthly-table tfoot .pg-total-row th{
      background:#f0f5ff!important;
      color:#2872e6!important;
      font-weight:800;
    }

    .pg-monthly-summary{
      width:42%;
      min-width:440px;
      margin:12px 14px 0 auto;
    }
    .pg-monthly-summary-row{
      display:grid;
      grid-template-columns:minmax(0,1fr) 150px;
      align-items:center;
      min-height:32px;
      border-bottom:1px solid #e7ebf2;
      color:#344054;
      font-size:12px;
    }
    .pg-monthly-summary-row span{
      padding:7px 10px;
      font-weight:700;
    }
    .pg-monthly-summary-row strong{
      padding:7px 10px;
      text-align:right;
      white-space:nowrap;
      font-weight:800;
      font-variant-numeric:tabular-nums;
    }
    .pg-monthly-summary-row:first-child strong{
      color:#2872e6;
    }
    .pg-monthly-empty{
      padding:32px 16px;
      text-align:center;
      color:#667085;
      font-weight:600;
    }

    @media (max-width:1200px){
      .pg-monthly-kpi{ padding-left:18px; padding-right:18px; }
      .pg-monthly-summary{ width:55%; }
    }
    @media (max-width:768px){
      .pg-monthly-kpis{
        grid-template-columns:1fr;
        padding:14px;
        gap:0;
      }
      .pg-monthly-kpi{
        gap:10px;
        padding:12px 10px;
        border-right:0;
        border-bottom:1px solid #e7ebf1;
      }
      .pg-monthly-kpi:last-child{ border-bottom:0; }
      .pg-monthly-kpi-icon{
        width:40px;
        height:40px;
        flex-basis:40px;
        font-size:17px;
      }
      .pg-monthly-kpi-copy small{
        font-size:10px;
        white-space:normal;
      }
      .pg-monthly-kpi-copy strong{ font-size:14px; }
      .pg-monthly-table-card{ padding:10px; }
      .pg-monthly-summary{
        width:100%;
        min-width:0;
        margin:10px 0 0;
      }
      .pg-monthly-summary-row{
        grid-template-columns:minmax(0,1fr) 118px;
        font-size:10px;
      }
    }
  </style>

  <div class="pg-monthly-kpis">
    <div class="pg-monthly-kpi pg-kpi-blue">
      <div class="pg-monthly-kpi-icon"><i class="fa fa-line-chart"></i></div>
      <div class="pg-monthly-kpi-copy"><small>Total Pendapatan</small><strong>Rp. <?= number_format($total_omset, 0, ',', '.'); ?></strong></div>
    </div>
    <div class="pg-monthly-kpi pg-kpi-green">
      <div class="pg-monthly-kpi-icon"><i class="fa fa-arrow-down"></i></div>
      <div class="pg-monthly-kpi-copy"><small>Total Pengeluaran</small><strong>Rp. <?= number_format($total_uang_keluar, 0, ',', '.'); ?></strong></div>
    </div>
    <div class="pg-monthly-kpi pg-kpi-orange">
      <div class="pg-monthly-kpi-icon"><i class="fa fa-money"></i></div>
      <div class="pg-monthly-kpi-copy"><small>Pendapatan Bersih</small><strong>Rp. <?= number_format($total_pendapatan_bersih, 0, ',', '.'); ?></strong></div>
    </div>
  </div>

  <div class="pg-monthly-table-card">
    <div class="pg-monthly-table-scroll">
      <table class="table pg-monthly-table table2excel">
        <thead>
          <tr>
            <th class="pg-col-no text-center">No</th>
            <th class="pg-col-date text-left">Tanggal</th>
            <th class="pg-col-number text-center">Tunai</th>
            <th class="pg-col-number text-center">Transfer</th>
            <th class="pg-col-number text-center">Sub Total</th>
            <th class="pg-col-number text-center">Pembelian</th>
            <th class="pg-col-number text-center">Pengeluaran</th>
            <th class="pg-col-number text-center">Total Pendapatan</th>
          </tr>
        </thead>

        <?php if (!empty($laporan)) { ?>
          <tbody>
            <?php $no = 1; foreach ($laporan as $row):
              $omset_harian = (float)$row->TOTAL;
              $pendapatan_harian = $omset_harian - (float)$row->PEMBELIAN - (float)$row->PENGELUARAN;
              $pendapatan_teks = ($pendapatan_harian < 0 ? '-Rp. ' : 'Rp. ') . number_format(abs($pendapatan_harian), 0, ',', '.');
            ?>
              <tr>
                <td class="text-center"><?= $no; ?></td>
                <td class="text-left nowrap"><?= dtime($row->tanggal); ?></td>
                <td class="text-right"><?= number_format($row->TUNAI, 0, ',', '.'); ?></td>
                <td class="text-right"><?= number_format($row->TRANSFER, 0, ',', '.'); ?></td>
                <td class="text-right"><?= number_format($omset_harian, 0, ',', '.'); ?></td>
                <td class="text-right"><?= number_format($row->PEMBELIAN, 0, ',', '.'); ?></td>
                <td class="text-right"><?= number_format($row->PENGELUARAN, 0, ',', '.'); ?></td>
                <td class="text-right pg-net-value <?= $pendapatan_harian < 0 ? 'is-negative' : ''; ?>"><?= $pendapatan_teks; ?></td>
              </tr>
            <?php $no++; endforeach; ?>
          </tbody>
          <tfoot>
            <tr class="pg-total-row">
              <th colspan="2" class="text-left">TOTAL</th>
              <th class="text-right">Rp. <?= number_format($total_tunai, 0, ',', '.'); ?></th>
              <th class="text-right">Rp. <?= number_format($total_transfer, 0, ',', '.'); ?></th>
              <th class="text-right">Rp. <?= number_format($total_omset, 0, ',', '.'); ?></th>
              <th class="text-right">Rp. <?= number_format($total_pembelian, 0, ',', '.'); ?></th>
              <th class="text-right">Rp. <?= number_format($total_pengeluaran, 0, ',', '.'); ?></th>
              <th class="text-right">Rp. <?= number_format($total_pendapatan_bersih, 0, ',', '.'); ?></th>
            </tr>
          </tfoot>
        <?php } else { ?>
          <tbody><tr><td colspan="8" class="pg-monthly-empty">Belum ada data</td></tr></tbody>
        <?php } ?>
      </table>
    </div>

    <?php if (!empty($laporan)) { ?>
      <div class="pg-monthly-summary">
        <div class="pg-monthly-summary-row"><span>TOTAL PENDAPATAN BERSIH</span><strong>Rp. <?= number_format($total_pendapatan_bersih, 0, ',', '.'); ?></strong></div>
        <div class="pg-monthly-summary-row"><span>PENDAPATAN RATA-RATA / HARI</span><strong>Rp. <?= number_format($total_rata, 0, ',', '.'); ?></strong></div>
        <div class="pg-monthly-summary-row"><span>PENDAPATAN TERTINGGI</span><strong>Rp. <?= number_format($harga_tertinggi, 0, ',', '.'); ?></strong></div>
        <div class="pg-monthly-summary-row"><span>PENDAPATAN TERENDAH</span><strong>Rp. <?= number_format($harga_terendah, 0, ',', '.'); ?></strong></div>
      </div>
    <?php } ?>
  </div>
</div>
