<?php
    include 'session.php';
    include 'style.php';
    include 'navbar.php'; 
?> 
<!DOCTYPE html>
<?php
$nama_update = '';
$jenis_update = '';
$harga_update = '';
$hpp_update = '';
$keterangan_update = '';
$status_update='';

    if(isset($_POST["edit"])){
        //echo $_POST["edit"];
        $status_update='update';
      
        $produk_id= $_POST["edit"];
        $query_sql = "SELECT * FROM produk WHERE produk_id = '$produk_id'";

        $result = mysqli_query($conn, $query_sql);
        while ($row = mysqli_fetch_array($result))
        {
            $nama_update = $row['produk_nama'];
            $jenis_update = $row['produk_jenis'];
            $harga_update = $row['produk_harga'];
            $hpp_update = $row['produk_hpp'];
            $stok_update = $row['produk_stok'];
            $keterangan_update = $row['produk_keterangan'];
        }
        //echo $gender_update;

    }

    if(isset($_POST["tujuan"])){
        $status_update = $_POST["status"];
        $produk_id = $_POST["produk_id"];

        $nama = $_POST["nama"];
        $jenis = $_POST["jenis"];
        $harga = $_POST["harga"];
        $hpp = $_POST["hpp"];
        $keterangan = $_POST["keterangan"];

        if ($status_update != 'update') { // INSERT
            if ($nama =='' || $harga==''){
                ?>
                    <div class="alert">
                        <span class="closebtn">&times;</span>  
                        <strong>Gagal!</strong> Data yang Anda masukkan belum lengkap.
                    </div> 
            <?php 
            } else {

              $query_sql = "SELECT * FROM produk WHERE produk_nama = '$nama'";

              $result = mysqli_query($conn, $query_sql);
              
              if(mysqli_num_rows($result) > 0){
  ?>
                  <div class="alert">
                      <span class="closebtn">&times;</span>  
                      <strong>Gagal!</strong> Nama Produk yang Anda pilih sudah ada, silahkan pilih Nama Produk yang lain.
                  </div> 
  <?php
              } else {
                $query_sql = "INSERT INTO produk (produk_nama, produk_jenis, produk_harga, produk_hpp, produk_keterangan, produk_aktif)
                VALUES ('$nama','$jenis','$harga','$hpp','$keterangan','Aktif')";

                if (mysqli_query($conn, $query_sql)) { 
                $last_id = $conn->insert_id;
                ?>
                    <div class="alert success">
                        <span class="closebtn">&times;</span>  
                        <strong>Success!</strong> Anda berhasil menambahkan produk baru ..
                    </div>
                <?php }
              }
            }
        } else { // UPDATE
          $query_sql_update_handle = "UPDATE produk SET 
                    produk_nama='$nama',
                    produk_jenis='$jenis',
                    produk_harga='$harga',
                    produk_hpp='$hpp',
                    produk_keterangan='$keterangan'
                    
                    WHERE produk_id = $produk_id";
                if (mysqli_query($conn, $query_sql_update_handle)) { 
                    header("Location: produklist.php?status=update");
                } 
        }
          
    }
    

?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  box-sizing: border-box;
}

body{
  font-family:'Segoe UI', Roboto, Oxygen, Ubuntu,   Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif
}

input[type=text], select, textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  resize: vertical;
}

input[type=number], select, textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  resize: vertical;
}

label {
  padding: 12px 12px 12px 0;
  display: inline-block;
}

input[type=submit] {
  background-color: #04AA6D;
  color: white;
  padding: 12px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  float: right;
}

input[type=submit]:hover {
  background-color: #45a049;
}

.container {
  border-radius: 5px;
  background-color: #f2f2f2;
  padding: 20px;
}

.col-25 {
  float: left;
  width: 25%;
  margin-top: 6px;
}

.col-75 {
  float: left;
  width: 75%;
  margin-top: 6px;
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}

/* Responsive layout - when the screen is less than 600px wide, make the two columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
  .col-25, .col-75, input[type=submit] {
    width: 100%;
    margin-top: 0;
  }
}

/* Label Warning*/
.alert {
  padding: 20px;
  background-color: #f44336;
  color: white;
  opacity: 1;
  transition: opacity 0.6s;
  margin-bottom: 15px;
}

.alert.success {background-color: #04AA6D;}
.alert.info {background-color: #2196F3;}
.alert.warning {background-color: #ff9800;}

.closebtn {
  margin-left: 15px;
  color: white;
  font-weight: bold;
  float: right;
  font-size: 22px;
  line-height: 20px;
  cursor: pointer;
  transition: 0.3s;
}

.closebtn:hover {
  color: black;
}

.btn {
  background-color: DodgerBlue;
  border: none;
  color: white;
  padding: 12px 16px;
  font-size: 16px;
  cursor: pointer;
}

/* Darker background on mouse-over */
.btn:hover {
  background-color: RoyalBlue;
}


/* FILE INPUT STYLE */
input[type=file]::file-selector-button {
  margin-right: 20px;
  border: none;
  background: #084cdf;
  padding: 10px 20px;
  border-radius: 10px;
  color: #fff;
  cursor: pointer;
  transition: background .2s ease-in-out;
}

input[type=file]::file-selector-button:hover {
  background: #0d45a5;
}
</style>
</head>


<body>
<h2>Input Master Produk</h2>


<div class="container">
  <form id="regForm" method="POST" action="master_produk.php" enctype="multipart/form-data">
    <div class="row">
      <div class="col-25">
        <label for="nama">Nama Produk</label>
      </div>
      <div class="col-75">
        <input type="text" id="nama" name="nama" value="<?php echo $nama_update; ?>" placeholder="Nama produk..">
      </div>
    </div>

    <div class="row">
      <div class="col-25">
        <label for="jenis">Jenis</label>
      </div>
      <div class="col-75">
        <select id="jenis" name="jenis" onchange="changeTextBoxPulang()">
        <?php 
          $query_sql = "SELECT * FROM jenis";
          $result = mysqli_query($conn, $query_sql);
          while ($row = mysqli_fetch_array($result))
          {
              if ($status_update=='update') {
                if ($jenis_update==$row['jenis_id']){
                  ?><option value="<?php echo $row['jenis_id']; ?>" selected><?php echo $row['jenis_nama']; ?></option><?php
                } else {
                  ?><option value="<?php echo $row['jenis_id']; ?>"><?php echo $row['jenis_nama']; ?></option><?php
                }
              } else {
            
              ?><option value="<?php echo $row['jenis_id']; ?>"><?php echo $row['jenis_nama']; ?></option><?php
              }
          }
        ?>
        </select>
      </div>
    </div>

    <div class="row">
      <div class="col-25">
        <label for="harga">Harga *</label>
      </div>
      <div class="col-75">
        <input type="number" id="harga" name="harga" value="<?php echo $harga_update; ?>" placeholder="Harga..">
      </div>
    </div>

    <div class="row">
      <div class="col-25">
        <label for="hpp">HPP *</label>
      </div>
      <div class="col-75">
        <input type="number" id="hpp" name="hpp" value="<?php echo $hpp_update; ?>" placeholder="HPP..">
      </div>
    </div>

    <div class="row">
      <div class="col-25">
        <label for="hpp">Jumlah Stok</label>
      </div>
      <div class="col-75">
        <input type="number" id="hpp" name="hpp" value="<?php echo $stok_update; ?>" disabled>
      </div>
    </div>

    <div class="row">
      <div class="col-25">
        <label for="keterangan">Keterangan</label>
      </div>
      <div class="col-75">
        <textarea id="keterangan" name="keterangan" value="<?php echo $keterangan_update; ?>" placeholder="Write something.." style="height:100px"><?php echo $keterangan_update; ?></textarea>
      </div>
    </div>
    <div class="row">
        <input name="tujuan" type="hidden" value="USER_ADD">
        <input name="produk_id" type="hidden" value="<?php echo $produk_id; ?>">
        <input name="status" type="hidden" value="<?php echo $status_update; ?>">
        <input type="submit" value="Submit">
    </div>
  </form>
</div>

</body>
</html>

