<!DOCTYPE html>
<html :class="{ 'theme-dark': dark }" x-data="data()" lang="en">
  <head>
    <style>
  @media print {
    #formLaporanProduksi, button, .navbar, .sidebar { display: none !important; }
    body { margin: 10mm; }
  }
</style>

    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Laporan Hasil Produksi</title>
    <link
      href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap"
      rel="stylesheet"
    />
    <link rel="stylesheet" href="./assets/css/tailwind.output.css" />
    <script
      src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js"
      defer
    ></script>
    <script src="./assets/js/init-alpine.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/alpinejs@2.8.2" defer></script>
  </head>
  <body>
    <div
      class="flex h-screen bg-gray-50 dark:bg-gray-900"
      :class="{ 'overflow-hidden': isSideMenuOpen}"
    >
    <?php
      $currentPage = basename($_SERVER['PHP_SELF']);
      include 'sidebar.php';
      ?>
<?php include 'header.php'; ?>
        <main class="h-full pb-16 overflow-y-auto">
          <div class="container px-6 mx-auto grid">
            <h2
              class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200"
            >
              Laporan Produksi Panglejar
            </h2>

            <form id="formLaporanTanggal" class="mb-6" method="GET">
  <!-- Judul Filter -->
  <label class="block text-lg font-semibold text-gray-800 dark:text-gray-200 mb-4">
    Filter Periode
  </label>

  <div class="flex flex-wrap items-end gap-6 mb-4">
    <!-- Input Tanggal Mulai -->
    <div class="flex flex-col">
      <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
        Tanggal Mulai
      </label>
      <input type="date" name="tgl_awal" 
        value="<?= isset($_GET['tgl_awal']) ? $_GET['tgl_awal'] : '' ?>"
        class="px-4 py-2 text-sm text-gray-700 bg-white border border-gray-300 rounded-md 
               dark:bg-gray-800 dark:text-white dark:border-gray-600" required>
    </div>

    <!-- Input Tanggal Akhir -->
    <div class="flex flex-col">
      <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
        Tanggal Akhir
      </label>
      <input type="date" name="tgl_akhir" 
        value="<?= isset($_GET['tgl_akhir']) ? $_GET['tgl_akhir'] : '' ?>"
        class="px-4 py-2 text-sm text-gray-700 bg-white border border-gray-300 rounded-md 
               dark:bg-gray-800 dark:text-white dark:border-gray-600" required>
    </div>

    <!-- Grup Tombol -->
    <div class="flex flex-col">
      <label class="block text-sm font-medium text-transparent mb-2">.</label> <!-- spacer -->
      <div class="flex items-center space-x-2">
        <!-- Tombol Cetak -->
        <button type="submit"
          class="px-4 py-2 text-sm font-medium text-white bg-purple-600 rounded-md 
                 hover:bg-purple-700 focus:outline-none focus:ring-2 focus:ring-purple-500">
          Cetak Laporan
        </button>

        <!-- Tombol Reset -->
        <a href="laporan.php"
          class="px-4 py-2 text-sm font-medium bg-gray-100 rounded-md 
                 dark:bg-gray-700 dark:text-gray-200">
          Reset
        </a>
      </div>
    </div>
  </div>
</form>


<!-- Iframe untuk Print -->
<iframe id="printFrame" style="display:none;"></iframe>

<!-- Laporan Akan Ditampilkan Disini -->
<div id="laporanContent" style="display:none;">
<?php
if (!empty($_GET['tgl_awal']) && !empty($_GET['tgl_akhir'])) {
  $tgl_awal = $_GET['tgl_awal'];
  $tgl_akhir = $_GET['tgl_akhir'];
  include 'koneksi.php';

  function sectionHeader($title) {
    echo "<div style='page-break-inside: avoid;'>";
    echo "<h2 style='margin-top:30px; margin-bottom:10px;'>$title</h2>";
    echo "<table style='width:100%; border-collapse:collapse; margin-bottom:20px;'>";
    echo "<thead><tr>";
  }

  function sectionFooter() {
    echo "</tbody></table></div>";
  }

  // ------------------ PENERIMAAN PUCUK ------------------
  sectionHeader('Penerimaan Pucuk');
  echo "
    <th>No</th><th>Kode Penerimaan</th><th>Tanggal</th><th>Jumlah Pucuk</th>
    <th>MC Basah</th><th>Kontaminasi</th><th>Standar</th><th>Keterangan</th>
  </tr></thead><tbody>";

  $query = mysqli_query($conn, "SELECT p.*, m.tanggal_mulai 
    FROM penerimaan_pucuk p 
    JOIN produksi_master m ON p.id_produksi = m.id_produksi 
    WHERE m.tanggal_mulai BETWEEN '$tgl_awal' AND '$tgl_akhir'");
  $no = 1;
  while ($row = mysqli_fetch_assoc($query)) {
    echo "<tr>
      <td>$no</td><td>{$row['kode_penerimaan']}</td><td>{$row['tanggal_mulai']}</td>
      <td>{$row['jumlah_pucuk']} Kg</td><td>{$row['mc_basah']}%</td>
      <td>{$row['kontaminasi']}</td><td>{$row['standar_pucuk']}</td><td>{$row['keterangan']}</td>
    </tr>";
    $no++;
  }
  sectionFooter();

  // ------------------ PELAYUAN ------------------
  sectionHeader('Pelayuan');
  echo "
    <th>No</th><th>Kode Pelayuan</th><th>Tanggal</th><th>Jumlah Pucuk</th>
    <th>Berat Layu</th><th>Durasi</th><th>MC Akhir</th>
    <th>Kerataan</th><th>Standar</th><th>Keterangan</th>
  </tr></thead><tbody>";
  $query = mysqli_query($conn, "SELECT p.*, m.tanggal_mulai, pp.jumlah_pucuk 
    FROM pelayuan p 
    JOIN produksi_master m ON p.id_produksi = m.id_produksi 
    LEFT JOIN penerimaan_pucuk pp ON p.id_penerimaan = pp.id_penerimaan 
    WHERE m.tanggal_mulai BETWEEN '$tgl_awal' AND '$tgl_akhir'");
  $no = 1;
  while ($row = mysqli_fetch_assoc($query)) {
    echo "<tr>
      <td>$no</td><td>{$row['kode_pelayuan']}</td><td>{$row['tanggal_mulai']}</td>
      <td>{$row['jumlah_pucuk']} Kg</td><td>{$row['berat_layu']} Kg</td>
      <td>{$row['lama_pelayuan']} menit</td><td>{$row['mc_akhir']}%</td>
      <td>{$row['kerataan']}%</td><td>{$row['standar_pelayuan']}</td><td>{$row['keterangan']}</td>
    </tr>";
    $no++;
  }
  sectionFooter();

  // ------------------ PENGGILINGAN ------------------
  sectionHeader('Penggilingan');
  echo "
    <th>No</th><th>Kode Penggilingan</th><th>Tanggal</th><th>Berat Layu</th>
    <th>Berat Giling</th><th>Suhu Bubuk</th><th>Kelembapan</th>
    <th>Suhu Ruangan</th><th>Standar</th><th>Keterangan</th>
  </tr></thead><tbody>";
  $query = mysqli_query($conn, "SELECT p.*, m.tanggal_mulai, pp.berat_layu 
    FROM penggilingan p 
    JOIN produksi_master m ON p.id_produksi = m.id_produksi 
    LEFT JOIN pelayuan pp ON p.id_pelayuan = pp.id_pelayuan 
    WHERE m.tanggal_mulai BETWEEN '$tgl_awal' AND '$tgl_akhir'");
  $no = 1;
  while ($row = mysqli_fetch_assoc($query)) {
    echo "<tr>
      <td>$no</td><td>{$row['kode_penggilingan']}</td><td>{$row['tanggal_mulai']}</td>
      <td>{$row['berat_layu']} Kg</td><td>{$row['berat_giling']} Kg</td>
      <td>{$row['suhu_bubuk']} °C</td><td>{$row['kelembapan']}%</td>
      <td>{$row['suhu_ruangan']} °C</td><td>{$row['standar_penggilingan']}</td><td>{$row['keterangan']}</td>
    </tr>";
    $no++;
  }
  sectionFooter();

  // ------------------ OKSIDASI ------------------
  sectionHeader('Oksidasi');
  echo "
    <th>No</th><th>Kode Oksidasi</th><th>Tanggal</th><th>Berat Giling</th>
    <th>Berat Oksidasi</th><th>Kelembapan</th><th>Suhu</th><th>Durasi</th>
    <th>Standar</th><th>Keterangan</th>
  </tr></thead><tbody>";
  $query = mysqli_query($conn, "SELECT p.*, m.tanggal_mulai, pg.berat_giling 
    FROM oksidasi p 
    JOIN produksi_master m ON p.id_produksi = m.id_produksi 
    LEFT JOIN penggilingan pg ON p.id_penggilingan = pg.id_penggilingan 
    WHERE m.tanggal_mulai BETWEEN '$tgl_awal' AND '$tgl_akhir'");
  $no = 1;
  while ($row = mysqli_fetch_assoc($query)) {
    echo "<tr>
      <td>$no</td><td>{$row['kode_oksidasi']}</td><td>{$row['tanggal_mulai']}</td>
      <td>{$row['berat_giling']} Kg</td><td>{$row['berat_oksidasi']} Kg</td>
      <td>{$row['kelembapan']}%</td><td>{$row['suhu']} °C</td>
      <td>{$row['durasi']} menit</td><td>{$row['standar_oksidasi']}</td><td>{$row['keterangan']}</td>
    </tr>";
    $no++;
  }
  sectionFooter();

  // ------------------ PENGERINGAN ------------------
  sectionHeader('Pengeringan');
  echo "
    <th>No</th><th>Kode Pengeringan</th><th>Tanggal</th><th>Berat Oksidasi</th>
    <th>Berat Kering</th><th>MC Kering</th><th>Suhu Bubuk</th><th>Standar</th><th>Keterangan</th>
  </tr></thead><tbody>";
  $query = mysqli_query($conn, "SELECT p.*, m.tanggal_mulai, o.berat_oksidasi 
    FROM pengeringan p 
    JOIN produksi_master m ON p.id_produksi = m.id_produksi 
    LEFT JOIN oksidasi o ON p.id_oksidasi = o.id_oksidasi 
    WHERE m.tanggal_mulai BETWEEN '$tgl_awal' AND '$tgl_akhir'");
  $no = 1;
  while ($row = mysqli_fetch_assoc($query)) {
    echo "<tr>
      <td>$no</td><td>{$row['kode_pengeringan']}</td><td>{$row['tanggal_mulai']}</td>
      <td>{$row['berat_oksidasi']} Kg</td><td>{$row['berat_kering']} Kg</td>
      <td>{$row['mc_kering']}%</td><td>{$row['suhu_bubuk']} °C</td>
      <td>{$row['standar_pengeringan']}</td><td>{$row['keterangan']}</td>
    </tr>";
    $no++;
  }
  sectionFooter();

  // ------------------ SORTASI ------------------
  sectionHeader('Sortasi');
  echo "
    <th>No</th><th>Kode Sortasi</th><th>Tanggal</th><th>Berat Kering</th>
    <th>Berat Sortasi</th><th>Mutu</th><th>Grade</th><th>Kondisi</th><th>Keterangan</th>
  </tr></thead><tbody>";
  $query = mysqli_query($conn, "SELECT s.*, m.tanggal_mulai, pg.berat_kering, jt.kelas_mutu, jt.mutu, jt.grade 
    FROM sortasi s 
    JOIN produksi_master m ON s.id_produksi = m.id_produksi 
    LEFT JOIN pengeringan pg ON s.id_pengeringan = pg.id_pengeringan 
    LEFT JOIN jenis_teh jt ON s.id_teh = jt.id_teh 
    WHERE m.tanggal_mulai BETWEEN '$tgl_awal' AND '$tgl_akhir'");
  $no = 1;
  while ($row = mysqli_fetch_assoc($query)) {
    $berat = $row['kondisi'] === 'Rusak' ? ($row['berat_rusak'] ?? 0) : ($row['berat_sortasi'] ?? 0);
    echo "<tr>
      <td>$no</td><td>{$row['kode_sortasi']}</td><td>{$row['tanggal_mulai']}</td>
      <td>{$row['berat_kering']} Kg</td><td>{$berat} Kg</td>
      <td>{$row['mutu']}</td><td>Grade {$row['grade']}</td>
      <td>{$row['kondisi']}</td><td>{$row['keterangan']}</td>
    </tr>";
    $no++;
  }
  sectionFooter();

  // ------------------ PENGEMASAN ------------------
  sectionHeader('Pengepakan');
  echo "
    <th>No</th><th>Kode Pengepakan</th><th>Tanggal</th><th>Berat Sortasi</th>
    <th>Mutu</th><th>Grade</th><th>Jenis Kemasan</th><th>Jumlah Kemasan</th><th>Keterangan</th>
  </tr></thead><tbody>";
  $query = mysqli_query($conn, "SELECT pk.*, m.tanggal_mulai, s.berat_sortasi, jt.kelas_mutu, jt.mutu, jt.grade 
    FROM pengepakan pk 
    JOIN produksi_master m ON pk.id_produksi = m.id_produksi 
    LEFT JOIN sortasi s ON pk.id_sortasi = s.id_sortasi 
    LEFT JOIN jenis_teh jt ON s.id_teh = jt.id_teh 
    WHERE m.tanggal_mulai BETWEEN '$tgl_awal' AND '$tgl_akhir'");
  $no = 1;
  while ($row = mysqli_fetch_assoc($query)) {
    $berat_sortasi = $row['berat_sortasi'] ?? 0;
    echo "<tr>
      <td>$no</td><td>{$row['kode_pengepakan']}</td><td>{$row['tanggal_mulai']}</td>
      <td>{$berat_sortasi} Kg</td><td>{$row['mutu']}</td>
      <td>Grade {$row['grade']}</td><td>{$row['jenis_kemasan']}</td>
      <td>{$row['jumlah_kemasan']}</td><td>{$row['keterangan']}</td>
    </tr>";
    $no++;
  }
  sectionFooter();
}
?>
</div>

<script>
window.onload = function () {
  const urlParams = new URLSearchParams(window.location.search);
  const tgl_awal = urlParams.get("tgl_awal");
  const tgl_akhir = urlParams.get("tgl_akhir");

  if (tgl_awal && tgl_akhir && document.getElementById("laporanContent")) {
    const laporanHTML = document.getElementById("laporanContent").innerHTML;

    const printWindow = document.getElementById("printFrame").contentWindow;
    const doc = printWindow.document;
    doc.open();
    doc.write(`
      <html>
        <head>
          <title>Laporan Produksi ${tgl_awal} - ${tgl_akhir}</title>
          <style>
            body { font-family: Arial, sans-serif; padding: 20px; }
            img { width: 100%; margin-bottom: 20px; }
            h2 { margin-top: 30px; margin-bottom: 10px; }
            table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
            th, td { border: 1px solid #000; padding: 6px; text-align: left; }
            div { page-break-inside: avoid; }
          </style>
        </head>
        <body>
          <img src="assets/img/kop.png">
          <center><h1>LAPORAN PRODUKSI PANGLEJAR</h1>
          <h3>Periode: ${tgl_awal} s/d ${tgl_akhir}</h3></center>
          ${laporanHTML}
          <script>
            window.onload = function() {
              window.print();
            }
          <\/script>
        </body>
      </html>
    `);
    doc.close();
  }
};
</script>


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

</body>
</html>
