
<?php
require 'config/db_conn.php';

if (!isset($_GET['id'])) {
    die("Invalid request");
}

$id = intval($_GET['id']);

$stmt = $conn->prepare("SELECT * FROM users WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute();
$data = $stmt->get_result()->fetch_assoc();

if (!$data) {
    die("Data not found");
}
?>

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1">

<html>
<head>
    <title>Edit Passport</title>
    <style>
        body { font-family: Arial; background:#f5f5f5; }
        .box { background:#fff; padding:20px; width:500px; margin:30px auto; }
        label { display:block; margin-top:10px; }
        input { width:100%; padding:7px; }
        button { margin-top:15px; padding:10px 20px; }
    </style>
</head>
<body>
<?php
// ধরুন $data তে users টেবিলের row আছে

$baseUrl = "https://online.csgb.gov.ct.tr.calismac.com/CIP/BasvuruAnaQRKODValidation/Validate/";

$ne  = "onizinbelgepdf";
$pkey = $data['uid'];          // Pkey = UID
$imgFull = $data['image'];     // যেমন: uploads/67ef9e4b002a11743756875.jpg

// শুধু filename বের করা (uploads/ বাদ দিয়ে)
$bruteForceKey = basename($imgFull);

// Final validation URL (এটাই QR এর ভিতরে থাকবে)
$validationUrl = $baseUrl . "?Ne=" . urlencode($ne)
              . "&BruteForceKey=" . urlencode($bruteForceKey)
              . "&Pkey=" . urlencode($pkey);
?>

<div class="box" align="center"><h5>QR Code</h5>

<?php
$qrImgUrl = "https://api.qrserver.com/v1/create-qr-code/?size=220x220&data=" . urlencode($validationUrl);
?>

<img id="qrImage" src="<?= $qrImgUrl ?>" alt="QR Code" class="mb-2">

<br>

<a 
  href="<?= $qrImgUrl ?>" 
  download="QR_<?= htmlspecialchars($data['uid']) ?>.png"
  class="btn btn-success btn-sm mt-2">
  Download QR Code
</a>


<h2>Edit Passport Data</h2>

<form action="update.php" method="post">
    <input type="hidden" name="id" value="<?= $data['id'] ?>">

    <label>UID</label>
    <input type="text" name="uid" value="<?= htmlspecialchars($data['uid']) ?>" required>

    <label>Passport</label>
    <input type="text" name="passport" value="<?= htmlspecialchars($data['passport']) ?>" required>

    <label>Name</label>
    <input type="text" name="name" value="<?= htmlspecialchars($data['name']) ?>" required>

    <label>Father</label>
    <input type="text" name="father" value="<?= htmlspecialchars($data['father']) ?>">

    <label>Mother</label>
    <input type="text" name="mother" value="<?= htmlspecialchars($data['mother']) ?>">

    <label>Start Date</label>
    <input type="text" name="start" value="<?= $data['start'] ?>">

    <label>End Date</label>
    <input type="text" name="end" value="<?= $data['end'] ?>">

    <label>Birth Date</label>
    <input type="text" name="birth" value="<?= $data['birth'] ?>">

    <button type="submit">Update</button>
</form>
<form action="update.php" method="post">
    <input type="hidden" name="id" value="<?= $data['id'] ?>">
    <!-- DELETE BUTTON -->
    <button type="button" onclick="deleteData(<?= $data['id'] ?>)" style="background:red;color:#fff;">
        Delete
    </button>
   
</form>
<a href="https://online.csgb.gov.ct.tr.calismac.com/CIP/BasvuruAnaQRKODValidation/Validate/permit_template.php?id=<?= $data['id'] ?>">Print</a>
<script>
function deleteData(id){
    if(confirm("Are you sure? This data will be permanently deleted!")){
        window.location.href = "delete.php?id=" + id;
    }
}
</script>

</div>

</body>
</html>
