
<?php
ob_start();
include "vendor/autoload.php"
?>
<?php
session_start();
require  '../functions.php';

if (isset($_GET['id'])) {
  // Mengambil nilai 'id' dari parameter GET
  $id = $_GET['id'];

  // Mengatur nilai 'id' di $_SESSION
  $_SESSION['id'] = $id;
}

// Lanjutkan dengan kode Anda selanjutnya...
$user_id = $_SESSION['id'];
$query = "SELECT * FROM tb_payments JOIN tb_booklog ON tb_payments.user_id = tb_booklog.user_id WHERE tb_payments.user_id = '$user_id'";
$result = mysqli_query($conn, $query);

if (!$result) {
  die("Query error: " . mysqli_error($conn));
}

$books = array();

while ($row = mysqli_fetch_assoc($result)) {
  $books[] = $row;
  $totalHarga = $row['jumlah_bayar'];
  $status = $row['status_pembayaran'];
  $currentDate = date('d F Y');
}

?>
<html>
<head>
<title>Faktur Pembayaran</title>
<style>
    #tabel {
        font-size: 15px;
        border-collapse: collapse;
        margin: 0 auto; /* Tengahkan tabel */
    }
    #tabel td {
        padding: 5px;
        border: 1px solid black;
        text-align: left; /* Rata kiri teks di dalam sel */
    }
</style>
</head>
<body style='font-family: tahoma; font-size: 8pt;'>
<!-- Kode lainnya -->
<div style="display: flex; justify-content: center;">
    <table id="tabel" style='width: 550px; font-size: 8pt; font-family: calibri; border-collapse: collapse;' border='0'>
        <!-- Konten Tabel -->
    </table>
</div>
<!-- Kode lainnya -->
</body>
</html>


<center>
<table style='width:550px; font-size:8pt; font-family:calibri; border-collapse: collapse;' border='0'>
<td width='70%' align='left' style='padding-right:0px; vertical-align:top'>
<span style='font-size:12pt'><b>Villa Hayati</b></span><br>
Slepi, Trawas, Jawa Timur<br>
Telp: 081216186521
</td>
<td style='vertical-align:top' width='30%' align='left'>
<b><span style='font-size:12pt'>INVOICE</span></b><br>
No Trans.: 4<br>
Tanggal: <?php echo $currentDate; ?><br>
</td>
</table>
<br> <br>
<table cellspacing='0' style='width:550px; font-size:8pt; font-family:calibri; border-collapse: collapse;' border='1'>
  <tr align='left'>
    <td width='30%' style='background-color: black; color: white'>Nama Pemesan</td>
    <td width='20%' style='background-color: black; color: white'>Check In</td>
    <td width='20%' style='background-color: black; color: white'>Check Out</td>
    <td width='30%' style='background-color: black; color: white'>Total Harga</td>
  </tr>
  <?php
  foreach ($books as $book) {
    $nama = $book['nama'];
    $checkin = $book['tanggal_pesan'];
    $checkout = $book['tanggal_pulang'];
    $totalHarga = $book['jumlah_bayar'];

    echo "<tr>
            <td>$nama</td>
            <td>$checkin</td>
            <td>$checkout</td>
            <td >Rp$totalHarga</td>
          </tr>";
  }
  ?>
</table>

  <br> <br>
  
  <table style='width:550px; font-size:8pt; font-family:calibri; border-collapse: collapse;'>
  <tr>
    <td colspan='4' style='text-align: right; padding-right: 5px'><div style='text-align:right'>Total Yang Harus Dibayar Adalah: Rp<?php echo number_format($totalHarga, 2, ',', '.'); ?></div></td>
  </tr>
  <tr>
    <td colspan='4' style='text-align: right; padding-right: 5px'><div style='text-align:right'>Status:  <?php echo($status); ?>/Lunas </div></td>
  </tr>
  </table>

  <br>  <br>
  <table style='width:650px; font-size:7pt;' cellspacing='2'>
    <tr>
      <td align='center'>TTD,</br></br><u>(Pengelola Villa Hayati)</u></td>
    </tr>
  </table>
</center>
</body>
</html>
<?php
// Mengatur instance Mpdf
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'format' => 'A4','margin_top' => 25, 'margin_bottom' => 25, 'margin_left' => 25, 'margin_right' => 25]);

// Memasukkan output yang diambil dari output buffering ke variabel html
$html = ob_get_contents();

// Menghapus isi output buffering
ob_end_clean();

try {
    $mpdf->WriteHTML(utf8_encode($html));

    // Membuat output file
    $content = $mpdf->Output("Invoice.pdf", "D");
} catch (\Mpdf\MpdfException $e) {
    echo 'Error creating PDF: ' . $e->getMessage();
}

?>
