<?php
if (isset($_GET['id_produksi']) && is_numeric($_GET['id_produksi'])) {
  $id_produksi = intval($_GET['id_produksi']);
  include 'koneksi.php';

  function sectionHeader($title) {
    echo "
    <div class='mb-8'>
      <h2 class='text-lg font-semibold text-gray-700 dark:text-gray-200 mb-4 border-b pb-1'>$title</h2>
      <div class='overflow-x-auto'>
        <table class='w-full text-sm text-left text-gray-500 dark:text-gray-400'>
          <thead class='text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400'>
            <tr class= ''text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b dark:border-gray-700 bg-gray-50 dark:text-gray-400 dark:bg-gray-800>";
  }

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

  // ------------------ PENERIMAAN PUCUK ------------------
sectionHeader('Penerimaan Pucuk');
echo "
  <th class='px-4 py-3'>No</th>
  <th class='px-4 py-3'>Kode Penerimaan</th>
  <th class='px-4 py-3'>Tanggal Produksi</th>
  <th class='px-4 py-3'>Jumlah Pucuk</th>
  <th class='px-4 py-3'>MC Basah</th>
  <th class='px-4 py-3'>Kontaminasi</th>
  <th class='px-4 py-3'>Standar</th>
  <th class='px-4 py-3'>Keterangan</th>
  <th class='px-4 py-3'>Diinput Oleh</th>
  <th class='px-4 py-3'>Tanggal Input</th>
  <th class='px-4 py-3'>Status Input</th>
</tr></thead><tbody>";

// ✅ Join ke users untuk ambil name & jabatan
$query = mysqli_query($conn, "
    SELECT p.*, m.tanggal_mulai, u.name, u.jabatan
    FROM penerimaan_pucuk p
    JOIN produksi_master m ON p.id_produksi = m.id_produksi
    LEFT JOIN users u ON p.id_user = u.id_user
    WHERE p.id_produksi = $id_produksi
");

$no = 1;
while ($row = mysqli_fetch_assoc($query)) {
  echo "<tr class='bg-white border-b dark:bg-gray-800 dark:border-gray-700'>
    <td class='px-4 py-3'>$no</td>
    <td class='px-4 py-3'>{$row['kode_penerimaan']}</td>
    <td class='px-4 py-3'>{$row['tanggal_mulai']}</td>
    <td class='px-4 py-3'>{$row['jumlah_pucuk']} Kg</td>
    <td class='px-4 py-3'>{$row['mc_basah']}%</td>
    <td class='px-4 py-3'>{$row['kontaminasi']}</td>
    <td class='px-4 py-3'>{$row['standar_pucuk']}</td>
    <td class='px-4 py-3'>{$row['keterangan']}</td>
    <td class='px-4 py-3'>".
        (!empty($row['name']) ? "{$row['name']} ({$row['jabatan']})" : "-").
    "</td>
    <td class='px-4 py-3'>{$row['tanggal_input']}</td>
    <td class='px-4 py-3'>{$row['status']}</td>
  </tr>";
  $no++;
}
sectionFooter();



// ------------------ PELAYUAN ------------------
sectionHeader('Pelayuan');
echo "
  <th class='px-4 py-3'>No</th>
  <th class='px-4 py-3'>Kode Pelayuan</th>
  <th class='px-4 py-3'>Tanggal Produksi</th>
  <th class='px-4 py-3'>Jumlah Pucuk</th>
  <th class='px-4 py-3'>Berat Layu</th>
  <th class='px-4 py-3'>Durasi</th>
  <th class='px-4 py-3'>MC Akhir</th>
  <th class='px-4 py-3'>Kerataan</th>
  <th class='px-4 py-3'>Standar</th>
  <th class='px-4 py-3'>Keterangan</th>
  <th class='px-4 py-3'>Diinput Oleh</th>
  <th class='px-4 py-3'>Tanggal Input</th>
  <th class='px-4 py-3'>Status</th>
</tr></thead><tbody>";

// ✅ Join ke users untuk ambil name & jabatan
$query = mysqli_query($conn, "
    SELECT p.*, m.tanggal_mulai, pp.jumlah_pucuk, u.name, u.jabatan
    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
    LEFT JOIN users u ON p.id_user = u.id_user
    WHERE p.id_produksi = $id_produksi
");

$no = 1;
while ($row = mysqli_fetch_assoc($query)) {
  echo "<tr class='bg-white border-b dark:bg-gray-800 dark:border-gray-700'>
    <td class='px-4 py-3'>$no</td>
    <td class='px-4 py-3'>{$row['kode_pelayuan']}</td>
    <td class='px-4 py-3'>{$row['tanggal_mulai']}</td>
    <td class='px-4 py-3'>{$row['jumlah_pucuk']} Kg</td>
    <td class='px-4 py-3'>{$row['berat_layu']} Kg</td>
    <td class='px-4 py-3'>{$row['lama_pelayuan']} menit</td>
    <td class='px-4 py-3'>{$row['mc_akhir']}%</td>
    <td class='px-4 py-3'>{$row['kerataan']}%</td>
    <td class='px-4 py-3'>{$row['standar_pelayuan']}</td>
    <td class='px-4 py-3'>{$row['keterangan']}</td>
    <td class='px-4 py-3'>".
        (!empty($row['name']) ? "{$row['name']} ({$row['jabatan']})" : "-").
    "</td>
    <td class='px-4 py-3'>{$row['tanggal_input']}</td>
    <td class='px-4 py-3'>{$row['status']}</td>
  </tr>";
  $no++;
}
sectionFooter();


// ------------------ PENGGILINGAN ------------------
sectionHeader('Penggilingan');
echo "
  <th class='px-4 py-3'>No</th>
  <th class='px-4 py-3'>Kode Penggilingan</th>
  <th class='px-4 py-3'>Tanggal Produksi</th>
  <th class='px-4 py-3'>Berat Layu</th>
  <th class='px-4 py-3'>Berat Giling</th>
  <th class='px-4 py-3'>Suhu Bubuk</th>
  <th class='px-4 py-3'>Kelembapan</th>
  <th class='px-4 py-3'>Suhu Ruangan</th>
  <th class='px-4 py-3'>Standar</th>
  <th class='px-4 py-3'>Keterangan</th>
  <th class='px-4 py-3'>Diinput Oleh</th>
    <th class='px-4 py-3'>Tanggal Input</th>
  <th class='px-4 py-3'>Status</th>
</tr></thead><tbody>";
$query = mysqli_query($conn, "
    SELECT p.*, m.tanggal_mulai, pp.berat_layu, u.name, u.jabatan
    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
    LEFT JOIN users u ON p.id_user = u.id_user
    WHERE p.id_produksi = $id_produksi
");
$no = 1;
while ($row = mysqli_fetch_assoc($query)) {
  echo "<tr class='bg-white border-b dark:bg-gray-800 dark:border-gray-700'>
    <td class='px-4 py-3'>$no</td>
    <td class='px-4 py-3'>{$row['kode_penggilingan']}</td>
    <td class='px-4 py-3'>{$row['tanggal_mulai']}</td>
    <td class='px-4 py-3'>{$row['berat_layu']} Kg</td>
    <td class='px-4 py-3'>{$row['berat_giling']} Kg</td>
    <td class='px-4 py-3'>{$row['suhu_bubuk']} °C</td>
    <td class='px-4 py-3'>{$row['kelembapan']}%</td>
    <td class='px-4 py-3'>{$row['suhu_ruangan']} °C</td>
    <td class='px-4 py-3'>{$row['standar_penggilingan']}</td>
    <td class='px-4 py-3'>{$row['keterangan']}</td>
    <td class='px-4 py-3'>".(!empty($row['name']) ? "{$row['name']} ({$row['jabatan']})" : "-")."</td>
    <td class='px-4 py-3'>{$row['tanggal_input']}</td>
    <td class='px-4 py-3'>{$row['status']}</td>
  </tr>";
  $no++;
}
sectionFooter();

// ------------------ OKSIDASI ------------------
sectionHeader('Oksidasi');
echo "
  <th class='px-4 py-3'>No</th>
  <th class='px-4 py-3'>Kode Oksidasi</th>
  <th class='px-4 py-3'>Tanggal Produksi</th>
  <th class='px-4 py-3'>Berat Giling</th>
  <th class='px-4 py-3'>Berat Oksidasi</th>
  <th class='px-4 py-3'>Kelembapan</th>
  <th class='px-4 py-3'>Suhu</th>
  <th class='px-4 py-3'>Durasi</th>
  <th class='px-4 py-3'>Standar</th>
  <th class='px-4 py-3'>Keterangan</th>
  <th class='px-4 py-3'>Diinput Oleh</th>
    <th class='px-4 py-3'>Tanggal Input</th>
  <th class='px-4 py-3'>Status</th>
</tr></thead><tbody>";
$query = mysqli_query($conn, "
    SELECT p.*, m.tanggal_mulai, pp.berat_giling, u.name, u.jabatan
    FROM oksidasi p
    JOIN produksi_master m ON p.id_produksi = m.id_produksi
    LEFT JOIN penggilingan pp ON p.id_penggilingan = pp.id_penggilingan
    LEFT JOIN users u ON p.id_user = u.id_user
    WHERE p.id_produksi = $id_produksi
");
$no = 1;
while ($row = mysqli_fetch_assoc($query)) {
  echo "<tr class='bg-white border-b dark:bg-gray-800 dark:border-gray-700'>
    <td class='px-4 py-3'>$no</td>
    <td class='px-4 py-3'>{$row['kode_oksidasi']}</td>
    <td class='px-4 py-3'>{$row['tanggal_mulai']}</td>
    <td class='px-4 py-3'>{$row['berat_giling']} Kg</td>
    <td class='px-4 py-3'>{$row['berat_oksidasi']} Kg</td>
    <td class='px-4 py-3'>{$row['kelembapan']}%</td>
    <td class='px-4 py-3'>{$row['suhu']} °C</td>
    <td class='px-4 py-3'>{$row['durasi']} menit</td>
    <td class='px-4 py-3'>{$row['standar_oksidasi']}</td>
    <td class='px-4 py-3'>{$row['keterangan']}</td>
    <td class='px-4 py-3'>".(!empty($row['name']) ? "{$row['name']} ({$row['jabatan']})" : "-")."</td>
    <td class='px-4 py-3'>{$row['tanggal_input']}</td>
    <td class='px-4 py-3'>{$row['status']}</td>
  </tr>";
  $no++;
}
sectionFooter();

// ------------------ PENGERINGAN ------------------
sectionHeader('Pengeringan');
echo "
  <th class='px-4 py-3'>No</th>
  <th class='px-4 py-3'>Kode Pengeringan</th>
  <th class='px-4 py-3'>Tanggal Produksi</th>
  <th class='px-4 py-3'>Berat Oksidasi</th>
  <th class='px-4 py-3'>Berat Kering</th>
  <th class='px-4 py-3'>MC Kering</th>
  <th class='px-4 py-3'>Suhu Bubuk</th>
  <th class='px-4 py-3'>Standar</th>
  <th class='px-4 py-3'>Keterangan</th>
  <th class='px-4 py-3'>Diinput Oleh</th>
    <th class='px-4 py-3'>Tanggal Input</th>
  <th class='px-4 py-3'>Status</th>
</tr></thead><tbody>";
$query = mysqli_query($conn, "
    SELECT p.*, m.tanggal_mulai, pp.berat_oksidasi, u.name, u.jabatan
    FROM pengeringan p
    JOIN produksi_master m ON p.id_produksi = m.id_produksi
    LEFT JOIN oksidasi pp ON p.id_oksidasi = pp.id_oksidasi
    LEFT JOIN users u ON p.id_user = u.id_user
    WHERE p.id_produksi = $id_produksi
");
$no = 1;
while ($row = mysqli_fetch_assoc($query)) {
  echo "<tr class='bg-white border-b dark:bg-gray-800 dark:border-gray-700'>
    <td class='px-4 py-3'>$no</td>
    <td class='px-4 py-3'>{$row['kode_pengeringan']}</td>
    <td class='px-4 py-3'>{$row['tanggal_mulai']}</td>
    <td class='px-4 py-3'>{$row['berat_oksidasi']} Kg</td>
    <td class='px-4 py-3'>{$row['berat_kering']} Kg</td>
    <td class='px-4 py-3'>{$row['mc_kering']}%</td>
    <td class='px-4 py-3'>{$row['suhu_bubuk']} °C</td>
    <td class='px-4 py-3'>{$row['standar_pengeringan']}</td>
    <td class='px-4 py-3'>{$row['keterangan']}</td>
    <td class='px-4 py-3'>".(!empty($row['name']) ? "{$row['name']} ({$row['jabatan']})" : "-")."</td>
    <td class='px-4 py-3'>{$row['tanggal_input']}</td>
    <td class='px-4 py-3'>{$row['status']}</td>
  </tr>";
  $no++;
}
sectionFooter();

// ------------------ SORTASI ------------------
sectionHeader('Sortasi');
echo "
  <th class='px-4 py-3'>No</th>
  <th class='px-4 py-3'>Kode Sortasi</th>
  <th class='px-4 py-3'>Tanggal Produksi</th>
  <th class='px-4 py-3'>Berat Kering</th>
  <th class='px-4 py-3'>Berat Sortasi</th>
  <th class='px-4 py-3'>Mutu</th>
  <th class='px-4 py-3'>Grade</th>
  <th class='px-4 py-3'>Kondisi</th>
  <th class='px-4 py-3'>Keterangan</th>
  <th class='px-4 py-3'>Diinput Oleh</th>
    <th class='px-4 py-3'>Tanggal Input</th>
  <th class='px-4 py-3'>Status</th>
</tr></thead><tbody>";
$query = mysqli_query($conn, "
    SELECT p.*, m.tanggal_mulai, pg.berat_kering, jt.kelas_mutu, jt.mutu, jt.grade, u.name, u.jabatan
    FROM sortasi p
    JOIN produksi_master m ON p.id_produksi = m.id_produksi
    LEFT JOIN pengeringan pg ON p.id_pengeringan = pg.id_pengeringan
    LEFT JOIN jenis_teh jt ON p.id_teh = jt.id_teh
    LEFT JOIN users u ON p.id_user = u.id_user
    WHERE p.id_produksi = $id_produksi
");
$no = 1;
while ($row = mysqli_fetch_assoc($query)) {
  $berat = $row['kondisi'] === 'Rusak' ? ($row['berat_rusak'] ?? 0) : ($row['berat_sortasi'] ?? 0);
  echo "<tr class='bg-white border-b dark:bg-gray-800 dark:border-gray-700'>
    <td class='px-4 py-3'>$no</td>
    <td class='px-4 py-3'>{$row['kode_sortasi']}</td>
    <td class='px-4 py-3'>{$row['tanggal_mulai']}</td>
    <td class='px-4 py-3'>{$row['berat_kering']} Kg</td>
    <td class='px-4 py-3'>{$berat} Kg</td>
    <td class='px-4 py-3'>{$row['mutu']}</td>
    <td class='px-4 py-3'>Grade {$row['grade']}</td>
    <td class='px-4 py-3'>{$row['kondisi']}</td>
    <td class='px-4 py-3'>{$row['keterangan']}</td>
    <td class='px-4 py-3'>".(!empty($row['name']) ? "{$row['name']} ({$row['jabatan']})" : "-")."</td>
    <td class='px-4 py-3'>{$row['tanggal_input']}</td>
    <td class='px-4 py-3'>{$row['status']}</td>
  </tr>";
  $no++;
}
sectionFooter();

// ------------------ PENGEPACKAN ------------------
sectionHeader('Pengepakan');
echo "
  <th class='px-4 py-3'>No</th>
  <th class='px-4 py-3'>Kode Pengepakan</th>
  <th class='px-4 py-3'>Tanggal Produksi</th>
  <th class='px-4 py-3'>Berat Sortasi</th>
  <th class='px-4 py-3'>Mutu</th>
  <th class='px-4 py-3'>Grade</th>
  <th class='px-4 py-3'>Jenis Kemasan</th>
  <th class='px-4 py-3'>Jumlah Kemasan</th>
  <th class='px-4 py-3'>Keterangan</th>
  <th class='px-4 py-3'>Diinput Oleh</th>
    <th class='px-4 py-3'>Tanggal Input</th>
  <th class='px-4 py-3'>Status</th>
</tr></thead><tbody>";
$query = mysqli_query($conn, "
    SELECT pk.*, pm.tanggal_mulai, st.berat_sortasi, jt.kelas_mutu, jt.mutu, jt.grade, u.name, u.jabatan
    FROM pengepakan pk
    JOIN sortasi st ON pk.id_sortasi = st.id_sortasi
    JOIN jenis_teh jt ON st.id_teh = jt.id_teh
    JOIN produksi_master pm ON pk.id_produksi = pm.id_produksi
    LEFT JOIN users u ON pk.id_user = u.id_user
    WHERE pk.id_produksi = $id_produksi
");
$no = 1;
while ($row = mysqli_fetch_assoc($query)) {
  $berat_sortasi = $row['berat_sortasi'] ?? 0;
  echo "<tr class='bg-white border-b dark:bg-gray-800 dark:border-gray-700'>
    <td class='px-4 py-3'>$no</td>
    <td class='px-4 py-3'>{$row['kode_pengepakan']}</td>
    <td class='px-4 py-3'>{$row['tanggal_mulai']}</td>
    <td class='px-4 py-3'>{$berat_sortasi} Kg</td>
    <td class='px-4 py-3'>{$row['mutu']}</td>
    <td class='px-4 py-3'>Grade {$row['grade']}</td>
    <td class='px-4 py-3'>{$row['jenis_kemasan']}</td>
    <td class='px-4 py-3'>{$row['jumlah_kemasan']}</td>
    <td class='px-4 py-3'>{$row['keterangan']}</td>
    <td class='px-4 py-3'>".(!empty($row['name']) ? "{$row['name']} ({$row['jabatan']})" : "-")."</td>
    <td class='px-4 py-3'>{$row['tanggal_input']}</td>
    <td class='px-4 py-3'>{$row['status']}</td>
  </tr>";
  $no++;
}
sectionFooter();

}
?>
