<?php
session_start();

// Cek apakah user sudah login
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
    // Jika belum login, redirect ke halaman login
    header("Location: login.php");
    exit();
}

$nama = isset($_SESSION['nama']) ? $_SESSION['nama'] : 'User'; // Jika belum login, tampilkan 'User' sebagai default
$user = isset($_SESSION['user']) ? $_SESSION['user'] : 'warehouse'; // Jika belum login, tampilkan 'User' sebagai default
$jenis = isset($_SESSION['jenis']) ? $_SESSION['jenis'] : 'Coba'; // Jika belum login, tampilkan 'User' sebagai default
$aom = isset($_SESSION['aom']) ? $_SESSION['aom'] : 'Coba'; // Jika belum login, tampilkan 'User' sebagai default


// koneksi ke database
include 'connection.php';


// Mendapatkan data dari URL
$idkeystockdays = $_GET['idkeystockdays'];

// Query untuk mengambil data dari allocation_key_stockdays berdasarkan idkeystockdays
$query = "SELECT *
          FROM allocation_key_stockdays
          WHERE idkeystockdays = '$idkeystockdays'";
$result = $conn->query($query);

// Cek apakah data ditemukan
if ($result->num_rows > 0) {
  // Mengambil data dari hasil query
  $row = $result->fetch_assoc();
  $tipe = $row['tipe'];
  $warna = $row['warna'];
  $key_sd = $row['key_sd'];
} else {
  // Jika data tidak ditemukan, set nilai default
  $tipe = 0;
  $warna = 0;
  $key_sd = 0;
}

// Cek jika form disubmit
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  // Mengambil data dari form
  $key_sd = $_POST['key_sd'];

  // Update database
  $update_query = "UPDATE allocation_key_stockdays SET
        key_sd = '$key_sd'
        WHERE idkeystockdays = '$idkeystockdays'";

  if ($conn->query($update_query) === TRUE) {
    // Redirect ke halaman target.php setelah update berhasil
    header("Location: key_sd.php");
    exit(); // pastikan script berhenti setelah redirect
  } else {
    echo "Error updating data: " . $conn->error;
  }
}



?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">
  <title>Allocation Unit</title>
  <meta content="" name="description">
  <meta content="" name="keywords">

  <!-- Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">

  <!-- Select2 Bootstrap Theme -->
  <link href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.3.0/dist/select2-bootstrap-5-theme.min.css" rel="stylesheet" />

  <!-- DataTables CSS -->
  <link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">
  
  <!-- Google Fonts -->
  <link href="https://fonts.gstatic.com" rel="preconnect">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Nunito:300,300i,400,400i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">

  <!-- Vendor CSS Files -->
  <link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
  <link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  <link href="assets/vendor/quill/quill.snow.css" rel="stylesheet">
  <link href="assets/vendor/quill/quill.bubble.css" rel="stylesheet">
  <link href="assets/vendor/remixicon/remixicon.css" rel="stylesheet">
  <link href="assets/vendor/simple-datatables/style.css" rel="stylesheet">

  <!-- Template Main CSS File -->
  <link href="assets/css/style.css" rel="stylesheet">

  <!-- Favicons -->
  <link href="assets/img/favicon.png" rel="icon">
  <link href="assets/img/apple-touch-icon.png" rel="apple-touch-icon">

  <!-- UNICONS -->
  <link rel="stylesheet" href="https://unicons.iconscout.com/release/v4.0.8/css/line.css">

  <style>
    thead th {
      border: 1px solid rgb(230, 230, 230) !important;
    }

    table {
      overflow-y: auto;
      overflow-x: auto !important;
      min-height: 40px;
    }
    .top {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .bottom {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  </style>
</head>

<body>



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


<?php
$activeMenu = 'key_sd'; // Sesuaikan dengan halaman yang sedang dibuka
include 'sidebar.php';
?>


  <main id="main" class="main">


    <section class="section dashboard">
      <!-- Left side columns -->
      <div class="col-lg-12">
          <div class="row">

            <!-- Form -->
            <div class="col-12">
              <div class="card recent-sales overflow-auto">

                <div class="card-body">
                  <h5 class="card-title">Edit Key SD</h5>
                  

                  <!-- Form untuk menambahkan data -->
                  <form method="POST" onsubmit="return confirmSubmit()">
                    <div class="row mb-3">
                      <label class="col-sm-2 col-form-label" style="font-size:14px">Tipe</label>
                      <div class="col-sm-10">
                        <div class="col-sm-10">
                          <input type="text" class="form-control" value="<?php echo $tipe; ?>" style="font-size:14px" disabled>
                        </div>
                      </div>
                    </div>

                    <div class="row mb-3">
                      <label class="col-sm-2 col-form-label" style="font-size:14px">Warna</label>
                        <div class="col-sm-10">
                          <div class="col-sm-10">
                            <input type="text" class="form-control" value="<?php echo $warna; ?>" style="font-size:14px" disabled>
                          </div>
                        </div>
                    </div>

                    <div class="row mb-3">
                      <label class="col-sm-2 col-form-label" style="font-size:14px">key_sd</label>
                      <div class="col-sm-10">
                        <input type="number" class="form-control" name="key_sd" placeholder="Masukkan key_sd" style="font-size:14px" value="<?php echo $key_sd; ?>" step="0.01" required>
                      </div>
                    </div>

                    <div class="col-12 d-flex justify-content-start">
                      <button class="btn btn-primary mt-3 me-2" type="submit" style="font-size:14px">Simpan</button>
                      <button class="btn btn-secondary mt-3" type="button" onclick="window.history.back()" style="font-size:14px">Batal</button>
                    </div>
                  </form><!-- END General Form Elements -->

                </div>

              </div>
            </div><!-- End Filter Laporan -->

          </div>
        </div><!-- End Left side columns -->

    </section>

  </main><!-- End #main -->


  <!-- ======= Footer ======= -->
  <footer id="footer" class="footer">
    <div class="copyright">
      IT Lautan Teduh Interniaga <strong><span>&copy; 2024</span></strong>
    </div>
    <div class="credits">
      Created by <a href="https://github.com/Rizky-Boolean/"><strong>Fadhil IT</strong></a><strong></strong>
    </div>
  </footer><!-- End Footer -->

  <a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>

  <!-- Vendor JS Files -->
  <script src="assets/vendor/apexcharts/apexcharts.min.js"></script>
  <script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="assets/vendor/chart.js/chart.umd.js"></script>
  <script src="assets/vendor/echarts/echarts.min.js"></script>
  <script src="assets/vendor/quill/quill.js"></script>
  <script src="assets/vendor/simple-datatables/simple-datatables.js"></script>
  <script src="assets/vendor/tinymce/tinymce.min.js"></script>
  <script src="assets/vendor/php-email-form/validate.js"></script>

<!-- jQuery (harus sebelum DataTables) -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<!-- DataTables JS -->
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>


  
  <!-- Main JS File -->
  <script src="assets/js/main.js"></script>

  <script>
    function confirmSubmit() {
      return confirm("Apakah yakin ingin menyimpan perubahan?");
    }
  </script>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>


</body>

</html>