<?php
// koneksi ke database
// error_reporting(0);
include 'connection.php';

// Mendapatkan data dealer untuk dropdown
$dealerQuery = "SELECT subyek, kode_lks FROM fkt_dealer where grup='LT' AND nonaktif=0 and kode_lks NOT IN ('FLEET01','GUDANG UNIT','MD') order by urutan";
$dealerResult = $conn->query($dealerQuery);

// Mendapatkan data tipe dan warna
$query = "SELECT DISTINCT nama_tipe, warna FROM `allocation_tipewarna`";
$result = $conn->query($query);

// Menyusun array tipe dengan warna terkaitnya
$tipeWarna = [];
while ($row = $result->fetch_assoc()) {
    $tipeWarna[$row['nama_tipe']][] = $row['warna'];
}

// Hitung jumlah tipe dan warna untuk pengaturan colspan
$jumlah_tipe = count($tipeWarna);
$colspan_per_tipe = 4; // Asumsi 4 kolom per warna
$colspan_total = $colspan_per_tipe * $jumlah_tipe;

?>

<!DOCTYPE html>
<html lang="en">
<?php
include 'head.php';
?>

<body>

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



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



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

    <div class="pagetitle">
      <h1>Allocation</h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="index.html">Home</a></li>
          <li class="breadcrumb-item active">Allocation</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 Stok Unit -->
            <div class="col-12">
              <div class="card recent-sales overflow-auto">

                <div class="card-body">
                  <!-- <h5 class="card-title">Allocation Report</h5> -->
                  <div class="overflow-auto  mt-3"  style="max-height: 600px;">
                    <table class="table table-hover table-bordered align-middle text-center">
                      <thead class="align-middle text-center sticky-row-head">
                          <tr id="myTable">
                              <th rowspan="2" class="fixed-width-no">No</th>
                              <th rowspan="2" class="fixed-width-name">Tanggal: <?php echo date('d/m/Y'); ?></th>
                              <!-- Loop Tipe -->
                              <?php foreach ($tipeWarna as $tipe => $warnas): ?>
                                  <th colspan="<?php echo $colspan_per_tipe * count($warnas); ?>"><?php echo $tipe; ?></th>
                                  <th colspan="4" class="table-secondary">TOTAL</th>
                              <?php endforeach; ?>
                          </tr>
                          <tr>
                              <!-- Loop Tipe dan Warna -->
                              <?php foreach ($tipeWarna as $tipe => $warnas): ?>
                                  <?php foreach ($warnas as $warna): ?>
                                      <th colspan="4"><?php echo $warna; ?></th>
                                  <?php endforeach; ?>
                                  <th colspan="4" class="table-secondary"><?php echo $tipe; ?></th>
                              <?php endforeach; ?>
                          </tr>
                      </thead>
                      <tbody>
                        <tr id="myTable">
                          <th scope="row">A</th>
                          <td>STOCK GUDANG</td>
                          <!-- Loop untuk mengisi data per tipe dan warna -->
                          <?php foreach ($tipeWarna as $tipe => $warnas): ?>
                                      <?php foreach ($warnas as $warna): ?>
                                          <td>0</td>
                                          <td>0</td>
                                          <td>0</td>
                                          <td>0</td>
                                      <?php endforeach; ?>
                                      <td class="table-secondary">0</td>
                                      <td class="table-secondary">0</td>
                                      <td class="table-secondary">0</td>
                                      <td class="table-secondary">0</td>
                                  <?php endforeach; ?>
                        </tr>
                        <tr id="myTable">
                          <th scope="row">B</th>
                          <td>IN</td>
                          <!-- Loop untuk mengisi data per tipe dan warna -->
                          <?php foreach ($tipeWarna as $tipe => $warnas): ?>
                                      <?php foreach ($warnas as $warna): ?>
                                          <td>0</td>
                                          <td>0</td>
                                          <td>0</td>
                                          <td>0</td>
                                      <?php endforeach; ?>
                                      <td class="table-secondary">0</td>
                                      <td class="table-secondary">0</td>
                                      <td class="table-secondary">0</td>
                                      <td class="table-secondary">0</td>
                                  <?php endforeach; ?>
                        </tr>
                        <tr id="myTable">
                          <th scope="row">C</th>
                          <td>STOCK IN HEAD</td>
                          <!-- Loop untuk mengisi data per tipe dan warna -->
                          <?php foreach ($tipeWarna as $tipe => $warnas): ?>
                                      <?php foreach ($warnas as $warna): ?>
                                          <td>0</td>
                                          <td>0</td>
                                          <td>0</td>
                                          <td>0</td>
                                      <?php endforeach; ?>
                                      <td class="table-secondary">0</td>
                                      <td class="table-secondary">0</td>
                                      <td class="table-secondary">0</td>
                                      <td class="table-secondary">0</td>
                                  <?php endforeach; ?>
                        </tr>
                        <tr id="myTable">
                          <th scope="row">D</th>
                          <td>DISTRIBUSI HARIAN</td>
                          <!-- Loop untuk mengisi data per tipe dan warna -->
                          <?php foreach ($tipeWarna as $tipe => $warnas): ?>
                                      <?php foreach ($warnas as $warna): ?>
                                          <td>0</td>
                                          <td>0</td>
                                          <td>0</td>
                                          <td>0</td>
                                      <?php endforeach; ?>
                                      <td class="table-secondary">0</td>
                                      <td class="table-secondary">0</td>
                                      <td class="table-secondary">0</td>
                                      <td class="table-secondary">0</td>
                                  <?php endforeach; ?>
                        </tr>
                        <tr id="myTable">
                          <th scope="row">E</th>
                          <td>SISA STOCK HARIAN</td>
                          <!-- Loop untuk mengisi data per tipe dan warna -->
                          <?php foreach ($tipeWarna as $tipe => $warnas): ?>
                                      <?php foreach ($warnas as $warna): ?>
                                          <td>0</td>
                                          <td>0</td>
                                          <td>0</td>
                                          <td>0</td>
                                      <?php endforeach; ?>
                                      <td class="table-secondary">0</td>
                                      <td class="table-secondary">0</td>
                                      <td class="table-secondary">0</td>
                                      <td class="table-secondary">0</td>
                                  <?php endforeach; ?>
                        </tr>
                        
                        <!-- Baris DEALER -->
                        <tr class="sticky-row" id="myTable">
                          <th scope="row"></th>
                          <td><strong>DEALER</strong></td>
                          <?php foreach ($tipeWarna as $tipe => $warnas): ?>
                              <?php foreach ($warnas as $warna): ?>
                                  <td><strong><span data-bs-toggle="tooltip" data-bs-placement="top" title="DO">SL</span></strong></td>
                                  <td><strong><span data-bs-toggle="tooltip" data-bs-placement="top" title="STOCK">ST</span></strong></td>
                                  <td><strong><span data-bs-toggle="tooltip" data-bs-placement="top" title="RASIO">RS</span></strong></td>
                                  <td><strong><span data-bs-toggle="tooltip" data-bs-placement="top" title="ALOKASI">AL</span></strong></td>
                              <?php endforeach; ?>
                              <td class="table-secondary"><strong><span data-bs-toggle="tooltip" data-bs-placement="top" title="TOTAL DO">SL</span></strong></td>
                              <td class="table-secondary"><strong><span data-bs-toggle="tooltip" data-bs-placement="top" title="TOTAL STOCK">ST</span></strong></td>
                              <td class="table-secondary"><strong><span data-bs-toggle="tooltip" data-bs-placement="top" title="TOTAL RASIO">RS</span></strong></td>
                              <td class="table-secondary"><strong><span data-bs-toggle="tooltip" data-bs-placement="top" title="TOTAL ALOKASI">AL</span></strong></td>
                          <?php endforeach; ?>
                        </tr>

                        <?php
                        

                        // Loop untuk menampilkan baris dealer
                        if ($dealerResult->num_rows > 0) {
                            $no = 1; // Variabel untuk nomor urut dealer
                            while ($dealerRow = $dealerResult->fetch_assoc()) {
                        ?>
                        <tr id="myTable">
                          <th scope="row"><?php echo $no++; ?></th>
                          <td class="text-start"><?php echo $dealerRow['subyek']; ?></td>
                          <!-- Loop Tipe -->
                          <?php foreach ($tipeWarna as $tipe => $warnas): ?>
                            <!-- Loop Warna -->
                            <?php foreach ($warnas as $warna): ?>
                              <td>0</td> <!-- SL -->
                              <!-- Query untuk ST -->
                              <?php
                              // Ambil nilai qty dari database
                              $dealer_id = $dealerRow['kode_lks']; // Mengambil dealer_id dari hasil query
                              $query = "SELECT qty FROM allocation_stock WHERE dealer_id = '$dealer_id' AND tipe = '$tipe' AND warna = '$warna'";
                              $result_st = $conn->query($query); // Eksekusi query

                              if ($result_st->num_rows > 0) {
                                  // Jika ada hasil, ambil nilai qty
                                  $row_st = $result_st->fetch_assoc();
                                  $qty = $row_st['qty'];
                              ?>
                                  <td><?php echo $qty; ?></td> <!-- ST -->
                              <?php
                              } else {
                                  // Jika tidak ada hasil, tampilkan 0
                              ?>
                                  <td>0</td> <!-- ST -->
                              <?php
                              }
                              ?>
                              <td>0</td> <!-- RS -->
                              <td>0</td> <!-- AL -->
                            <?php endforeach; ?>
                            <!-- Total -->
                            <td class="table-secondary">0</td> <!-- TOTAL SL -->
                            <td class="table-secondary">0</td> <!-- TOTAL ST -->
                            <td class="table-secondary">0</td> <!-- TOTAL RS -->
                            <td class="table-secondary">0</td> <!-- TOTAL AL -->
                          <?php endforeach; ?>
                        </tr>
                        <?php
                          }
                        }
                        ?>

                      </tbody>
                    </table>
                  </div>

                </div>

              </div>
            </div><!-- End Filter Stok Unit -->


          </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
      });
    });

    function loadTargetDealer(dealerCode, nameDealer, tipe, warna) {
      $.ajax({
        url: 'get_target_dealer_data.php',
        type: 'POST',
        data: {
          kode_lks: dealerCode,
          subyek: nameDealer,
          tipe: tipe,
          warna: warna
        },
        success: function(response) {
          // Tampilkan data di dalam modal
          $('#modalContent').html(response);

          // Update the edit button's href to point to target_dealer_edit.php with the right parameters
          $('#editBtn').click(function() {
            window.location.href = 'target_dealer_edit.php?kode_lks=' + dealerCode +
              '&subyek=' + nameDealer +
              '&tipe=' + tipe +
              '&warna=' + warna;
          });
        },
        error: function() {
          $('#modalContent').html('<p>Error loading data.</p>');
        }
      });
    }
  </script>

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


</body>

</html>