<?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();
}
// koneksi ke database
include 'connection.php';

// Mendapatkan data dealer untuk dropdown
$dealer_query = "SELECT name_dealer, code_dealer FROM dealer";
$dealer_result = $conn->query($dealer_query);

// Mendapatkan data bulan untuk dropdown
$bulan_query = "SELECT bulan FROM api_service_ach_vw2 GROUP BY bulan";
$bulan_result = $conn->query($bulan_query);

// Mendapatkan data tahun untuk dropdown
$tahun_query = "SELECT tahun FROM api_service_ach_vw2 GROUP BY tahun";
$tahun_result = $conn->query($tahun_query);
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">

  <title>Query Master Service</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 CSS -->
  <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
  <!-- Select2 Bootstrap Theme CSS -->
  <link href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.1.1/dist/select2-bootstrap-5-theme.min.css" rel="stylesheet" />
  <!-- jQuery -->
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <!-- Select2 JS -->
  <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>




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

  <!-- UNICOS -->
  <link rel="stylesheet" href="https://unicons.iconscout.com/release/v4.0.8/css/line.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/css/bootstrap.min.css" rel="stylesheet">
  <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">





  <style>
    thead th {
      border: 1px solid rgb(230, 230, 230) !important;
      /* Custom border for table header */
    }

    table {
      overflow-y: auto;
      overflow-x: auto !important;
      min-height: 60px;

    }

    .top {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .bottom {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
		.table-secondary {
			background-color: #F4F4F4 !important; /* Warna latar yang kamu inginkan */
			border: 1px solid #dee2e6 !important; /* Menambahkan border */
		}

		.table-bordered th, .table-bordered td {
			border: 1px solid #dee2e6 !important; /* Border untuk semua sel dalam tabel */
		}
  </style>
</head>
<body>

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


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


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

<div class="pagetitle">
  <h1>Stock</h1>
  
  <nav>
    <ol class="breadcrumb">
      <li class="breadcrumb-item"><a href="index.html">Home</a></li>
      <li class="breadcrumb-item active">Stock</li>
    </ol>
  </nav>
</div><!-- End Page Title -->

<section class="section dashboard">
  <div class="row">

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

        <!-- Filter Tujuan Dealer -->
        <div class="col-12">
          <div class="card recent-sales overflow-auto">

            <div class="card-body">
              <h5 class="card-title">Filter Tujuan Dealer</h5>

              <form method="POST" action="" class="row g-3 align-items-end">
                <div class="col-md-6">
                  <label for="dealer" class="form-label">Dealer</label>
                  <select name="dealer_code" id="dealer" class="form-control select2">
                    <option value="" disabled selected hidden>Pilih Dealer</option>
                    <?php
                    if ($dealer_result->num_rows > 0) {
                      while ($dealer = $dealer_result->fetch_assoc()) {
                    ?>
                        <option value="<?php echo "$dealer[code_dealer]" ?>"
                          <?php if (isset($_POST['dealer_code']) && $_POST['dealer_code'] == $dealer['code_dealer']) { ?>selected="" <?php } ?>>
                          <?php echo htmlspecialchars($dealer['name_dealer']) ?></option>
                    <?php
                      }
                    }
                    ?>
                  </select>
                </div>

                <div class="col-md-2">
                  <label for="bulan" class="form-label">Bulan</label>
                  <select name="bulan" id="bulan" class="form-control">
                    <option value="" disabled selected hidden>Pilih Bulan</option>
                    <?php
                    if ($bulan_result->num_rows > 0) {
                      while ($bulan = $bulan_result->fetch_assoc()) {
                    ?>
                        <option value="<?php echo "$bulan[bulan]" ?>"
                          <?php if (isset($_POST['bulan']) && $_POST['bulan'] == $bulan['bulan']) { ?>selected="" <?php } ?>>
                          <?php echo htmlspecialchars($bulan['bulan']) ?></option>
                    <?php
                      }
                    }
                    ?>
                  </select>
                </div>

                <div class="col-md-2">
                  <label for="tahun" class="form-label">Tahun</label>
                  <select name="tahun" id="tahun" class="form-control">
                    <option value="" disabled selected hidden>Pilih Tahun</option>
                    <?php
                    if ($tahun_result->num_rows > 0) {
                      while ($tahun = $tahun_result->fetch_assoc()) {
                    ?>
                        <option value="<?php echo "$tahun[tahun]" ?>"
                          <?php if (isset($_POST['tahun']) && $_POST['tahun'] == $tahun['tahun']) { ?>selected="" <?php } ?>>
                          <?php echo htmlspecialchars($tahun['tahun']) ?></option>
                    <?php
                      }
                    }
                    ?>
                  </select>
                </div>

                <div class="col-md-2 d-flex justify-content-end">
                  <button type="submit" class="btn btn-primary mt-3 w-100">Tampilkan</button>
                </div>
              </form><!-- End Custom Styled Validation with Tooltips -->

            </div>

          </div>
        </div><!-- End Filter Tujuan Dealer -->

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

            <div class="card-body">



              <?php
              // Menangani form submission
              if ($_SERVER["REQUEST_METHOD"] == "POST") {
                $site_id = $_POST['dealer_code'];
                $bulan = $_POST['bulan'];
                $tahun = $_POST['tahun'];

                // Validasi input
                if (empty($site_id) || empty($bulan) || empty($tahun)) {
                  echo "<br><div class='alert alert-danger'>Pastikan anda memilih Dealer, Bulan, dan Tahun.</div>";
                } else {

                  // Cek apakah kombinasi bulan & tahun sudah ada di target_service_dealer
                  $check_query = "SELECT * FROM target_service WHERE bulan = '$bulan' AND tahun = '$tahun'";
                  $check_result = $conn->QUERY($check_query);

                  IF ($check_result->num_rows == 0) {
                      // Jika belum ada, isi otomatis target_service dengan DATA dari api_service_ach_vw2
                      $insert_query = "INSERT INTO target_service (site_id, name_dealer, bulan, tahun)
                                      SELECT site_id, name_dealer, '$bulan', '$tahun' FROM api_service_ach_vw2
                                      WHERE bulan = '$bulan' AND tahun = '$tahun'
                                      GROUP BY site_id, bulan, tahun";
                      IF ($conn->QUERY($insert_query) === TRUE) {
                          echo "<br><div class='alert alert-success'>Data Target bulan ini telah terisi Otomatis.</div>";
                      } ELSE {
                          echo "<br><div class='alert alert-danger'>Gagal mengisi Data Target bulan ini: " . $conn->error . "</div>";
                      }
                  }



                  // Query untuk mengambil data berdasarkan site_id, bulan, dan tahun
                  $sql = "SELECT *
                      FROM api_service_ach_vw2
                      WHERE site_id = '$site_id' AND bulan = '$bulan' AND tahun = '$tahun'";

                  // echo $sql; // Debugging: menampilkan query

                  $result = $conn->query($sql);

                  if ($result->num_rows > 0) {
                    // Query untuk mengambil nama dealer secara terpisah
                    $sql_name = "SELECT *
                          FROM api_service_ach_vw2
                          WHERE site_id = '$site_id' AND bulan = '$bulan' AND tahun = '$tahun' LIMIT 1";
                    $result_name = $conn->query($sql_name);

                    if ($result_name->num_rows > 0) {
                      $row_name = $result_name->fetch_assoc();
                      $name_dealer = $row_name['name_dealer'];
                      $bulan = $row_name['bulan'];
                      $tahun = $row_name['tahun'];
                    } else {
                      $name_dealer = "Nama dealer tidak ditemukan";
                    }
              ?>
                    <!-- Display Dealer and Period -->

                    <div class="row">
                      <div class="col-md-12 text-end">

                        <!-- <div class="col-3 w-100">
                          <button type="submit" class="btn btn-outline-success mt-3 col-3"><i class="uil uil-file-export"></i> Export</button>
                        </div> -->
                      </div>
                    </div>
                    <h3 class="card-title text-center mt-3 mb-1"><strong>INVENTORY CONTROL</strong> </h3>
                    <h3 class="card-title text-center mb-3"><strong>Periode : <?php echo " $bulan/$tahun"; ?><br><br><?php echo $name_dealer; ?></strong> </h3>

                    <table class='table table-hover table-bordered align-middle text-center'>
                      <thead class="thead-light text-center">
                        <tr>
                          <th class="table-secondary" style="width: 180px;"> </th>
                          <th class="table-secondary">Item</th>
                          <th class="table-secondary">A</th>
                          <th class="table-secondary">B</th>
                          <th class="table-secondary">C</th>
                          <th class="table-secondary">D</th>
                          <th class="table-secondary">N</th>
                          <th class="table-secondary">S</th>
                          <th class="table-secondary" class="text-start">Total Stock</th>
                        </tr>
                      </thead>
                      <tbody>
                        <tr>
                          <th rowspan="3" class="table-secondary">PEMBELIAN</th>
                          <td class="text-start">YGP</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                        </tr>
                        <tr>
                          <td class="text-start">OIL</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                        </tr>
                        <tr>
                          <td class="text-start">Total</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                        </tr>
                      </tbody>
                    </table>

                    <table class='table table-hover table-bordered align-middle text-center'>
                      <thead class="thead-light text-center">
                        <tr>
                          <th class="table-secondary" style="width: 180px;"> </th>
                          <th class="table-secondary">Item</th>
                          <th class="table-secondary">A</th>
                          <th class="table-secondary">B</th>
                          <th class="table-secondary">C</th>
                          <th class="table-secondary">D</th>
                          <th class="table-secondary">N</th>
                          <th class="table-secondary">S</th>
                          <th class="table-secondary" class="text-start">Total Stock</th>
                        </tr>
                      </thead>
                      <tbody>
                        <tr>
                          <th rowspan="3" class="table-secondary text-center">PENJUALAN</th>
                          <td class="text-start">YGP</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                        </tr>
                        <tr>
                          <td class="text-start">OIL</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                        </tr>
                        <tr>
                          <td class="text-start">Total</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                        </tr>
                      </tbody>
                    </table>

                    <table class='table table-hover table-bordered align-middle text-center'>
                      <thead class="thead-light text-center">
                        <tr>
                          <th class="table-secondary" style="width: 180px;"> </th>
                          <th class="table-secondary">Item</th>
                          <th class="table-secondary">A</th>
                          <th class="table-secondary">B</th>
                          <th class="table-secondary">C</th>
                          <th class="table-secondary">D</th>
                          <th class="table-secondary">N</th>
                          <th class="table-secondary">S</th>
                          <th class="table-secondary" class="text-start">Total Stock</th>
                        </tr>
                      </thead>
                      <tbody>
                        <tr>
                          <th rowspan="3" class="table-secondary text-center">MOS</th>
                          <td class="text-start">YGP</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                        </tr>
                        <tr>
                          <td class="text-start">OIL</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                        </tr>
                        <tr>
                          <td class="text-start">Total</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                          <td>0</td>
                        </tr>
                      </tbody>
                    </table>

                <?php
                  } else {
                    echo "<div class='alert alert-danger mt-4 mb-1'>Tidak ada data yang ditemukan untuk Dealer, Bulan, dan Tahun yang dipilih.</div>";
                  }
                }
              } else { ?>
                <!-- Default Title -->
                <div class="alert alert-warning text-center mt-4 mb-1" role="alert">
                  <h3 class="card-title">Silahkan Pilih Dealer, Bulan dan Tahun</h3>
                </div>
              <?php } ?>

            </div>

          </div>
        </div><!-- End Table -->


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


  </div>
</section>

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

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

  <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>

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

  <script>
    $(document).ready(function() {
      $('#dealer').select2({
        theme: 'bootstrap-5', // Menggunakan tema Bootstrap 5
        placeholder: "Pilih Dealer",
        allowClear: true
      });
    });
  </script>

</body>
</html>
