<?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>
    ";
  }  
?>
<?php  
// ambil data di URL
$id = abs((int)base64_decode($_GET['id']));

// query data mahasiswa berdasarkan id
$hilangbarang = query("SELECT * FROM hilangbarang WHERE hilangbarang_id = $id ")[0];

// cek apakah tombol submit sudah ditekan atau belum
if( isset($_POST["submit"]) ){
  // var_dump($_POST);

  // cek apakah data berhasil di tambahkan atau tidak
  if( edithilangbarang($_POST) > 0 ) {
    echo "
      <script>
        document.location.href = 'hilangbarang';
      </script>
    ";
  } else {
    echo "
      <script>
        alert('data gagal ditambahkan');
      </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>Edit Data Hilang Barang</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">Data Hilang Barang</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <!-- left column -->
          <div class="col-md-12">
            <!-- general form elements -->
            <div class="card card-primary">
              <div class="card-header">
                <h3 class="card-title">Data Hilang Barang</h3>
              </div>
              <!-- /.card-header -->
              <!-- form start -->
              <form role="form" action="" method="post">
                <div class="card-body">
                  <div class="row">
                    <div class="col-md-6 col-lg-6">
                        <input type="hidden" name="hilangbarang_id" value="<?= $hilangbarang['hilangbarang_id']; ?>">
                        <div class="form-group">
                          <label for="hilangbarang_kode">Barcode / Kode Hilang Barang</label>
                          <input type="text" name="hilangbarang_kode" class="form-control" id="hilangbarang_kode" value="<?= $hilangbarang['hilangbarang_kode']; ?>" readonly required>
                        </div>
                        <div class="form-group">
                            <label for="hilangbarang_nama">Nama Hilang Barang</label>
                            <input type="text" name="hilangbarang_nama" class="form-control" id="hilangbarang_nama" value="<?= $hilangbarang['hilangbarang_nama']; ?>" required>
                        </div>
                        <div class="form-group">
                            <label for="hilangbarang_deskripsi">Deskripsi</label>
                            <textarea name="hilangbarang_deskripsi" id="hilangbarang_deskripsi" class="form-control" rows="5" required="required" ><?= $hilangbarang['hilangbarang_deskripsi']; ?></textarea>
                        </div>
                        <div class="form-group">
                            <label for="hilangbarang_harga_beli">Harga Beli</label> 
                            <input type="text" name="hilangbarang_harga_beli" class="form-control" id="hilangbarang_harga" value="<?= $hilangbarang['hilangbarang_harga_beli']; ?>" required>
                        </div>
                        <div class="form-group">
                            <label for="hilangbarang_harga">Harga Jual</label>
                            <input type="text" name="hilangbarang_harga" class="form-control" id="hilangbarang_harga" value="<?= $hilangbarang['hilangbarang_harga']; ?>" required>
                        </div>
                    </div>

                    <div class="col-md-6 col-lg-6">
                        <div class="form-group ">
                            <label for="kategori_id" class="">Kategori</label>
                            <div class="">
                              <select name="kategori_id" required="" class="form-control ">
                                <?php  
                                    $kategori = $hilangbarang['kategori_id'];
                                    $kategoriParent = mysqli_query( $conn, "select kategori_nama from kategori where kategori_id = ".$kategori." && kategori_status > 0 && kategori_cabang = ".$sessionCabang." ");
                                    $kn = mysqli_fetch_array($kategoriParent); 
                                    $nKn = $kn['kategori_nama'];
                                ?>

                                  <option value="<?= $kategori; ?>"><?= $nKn; ?></option>

                                  <?php $data = query("SELECT * FROM kategori WHERE  kategori_status > 0 && kategori_cabang = $sessionCabang ORDER BY kategori_id DESC"); ?>
                                  <?php foreach ( $data as $row ) : ?>
                                    <?php if ( $row['kategori_status'] === '1' ) { ?>
                                    <?php if ( $row['kategori_id'] !== $hilangbarang['kategori_id'] ) { ?>
                                      <option value="<?= $row['kategori_id']; ?>">
                                        <?= $row['kategori_nama']; ?> 
                                      </option>
                                    <?php } ?>
                                    <?php } ?>
                                  <?php endforeach; ?>
                              </select>
                            </div>
                          </div>
                          <div class="form-group ">
                              <label for="satuan_id">Satuan</label>
                              <div class="">
                                  <select name="satuan_id" required="" class="form-control ">
                                  <?php  
                                    $satuan = $hilangbarang['satuan_id'];
                                    $satuanParent = mysqli_query( $conn, "select satuan_nama from satuan where satuan_id = ".$satuan." && satuan_status > 0 && satuan_cabang = ".$sessionCabang." ");
                                    $sn = mysqli_fetch_array($satuanParent); 
                                    $nSn = $sn['satuan_nama'];
                                ?>

                                  <option value="<?= $satuan; ?>"><?= $nSn; ?></option>

                                  <?php $data1 = query("SELECT * FROM satuan WHERE satuan_status > 0 && satuan_cabang = $sessionCabang ORDER BY satuan_id DESC"); ?>
                                  <?php foreach ( $data1 as $row ) : ?>
                                    <?php if ( $row['satuan_status'] === '1' ) { ?>
                                    <?php if ( $row['satuan_id'] !== $hilangbarang['satuan_id'] ) { ?>
                                      <option value="<?= $row['satuan_id']; ?>">
                                        <?= $row['satuan_nama']; ?> 
                                      </option>
                                    <?php } ?>
                                    <?php } ?>
                                  <?php endforeach; ?>
                              </select>
                              </div>
                          </div>
                        <div class="form-group ">
                            <label for="hilangbarang_option_sn">SN or Non-SN</label>
                            <div class="">
                              <?php  
                                if ( $hilangbarang['hilangbarang_option_sn'] === '1' ) {
                                  $sn = "SN";
                                } else {
                                  $sn = "Non-SN";
                                }
                              ?>
                                <select name="hilangbarang_option_sn" required="" id="hilangbarang_option_sn" class="form-control stock-pilihan">
                                        <option value="<?= $hilangbarang['hilangbarang_option_sn']; ?>">
                                          <?= $sn; ?>
                                        </option>
                                        <?php  
                                          if ( $hilangbarang['hilangbarang_option_sn'] === '1' ) {
                                            echo '
                                              <option value="0">Non-SN</option>
                                            ';
                                          } else {
                                            echo '
                                              <option value="1">SN</option>
                                            ';
                                          }
                                        ?>
                                  </select>
                              </div>
                        </div>
                        <div class="form-group">
                          <label for="hilangbarang_stock">Stock</label>
                          <input type="number" name="hilangbarang_stock" class="form-control" id="hilangbarang_stock" value="<?= $hilangbarang['hilangbarang_stock']; ?>" required>
                        </div>
                        <div class="form-group ">
                            <label for="hilangbarang_status">Status</label>
                            <?php  
                              $status =  $hilangbarang['hilangbarang_status'];
                              if ( $status == 1 ) {
                                $namaStatushilangbarang = "Dijual";

                              } elseif ( $status == 2 ) {
                                $namaStatushilangbarang = "Khusus Servis";

                              } elseif ( $status == 3 ) {
                                $namaStatushilangbarang = "Dijual & Untuk Sevis";

                              } else {
                                $namaStatushilangbarang = "Tidak Dijual";
                              }
                            ?>
                            <div class="">
                              <select name="hilangbarang_status" required="" id="hilangbarang_status" class="form-control stock-pilihan">
                                  <option value="<?= $status; ?>"><?= $namaStatushilangbarang; ?></option>
                                  <?php if ( $status == 1 ) : ?>
                                    <option value="2">Khusus Servis</option>
                                    <option value="3">Dijual & Untuk Sevis</option>
                                    <option value="0">Tidak Dijual</option>

                                  <?php elseif ( $status == 2 ) : ?>
                                    <option value="1">Dijual</option>
                                    <option value="3">Dijual & Untuk Sevis</option>
                                    <option value="0">Tidak Dijual</option>
                                    
                                  <?php elseif ( $status == 3 ) : ?>
                                    <option value="1">Dijual</option>
                                    <option value="2">Khusus Servis</option>
                                    <option value="0">Tidak Dijual</option>
                                    
                                  <?php else : ?>
                                    <option value="1">Dijual</option>
                                    <option value="2">Khusus Servis</option>
                                    <option value="3">Dijual & Untuk Sevis</option>
                                    
                                  <?php endif; ?>
                                </select>
                            </div>
                        </div>
                    </div>
                  </div>
                </div>
                <!-- /.card-body -->

                <div class="card-footer text-right">
                  <a href="hilangbarang" class="btn btn-default">Kembali</a>
                  <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    </section>


  </div>


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