<?php
session_start();
include "../config.php";

// Cek akses
if (!isset($_SESSION['id_user']) || $_SESSION['role'] != 'director') {
    header("Location: ../index.php");
    exit;
}
function btnFile($file){

    if(empty($file)){
        return "-";
    }

    return htmlspecialchars($file);
}
// ==============================
// FILTER DATA
// ==============================
$where = "status='arsip'";

if (isset($_POST['filter'])) {

    $tgl_awal  = $_POST['tgl_awal'] ?? '';
    $tgl_akhir = $_POST['tgl_akhir'] ?? '';
    $keyword   = trim($_POST['keyword'] ?? '');

    // Filter tanggal
    if (!empty($tgl_awal) && !empty($tgl_akhir)) {
        $where .= " AND jv_date BETWEEN '$tgl_awal' AND '$tgl_akhir'";
    }

   // ==============================
// FILTER KEYWORD
// ==============================
if (!empty($keyword)) {

    $keyword = mysqli_real_escape_string($koneksi, $keyword);

    $fields = [
        'id_jv',
        'nomor_jv',
        'jv_date',
        'tgl_registrasi',
        'descripsi',
        'tgl_terima',
        'nomor_prt',
        'division',
        'vendor_name',
        'nomor_tpo',
        'nomor_apt',
        'nomor_ev2',
        'nomor_its',
        'nomor_prt2',
        'jenis_pembayaran',
        'deskripsi2',
        'nama_verifikador',
        'nomor_pep',
        'nomor_tpo2',
        'status2',
        'alasan2',
        'status'
    ];

    $search = [];

    foreach ($fields as $field) {
        $search[] = "$field LIKE '%$keyword%'";
    }

    $where .= " AND (" . implode(" OR ", $search) . ")";
  }}
        
// ==============================
// EXPORT EXCEL
// ==============================
if (isset($_POST['export_excel'])) {

    // Ambil ulang filter dari POST
    $tgl_awal  = $_POST['tgl_awal'] ?? '';
    $tgl_akhir = $_POST['tgl_akhir'] ?? '';
    $keyword   = trim($_POST['keyword'] ?? '');

    $where = "status='arsip'";

    // Filter tanggal
    if (!empty($tgl_awal) && !empty($tgl_akhir)) {
        $where .= " AND jv_date BETWEEN '$tgl_awal' AND '$tgl_akhir'";
    }

   // ==============================
// FILTER KEYWORD
// ==============================
if (!empty($keyword)) {

    $keyword = mysqli_real_escape_string($koneksi, $keyword);

    $fields = [
        'id_jv',
        'nomor_jv',
        'jv_date',
        'tgl_registrasi',
        'descripsi',
        'tgl_terima',
        'nomor_prt',
        'division',
        'vendor_name',
        'nomor_tpo',
        'nomor_apt',
        'nomor_ev2',
        'nomor_its',
        'nomor_prt2',
        'jenis_pembayaran',
        'deskripsi2',
        'nama_verifikador',
        'nomor_pep',
        'nomor_tpo2',
        'status2',
        'alasan2',
        'status'
    ];

    $search = [];

    foreach ($fields as $field) {
        $search[] = "$field LIKE '%$keyword%'";
    }

    $where .= " AND (" . implode(" OR ", $search) . ")";
}


    // Hitung total
    $totalA = $koneksi->query("SELECT SUM(total_adiantamento) AS t FROM jv_documents WHERE $where")->fetch_assoc()['t'] ?? 0;
    $totalR = $koneksi->query("SELECT SUM(total_relatorio) AS t FROM jv_documents WHERE $where")->fetch_assoc()['t'] ?? 0;
    $totalO = $koneksi->query("SELECT SUM(outstanding) AS t FROM jv_documents WHERE $where")->fetch_assoc()['t'] ?? 0;
    $totalOver = $koneksi->query("SELECT SUM(overdue) AS t FROM jv_documents WHERE $where")->fetch_assoc()['t'] ?? 0;

    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=JV_Arsip_" . date('Y-m-d') . ".xls");
    header("Pragma: no-cache");
    header("Expires: 0");

    $q = $koneksi->query("SELECT * FROM jv_documents WHERE $where ORDER BY jv_date DESC");

    echo "<table border='1'>
            <thead>
                <tr>
                  <th>ID</th>
<th>Dokumentu Pagamento</th>
<th>Anexo 1</th><th>Anexo2</th><th>Anexo 3</th><th>Anexo 4</th><th>Anexo 5</th>
<th>Data Simu Pedido</th>
<th>Diresaun</th>
<th>Numeru Pedido</th>
<th>Assunto</th>
<th>Numeru CPV / PR</th>
<th>Data Kria CPV</th>
<th>Montante</th>
<th>Numeru Obl / PO</th>
<th>Data Kria CPV</th>
<th>Vendor</th>
<th>Numeru Noumeru ITS / APT</th>
<th>Data Kria ITS / APT</th>
<th>Numeru PRT</th>
<th>Data Kria PRT</th>
<th>Tipo Pagamento</th>
<th>Assunto</th>
<th>Montante</th>
<th>Numeru EV</th>
<th>Data Kria EV</th>
<th>Naran Verifikador</th>
<th>Data Verikado</th>
<th>Razaun</th>
<th>Numeru PEP</th>
<th>Data Kria PEP</th>
<th>Numeru TPO</th>
<th>Data Produs TPO</th>
<th>Statuto</th>
<th>Razaun</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>";

   while ($row = $q->fetch_assoc()) {

    echo "<tr>
        <td>" . htmlspecialchars($row['id_jv']) . "</td>
        <td>" . htmlspecialchars($row['file_nota']) . "</td>
        <td>" . htmlspecialchars($row['lampiran1']) . "</td>
        <td>" . htmlspecialchars($row['lampiran2']) . "</td>
        <td>" . htmlspecialchars($row['lampiran3']) . "</td>
        <td>" . htmlspecialchars($row['lampiran4']) . "</td>
        <td>" . htmlspecialchars($row['lampiran5']) . "</td>

        <td>" . htmlspecialchars($row['tgl_terima']) . "</td>
        <td>" . htmlspecialchars($row['division']) . "</td>
        <td>" . htmlspecialchars($row['nomor_prt']) . "</td>
        <td>" . htmlspecialchars($row['descripsi']) . "</td>
        <td>" . htmlspecialchars($row['nomor_tpo']) . "</td>
        <td>" . htmlspecialchars($row['tgl_registrasi']) . "</td>

        <td>$ " . number_format((float)$row['total_relatorio'], 2, ',', '.') . "</td>

        <td>" . htmlspecialchars($row['nomor_apt']) . "</td>
        <td>" . htmlspecialchars($row['jv_date']) . "</td>
        <td>" . htmlspecialchars($row['vendor_name']) . "</td>
        <td>" . htmlspecialchars($row['nomor_its']) . "</td>
        <td>" . htmlspecialchars($row['tanggal_kria_its']) . "</td>
        <td>" . htmlspecialchars($row['nomor_prt2']) . "</td>
        <td>" . htmlspecialchars($row['tanggal_kria_prt']) . "</td>
        <td>" . htmlspecialchars($row['jenis_pembayaran']) . "</td>
        <td>" . htmlspecialchars($row['deskripsi2']) . "</td>

        <td>$ " . number_format((float)$row['total_relatorio2'], 2, ',', '.') . "</td>

        <td>" . htmlspecialchars($row['nomor_ev2']) . "</td>
        <td>" . htmlspecialchars($row['tanggal_kria_ev']) . "</td>
        <td>" . htmlspecialchars($row['nama_verifikador']) . "</td>
        <td>" . htmlspecialchars($row['tanggal_verifikasi']) . "</td>
        <td>" . htmlspecialchars($row['alasan_verifikasi']) . "</td>
        <td>" . htmlspecialchars($row['nomor_pep']) . "</td>
        <td>" . htmlspecialchars($row['tanggal_kria_pep']) . "</td>
        <td>" . htmlspecialchars($row['nomor_tpo2']) . "</td>
        <td>" . htmlspecialchars($row['tanggal_proses_tpo']) . "</td>
        <td>" . htmlspecialchars($row['status2']) . "</td>
        <td>" . htmlspecialchars($row['alasan2']) . "</td>

        <td>Arkivu</td>
    </tr>";
}

    echo "</tbody>
          <tfoot>
            <tr><td colspan='14' align='right'><b>TOTAL ADIANTAMENTO</b></td><td>$totalA</td><td colspan='3'></td></tr>
            <tr><td colspan='14' align='right'><b>TOTAL RELATORIO</b></td><td>$totalR</td><td colspan='3'></td></tr>
            <tr><td colspan='14' align='right'><b>TOTAL OUTSTANDING</b></td><td>$totalO</td><td colspan='3'></td></tr>
            <tr><td colspan='14' align='right'><b>TOTAL OVERDUE</b></td><td>$totalOver</td><td colspan='3'></td></tr>
          </tfoot>
        </table>";

    exit;
}

// ==============================
// QUERY DATA UNTUK TAMPILAN
// ==============================
$result = $koneksi->query("SELECT * FROM jv_documents WHERE $where ORDER BY jv_date DESC");

// Total
$total_adiantamento = $koneksi->query("SELECT SUM(total_adiantamento) AS t FROM jv_documents WHERE $where")->fetch_assoc()['t'] ?? 0;
$total_relatorio    = $koneksi->query("SELECT SUM(total_relatorio) AS t FROM jv_documents WHERE $where")->fetch_assoc()['t'] ?? 0;
$total_outstanding  = $koneksi->query("SELECT SUM(outstanding) AS t FROM jv_documents WHERE $where")->fetch_assoc()['t'] ?? 0;
$total_overdue      = $koneksi->query("SELECT SUM(overdue) AS t FROM jv_documents WHERE $where")->fetch_assoc()['t'] ?? 0;

include "../header.php";
?>


<style>
/* === Header tabel satu baris & rapi === */
#table1 th {
  white-space: nowrap;      /* agar teks header tidak turun ke baris berikutnya */
  text-align: center;
  vertical-align: middle;
  font-size: 13px;
}

/* === Isi tabel rapi kiri-kanan === */
#table1 td {
  vertical-align: middle;
  font-size: 13px;
  white-space: normal;
  word-wrap: break-word;
}

/* Kolom teks panjang seperti deskripsi, aktivitas, vendor, divisi → rata kiri justify */
#table1 td:nth-child(5),
#table1 td:nth-child(8),
#table1 td:nth-child(10),
#table1 td:nth-child(11) {
  text-align: justify;
}

/* Kolom angka & tanggal → rata tengah */
#table1 td:nth-child(1),
#table1 td:nth-child(2),
#table1 td:nth-child(3),
#table1 td:nth-child(4),
#table1 td:nth-child(6),
#table1 td:nth-child(7),
#table1 td:nth-child(9),
#table1 td:nth-child(12),
#table1 td:nth-child(13),
#table1 td:nth-child(14),
#table1 td:nth-child(15),
#table1 td:nth-child(16),
#table1 td:nth-child(17),
#table1 td:nth-child(18),
#table1 td:nth-child(19) {
  text-align: center;
}

/* === Scroll horizontal jika tabel lebar === */
.table-responsive {
  overflow-x: auto;
}

/* === Header tetap menempel di atas saat di-scroll (sticky header) === */
#table1 thead th {
  position: sticky;
  top: 0;
  background-color: #343a40; /* warna sama dengan bg-dark */
  color: white;
  z-index: 2;
  border-bottom: 2px solid #222;
}

/* === Baris bergantian warna lembut === */
#table1 tbody tr:nth-child(even) {
  background-color: #f8f9fa;
}

/* === Efek hover baris === */
#table1 tbody tr:hover {
  background-color: #e9f7ef;
}
/* --- Agar nama kolom (header) tetap satu baris --- */
.table th {
  white-space: nowrap;   /* mencegah teks kolom turun ke baris baru */
  text-align: center;    /* biar tetap rapi di tengah */
  vertical-align: middle;
}



</style>

<div class="content-wrapper">
  <section class="content-header">
    <div class="container-fluid d-flex align-items-center justify-content-between">
      <h1><i class=""></i> Relatoriu Rejistu JV</h1>
    </div>
  </section>

  <section class="content">
    <div class="container-fluid">

    <!-- FILTER DAN PENCARIAN -->
<div class="card shadow-sm no-print mb-3 border-left-primary">
  <div class="card-body">
    <form method="POST" class="flex-wrap">
      <!-- BARIS 1: Filter tanggal & pencarian -->
      <div class="form-inline flex-wrap mb-2">
        <label class="mr-2"><strong>Data TPO Husi:</strong></label>
        <input type="date" name="tgl_awal" class="form-control mr-2">

        <label class="mr-2"><strong>To'o:</strong></label>
        <input type="date" name="tgl_akhir" class="form-control mr-3">

        <label class="mr-2"><strong>Buka Dokumentu:</strong></label>
        <input type="text" name="keyword" class="form-control mr-2" placeholder="Numeru JV, Deskrisaun, Vendor...">
      </div>

      <!-- BARIS 2: Tombol Filtru & Imprime -->
      <div class="form-group mb-2">
        <button type="submit" name="filter" class="btn btn-primary mr-2">
          <i class="fas fa-filter"></i> Filtru
        </button>

        <button type="button" class="btn btn-success mr-2" onclick="window.print()">
          <i class="fas fa-print"></i> Imprime
        </button>
      </div>
      
      <button type="submit" name="export_excel" class="btn btn-success mr-2">
        <i class="fas fa-file-excel"></i> Export Excel
    </button>

      <!-- BARIS 3: Tombol Reset -->
      <div class="form-group">
        <button type="button" class="btn btn-secondary mt-2" onclick="window.location.href='<?= $_SERVER['PHP_SELF']; ?>'">
          <i class="fas fa-sync-alt"></i> Reset
        </button>
      </div>
    </form>
  </div>
</div>


               <!-- =========================
<!--     FITUR PENCARIAN-->
<!--========================== -->
<!--<div class="card shadow-sm border-left-primary mb-4">-->
<!--  <div class="card-body">-->
<!--    <div class="form-inline flex-wrap">-->
<!--      <label class="mr-2"><strong>🔍 Buka Dokumentu sira:</strong></label>-->
<!--      <input type="text" id="search_manual" class="form-control mr-2" placeholder="Numeru JV, Deskrisaun...">-->
<!--      <button type="button" id="resetBtn" class="btn btn-secondary">-->
<!--        <i class="fas fa-sync-alt"></i> Reset-->
<!--      </button>-->
<!--    </div>-->
<!--  </div>-->
<!--</div>-->

      <!-- DATA TABEL -->
      <div class="card shadow border-left-success">
        <div class="card-body">

        <!-- ================= HEADER CETAK PROFESIONAL ================= -->
<div class="print-header text-center mb-3" style="display:none;">
  <h4 style="margin:0;">REPUBLICA DEMOCRATICA DE TIMOR-LESTE</h4>
  <h5 style="margin:0;">INSTITUTO PARA A DEFESA DOS DIREITOS DA CRIANÇA</h5>
  <h6 style="margin:0;">Direcao Nacional do Planeamento, Financas e Orcamento</h6>
  <h6 style="margin:0;">Relatoriu Registu Financas - <?= date('Y') ?></h6>
  <hr style="border-top:2px solid #000; width:80%; margin:auto; margin-top:5px; margin-bottom:15px;">
</div>
  <style>
.table thead th {
  background: linear-gradient(45deg, #ffc107, #ffda6a); /* kuning */
  color: #000 !important; /* teks hitam */
  text-align: center;
  font-weight: bold;
  white-space: nowrap;
}
.table thead th:hover {
  opacity: 0.9;
}
</style>
<!-- ================= TABEL LAPORAN JV ================= -->
<div class="table-responsive">
  <table class="table table-bordered table-striped" id="table1">
    <thead class="bg-dark text-center text-white">
      <tr>
           <th>ID</th>
<th>Dokumentu Pagamento</th>
<th>Anexo 1</th><th>Anexo2</th><th>Anexo 3</th><th>Anexo 4</th><th>Anexo 5</th>
<th>Data Simu Pedido</th>
<th>Diresaun</th>
<th>Numeru Pedido</th>
<th>Assunto</th>
<th>Numeru CPV / PR</th>
<th>Data Kria CPV</th>
<th>Montante</th>
<th>Numeru Obl / PO</th>
<th>Data Kria CPV</th>
<th>Vendor</th>
<th>Numeru Noumeru ITS / APT</th>
<th>Data Kria ITS / APT</th>
<th>Numeru PRT</th>
<th>Data Kria PRT</th>
<th>Tipo Pagamento</th>
<th>Assunto</th>
<th>Montante</th>
<th>Numeru EV</th>
<th>Data Kria EV</th>
<th>Naran Verifikador</th>
<th>Data Verikado</th>
<th>Razaun</th>
<th>Numeru PEP</th>
<th>Data Kria PEP</th>
<th>Numeru TPO</th>
<th>Data Produs TPO</th>
<th>Statuto</th>
<th>Razaun</th>
        <th>Status</th>
      </tr>
    </thead>
    <tbody>
      <?php while($row = $result->fetch_assoc()): ?>
      <tr>
          <td><?= $row['id_jv'] ?></td>
<td><?= btnFile($row['file_nota']) ?></td>
<td><?= btnFile($row['lampiran1']) ?></td>
<td><?= btnFile($row['lampiran2']) ?></td>
<td><?= btnFile($row['lampiran3']) ?></td>
<td><?= btnFile($row['lampiran4']) ?></td>
<td><?= btnFile($row['lampiran5']) ?></td>

<td><?= htmlspecialchars($row['tgl_terima']) ?></td>
<td><?= htmlspecialchars($row['division']) ?></td>
<td><?= htmlspecialchars($row['nomor_prt']) ?></td>
<td class="text-wrap"><?= htmlspecialchars($row['descripsi']) ?></td>
<td><?= htmlspecialchars($row['nomor_tpo']) ?></td>
<td><?= htmlspecialchars($row['tgl_registrasi']) ?></td>
<td>$ <?= number_format((float)$row['total_relatorio'], 2, ',', '.') ?></td>
<td><?= htmlspecialchars($row['nomor_apt']) ?></td>
<td><?= htmlspecialchars($row['jv_date']) ?></td>
<td><strong><?= htmlspecialchars($row['vendor_name'] ?? '-') ?></strong></td>
<td><?= htmlspecialchars($row['nomor_its']) ?></td>
<td><?= htmlspecialchars($row['tanggal_kria_its']) ?></td>
<td><?= htmlspecialchars($row['nomor_prt2']) ?></td>
<td><?= htmlspecialchars($row['tanggal_kria_prt']) ?></td>
<td><?= htmlspecialchars($row['jenis_pembayaran']) ?></td>
<td class="text-wrap"><?= htmlspecialchars($row['deskripsi2']) ?></td>
<td>$ <?= number_format((float)$row['total_relatorio2'], 2, ',', '.') ?></td>
<td><?= htmlspecialchars($row['nomor_ev2']) ?></td>
<td><?= htmlspecialchars($row['tanggal_kria_ev']) ?></td>
<td><?= htmlspecialchars($row['nama_verifikador']) ?></td>
<td><?= htmlspecialchars($row['tanggal_verifikasi']) ?></td>
<td><?= htmlspecialchars($row['alasan_verifikasi']) ?></td>
<td><?= htmlspecialchars($row['nomor_pep']) ?></td>
<td><?= htmlspecialchars($row['tanggal_kria_pep']) ?></td>
<td><?= htmlspecialchars($row['nomor_tpo2']) ?></td>
<td><?= htmlspecialchars($row['tanggal_proses_tpo']) ?></td>
<td><?= htmlspecialchars($row['status2']) ?></td>
<td><?= htmlspecialchars($row['alasan2']) ?></td>
                <td style="color:green; font-weight:bold;">Arkivu</td>
      </tr>
      <?php endwhile; ?>
    </tbody>
   <tfoot>
  <tr class="bg-light font-weight-bold">
    <td colspan="14" class="text-right">TOTAL ADIANTAMENTO:</td>
    <td class="text-left">$ <?= number_format((float)$total_adiantamento, 2, ',', '.') ?></td>
    <td></td><td></td>
  </tr>

  <tr class="bg-light font-weight-bold">
    <td colspan="14" class="text-right">TOTAL RELATORIO:</td>
    <td></td>
    <td class="text-left">$ <?= number_format((float)$total_relatorio, 2, ',', '.') ?></td>
    <td></td>
  </tr>

  <tr class="bg-light font-weight-bold">
    <td colspan="14" class="text-right">TOTAL OUTSTANDING:</td>
    <td></td><td></td>
    <td class="text-left">$ <?= number_format((float)$total_outstanding, 2, ',', '.') ?></td>
  </tr>
  
<tr class="bg-light font-weight-bold">
    <td colspan="17" class="text-right">TOTAL OVERDUE:</td>
    <td class="text-left"><?= number_format((float)$total_overdue, 2, ',', '.') ?></td>
    <td></td>
</tr>


</tfoot>


  </table>
</div>

        </div>
      </div>

    </div>
  </section>
</div>

<style>
/* ==== CETAK ==== */
/* ==== CETAK ==== */
@media print {
  body {
    margin: 0;
    padding: 0;
    font-size: 12px;
  }
  body * {
    visibility: hidden;
  }

  /* Elemen yang dicetak */
  .print-header, .print-header *,
  #table1, #table1 *,
  #table1 thead, #table1 thead * {
    visibility: visible;
  }

  .print-header {
    display: block !important;
    text-align: center;
    margin-bottom: 15px;
  }

  /* Agar header tabel muncul di tiap halaman */
  #table1 thead {
    display: table-header-group !important;
  }

  #table1 tfoot {
    display: table-footer-group !important;
  }

  /* ==== Gaya tabel cetak ==== */
  #table1 {
    width: 100% !important;
    table-layout: fixed;
    word-wrap: break-word;
    border-collapse: collapse !important;
    border: 2px solid #000 !important; /* bingkai luar tabel */
  }

  #table1 th,
  #table1 td {
    font-size: 10px;
    padding: 4px;
    border: 1px solid #000 !important; /* semua garis tabel */
    color: #000 !important;
    background: #fff !important;
  }

  /* === Header tabel (thead) === */
  #table1 thead th {
    background-color: #ddd !important;
    color: #000 !important;
    border: 1.5px solid #000 !important;
    font-weight: bold;
    text-align: center;
    vertical-align: middle;
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  /* === Footer tabel === */
  #table1 tfoot td {
    background-color: #f2f2f2 !important;
    font-weight: bold;
    border: 1px solid #000 !important;
    -webkit-print-color-adjust: exact !important;
  }

  /* Hilangkan elemen yang tidak perlu dicetak */
  .no-print,
  .dataTables_length,
  .dataTables_filter,
  .dataTables_info,
  .dataTables_paginate {
    display: none !important;
  }

  @page {
    size: A4 landscape;
    margin: 15mm;
  }

@media print {

  /* Pastikan hanya satu footer yang tampil */
  #table1 tfoot {
    display: table-footer-group !important;
  }

  /* Jangan izinkan DataTables membuat footer tambahan */
  table.dataTable tfoot {
    display: none !important;
  

}






</style>

<script>
$(document).ready(function() {
  $('#table1').DataTable({
    "order": [[2, "desc"]],
    "scrollX": true,
    "language": {
      "search": "Cari:",
      "lengthMenu": "Tampilkan _MENU_ data",
      "info": "Menampilkan _START_ - _END_ dari _TOTAL_ data"
    }
  });
});
</script>
<script>
// 🔍 PENCARIAN MANUAL TANPA RELOAD
document.getElementById("search_manual")?.addEventListener("keyup", function() {
  const keyword = this.value.toLowerCase();
  const rows = document.querySelectorAll("#table1 tbody tr");

  rows.forEach(row => {
    const text = row.innerText.toLowerCase();
    row.style.display = text.includes(keyword) ? "" : "none";
  });
});

// 🔄 RESET PENCARIAN
document.getElementById("resetBtn")?.addEventListener("click", function() {
  const input = document.getElementById("search_manual");
  if (input) input.value = "";

  // Tampilkan semua data kembali
  document.querySelectorAll("#table1 tbody tr").forEach(row => {
    row.style.display = "";
  });
});



<script>

  // ==== HAPUS DUPLIKASI TFOOT SAAT PRINT ====
  const originalTable = document.getElementById("table1");

  window.onbeforeprint = function() {
    // Hapus tabel kloning yang dibuat DataTables
    document.querySelectorAll("table.dataTable").forEach(tbl => {
      if (tbl !== originalTable) tbl.remove();
    });

    // Nonaktifkan datatables (hilangkan paging & clone)
    $('#table1').DataTable().destroy();
  };

  window.onafterprint = function() {
    // Aktifkan kembali datatables setelah print
    $('#table1').DataTable({
      "order": [[2, "desc"]],
      "scrollX": true,
      "language": {
        "search": "Cari:",
        "lengthMenu": "Tampilkan _MENU_ data",
        "info": "Menampilkan _START_ - _END_ dari _TOTAL_ data"
      }
    });
  };
  
  
  <script>
var dt;

$(document).ready(function () {

    dt = $('#table1').DataTable({
        scrollX: true,
        order: [[2, "desc"]],
        language: {
            search: "Cari:",
            lengthMenu: "Tampilkan _MENU_ data",
            info: "Menampilkan _START_ - _END_ dari _TOTAL_ data"
        }
    });

    // Pencarian manual
    document.getElementById("search_manual")?.addEventListener("keyup", function () {

        const keyword = this.value.toLowerCase();

        document.querySelectorAll("#table1 tbody tr").forEach(row => {

            const text = row.innerText.toLowerCase();

            row.style.display = text.includes(keyword) ? "" : "none";
        });
    });

    // Reset pencarian
    document.getElementById("resetBtn")?.addEventListener("click", function () {

        const input = document.getElementById("search_manual");

        if (input) input.value = "";

        document.querySelectorAll("#table1 tbody tr").forEach(row => {
            row.style.display = "";
        });
    });

});

// Sebelum print
window.onbeforeprint = function () {

    if ($.fn.DataTable.isDataTable('#table1')) {
        dt.destroy();
    }
};

// Sesudah print
window.onafterprint = function () {

    dt = $('#table1').DataTable({
        scrollX: true,
        order: [[2, "desc"]],
        language: {
            search: "Cari:",
            lengthMenu: "Tampilkan _MENU_ data",
            info: "Menampilkan _START_ - _END_ dari _TOTAL_ data"
        }
    });
};
</script>

</script>



</script>

<?php include "../footer.php"; ?>
