<?php include 'header.php'; ?>

<div class="content-wrapper">

  <section class="content-header">
    <h1>
      Transaksi
      <small>Data Transaksi</small>
    </h1>
    <ol class="breadcrumb">
      <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
      <li class="active">Dashboard</li>
    </ol>
  </section>

  <section class="content">
    <div class="row">
      <section class="col-lg-12">
        <div class="box box-info">

          <div class="box-header">
            <h3 class="box-title">Transaksi Pemasukan & Pengeluaran</h3>
            <div class="btn-group pull-right">            

              <button type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
                <i class="fa fa-plus"></i> &nbsp Tambah Transaksi Kas
              </button>
            </div><hr>
            <?php 
                // Notifikasi akan ditampilkan via SweetAlert di footer.php
                ?>
          </div>
          
          <!-- Filter Section -->
          <div class="box-body" style="border-bottom: 1px solid #f4f4f4;">
            <form id="filterForm" method="get">
              <div class="row">
                <div class="col-md-3">
                  <div class="form-group">
                    <label>Dari Tanggal</label>
                    <input autocomplete="off" type="text" id="tanggal_dari" name="tanggal_dari" class="form-control datepicker2" placeholder="Dari Tanggal">
                  </div>
                </div>
                <div class="col-md-3">
                  <div class="form-group">
                    <label>Sampai Tanggal</label>
                    <input autocomplete="off" type="text" id="tanggal_sampai" name="tanggal_sampai" class="form-control datepicker2" placeholder="Sampai Tanggal">
                  </div>
                </div>
                <div class="col-md-3">
                  <div class="form-group">
                    <label>Kategori</label>
                    <select id="filter_kategori" name="filter_kategori" class="form-control select2-filter">
                      <option value="">Semua Kategori</option>
                      <?php 
                      include '../koneksi.php';
                      $kategori = mysqli_query($koneksi,"SELECT * FROM kategori ORDER BY kategori ASC");
                      while($k = mysqli_fetch_array($kategori)){
                        ?>
                        <option value="<?php echo $k['kategori_id']; ?>"><?php echo $k['kategori']; ?></option>
                        <?php 
                      }
                      ?>
                    </select>
                  </div>
                </div>
                <div class="col-md-3">
                  <div class="form-group">
                    <label>Jenis Transaksi</label>
                    <select id="filter_jenis" name="filter_jenis" class="form-control select2-filter">
                      <option value="">Semua Jenis</option>
                      <option value="Pemasukan">Pemasukan</option>
                      <option value="Pengeluaran">Pengeluaran</option>
                    </select>
                  </div>
                </div>
              </div>
              <div class="row">
                <div class="col-md-12">
                  <button type="button" id="btnFilter" class="btn btn-primary">
                    <i class="fa fa-filter"></i> Filter
                  </button>
                  <button type="button" id="btnReset" class="btn btn-default">
                    <i class="fa fa-refresh"></i> Reset
                  </button>
                </div>
              </div>
            </form>
          </div>
          
          <div class="box-body">

            <!-- Modal -->
            <form action="transaksi_act.php" method="post" enctype="multipart/form-data">
              <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div class="modal-dialog" role="document">
                  <div class="modal-content">
                    <div class="modal-header">
                      <h4 class="modal-title" id="exampleModalLabel">Tambah Transaksi Kas</h4>
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                      </button>
                    </div>
                    <div class="modal-body">

                      <div class="form-group">
                        <label>Tanggal</label>
                        <input type="text" name="tanggal" required="required" class="form-control datepicker2" placeholder="Pilih tanggal (dd/mm/yyyy)" autocomplete="off">
                      </div>

                      <div class="form-group">
                        <label>Jenis Transaksi</label>
                        <?php if (!empty($is_input_pengeluaran)) { ?>
                        <input type="hidden" name="jenis" value="Pengeluaran">
                        <input type="text" class="form-control" value="Pengeluaran" readonly disabled>
                        <small class="text-muted">Role Anda hanya dapat input transaksi pengeluaran.</small>
                        <?php } else { ?>
                        <select name="jenis" class="form-control" required="required">
                          <option value="">- Pilih -</option>
                          <option value="Pemasukan">Pemasukan</option>
                          <option value="Pengeluaran">Pengeluaran</option>
                        </select>
                        <?php } ?>
                      </div>

                      <div class="form-group">
                        <label>Kategori</label>
                        <select name="kategori" class="form-control select2-form" required="required" data-placeholder="Cari atau pilih kategori...">
                          <option value="">- Pilih Kategori -</option>
                          <?php 
                          $kategori = mysqli_query($koneksi,"SELECT * FROM kategori ORDER BY kategori ASC");
                          while($k = mysqli_fetch_array($kategori)){
                            ?>
                            <option value="<?php echo $k['kategori_id']; ?>"><?php echo htmlspecialchars($k['kategori']); ?></option>
                            <?php 
                          }
                          ?>
                        </select>
                      </div>

                      <div class="form-group">
                        <label>Nominal</label>
                        <input type="text" name="nominal" required="required" class="form-control input-rupiah" placeholder="0" autocomplete="off">
                      </div>

                      <div class="form-group">
                        <label>Keterangan</label>
                        <textarea name="keterangan" class="form-control" rows="3"></textarea>
                      </div>

                      <div class="form-group">
                        <label>Bukti Upload <small class="text-muted">(Opsional)</small></label>
                        <input type="file" name="trnfoto" class="form-control">
                        <small class="text-muted">Kosongkan jika tidak ada bukti. File: PDF, JPG, JPEG.</small>
                      </div>

                      <div class="form-group">
                        <label>Rekening Bank</label>
                        <select name="bank" class="form-control select2-form" required="required" data-placeholder="Cari atau pilih rekening...">
                          <option value="">- Pilih Rekening Bank -</option>
                          <?php 
                          $bank = mysqli_query($koneksi,"SELECT * FROM bank ORDER BY bank_nama ASC");
                          while($b = mysqli_fetch_array($bank)){
                            $label = $b['bank_nama'];
                            if (!empty($b['bank_nomor'])) $label .= ' - ' . $b['bank_nomor'];
                            ?>
                            <option value="<?php echo $b['bank_id']; ?>"><?php echo htmlspecialchars($label); ?></option>
                            <?php 
                          }
                          ?>
                        </select>
                      </div>

                    </div>
                    <div class="modal-footer">
                      <button type="button" class="btn btn-danger" data-dismiss="modal">Tutup</button>
                      <button type="submit" class="btn btn-success">Simpan</button>
                    </div>
                  </div>
                </div>
              </div>
            </form>


            <div class="table-responsive">
              <table class="table table-bordered table-striped" id="table-datatable">
                <thead>
                  <tr>
                    <th width="1%" rowspan="2">NO</th>
                    <th width="10%" rowspan="2" class="text-center">TANGGAL</th>
                    <th rowspan="2" class="text-center">KATEGORI</th>
                    <th rowspan="2" class="text-center">KETERANGAN</th>
                    <th colspan="2" class="text-center">JENIS</th>
                    <th rowspan="2" width="10%" class="text-center">OPSI</th>
                  </tr>
                  <tr>
                    <th class="text-center">PEMASUKAN</th>
                    <th class="text-center">PENGELUARAN</th>
                  </tr>
                </thead>

                <tbody>
                  <!-- Data akan di-load via AJAX -->
                </tbody>
              </table>
            </div>

            <!-- Total Pemasukan & Pengeluaran (dinamis sesuai filter) -->
            <div class="row" id="transaksi-totals" style="margin-top: 16px;">
              <div class="col-md-4">
                <div class="info-box bg-green">
                  <span class="info-box-icon"><i class="fa fa-arrow-down"></i></span>
                  <div class="info-box-content">
                    <span class="info-box-text">Total Pemasukan</span>
                    <span class="info-box-number" id="total-pemasukan">Rp. 0 ,-</span>
                  </div>
                </div>
              </div>
              <div class="col-md-4">
                <div class="info-box bg-red">
                  <span class="info-box-icon"><i class="fa fa-arrow-up"></i></span>
                  <div class="info-box-content">
                    <span class="info-box-text">Total Pengeluaran</span>
                    <span class="info-box-number" id="total-pengeluaran">Rp. 0 ,-</span>
                  </div>
                </div>
              </div>
              <div class="col-md-4">
                <div class="info-box bg-aqua">
                  <span class="info-box-icon"><i class="fa fa-balance-scale"></i></span>
                  <div class="info-box-content">
                    <span class="info-box-text">Selisih (Pemasukan - Pengeluaran)</span>
                    <span class="info-box-number" id="total-selisih">Rp. 0 ,-</span>
                  </div>
                </div>
              </div>
            </div>
          </div>

        </div>
      </section>
    </div>
  </section>

</div>

<!-- Modal Dinamis untuk Edit -->
<div class="modal fade" id="modalEdit" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content" id="modalEditContent">
      <!-- Content akan di-load via AJAX -->
    </div>
  </div>
</div>

<!-- Modal Dinamis untuk Lihat -->
<div class="modal fade" id="modalLihat" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title" id="exampleModalLabel">Lihat Bukti Upload</h4>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body" id="modalLihatContent">
        <!-- Content akan di-load via JavaScript -->
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
      </div>
    </div>
  </div>
</div>

<!-- Modal Dinamis untuk Hapus -->
<div class="modal fade" id="modalHapus" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title" id="exampleModalLabel">Peringatan!</h4>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Yakin ingin menghapus data ini ?</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
        <a href="#" id="btnHapusConfirm" class="btn btn-primary">Hapus</a>
      </div>
    </div>
  </div>
</div>

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