<?php 
  error_reporting(0);
  include '_header.php';
  include '_nav.php';
  include '_sidebar.php'; 

   if (isset($_GET['date'])) {
      $bulan_Ini = $_GET['date'];
   } else {
      $bulan_Ini = date("Y-m");
   }

   $pecah_data       = explode("-",$bulan_Ini);
   $tahun_ini        = $pecah_data[0];
   $bln_ini          = $pecah_data[1];
?>
<?php  
  if ( $levelLogin === "kurir") {
    echo "
      <script>
        document.location.href = 'bo';
      </script>
    ";
  }
    
?>

  <style>
    .title-bo span {
      display: inline-block;
    }
    .title-bo span:nth-child(1) {
      padding-right: 20px;
    }
    .title-bo .col-3 {
      margin-top: -2px;
      padding-left: 0px;
    }
  </style>

	<!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-9">
            <div class="title-bo">
                <span>
                    <h1 class="m-0 text-dark">Data Cashdrawer</h1>
                </span>
                <span>
                  <form action="" method="GET" role="form">
                    <div class="row">
                        <div class="col-9">
                            <input type="month" class="form-control" name="date" id="bulan-tahun" value="<?= $bulan_Ini; ?>">
                        </div>
                        <div class="col-3">
                            <button type="submit" class="btn btn-primary"><i class="fa fa-filter"></i></button>
                        </div>
                    </div>
                  </form>
                </span>
            </div>
          </div>
          <div class="col-sm-3">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="bo">Home</a></li>
              <li class="breadcrumb-item active">Cashdrawer</li>
            </ol>
          </div>
          <div class="tambah-data">
          	<a href="laba-bersih-cashdrawer-add" class="btn btn-primary">Tambah Data</a>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>


    <?php  
    	$data = query("SELECT * FROM cashdrawer WHERE MONTH(cashdrawer_date)='".$bln_ini."' && YEAR(cashdrawer_date)='".$tahun_ini."' && cashdrawer_cabang = $sessionCabang ORDER BY cashdrawer_id DESC");
    ?>
    <!-- Main content -->
    <section class="content">
      <div class="row">
        <div class="col-12">

          <div class="card">
            <div class="card-header">
              <h3 class="card-title">Data Cashdrawer Keseluruhan</h3>
            </div>
            <!-- /.card-header -->
            <div class="card-body">
              <div class="table-auto">
                <table id="example1" class="table table-bordered table-striped">
                  <thead>
                  <tr>
                    <th style="width: 5%;">No.</th>
                    <th>Cashdrawer</th>
                    <th>Penerima</th>
                    <th>Tanggal</th>
                    <th>User Create</th>
                    <th>Metode</th>
                    <th>Total Dibayar</th>
                    <th style="width: 10%; text-align: center;">Aksi</th>
                  </tr>
                  </thead>
                  <tbody>

                  <?php $i = 1; ?>
                  <?php foreach ( $data as $row ) : ?>
                  <tr>
                    	<td><?= $i; ?></td>
                    	<td><?= $row['cashdrawer_name']; ?></td>
                      <td><?= $row['cashdrawer_penerima']; ?></td>
                      <td><?= tanggal_indo($row['cashdrawer_date']); ?></td>
                      <td>
                          <?php  
                            $cashdrawer_create = $row['cashdrawer_create'];
                            $userId = mysqli_query($conn, "SELECT user_nama FROM user WHERE user_id = $cashdrawer_create ");
                            $userId = mysqli_fetch_array($userId);
                            $userId = $userId['user_nama'];
                            echo $userId;
                          ?>      
                      </td>
                      <td>
                          <?php  
                            $cashdrawer_metode = $row['cashdrawer_metode'];

                            if ( $cashdrawer_metode == 1 ) {
                              echo "Transfer";
                            } elseif ( $cashdrawer_metode == 2 ) {
                              echo "Debit";
                            } elseif ( $cashdrawer_metode == 3 ) {
                              echo "QRIS";
                            } else {
                              echo "Cash";
                            }
                          ?>    
                      </td>
                      <td>Rp. <?= number_format($row['cashdrawer_total_dibayar'], 0, ',', '.'); ?></td>
                      <td class="orderan-online-button">
                        <?php $id = base64_encode($row["cashdrawer_id"]); ?>
                          <a href="laba-bersih-cashdrawer-zoom?id=<?= $id; ?>" target="_blank" title="Lihat Data">
                              <button class="btn btn-success" type="submit">
                                 <i class="fa fa-search"></i>
                              </button>
                          </a>
                          <?php if ( $levelLogin === "super admin" ) { ?>
                      	  <a href="laba-bersih-cashdrawer-edit?id=<?= $id; ?>" title="Edit Data">
                              <button class="btn btn-primary" type="submit">
                                 <i class="fa fa-edit"></i>
                              </button>
                          </a>
                          <a href="laba-bersih-cashdrawer-delete?id=<?= $id; ?>" onclick="return confirm('Yakin dihapus ?')" title="Delete Data">
                              <button class="btn btn-danger" type="submit" name="hapus">
                                  <i class="fa fa-trash-o"></i>
                              </button>
                          </a>
                          <?php } ?>
                      </td>
                  </tr>
                  <?php $i++; ?>
              	<?php endforeach; ?>
                </tbody>
                </table>
              </div>
            </div>
            <!-- /.card-body -->
          </div>
          <!-- /.card -->
        </div>
        <!-- /.col -->
      </div>
      <!-- /.row -->
    </section>
    <!-- /.content -->
  </div>
</div>



<?php include '_footer.php'; ?>
<script>
  const input = document.getElementById("bulan-tahun");
  input.addEventListener("change", function() {
    const selectedValue = input.value;
    console.log("Bulan dan Tahun yang dipilih:", selectedValue);
    // Lakukan tindakan lain dengan nilai bulan dan tahun yang dipilih
  });
</script>