<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// --- Variables from URL ---
$ownerName = $_GET['ownerName'] ?? '................................';
$village = $_GET['village'] ?? '....................';
$district = $_GET['district'] ?? '....................';
$titleNo = $_GET['titleNo'] ?? '....................';
$titleDate = $_GET['titleDate'] ?? '....................';
$mapNo = $_GET['mapNo'] ?? '....................';
$plotNo = $_GET['plotNo'] ?? '....................';
$landType = $_GET['landType'] ?? '....................';
$photo = $_GET['photo_URL'] ?? ''; // ✅ Fixed: lowercase “photo” matches the URL parameter
$currentUse = $_GET['currentUse'] ?? '....................';
$convertPurpose = $_GET['convertPurpose'] ?? '....................';
$totalArea = $_GET['totalArea'] ?? '....................';
$convertArea = $_GET['convertArea'] ?? '....................';
$remainArea = $_GET['remainArea'] ?? '....................';
?>
<!DOCTYPE html>
<html lang="lo">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ໃບຢັ້ງຢືນຫັນປ່ຽນທີ່ດິນກະສິກໍາ</title>

<style>
@font-face {
  font-family: 'Phetsarath OT';
  src: url("data:font/ttf;base64,AF6ejAxABYAAP//AAMAAAAEAAYAAP//AAwAAQAIAAoADAAOABAAEgAUABYAGAAaA...AEBowACAAEBvAG/AAAAAQABAEEAAQAGAAAAAQABAFIAAQAGAAAAAQABAFIAAA==") format("truetype");
}

body {
  font-family: 'Phetsarath OT', 'Noto Sans Lao', sans-serif;
  font-size: 16px;
  line-height: 1.8;
  margin: 15mm 20mm 20mm 30mm;
  color: #000;
}

@page { size: A4 portrait; margin: 15mm 20mm 20mm 30mm; }

.header { text-align: center; margin-bottom: 5px; line-height: 1.4; }
.header img { width: 80px; margin-bottom: 0; }

.dept-info { margin-top: 5px; line-height: 1.4; text-align: left; }
.dept-line { display: flex; justify-content: space-between; margin-top: 2px; }

.report-title { text-align: center; font-weight: bold; font-size: 18px; margin: 5px 0; }

.hanging-indent { text-indent: -15px; margin-left: 60px; line-height: 1.4; text-align: justify; margin: 3px 0; }

.justified { text-indent: 40px; text-align: justify; line-height: 1.6; margin: 5px 0; }
.hanging { text-indent: -15px; margin-left: 60px; text-align: justify; margin: 3px 0; }

.signature { text-align: right; margin-top: 10mm; margin-right: 10mm; font-weight: bold; font-size: 14px; }

button {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background-color: #0867CA;
  color: white;
  border: none;
  border-radius: 5px;
  padding: 10px 20px;
  cursor: pointer;
  font-family: 'Phetsarath OT', 'Noto Sans Lao', sans-serif;
  font-size: 16px;
}
button:hover { background-color: #0056b3; }
@media print { button { display: none; } }
</style>
</head>

<body>

<div class="header">
  <img src="https://img1.pic.in.th/images/Laos-Emblem.jpeg" alt="Lao National Emblem">
  <div><strong>ສາທາລະນະລັດ ປະຊາທິປະໄຕ ປະຊາຊົນລາວ</strong></div>
  <div><strong>ສັນຕິພາບ ເອກະລາດ ປະຊາທິປະໄຕ ເອກະພາບ ວັດທະນະຖາວອນ</strong></div>

  <div class="dept-info">
    <div>ພະແນກກະສິກໍາ ແລະ ສິ່ງແວດລ້ອມ ນະຄອນຫຼວງວຽງຈັນ</div>
    <div class="dept-line">
      <div>ຂະແໜງຄຸ້ມຄອງ ແລະ ພັດທະນາທີ່ດິນ</div>
      <div>ເລກທີ: ................../ຂຄພດ</div>
    </div>
    <div style="text-align: right;">ວັນທີ: ...........................</div>
  </div>
</div>

<div class="report-title">ໃບຢັ້ງຢືນຫັນປ່ຽນທີ່ດິນກະສິກໍາ</div>

<p class="justified"><strong>ຮຽນ:</strong> ທ່ານຫົວໜ້າພະແນກກະສິກໍາ ແລະ ສິ່ງແວດລ້ອມ ນະຄອນຫຼວງວຽງຈັນ</p>
<p class="justified"><strong>ເລື່ອງ:</strong> ຂໍລາຍງານຜົນການຄົ້ນຄວ້າເອກະສານຂໍອະນຸຍາດຫັນປ່ຽນທີ່ດິນກະສິກໍາ</p>

<p class="hanging-indent">– ອີງຕາມ ມາດຕາ 25, 27 ແລະ 30 ຂອງກົດໝາຍວ່າດ້ວຍທີ່ດິນ...</p>

<p class="justified">ຂໍຖືເປັນກຽດຮຽນລາຍງານມາຍັງທ່ານຊາບກ່ຽວັບຜົນການຄົ້ນຄວ້າເອກະສານ...</p>

<!-- Owner & Land Info -->
<p class="hanging">– ໃບຕາດິນອອກຊື່: <?= htmlspecialchars($ownerName) ?></p>
<p class="hanging">– ບ້ານ: <?= htmlspecialchars($village) ?>, ເມືອງ: <?= htmlspecialchars($district) ?>, ນະຄອນຫຼວງວຽງຈັນ</p>
<p class="hanging">– ໃບຕາດິນເລກທີ: <?= htmlspecialchars($titleNo) ?>, ລົງວັນທີ: <?= htmlspecialchars($titleDate) ?></p>
<p class="hanging">– ແຜນທີ່ເລກທີ: <?= htmlspecialchars($mapNo) ?>, ຕອນດິນເລກທີ: <?= htmlspecialchars($plotNo) ?></p>
<p class="hanging">– ປະເພດທີ່ດິນ: <?= htmlspecialchars($landType) ?>, ການນໍາໃຊ້: <?= htmlspecialchars($currentUse) ?></p>
<p class="hanging">– ເນື້ອທີ່ທັງໝົດ: <?= $totalArea ? number_format((float)$totalArea, 0, '.', ',') . ' ຕາແມັດ' : '....................' ?></p>
<p class="hanging">– ເນື້ອທີ່ຫັນປ່ຽນໄດ້: <?= $convertArea ? number_format((float)$convertArea, 0, '.', ',') . ' ຕາແມັດ' : '....................' ?></p>
<p class="hanging">– ເນື້ອທີ່ຮັກສາໄວ້ເປັນກະສິກໍາ: <?= $remainArea ? number_format((float)$remainArea, 0, '.', ',') . ' ຕາແມັດ' : '....................' ?></p>
<p class="hanging">– ເປົ້າໝາຍຫັນປ່ຽນ: <?= htmlspecialchars($convertPurpose) ?></p>

<div style="text-align:center; margin-top:10px;">
  <p><strong>ແຜນທີ່ຕອນດິນ</strong></p>
 
  <?php if (!empty($photo)): ?>
    <img src="<?= htmlspecialchars($photo) ?>" alt="Land Photo" style="max-width:100%; border:1px solid #ccc; border-radius:6px;">
  <?php else: ?>
    <p style="color:gray;">(ບໍ່ມີຮູບພາບ)</p>
  <?php endif; ?>
</div>

<p class="justified">ດັ່ງນັ້ນ, ຈຶ່ງຮຽນລາຍງານມາຍັງທ່ານເພື່ອຊາບ ແລະ ພິຈາລະນາຕາມທາງຄວນ.</p>

<div class="signature">
  <p>ຫົວໜ້າຂະແໜງ</p>
  <br><br>
</div>

<button onclick="printPDF()">ດາວໂຫຼດ PDF</button>

<script>
function printPDF() {
  document.title = "Land_Report_<?= htmlspecialchars($ownerName) ?>";
  window.print();
}

window.addEventListener("load", () => {
  const urlParams = new URLSearchParams(window.location.search);
  if (urlParams.get("autoPrint") === "1") {
    setTimeout(() => window.print(), 300);
  }
});
</script>
</body>
</html>
