<?php 
  include '_header.php';
  include '_nav.php';
  include '_sidebar.php'; 
  error_reporting(0);
?>
<?php  
  if ( $levelLogin === "kasir" && $levelLogin === "kurir" ) {
    echo "
      <script>
        document.location.href = 'bo';
      </script>
    ";
  }  
?>

	<!-- 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-6">
            <h1>Data hilangbarang</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="bo">Home</a></li>
              <li class="breadcrumb-item active">hilangbarang</li>
            </ol>
          </div>
          <div class="tambah-data">
          	<a href="hilangbarang-add" class="btn btn-primary">Tambah Data</a>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>


    <?php  
    	// $data = query("SELECT * FROM hilangbarang ORDER BY hilangbarang_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 hilangbarang 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: 6%;">No.</th>
                    <th style="width: 13%;">Kode hilangbarang</th>
                    <th>Nama</th>
                    <th>Kategori</th>
                    <th>Harga Beli</th>
                    <th>Harga Jual</th>
                    <th>Stock</th>
                    <th style="text-align: center; width: 14%">Aksi</th>
                  </tr>
                  </thead>
                  <tbody>

                  <?php 
                    $i = 1; 
                    $queryProduct = $conn->query("SELECT hilangbarang.hilangbarang_id, hilangbarang.hilangbarang_kode, hilangbarang.hilangbarang_kode_count, hilangbarang.hilangbarang_nama, hilangbarang.hilangbarang_harga_beli, hilangbarang.hilangbarang_harga, hilangbarang.hilangbarang_stock, hilangbarang.hilangbarang_option_sn, hilangbarang.hilangbarang_cabang, kategori.kategori_id, kategori.kategori_nama, satuan.satuan_id, satuan.satuan_nama
                               FROM hilangbarang 
                               JOIN kategori ON hilangbarang.kategori_id = kategori.kategori_id
                               JOIN satuan ON hilangbarang.satuan_id = satuan.satuan_id
                               WHERE hilangbarang_cabang = ".$sessionCabang." ORDER BY hilangbarang_id DESC
                               ");
                    while ($rowProduct = mysqli_fetch_array($queryProduct)) {
                  ?>
                  <tr>
                    	<td><?= $i; ?></td>
                      <td><?= $rowProduct['hilangbarang_kode']; ?></td>
                      <td><?= $rowProduct['hilangbarang_nama']; ?></td>
                      <td><?= $rowProduct['kategori_nama']; ?></td>
                      <td>Rp. <?= number_format($rowProduct['hilangbarang_harga_beli'], 0, ',', '.'); ?></td></td>
                      <td>Rp. <?= number_format($rowProduct['hilangbarang_harga'], 0, ',', '.'); ?></td>
                      <td><?= $rowProduct['hilangbarang_stock']; ?></td>
                      <td class="orderan-online-button">
                        <?php 
                          $id = $rowProduct["hilangbarang_id"]; 
                          $urlCount = base64_encode($rowProduct['hilangbarang_kode_count']);
                        ?>
                          <a href="hilangbarang-zoom?id=<?= $id; ?>-<?= strtolower(str_replace(' ', '-', $rowProduct['hilangbarang_nama'])); ?>" title="Zoom Data" target="_blank">
                              <button class="btn btn-success" type="submit">
                                 <i class="fa fa-search"></i>
                              </button>
                          </a>
                      	  <a href="hilangbarang-edit?id=<?= $id; ?>-<?= strtolower(str_replace(' ', '-', $rowProduct['hilangbarang_nama'])); ?>" title="Edit Data">
                              <button class="btn btn-primary" type="submit">
                                 <i class="fa fa-edit"></i>
                              </button>
                          </a>

                          <?php if ( $rowProduct['hilangbarang_option_sn'] > 0 ) { ?>
                          <a href="hilangbarang-sn?no=<?= $urlCount; ?>" title="Data No. SN">
                              <button class="btn btn-warning" type="submit">
                                 <i class="fa fa-sort-numeric-desc"></i>
                              </button>
                          </a>
                          <?php } ?>

                          <?php if ( $rowProduct['hilangbarang_option_sn'] < 1 ) { ?>
                          <a href="#!" title="Data No. SN" class="Data No. SN">
                              <button class="btn btn-default" type="" name="hapus">
                                  <i class="fa fa-sort-numeric-desc"></i>
                              </button>
                          </a>
                          <?php } ?>


                          <a href="hilangbarang-generate-barcode?id=<?= $id; ?>-<?= strtolower(str_replace(' ', '-', $rowProduct['hilangbarang_nama'])); ?>" title="Generate Barcode">
                              <button class="btn btn-info" type="submit">
                                 <i class="fa fa-barcode"></i>
                              </button>
                          </a>

                          <?php  
                            $penjualan = mysqli_query($conn, "select * from penjualan where hilangbarang_id = $id");
                            $jmlPenjualan = mysqli_num_rows($penjualan);
                          ?>

                          <?php if ( $jmlPenjualan < 1 ) { ?>
                          <a href="hilangbarang-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 } ?>

                          <?php if ( $jmlPenjualan > 0 ) { ?>
                          <a href="#!" title="Delete Data" class="delete-data">
                              <button class="btn btn-default" type="" name="hapus">
                                  <i class="fa fa-trash-o"></i>
                              </button>
                          </a>
                          <?php } ?>

                      </td>
                  </tr>
                  <?php $i++; ?>
                  <?php } ?>
                  </tbody>
                </table>
              </div>
            </div>
            <!-- /.card-body -->
          </div>
          <!-- /.card -->
        </div>
        <!-- /.col -->
      </div>
      <!-- /.row -->
    </section>
    <!-- /.content -->
  </div>
</div>



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

<!-- DataTables -->
<script src="plugins/datatables/jquery.dataTables.js"></script>
<script src="plugins/datatables-bs4/js/dataTables.bootstrap4.js"></script>
<script>
  $(function () {
    $("#example1").DataTable();
  });

  $(".delete-data").click(function(){
    alert("Data tidak bisa dihapus karena masih ada di data Invoice");
  });
</script>
</body>
</html>