<?php
$data = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['pdf']) && $_FILES['pdf']['error'] === 0) {
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => "https://esheba24.xyz/bd/make.php",
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => [
            "pdf" => new CURLFile($_FILES['pdf']['tmp_name'], "application/pdf", $_FILES['pdf']['name'])
        ],
        CURLOPT_RETURNTRANSFER => true
    ]);
    $res = curl_exec($ch);
    curl_close($ch);
    $data = json_decode($res, true);

    if ($data) {
        $data['photo'] = $data['userIMG'] ?? '';
        $data['sign']  = $data['signIMG'] ?? '';
        $data['nameBangla']  = $data['nameBn'] ?? '';
        $data['nameEnglish'] = $data['nameEn'] ?? '';
        $data['bloodGroup']  = $data['blood_group'] ?? '';
        $data['dateOfToday'] = date('d/m/Y');
    }
}

$fields = [
  "nameBangla"   => "নামঃ (বাংলা)",
  "nameEnglish"  => "নামঃ (ইংরেজী)",
  "nationalId"   => "স্মার্ট কার্ড নম্বরঃ",
  "pin"          => "পিন নম্বরঃ",
  "dateOfBirth"  => "জন্ম তারিখঃ",
  "fatherName"   => "পিতার নামঃ",
  "motherName"   => "মাতার নামঃ",
  "birthPlace"   => "জন্মস্থানঃ",
  "bloodGroup"   => "রক্তের গ্রুপঃ",
  "address"      => "ঠিকানাঃ",
  "dateOfToday"  => "প্রদানের তারিখঃ"
];
?>
<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>NID Form</title>
<link href="https://fonts.googleapis.com/css2?family=Asap&display=swap" rel="stylesheet">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
<style>
body {
  font-family: "Asap", sans-serif;
  background-color: #121212;
  color: #e0e0e0;
  min-height: 100vh;
  margin: 0;
}
.container {
  max-width: 700px;
  margin: auto;
  padding: 20px;
}
@font-face {
  font-family: "SolaimanLipi";
  src: url("SolaimanLipi.ttf") format("truetype");
}
.card {
  background-color: #1e1e1e;
  border: 1px solid #333;
  border-radius: 16px;
  padding: 30px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
}
.form-control, .form-control:focus {
  background-color: #2b2b2b;
  color: #fff;
  border: 1px solid #444;
  border-radius: 8px;
}
.form-group label {
  color: #bbb;
  font-weight: 600;
  font-size: 14px;
}
.btn, .custom-upload-btn {
  background-color: #292929;
  color: #fff;
  border: 1px solid #444;
  padding: 10px;
  width: 100%;
  font-weight: bold;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.3s ease;
  text-align: center;
}
.btn:hover, .custom-upload-btn:hover {
  background-color: #383838;
}
.image-upload {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}
.photo-container, .sign-container {
  background:#2b2b2b;
  flex-direction:column;
  display:flex;
  align-items:center;
  justify-content:center;
  padding:5px;
  border-radius:10px;
}
.photo-container { width:80px; height:100px; }
.sign-container  { width:200px; height:80px; }
.image-upload img { border:2px solid #444; border-radius:8px; object-fit:cover; }
input[type="file"] { display:none; }
</style>
</head>
<body>

<div class="container">
<div class="card">

<form action="" method="POST" enctype="multipart/form-data">
  <label for="pdf" class="custom-upload-btn mb-4">Upload SignCopy (PDF)</label>
  <input type="file" name="pdf" id="pdf" accept=".pdf" onchange="this.form.submit()">
</form>

<form action="card.php" method="post" enctype="multipart/form-data" target="_blank">
<div class="image-upload">

<div class="photo-container">
  <img id="photoPreview" src="<?= $data['photo'] ?? '' ?>" alt="Photo" style="width:80px;height:100px;<?= empty($data['photo'])?'display:none;':'' ?>">
  <span id="photoPlaceholder" style="color:#777;font-size:12px;<?= !empty($data['photo'])?'display:none;':'' ?>">No Photo</span>
  <input type="file" id="photoFile" accept="image/*" onchange="previewImage(this, 'photoPreview', 'photoPlaceholder', 'photoData')">
  <input type="hidden" name="photo" id="photoData" value="<?= $data['photo'] ?? '' ?>">
  <label class="custom-upload-btn mt-2" onclick="document.getElementById('photoFile').click()">Upload Photo</label>
</div>

<div class="sign-container">
  <img id="signPreview" src="<?= $data['sign'] ?? '' ?>" alt="Signature" style="width:200px;height:80px;<?= empty($data['sign'])?'display:none;':'' ?>">
  <span id="signPlaceholder" style="color:#777;font-size:12px;<?= !empty($data['sign'])?'display:none;':'' ?>">No Signature</span>
  <input type="file" id="signFile" accept="image/*" onchange="previewImage(this, 'signPreview', 'signPlaceholder', 'signData')">
  <input type="hidden" name="sign" id="signData" value="<?= $data['sign'] ?? '' ?>">
  <label class="custom-upload-btn mt-2" onclick="document.getElementById('signFile').click()">Upload Signature</label>
</div>

</div>
</div>

<?php foreach ($fields as $name => $label): ?>
<div class="form-group">
  <label for="<?= $name ?>" style="<?= $label === "প্রদানের তারিখঃ" ? 'font-family:SolaimanLipi, Asap, sans-serif;font-size:16px;' : '' ?>">
    <?= $label ?>
  </label>

  <?php if ($name === "address"): ?>
    <textarea name="<?= $name ?>" class="form-control" id="<?= $name ?>" rows="2"><?= $data[$name] ?? '' ?></textarea>
  <?php else: ?>
    <input type="text" name="<?= $name ?>" class="form-control" id="<?= $name ?>" value="<?= $data[$name] ?? '' ?>">
  <?php endif; ?>

</div>
<?php endforeach; ?>

<button type="submit" class="btn">MAKE</button>
</form>

</div>
</div>

<script>
function previewImage(input, previewId, placeholderId, hiddenId) {
  const file = input.files[0];
  const reader = new FileReader();
  reader.onload = e => {
    document.getElementById(previewId).src = e.target.result;
    document.getElementById(previewId).style.display = "block";
    document.getElementById(placeholderId).style.display = "none";
    document.getElementById(hiddenId).value = e.target.result;
  }
  if(file) reader.readAsDataURL(file);
}

function toBanglaDigits(str) {
  const map = ['০','১','২','৩','৪','৫','৬','৭','৮','৯'];
  return str.replace(/\d/g, d => map[d]);
}

const today = document.getElementById("dateOfToday");
if (today) {
  today.value = toBanglaDigits(today.value);
  today.addEventListener("input", function() {
    this.value = toBanglaDigits(this.value);
  });
}
</script>

</body>
</html>
