<?php
    include 'session.php';
    include 'style.php';
    include 'config.php'; 
?> 
<!DOCTYPE html>
<?php



    if(isset($_POST["submit"])){
        $status_update = $_POST["status"];
        //$terima_id = $_POST["terima_id"];

        $tanggal = $_POST["tanggal"];
        $produk = $_POST["produk"];
        $jumlah = $_POST["jumlah"];
        
            if ($jumlah ==''){
                ?>
                    <div class="alert">
                        <span class="closebtn">&times;</span>  
                        <strong>Gagal!</strong> Data yang Anda masukkan belum lengkap.
                    </div> 
            <?php 
            } else {

                $query_sql = "INSERT INTO terima_barang (terima_tanggal, terima_produk, terima_jumlah)
                VALUES ('$tanggal','$produk','$jumlah')";

                if (mysqli_query($conn, $query_sql)) { 
                  $last_id = $conn->insert_id;
                  
                  // update ke master produk
                  $query_sql_update_handle = "UPDATE produk SET 
                      produk_stok=produk_stok+'$jumlah'
                      WHERE produk_id = $produk";
                  if (mysqli_query($conn, $query_sql_update_handle)) { }
                  
                  // insert ke kartu stok
                  $query_sql = "INSERT INTO kartu_stok (stok_status, stok_tanggal, stok_produk, stok_keterangan, stok_jumlah)
                  VALUES ('Masuk','$tanggal','$produk','Terima barang','$jumlah')";
                  if (mysqli_query($conn, $query_sql)) { }


                ?>
                    <div class="alert success">
                        <span class="closebtn">&times;</span>  
                        <strong>Success!</strong> Anda berhasil menambahkan Terima Barang baru ..
                    </div>
                <?php }
            }         
    }
    
    include 'navbar.php';
?>
<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;
}

input[type=datetime-local], 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: 10px 10px;
  font-size: 14px;
  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 Terima Barang</h2>

<div class="container">
  <form id="regForm" method="POST" action="master_terima.php" enctype="multipart/form-data">
    <div class="row">
      <div class="col-25">
        <label for="nama">Tanggal dan Jam *</label>
      </div>
      <div class="col-75">
        <input type="datetime-local" id="tanggal" name="tanggal">
      </div>
    </div>

    <div class="row">
      <div class="col-25">
        <label for="produk">Produk</label>
      </div>
      <div class="col-75">
        <select id="produk" name="produk" onchange="changeTextBoxPulang()">
        <?php 
          $query_sql = "SELECT * FROM produk";
          $result = mysqli_query($conn, $query_sql);
          while ($row = mysqli_fetch_array($result))
          {
            
              ?><option value="<?php echo $row['produk_id']; ?>"><?php echo $row['produk_nama']; ?></option><?php
          }
        ?>
        </select>
      </div>
    </div>

    <div class="row">
      <div class="col-25">
        <label for="jumlah">Jumlah</label>
      </div>
      <div class="col-75">
        <input type="number" id="jumlah" name="jumlah" placeholder="Jumlah..">
      </div>
    </div>


    <div class="row">
        <input name="submit" type="hidden" value="USER_ADD">
        <input name="status" type="hidden" value="<?php echo $status_update; ?>">
        <button class="btn info" onclick="window.location.href='list_terima.php'" type="button">Daftar Data</button>
        <input type="submit" value="Submit">
        
    </div>
  </form>
</div>

</body>
</html>

