<?php
// এখানে $data = DB থেকে row (users table) ধরে নিচ্ছি
// দরকার মতো আপনার DB query বসাবেন

// QR validation URL (আপনার নিয়ম: Pkey=uid, BruteForceKey=image filename)
$baseUrl = "https://online.csgb.gov.ct.tr.calismac.com/CIP/BasvuruAnaQRKODValidation/Validate/";
$validationUrl = $baseUrl
  . "?Ne=onizinbelgepdf"
  . "&BruteForceKey=" . urlencode(basename($data['image'] ?? ''))
  . "&Pkey=" . urlencode($data['uid'] ?? '');

$qrImgUrl = "https://api.qrserver.com/v1/create-qr-code/?size=260x260&data=" . urlencode($validationUrl);

// Word-এ যেগুলো আছে কিন্তু আপনার users টেবিলে নেই, সেগুলো fixed/variable করে দিলাম
$sex        = $sex ?? "E";
$nationality= $nationality ?? "BANGLADEŞ/ BANGLADESH";
$occupation = $occupation ?? "Ekskavatör(Kazıma Ve Yükleme) Operatörü"; // example from format
$employer   = $employer ?? "KOOP SÜT";
$regNo      = $regNo ?? "GY-03045";
$duration   = $duration ?? "1 YIL";

// approve field edit করবেন না বলেছেন, কিন্তু document-এ date লাগে
$docDate    = $data['approve'] ?? "";   // e.g. 30.12.2025 / 14.01.2026
$startDate  = $data['start'] ?? "";
$endDate    = $data['end'] ?? "";
$printDate  = $printDate ?? date("d.m.Y H:i:s");

// Police number আপনার DB-তে নেই—তাই variable
$policeNumber = $policeNumber ?? "2504595";

// Application number example: 99886/2025 (Word এ আছে)
$appNumber = ($data['uid'] ?? '') . "/2025";
?>
<!doctype html>
<html lang="tr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pre-permit Application</title>
<style>
  /* A4-like view in browser */
  body{font-family:"Times New Roman", serif; background:#f2f2f2; margin:0; padding:18px;}
  .sheet{
    width:210mm; min-height:297mm; margin:0 auto; background:#fff; color:#000;
    padding:14mm 14mm; box-sizing:border-box;
  }
  .center{text-align:center;}
  .bold{font-weight:700;}
  .small{font-size:12px;}
  .title1{font-size:18px; font-weight:700;}
  .title2{font-size:16px; font-weight:700;}
  .title3{font-size:14px; font-weight:700;}
  table{width:100%; border-collapse:collapse;}
  .t, .t th, .t td{border:2px solid #000;}
  .t th{padding:6px; font-size:12px; vertical-align:top;}
  .t td{padding:10px 8px; font-size:13px; vertical-align:middle;}
  .sub{display:block; font-weight:700;}
  .en{display:block; font-weight:400; font-size:11px; margin-top:2px;}
  .photo{
    width:62px; height:78px; object-fit:cover; border:2px solid #000;
  }
  .mt6{margin-top:6px;}
  .mt10{margin-top:10px;}
  .mt14{margin-top:14px;}
  .para{font-size:12px; line-height:1.35; margin-top:8px;}
  .twoCol td{padding:10px 8px; font-size:12px; vertical-align:top;}
  .approvalBox{border:2px solid #000; margin-top:10px;}
  .approvalHead{border-bottom:2px solid #000; padding:6px 8px; font-weight:700;}
  .approvalBody{display:flex; gap:10px; padding:8px;}
  .approvalLeft{flex:1; font-size:11px; line-height:1.25;}
  .approvalRight{width:45%; display:flex; justify-content:space-between; gap:10px; font-size:11px;}
  .stamp{width:55px; height:55px; border:2px solid #bbb; border-radius:50%; display:flex; align-items:center; justify-content:center; font-size:9px;}
  .internet{border:2px solid #000; padding:10px 12px; font-weight:700; height:fit-content;}
  .validRow td{font-size:12px; padding:6px 8px;}
  .qrBlock{margin-top:12px; text-align:center;}
  .qrImg{width:150px; height:150px; border:2px solid #000;}
  .attn{margin-top:10px; text-align:center; font-size:12px; line-height:1.35;}
  .attn .red{color:#c00; font-weight:700;}
  .footer{margin-top:10px; text-align:right; font-size:12px; font-weight:700;}
</style>
</head>
<body>
  <div class="sheet">

    <div class="center title1">ÇALIŞMA VE SOSYAL GÜVENLİK BAKANLIĞI</div>
    <div class="center title2">MINISTRY OF LABOUR AND SOCIAL SECURITY</div>
    <div class="center title3 mt6">Ön İzin Başvurusu/Pre-permit Application</div>
    <div class="center small">(Yurt Dışından Getirtilmek İstenen İşçi Listesi / The worker to be brought )</div>

    <div class="small mt10">
      <span class="bold">Note:</span> Bu form 3 (üç) Suret Olarak Düzenlenecektir.<br>
      <span class="bold">Note:</span> This form will be prepared in 3 (three) copies.
    </div>

    <!-- Application number block -->
    <table class="t mt10">
      <tr>
        <th style="width:22%">
          <span class="sub">Başvuru No</span>
          <span class="en">ApplicationNumber</span>
        </th>
        <td class="bold"><?= htmlspecialchars($appNumber) ?></td>
      </tr>
    </table>

    <!-- Main worker table -->
    <table class="t" style="margin-top:-2px;">
      <tr>
        <th style="width:18%"><span class="sub">Adı-Soyadı</span><span class="en">Name-Surname</span></th>
        <th style="width:8%"><span class="sub">CinsiyEt</span><span class="en">Sex</span></th>
        <th style="width:14%"><span class="sub">DoğumTarihi</span><span class="en">Date of Birth</span></th>
        <th style="width:18%"><span class="sub">Uyruğu</span><span class="en">Nationality</span></th>
        <th style="width:12%"><span class="sub">PasaportNo</span><span class="en">PassportNo</span></th>
        <th style="width:20%"><span class="sub">Mesleği</span><span class="en">Occupation</span></th>
        <th style="width:10%"><span class="sub">Resim</span><span class="en">Photo</span></th>
      </tr>
      <tr>
        <td class="bold"><?= htmlspecialchars($data['name'] ?? '') ?></td>
        <td class="center bold"><?= htmlspecialchars($sex) ?></td>
        <td class="center bold"><?= htmlspecialchars($data['birth'] ?? '') ?></td>
        <td class="bold"><?= htmlspecialchars($nationality) ?></td>
        <td class="center bold"><?= htmlspecialchars($data['passport'] ?? '') ?></td>
        <td><?= htmlspecialchars($occupation) ?></td>
        <td class="center">
          <?php if(!empty($data['image'])): ?>
            <img class="photo" src="https://online.csgb.gov.ct.tr.calismacy.com/CIP/BasvuruAnaQRKODValidation/Validate/images/<?= htmlspecialchars($data['image']) ?>">

          <?php else: ?>
            <div class="small">No Photo</div>
          <?php endif; ?>
        </td>
      </tr>
    </table>

    <!-- Police approval -->
    <div class="bold mt14">POLİS GENEL MÜDÜRLÜĞÜ ONAYI / APPROVAL OF THE POLICE HEADQUARTERS</div>
    <div class="small mt6">
      <span class="bold">Sayı/Number:</span> <?= htmlspecialchars($policeNumber) ?>
      &nbsp;&nbsp;
      <span class="bold">Tarih/Date:</span> <?= htmlspecialchars($docDate) ?>
    </div>

    <div class="para">
      <span class="bold">Çalışma ve Sosyal Güvenlik Bakanlığı'na / To the Ministry of Labor and Social Security</span><br>
      Şu anda yurt dışında olan yukarıda Adı, Soyadı, Cinsiyeti, Uyruğu, Pasaport Numarası ve Mesleği yazılı yabancı uyruklu kişileri işyerimde istihdam etmek istediğimi
      ve KKTC'ye girişlerinden itibaren en geç 15 gün içerisinde Çalışma İzni işlemlerini başlatacağımı ve 30 gün içerisinde tamamlayacağımı beyan ederim.
      / I declare that I want to employ the foreign national whose information is written above, who is currently abroad, in my workplace, and that I will initiate the Work
      Permit procedures within 15 days at the latest after entering the TRNC and will complete it within 30 days.
    </div>

    <!-- Employer table -->
    <table class="t mt10 twoCol">
      <tr>
        <td style="width:60%">
          <span class="bold">İşveren:</span> <span class="bold">Employeer:</span>
          <span style="margin-left:10px;"><?= htmlspecialchars($employer) ?></span>
        </td>
        <td style="width:40%">
          <span class="bold">Çalışma D. Tescil No:</span><br>
          <span class="bold">LaborDepartment RegistrationNumber:</span>
          <span style="margin-left:10px;"><?= htmlspecialchars($regNo) ?></span>
        </td>
      </tr>
      <tr>
        <td>
          <span class="bold">İşveren Temsilcisi (varsa):</span>
          <span class="bold">Employer Representative(ifany):</span>
        </td>
        <td>
          <span class="bold">Çalışma İzin Süresi:</span> <span class="bold">Work Permit Duration:</span>
          <span style="margin-left:10px;"><?= htmlspecialchars($duration) ?></span>
        </td>
      </tr>
      <tr>
        <td>
          <span class="bold">Başvuru Tarihi:</span> <span class="bold">Application Date:</span>
          <span style="margin-left:10px;"><?= htmlspecialchars($docDate) ?></span>
        </td>
        <td>&nbsp;</td>
      </tr>
    </table>

    <!-- Ministry approval box -->
    <div class="approvalBox">
      <div class="approvalHead">Bakanlık Onayı / Ministry Approval</div>
      <div class="approvalBody">
        <div class="approvalLeft">
          Yukarıdaki müracaat değerlendirilmiş ve formda adı yazılı işverenin yurt dışından işçi getirme talebi uygun bulunmuştur.
          The above application has been evaluated and the request of the employer whose name is written on the form to bring workers from abroad has been found appropriate.
        </div>
        <div class="approvalRight">
          <div>
            <div class="bold">Tarih:</div><div class="bold">Date:</div>
            <div style="margin-top:6px;"><?= htmlspecialchars($docDate) ?></div>
            <div class="mt10">Çalışma ve Sosyal Güvenlik Bakanlığı</div>
            <div>Ministry of Labour and Social Security</div>
          </div>
          <div class="stamp">STAMP</div>
          <div class="internet">Internet</div>
        </div>
      </div>
    </div>

    <!-- Validity row -->
    <table class="t validRow" style="margin-top:-2px;">
      <tr>
        <td style="width:50%">
          <span class="bold">Geçerlilik Başlangıç Tarihi/Validity Start Date:</span>
          <span><?= htmlspecialchars($startDate) ?></span>
        </td>
        <td style="width:50%">
          <span class="bold">Geçerlilik Bitiş Tarihi/Expiration Date:</span>
          <span><?= htmlspecialchars($endDate) ?></span>
        </td>
      </tr>
    </table>

    <div class="small mt10">
      Belgenin doğruluğunun teyidi için aşağıdaki karekodu okutun.
      Scan the QR code below to confirm the authenticity of the document.
    </div>

    <!-- QR -->
    <div class="qrBlock">
      <img class="qrImg" src="<?= htmlspecialchars($qrImgUrl) ?>" alt="qr">
      <div class="small mt6" style="word-break:break-all;">
        <?= htmlspecialchars($validationUrl) ?>
      </div>

      <div class="mt10">
        <a href="<?= htmlspecialchars($qrImgUrl) ?>"
           download="QR_<?= htmlspecialchars($data['uid'] ?? 'user') ?>.png"
           style="display:inline-block;padding:8px 12px;border:1px solid #000;text-decoration:none;color:#000;">
          Download QR Code
        </a>
      </div>
    </div>

    <!-- Attention -->
    <div class="attn">
      <span class="red">DİKKAT!</span> Bu belgenin KKTC'ye girişte Muhaceret polisine ibraz edilmesi zorunludur.<br>
      <span class="red">ATTENTION!</span> This Pre-permit document must be submitted to the immigration officer upon entry.
    </div>

    <div class="footer">
      Basım Tarihi/Print Date: <?= htmlspecialchars($printDate) ?> (Form.ÇSB.Yİ.02)
    </div>

  </div>
</body>
</html>
