<?php
require 'db.php';
require 'functions.php';
requireRole(['rnduser']);

$id = (int)($_GET['id'] ?? 0);

$stmt = $pdo->prepare("SELECT v.*, p.name AS panchayatname FROM villager v
    LEFT JOIN panchayat p ON p.id = v.panchayatid
    WHERE v.id = ? AND v.isdeleted = 0 AND v.status = 'approved' AND v.paymentstatus = 'paid'");
$stmt->execute([$id]);
$v = $stmt->fetch();

$pagetitle = 'Registration Detail';
$activemenu = 'registrations';
require 'header.php';

if (!$v) {
    echo '<div class="dfp-card">Registration not found or not yet completed (approved &amp; paid).</div>';
    require 'footer.php';
    require 'footer2.php';
    exit;
}

$vehiclestmt = $pdo->prepare("SELECT vehiclenumber FROM villagervehicle WHERE villagerid = ? AND isdeleted = 0");
$vehiclestmt->execute([$id]);
$vehicles = $vehiclestmt->fetchAll();
?>

<div class="dfp-card">
  <h3><?php echo safeOut($v['fullname']); ?></h3>
  <div style="display:flex;gap:20px;flex-wrap:wrap;">
    <div style="flex:0 0 120px;">
      <?php if ($v['photo']): ?>
        <img src="uploads/photos/<?php echo safeOut($v['photo']); ?>" style="width:110px;height:110px;object-fit:cover;border:1px solid var(--clr-border);border-radius:4px;">
      <?php endif; ?>
    </div>
    <div style="flex:1;min-width:220px;">
      <p><b>Phone:</b> <?php echo safeOut($v['phone']); ?></p>
      <p><b>Panchayat:</b> <?php echo safeOut($v['panchayatname']); ?></p>
      <p><b>Address:</b> <?php echo safeOut($v['address']); ?> <?php echo $v['houseno'] ? '(House No: ' . safeOut($v['houseno']) . ')' : ''; ?></p>
      <p><b>Vehicles:</b> <?php echo $vehicles ? implode(', ', array_map(fn($x) => safeOut($x['vehiclenumber']), $vehicles)) : '-'; ?></p>
    </div>
    <div style="flex:1;min-width:220px;">
      <p><b>Pass Active:</b> <?php echo $v['passactive'] == 1 ? 'Yes' : 'No'; ?></p>
      <p><b>Pass Validity:</b> <?php echo safeOut($v['passstartdate']) . ' to ' . safeOut($v['passenddate']); ?></p>
      <p><b>Barcode:</b> <?php echo safeOut($v['barcodetext']); ?></p>
      <p><b>Physical Pass Printed:</b> <?php echo $v['isprinted'] == 1 ? 'Yes (' . safeOut($v['printedat']) . ')' : 'No'; ?></p>
    </div>
  </div>

  <div style="margin-top:10px;">
    <a class="btn btn-sm btn-outline" href="rnd_printpass.php?id=<?php echo (int)$id; ?>" target="_blank">View Printable Pass</a>
    <?php if ($v['isprinted'] != 1): ?>
      <button class="btn btn-sm" onclick="markPrinted()">Mark as Printed</button>
    <?php endif; ?>
  </div>
  <p style="color:var(--clr-gray-mid);font-size:var(--fontsize-sm);margin-top:8px;">
    Note: printing a physical pass is optional and not required for the app-based pass to work.
  </p>
</div>

<?php require 'footer.php'; ?>
<script>
function markPrinted() {
  $.post('ajax_rnd_registration.php', { action: 'markprinted', id: <?php echo (int)$id; ?> }, function (resp) {
    dfpToast(resp.message, resp.success ? 'success' : 'error');
    if (resp.success) { setTimeout(function () { window.location.reload(); }, 800); }
  }, 'json');
}
</script>
<?php require 'footer2.php'; ?>
