<?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>
  body {
    background-color: #ffffff;
  }

  .container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    font-family: tahoma;
    font-size: 8pt;
    
  }

  .invoice-container {
    width: 550px;
    font-family: calibri;
    border: 1px solid black;
    padding: 10px;
    background-color: #DFDBE5;
    background-image: url("data:image/svg+xml,%3Csvg width='100' height='20' viewBox='0 0 100 20' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M21.184 20c.357-.13.72-.264 1.088-.402l1.768-.661C33.64 15.347 39.647 14 50 14c10.271 0 15.362 1.222 24.629 4.928.955.383 1.869.74 2.75 1.072h6.225c-2.51-.73-5.139-1.691-8.233-2.928C65.888 13.278 60.562 12 50 12c-10.626 0-16.855 1.397-26.66 5.063l-1.767.662c-2.475.923-4.66 1.674-6.724 2.275h6.335zm0-20C13.258 2.892 8.077 4 0 4V2c5.744 0 9.951-.574 14.85-2h6.334zM77.38 0C85.239 2.966 90.502 4 100 4V2c-6.842 0-11.386-.542-16.396-2h-6.225zM0 14c8.44 0 13.718-1.21 22.272-4.402l1.768-.661C33.64 5.347 39.647 4 50 4c10.271 0 15.362 1.222 24.629 4.928C84.112 12.722 89.438 14 100 14v-2c-10.271 0-15.362-1.222-24.629-4.928C65.888 3.278 60.562 2 50 2 39.374 2 33.145 3.397 23.34 7.063l-1.767.662C13.223 10.84 8.163 12 0 12v2z' fill='%231fd7b2' fill-opacity='0.4' fill-rule='evenodd'/%3E%3C/svg%3E");
  }

  .invoice-container table {
    width: 100%;
    font-size: 8pt;
    border-collapse: collapse;
  }

  .invoice-container table td {
    padding: 5px;
    border: 1px solid black;
    text-align: left;
  }

  .invoice-container .header td {
    color: black;
    border: none;
  }

  .invoice-container .header2 td {
    color: white;
    background-color: black;
  }

  .invoice-container .total td {
    text-align: right;
    padding-right: 5px;
    border: none;
  }

  .invoice-container .signature td {
    text-align: center;
    align-item: center;
    border: none;
  }

  .invoice-container .status td {
    text-align: center;
    border: none;
  }
</style>

</head>

<body>
  <div class="container">
    <div class="invoice-container">
      <table>
        <tr class="header">
          <td colspan="2" style="text-align: left">
            <span style="font-size: 12pt;"><b>Villa Hayati</b></span><br>
            Slepi, Trawas, Jawa Timur<br>
            Telp: 085693077004
          </td>
          <td colspan="2" style="text-align: left">
            <b><span style="font-size: 12pt;">INVOICE</span></b><br>
            No Trans.: 4<br>
            Tanggal: <?php echo $currentDate; ?><br>
          </td>
        </tr>
      </table>
      <br>
      <table>
        <tr class="header2">
          <td width="30%">Nama Pemesan</td>
          <td width="20%">Check In</td>
          <td width="20%">Check Out</td>
          <td width="30%">Total Harga</td>
        </tr>
        <?php
  foreach ($books as $book) {
    $nama = ucwords(strtolower($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>
      <table>
        <tr class="total">
          <td colspan="4" style="text-align: right">
            <div>Total Yang Harus Dibayar Adalah: Rp<?php echo number_format($totalHarga, 2, ',', '.'); ?></div>
          </td>
        </tr>
        <tr class="status">
          <td colspan="4" style="text-align: right">
            <div>Status: <?php echo ($status); ?>/Lunas</div>
          </td>
        </tr>
      </table>
      <br>
      <table style="width: 650px; font-size: 7pt;" cellspacing="2">
      <tr class="signature">
        <td align="center" colspan="4">
          TTD,</br></br><u>(Pengelola Villa Hayati)</u>
        </td>
      </tr>
     </table>
    </div>
  </div>
</body>

</html>
