<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<script src="<?= base_url('assets/'); ?>js/glightbox.min.js"></script>

<script>
  // Hanya membuat jika belum ada (tidak menimpa implementasi asli, jika ada)
  window.searchFilter = window.searchFilter || function(termOrInput, tableSelector) {
    try {
      var q = '';
      if (typeof termOrInput === 'string') q = termOrInput;
      else if (termOrInput && termOrInput.value !== undefined) q = termOrInput.value;
      else if (window.jQuery && termOrInput && termOrInput.jquery) q = termOrInput.val();

      q = (q || '').toString().toLowerCase();
      if (!window.jQuery) return;

      var $ = window.jQuery;
      var $table = tableSelector ? $(tableSelector) : $('.table:visible').first();
      if (!$table.length) return;

      $table.find('tbody tr').each(function () {
        var show = $(this).text().toLowerCase().indexOf(q) > -1;
        $(this).toggle(show);
      });
    } catch (err) {
      console.warn('searchFilter fallback warning:', err);
    }
  };
</script>

<div class="container-fluid" id="container-wrapper">
  <div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-gray-800">Laporan Pendapatan & Pengeluaran</h1>
    <ol class="breadcrumb pg-page-breadcrumb mb-0">
      <li class="breadcrumb-item">Laporan</li>
      <li class="breadcrumb-item active" aria-current="page">Pendapatan & Pengeluaran</li>
    </ol>
  </div>

  <div class="row">
    <div class="col-12">
      <form action="<?= base_url('transaksi/cetak_pdf'); ?>" method="post" id="cetak-transaksi" target="_blank">
        <div class="card">
          <div class="card-header pb-0">

            <div class="pg-filterbar">
              <div class="pg-filterbar__fields">
                <div class="pg-field">
                  <label for="jenis">PERIODE</label>
                  <select name="jenis" id="jenis" class="form-control form-control-sm custom-select pg-control"
                          onchange="onJenisChange();search_transaksi()">
                    <option value="1" selected>PER-HARI (Rentang)</option>
                    <option value="2">PERBULAN</option>
                  </select>
                </div>

                <div class="pg-field">
                  <label for="user_trx">KASIR</label>
                  <select name="user_trx" id="user_trx" class="form-control form-control-sm custom-select pg-control"
                          onchange="search_transaksi()">
                    <?php
                    if ($level == 'admin' || $level == 'owner') {
                      echo '<option value="0">Semua</option>';
                      foreach ($pilihan as $values) {
                        echo '<option value="'.$values['id_user'].'">'.$values['nama_lengkap'].'</option>';
                      }
                    } else {
                      foreach ($pilihan as $values) {
                        if ($this->session->idu == $values['id_user']) {
                          echo '<option value="'.$values['id_user'].'" selected>'.$values['nama_lengkap'].'</option>';
                        }
                      }
                    }
                    ?>
                  </select>
                </div>

                <div class="pg-field hide-1">
                  <label for="tgl_dari">DARI</label>
                  <div class="pg-date-control">
                    <i class="fa fa-calendar-o"></i>
                    <input type="text" class="form-control form-control-sm pg-control" name="tgl_dari" id="tgl_dari"
                           placeholder="dd/mm/yyyy" autocomplete="off">
                  </div>
                </div>

                <div class="pg-field hide-1">
                  <label for="tgl_sampai">SAMPAI</label>
                  <div class="pg-date-control">
                    <i class="fa fa-calendar-o"></i>
                    <input type="text" class="form-control form-control-sm pg-control" name="tgl_sampai" id="tgl_sampai"
                           placeholder="dd/mm/yyyy" autocomplete="off">
                  </div>
                </div>

                <div class="pg-field hide-2 d-none">
                  <label for="periode">BULAN</label>
                  <input type="text" class="form-control form-control-sm pg-control date-harian"
                         value="<?= $periode; ?>" name="periode" id="periode" autocomplete="off">
                </div>

                <div class="pg-actions pg-actions-inline hide-2 d-none">
                  <button type="button" class="btn btn-success btn-sm harian pg-btn">
                    <i class="fa fa-search"></i> Lihat
                  </button>
                  <button type="submit" formaction="<?= base_url('transaksi/cetak_pdf'); ?>" class="btn btn-danger btn-sm pg-btn">
                    <i class="fa fa-file-pdf-o"></i> PDF
                  </button>
                  <button type="submit" formaction="<?= base_url('transaksi/export_excel'); ?>" class="btn btn-success btn-sm pg-btn">
                    <i class="fa fa-file-excel-o"></i> Excel
                  </button>
                </div>

                <input type="hidden" name="tanggal" id="tanggal" value="<?= $tanggal; ?>">
              </div>

              <div class="pg-filterbar__bottom">
                <div class="pg-presets hide-1" id="presetGroup" role="group" aria-label="Preset">
                  <button type="button" class="btn btn-light btn-sm btn-preset" data-preset="today">Hari ini</button>
                  <button type="button" class="btn btn-light btn-sm btn-preset" data-preset="yesterday">Kemarin</button>
                  <button type="button" class="btn btn-light btn-sm btn-preset" data-preset="thisweek">Minggu ini</button>
                  <button type="button" class="btn btn-light btn-sm btn-preset" data-preset="lastweek">Minggu kemarin</button>
                </div>

                <div class="pg-actions hide-1">
                  <button type="button" class="btn btn-success btn-sm harian pg-btn">
                    <i class="fa fa-search"></i> Lihat
                  </button>
                  <button type="submit" formaction="<?= base_url('transaksi/cetak_pdf'); ?>" class="btn btn-danger btn-sm pg-btn">
                    <i class="fa fa-file-pdf-o"></i> PDF
                  </button>
                  <button type="submit" formaction="<?= base_url('transaksi/export_excel'); ?>" class="btn btn-success btn-sm pg-btn">
                    <i class="fa fa-file-excel-o"></i> Excel
                  </button>
                </div>
              </div>
            </div>

          </div>

          <div class="card-body">
            <div class="table-responsive">
              <div id="printableTable">
                <div id="laporan_harian" class="text-center text-muted py-3">
                  Silakan pilih rentang tanggal lalu klik <b>Lihat</b>.
                </div>
              </div>
            </div>
          </div>

        </div>
      </form>
    </div>
  </div>
</div>

<iframe name="print_frame" width="0" height="0" frameborder="0"></iframe>

<!-- Modal -->
<div id="myModals" class="modals">
  <span class="closes">&times;</span>
  <img class="modal-contents" id="viewimglogo" alt="Bukti transfer">
  <div id="caption">Bukti transfer</div>
</div>

<style>
/* ====== Tabel & Card ====== */
.table td, .table th { padding: 2px; }
.card .table td, .card .table th { padding: 0 5px; }
.small { height: 30px; padding: 2px 10px; }
button, input, select { font: inherit; line-height: inherit; }

/* =========================================================
   === PATCH FINAL: Toolbar makin rapih seperti referensi ===
   - tinggi kontrol seragam
   - baris bawah tidak “kosong” di tengah
   - tombol aksi lebih proporsional
========================================================= */
.pg-filterbar{
  background: #eef0f4;
  border: 1px solid rgba(0,0,0,.08);
  border-radius: 14px;
  padding: 12px;
  margin-bottom: 12px;
  box-shadow: 0 6px 14px rgba(0,0,0,.035);
}

/* GRID atas */
.pg-filterbar__top{
  display: grid;
  grid-template-columns: repeat(4, minmax(210px, 1fr));
  gap: 10px;
  align-items: stretch;
}

.pg-ig{
  margin: 0;
  width: 100%;
}

/* label pill */
.pg-pill{
  background: #3f51b5;           /* lebih selaras & tegas */
  color: #fff;
  border: 0;
  font-weight: 800;
  letter-spacing: .3px;
  padding: 0 .9rem;
  border-radius: 12px 0 0 12px;
  min-width: 92px;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 42px;                  /* seragam */
}

/* input/select */
.pg-control{
  height: 42px !important;
  font-size: 14px;
  border-radius: 0 12px 12px 0 !important;
  border: 1px solid rgba(0,0,0,.10);
  background: #fff;
}

/* ROW bawah */
.pg-filterbar__bottom{
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 10px;
}

/* presets mengisi ruang, biar tidak ada ruang kosong mengganggu */
/* ========= PRESET AUTO-STRETCH RATA (mirip panel Data Transaksi) ========= */
.pg-presets{
  display: grid !important;                 /* ganti flex → grid */
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  flex: 1 1 auto;
  min-width: 320px;
  align-items: stretch;
}

/* Pastikan preset tersembunyi saat class d-none dipasang (mode PERBULAN) */
.pg-presets.d-none{
  display: none !important;
}

.pg-presets .btn{
  width: 100%;                              /* isi kolom grid */
  height: 40px;
  border-radius: 12px;
  padding: .45rem .85rem;
  font-weight: 800;
  border: 1px solid rgba(0,0,0,.12);
  background: #fff;
  box-shadow: 0 2px 6px rgba(0,0,0,.04);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  white-space: nowrap;                      /* jangan turun baris */
}

.pg-presets .btn.active{
  background: #3f51b5;
  border-color: #3f51b5;
  color: #fff;
}

/* kalau layar agak sempit: jadi 2 kolom */
@media (max-width: 992px){
  .pg-presets{
    grid-template-columns: repeat(2, minmax(0, 1fr));
    min-width: 0;
  }
}

/* kalau mobile kecil: 1 kolom */
@media (max-width: 480px){
  .pg-presets{
    grid-template-columns: 1fr;
  }
}
/* ========= END PRESET AUTO-STRETCH RATA ========= */


/* tombol aksi kanan */
.pg-actions{
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  flex: 0 0 auto;
  margin-left: auto;
}

.pg-btn{
  height: 40px;
  border-radius: 12px;
  padding: .45rem .95rem;
  font-weight: 800;
  min-width: 110px;   /* lebih proporsional dari 120 */
}

/* Aksi inline khusus mode PERBULAN agar sejajar dengan kontrol row atas */
.pg-actions-inline{
  align-items: stretch;
  justify-content: flex-end;
}
.pg-actions-inline .pg-btn{
  height: 42px;
}

/* ====== Breakpoints ====== */
@media (max-width: 1200px){
  .pg-filterbar__top{
    grid-template-columns: repeat(2, minmax(210px, 1fr));
  }
}

@media (max-width: 768px){
  .pg-filterbar__top{ grid-template-columns: 1fr; }

  .pg-presets{
    width: 100%;
    min-width: 0;
  }

  .pg-actions{
    width: 100%;
  }

  .pg-actions .pg-btn{
    flex: 1 1 calc(33.333% - 10px);
    min-width: 140px;
  }
}

@media (max-width: 480px){
  .pg-actions .pg-btn{
    flex: 1 1 100%;
    min-width: 0;
  }
}
/* ======================== END PATCH ======================== */


/* ====== Tampilan final sesuai referensi Screenshot_327 ====== */
#container-wrapper{ color:#1f2937; }
#container-wrapper .h3{ font-size:24px; font-weight:800; color:#111827!important; }
.pg-page-breadcrumb{
  background:transparent;
  padding:0;
  font-size:12px;
  font-weight:800;
  color:#64748b;
}
.pg-page-breadcrumb .breadcrumb-item + .breadcrumb-item::before{
  color:#94a3b8;
}
.pg-page-breadcrumb .active{
  color:#334155;
}
#container-wrapper > .row > .col-12 > form > .card{
  border:0;
  border-radius:8px;
  background:#fff;
  box-shadow:0 8px 26px rgba(15,23,42,.08);
}
#container-wrapper .card-header{
  background:#fff;
  border-bottom:0;
  border-radius:8px 8px 0 0;
  padding:18px 18px 8px;
}
#container-wrapper .card-body{ padding:14px 18px 18px; }

.pg-filterbar{
  background:#fff;
  border:1px solid #e5e7eb;
  border-radius:8px;
  padding:18px 20px;
  margin-bottom:16px;
  box-shadow:0 10px 24px rgba(15,23,42,.07);
}
.pg-filterbar__fields{
  display:grid;
  grid-template-columns:repeat(4,minmax(180px,1fr));
  gap:18px 28px;
  align-items:end;
}
.pg-field{ min-width:0; }
.pg-field label{
  display:block;
  margin:0 0 8px;
  color:#475569;
  font-size:12px;
  font-weight:800;
  letter-spacing:.02em;
}
.pg-control{
  height:42px!important;
  border:1px solid #dfe5ef;
  border-radius:6px!important;
  background:#fff;
  color:#344054;
  font-size:14px;
  font-weight:600;
  box-shadow:inset 0 1px 2px rgba(15,23,42,.03);
}
.pg-date-control{ position:relative; }
.pg-date-control .fa{
  position:absolute;
  left:14px;
  top:50%;
  transform:translateY(-50%);
  color:#64748b;
  z-index:2;
}
.pg-date-control .pg-control{ padding-left:40px; }
.pg-filterbar__bottom{
  display:grid;
  grid-template-columns:minmax(360px,1.8fr) minmax(320px,1fr);
  gap:28px;
  align-items:center;
  margin-top:18px;
}
.pg-presets{
  display:grid!important;
  grid-template-columns:repeat(4,minmax(0,1fr));
  gap:14px;
  min-width:0;
}
.pg-presets.d-none{ display:none!important; }
.pg-presets .btn{
  width:100%;
  height:42px;
  border-radius:6px;
  border:1px solid #dfe5ef;
  background:#fff;
  color:#344054;
  font-size:14px;
  font-weight:800;
  box-shadow:0 2px 6px rgba(15,23,42,.04);
}
.pg-presets .btn.active{
  background:#1769e8;
  border-color:#1769e8;
  color:#fff;
  box-shadow:0 6px 12px rgba(23,105,232,.25);
}
.pg-actions{
  display:grid;
  grid-template-columns:repeat(3,minmax(0,1fr));
  gap:14px;
  justify-content:end;
  margin-left:0;
}
.pg-btn{
  height:42px;
  border-radius:6px;
  padding:.45rem .95rem;
  font-size:14px;
  font-weight:800;
  min-width:0;
  box-shadow:0 7px 13px rgba(15,23,42,.14);
}
.pg-actions-inline{ grid-column:span 2; align-self:end; }

@media (max-width:1200px){
  .pg-filterbar__fields{ grid-template-columns:repeat(2,minmax(180px,1fr)); }
  .pg-filterbar__bottom{ grid-template-columns:1fr; gap:14px; }
}
@media (max-width:992px){
  .pg-presets{ grid-template-columns:repeat(2,minmax(0,1fr)); }
}
@media (max-width:768px){
  #container-wrapper .card-header,
  #container-wrapper .card-body{ padding-left:12px; padding-right:12px; }
  .pg-filterbar{ padding:14px; }
  .pg-filterbar__fields{ grid-template-columns:1fr; gap:14px; }
  .pg-actions{ grid-template-columns:1fr; }
}
@media (max-width:480px){
  .pg-presets{ grid-template-columns:1fr; }
}

/* ====== Final match filter/date picker to reference ====== */
.pg-filterbar{
  border-color:#e2e8f0;
  border-radius:7px;
  padding:19px 24px 20px;
  box-shadow:0 10px 26px rgba(15,23,42,.07);
}
.pg-filterbar__fields{
  grid-template-columns:1.02fr 1.02fr 1fr 1fr;
  gap:18px 28px;
}
.pg-field label{
  margin-bottom:8px;
  color:#344054;
  font-size:12px;
  line-height:1;
  font-weight:800;
}
.pg-control{
  height:43px!important;
  border-color:#dfe7f1;
  border-radius:5px!important;
  color:#1f2937;
  font-size:14px;
  font-weight:700;
  padding:0 14px;
  box-shadow:0 1px 2px rgba(15,23,42,.02);
}
select.pg-control{
  font-weight:700;
}
.pg-date-control .fa{
  left:14px;
  color:#64748b;
  font-size:15px;
}
.pg-date-control .pg-control{
  padding-left:39px;
  font-weight:700;
}
.pg-filterbar__bottom{
  grid-template-columns:minmax(520px,1.62fr) minmax(500px,1fr);
  gap:32px;
  margin-top:18px;
}
.pg-presets{
  grid-template-columns:repeat(4,minmax(0,1fr));
  gap:14px;
}
.pg-presets .btn{
  height:42px;
  border-radius:5px;
  border-color:#dfe7f1;
  color:#1f2937;
  font-size:14px;
  font-weight:800;
  background:#fff;
  box-shadow:0 3px 6px rgba(15,23,42,.10);
}
.pg-presets .btn.active{
  background:#176eea;
  border-color:#176eea;
  box-shadow:0 7px 14px rgba(23,110,234,.28);
}
.pg-actions{
  grid-template-columns:1fr 1.15fr 1.15fr;
  gap:14px;
}
.pg-btn{
  height:42px;
  border-radius:5px;
  font-size:14px;
  font-weight:800;
  box-shadow:0 7px 13px rgba(15,23,42,.15);
}
@media (max-width:1200px){
  .pg-filterbar__fields{ grid-template-columns:repeat(2,minmax(180px,1fr)); }
  .pg-filterbar__bottom{ grid-template-columns:1fr; }
}

@media (max-width:576px){
  #container-wrapper{
    padding-left:14px;
    padding-right:14px;
  }
  #container-wrapper .d-sm-flex{
    display:block!important;
  }
  #container-wrapper .h3{
    font-size:22px;
    line-height:1.28;
    margin-bottom:6px!important;
  }
  .pg-page-breadcrumb{
    font-size:12px;
    margin-top:0;
  }
  #container-wrapper > .row > .col-12 > form > .card{
    border:0;
    box-shadow:none;
    background:transparent;
  }
  #container-wrapper .card-header,
  #container-wrapper .card-body{
    padding-left:0;
    padding-right:0;
  }
  .pg-filterbar{
    padding:18px 20px;
    border-radius:14px;
    box-shadow:0 10px 24px rgba(15,23,42,.08);
  }
  .pg-filterbar__fields{
    grid-template-columns:repeat(2,minmax(0,1fr))!important;
    gap:16px 18px;
  }
  .pg-field label{
    font-size:13px;
    text-transform:none;
  }
  .pg-control{
    height:43px!important;
    font-size:15px;
    padding-left:14px;
    padding-right:14px;
  }
  .pg-date-control .pg-control{
    padding-left:40px;
  }
  .pg-filterbar__bottom{
    grid-template-columns:1fr!important;
    gap:16px;
    margin-top:18px;
  }
  .pg-presets{
    grid-template-columns:repeat(2,minmax(0,1fr))!important;
    gap:12px;
  }
  .pg-presets .btn{
    height:40px;
    font-size:13px;
    white-space:nowrap;
  }
  .pg-actions{
    grid-template-columns:repeat(3,minmax(0,1fr))!important;
    gap:14px;
  }
  .pg-btn{
    height:42px;
    font-size:14px;
    padding:.45rem .6rem;
  }
  .scroll-fab,
  .ba-we-love-subscribers-wrap{
    display:none!important;
  }
}

@media (max-width:380px){
  .pg-filterbar{
    padding:16px 14px;
  }
  .pg-filterbar__fields{
    gap:14px 12px;
  }
  .pg-actions{
    gap:10px;
  }
  .pg-btn,
  .pg-presets .btn{
    font-size:12px;
  }
}

/* Mobile report filter: match the reference spacing and controls. */
@media (max-width:576px){
  #container-wrapper{
    padding-left:14px!important;
    padding-right:14px!important;
  }
  #container-wrapper .h3{
    font-size:22px!important;
    line-height:1.22!important;
    margin-bottom:8px!important;
  }
  .pg-page-breadcrumb{
    margin-bottom:14px!important;
    font-size:12px!important;
  }
  .pg-filterbar{
    width:100%!important;
    margin:0 0 18px!important;
    padding:18px 20px 20px!important;
    border:1px solid #e1e7f0!important;
    border-radius:16px!important;
    background:#fff!important;
    box-shadow:0 8px 22px rgba(15,23,42,.08)!important;
  }
  .pg-filterbar__fields{
    grid-template-columns:repeat(2,minmax(0,1fr))!important;
    gap:17px 20px!important;
  }
  .pg-field label{
    margin:0 0 8px!important;
    color:#334e7c!important;
    font-size:14px!important;
    line-height:1!important;
    font-weight:800!important;
    text-transform:none!important;
  }
  .pg-control{
    width:100%!important;
    height:43px!important;
    border:1px solid #dce4ef!important;
    border-radius:6px!important;
    color:#17233c!important;
    background:#fff!important;
    font-size:16px!important;
    font-weight:800!important;
    box-shadow:none!important;
  }
  .pg-date-control .fa{
    left:14px!important;
    color:#475569!important;
    font-size:16px!important;
  }
  .pg-date-control .pg-control{
    padding-left:42px!important;
  }
  .pg-filterbar__bottom{
    display:grid!important;
    grid-template-columns:1fr!important;
    gap:16px!important;
    margin-top:16px!important;
  }
  .pg-presets{
    display:grid!important;
    grid-template-columns:repeat(2,minmax(0,1fr))!important;
    gap:12px 14px!important;
  }
  .pg-presets .btn{
    height:40px!important;
    border:1px solid #dce4ef!important;
    border-radius:6px!important;
    color:#17233c!important;
    background:#fff!important;
    font-size:14px!important;
    font-weight:800!important;
    box-shadow:0 3px 7px rgba(15,23,42,.10)!important;
  }
  .pg-presets .btn.active{
    background:#d8def0!important;
    border-color:#d8def0!important;
    color:#17233c!important;
    box-shadow:none!important;
  }
  .pg-actions{
    display:grid!important;
    grid-template-columns:repeat(3,minmax(0,1fr))!important;
    gap:14px!important;
    width:100%!important;
  }
  .pg-btn{
    width:100%!important;
    height:44px!important;
    border-radius:6px!important;
    padding:0 8px!important;
    font-size:15px!important;
    font-weight:800!important;
    box-shadow:0 7px 13px rgba(15,23,42,.16)!important;
  }
  .pg-btn .fa{
    font-size:16px!important;
    margin-right:5px!important;
  }
  .scroll-fab,
  .ba-we-love-subscribers-wrap{
    display:none!important;
  }
}

@media (max-width:380px){
  .pg-filterbar{
    padding-left:18px!important;
    padding-right:18px!important;
  }
  .pg-filterbar__fields{
    gap:15px 16px!important;
  }
  .pg-control{
    font-size:14px!important;
  }
  .pg-presets{
    gap:11px 12px!important;
  }
  .pg-actions{
    gap:12px!important;
  }
  .pg-btn{
    font-size:13px!important;
  }
}

/* Modal bukti transfer */
.modals { display: none; position: fixed; z-index: 100; padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background: #fff; }
.modal-contents { margin: auto; display: block; width: 80%; max-width: 700px; position: fixed; top: 0; left: 10%; right: 10%; }
#caption { margin: auto; display: block; width: 80%; max-width: 700px; text-align: center; color: #666; padding: 10px 0; height: auto; }
@keyframes zoom { from { transform: scale(0) } to { transform: scale(1) } }
.closes { position: absolute; top: 15px; right: 35px; color: #333; font-size: 40px; font-weight: bold; transition: .3s; }
.closes:hover { color: #999; cursor: pointer; }


/* ====== PRINT STYLES (tetap seperti punya Aa) ====== */
@media print {
  body * { visibility: hidden; }
  #printableTable, #printableTable * { visibility: visible; }
  #printableTable { position: absolute; left: 0; top: 0; right: 0; width: 100%; }
  * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; color-adjust: exact !important; }
  .table, table { border-collapse: collapse !important; }
  .table th, .table td, table th, table td { border: 1px solid #ddd !important; padding: 6px 8px !important; font-size: 12px; }
  .pg-hero, .gradient-header, .card-header, .heading, .heading2, .badge, .label { -webkit-print-color-adjust: exact !important; }
  .page-break { page-break-before: always; }
  .avoid-break { page-break-inside: avoid; }
}

@media screen { .print-only { display: none !important; } }

@media print {
  table, .table { border-collapse: collapse !important; border-spacing: 0 !important; table-layout: fixed; }
  .table th, .table td, table th, table td {
    border: 0.8pt solid #6b7280 !important;
    padding: 6pt 8pt !important;
    background-clip: padding-box;
  }
  thead th { border-bottom: 1.2pt solid #4b5563 !important; }
  .table-striped tbody tr:nth-of-type(odd) { box-shadow: none !important; }
  tr, td, th { page-break-inside: avoid !important; }
  thead { display: table-header-group; }
  tfoot { display: table-footer-group; }
  .print-solid td, .print-solid th { outline: 0.6pt solid #6b7280 !important; outline-offset: -1px; }
}

@media print {
  * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; color-adjust: exact !important; }
  table, .table { border-collapse: collapse !important; border-spacing: 0 !important; table-layout: fixed; }
  .table th, .table td, table th, table td {
    border: 0.8pt solid #6b7280 !important;
    padding: 6pt 8pt !important;
    background-clip: padding-box;
  }
  thead { display: table-header-group; }
  tfoot { display: table-footer-group; }
  tr, td, th { page-break-inside: avoid !important; }

  .pg-hero, .gradient-header, .summary-gradient, .summary-card {
    background-image: none !important;
    background: #6a5acd !important;
    color: #fff !important;
    box-shadow: none !important;
    border-color: #5b4bc4 !important;
  }

  thead th, .heading, .bg-dark, .table thead th {
    background-image: none !important;
    background: #3f3f46 !important;
    color: #ffffff !important;
    border-color: #4b5563 !important;
  }

  .heading2, .bg-secondary, .text-muted.header {
    background-image: none !important;
    background: #6b7280 !important;
    color: #ffffff !important;
    border-color: #6b7280 !important;
  }

  .print-bg-success, .bg-success, .row-success, tr.tr-success > td {
    background-image: none !important;
    background: #d1fae5 !important;
    color: #065f46 !important;
    border-color: #86efac !important;
  }

  .print-bg-warning, .bg-warning, .row-warning, tr.tr-warning > td {
    background-image: none !important;
    background: #fde68a !important;
    color: #7c2d12 !important;
    border-color: #fcd34d !important;
  }

  .print-bg-gray, .bg-light, .row-gray, tr.tr-gray > td {
    background-image: none !important;
    background: #e5e7eb !important;
    color: #111827 !important;
    border-color: #d1d5db !important;
  }

  .print-solid td, .print-solid th {
    outline: 0.6pt solid #6b7280 !important;
    outline-offset: -1px;
  }
}
</style>


<script>var base_url = "<?= base_url(); ?>";</script>
<script>
function two(n){return (n<10?'0':'')+n;}
function fmtDate(d){return two(d.getDate())+'/'+two(d.getMonth()+1)+'/'+d.getFullYear();}
function buildRangeString(dari,sampai){
  if(!dari && !sampai) return '';
  if(dari && !sampai) return dari+' - '+dari;
  if(!dari && sampai) return sampai+' - '+sampai;
  return dari+' - '+sampai;
}
function syncHiddenTanggal(){ $('#tanggal').val(buildRangeString($('#tgl_dari').val(),$('#tgl_sampai').val())); }
function parseInitialTanggal(raw){
  raw=(raw||'').trim();
  if(!raw) return {dari:'',sampai:''};
  if(raw.indexOf(' - ')!==-1){ let p=raw.split(' - '); return {dari:(p[0]||'').trim(), sampai:(p[1]||'').trim()}; }
  return {dari:raw, sampai:raw};
}

function setPreset(preset){
  const today = new Date();
  let dariDate, sampaiDate;

  if (preset === 'today') {
    dariDate = new Date(today);
    sampaiDate = new Date(today);
  } else if (preset === 'yesterday') {
    dariDate = new Date(today); dariDate.setDate(today.getDate()-1);
    sampaiDate = new Date(today); sampaiDate.setDate(today.getDate()-1);
  } else if (preset === 'thisweek') {
    const dow = today.getDay();
    const diffToMonday = (dow === 0) ? 6 : (dow - 1);
    dariDate = new Date(today); dariDate.setDate(today.getDate() - diffToMonday);
    sampaiDate = new Date(dariDate); sampaiDate.setDate(dariDate.getDate() + 6);
  } else if (preset === 'lastweek') {
    const dow = today.getDay();
    const diffToMonday = (dow === 0) ? 6 : (dow - 1);
    let mondayThisWeek = new Date(today);
    mondayThisWeek.setDate(today.getDate() - diffToMonday);
    dariDate = new Date(mondayThisWeek); dariDate.setDate(mondayThisWeek.getDate() - 7);
    sampaiDate = new Date(dariDate); sampaiDate.setDate(dariDate.getDate() + 6);
  } else {
    return;
  }

  $('#tgl_dari').val(fmtDate(dariDate));
  $('#tgl_sampai').val(fmtDate(sampaiDate));
  $('#presetGroup .btn-preset').removeClass('active');
  $('#presetGroup .btn-preset[data-preset="'+preset+'"]').addClass('active');

  syncHiddenTanggal();
  search_transaksi();
}

$('.btn-preset').on('click', function(){ setPreset($(this).data('preset')); });

$('#tgl_dari,#tgl_sampai').datepicker({
  format:'dd/mm/yyyy', autoclose:true, todayHighlight:true, todayBtn:'linked'
}).on('changeDate change', function(){
  $('#presetGroup .btn-preset').removeClass('active');
  syncHiddenTanggal();
  search_transaksi();
});

$('.date-harian').datepicker({ format:'M yyyy', autoclose:true, startView:'months', minViewMode:'months' });

function onJenisChange(){
  if($("#jenis").val()=='2'){
    $(".hide-1").addClass('d-none');
    $(".hide-2").removeClass('d-none');
  } else {
    $(".hide-1").removeClass('d-none');
    $(".hide-2").addClass('d-none');
  }
}

function search_transaksi(){ $('.harian').trigger('click'); }

$(document).on('click', '.harian', function(e){
  e.preventDefault();
  syncHiddenTanggal();
  $.ajax({
    url: base_url+'transaksi/load_data',
    type: 'POST',
    data: {
      jenis: $("#jenis").val(),
      tanggal: $("#tanggal").val(),
      periode: $("#periode").val(),
      user: $("#user_trx").val()
    },
    beforeSend: function(){ $('body').loading && $('body').loading(); },
    success: function(html){
      $("#laporan_harian").html(html);

      $("#laporan_harian")
        .find("section, .card, .table, .table-responsive")
        .addClass("avoid-break");

      const $r = $("#laporan_harian");
      $r.find("thead th, .heading, .table thead th").addClass("print-solid");
      $r.find("thead th").addClass("print-text-white");
      $r.find("tr.ringkasan-abu, .ringkasan .row-gray").addClass("print-bg-gray");
      $r.find("tr.ringkasan-hijau, .ringkasan .row-success").addClass("print-bg-success");
      $r.find("tr.ringkasan-oren,  .ringkasan .row-warning").addClass("print-bg-warning");
      $("#laporan_harian #sec-ringkasan").addClass("break-before");

      $('body').loading && $('body').loading('stop');
    },
    error: function(x,s,e){
      alert('Gagal memuat data: '+e);
      $('body').loading && $('body').loading('stop');
    }
  });
});

/* init */
(function(){
  let parsed = parseInitialTanggal($("#tanggal").val()),
      today  = new Date(),
      tstr   = fmtDate(today);
  if(!parsed.dari)   parsed.dari   = tstr;
  if(!parsed.sampai) parsed.sampai = parsed.dari;
  $('#tgl_dari').val(parsed.dari);
  $('#tgl_sampai').val(parsed.sampai);
  syncHiddenTanggal();
  onJenisChange();
  search_transaksi();
})();
</script>
