<?php
require_once 'auth.php';
?>
<?php
// ---------- DATABASE ----------
$conn = new mysqli("localhost","root","","tradelicense");
mysqli_set_charset($conn, "utf8mb4");

if ($conn->connect_error) {
    die("Database error");
}

// ---------- GET ----------
$license_id = $_GET['Id'] ?? '';

if (!is_numeric($license_id)) {
    die("Invalid License ID");
}

// ---------- FETCH (SECURE) ----------
$stmt = $conn->prepare("SELECT * FROM details WHERE lic_id = ?");
$stmt->bind_param("s", $license_id);
$stmt->execute();
$res = $stmt->get_result();

if ($res->num_rows == 0) {
    die("No data found");
}

$data = $res->fetch_assoc();
$conn->close();

// ---------- BANGLA NUMBER ----------
function bn($str){
    return str_replace(
        ['0','1','2','3','4','5','6','7','8','9'],
        ['০','১','২','৩','৪','৫','৬','৭','৮','৯'],
        $str
    );
}

function bn_date($date)
{
    if(empty($date)) return '';

    $timestamp = strtotime($date);

    $formatted = date('d/m/Y', $timestamp);

    return str_replace(
        ['0','1','2','3','4','5','6','7','8','9'],
        ['০','১','২','৩','৪','৫','৬','৭','৮','৯'],
        $formatted
    );
}

// ---------- QR ----------
$verify_url = "https://pauroservices-lged-gov-bd.sreepur.top/Tradelicense/ViewLicense/VerifyLicense"
    . "?Id=".$data['lic_id']
    . "&ZoneInfoId=".$data['zone_info_id'];

$qr_img = "https://api.qrserver.com/v1/create-qr-code/?size=200x200&data="
    . urlencode($verify_url);
?>

<!DOCTYPE html>
<html lang="bn-BD">
<head>
<meta charset="UTF-8">
<title>Print License</title>

<link href="https://fonts.googleapis.com/css2?family=Noto+Serif+Bengali&display=swap" rel="stylesheet">

<style>


@font-face {
    font-family: 'NotoBangla';
    src: url('assets/fonts/SUTONNYOMJ.TTF') format('truetype');
    font-weight: normal;
    font-style: normal;
}

body{
    margin:0;
    background:#e5e5e5;
    font-family:'NotoBangla', serif;
}

/* BUTTON */
.print-btn{
    display:block;
    margin:15px auto;
    padding:10px 30px;
    cursor:pointer;
}

/* PAGE */
.page{
    position:relative;
    width:794px;
    height:1123px;
    margin:auto;
    background:#fff;
}

/* BG */
.bg{
    width:100%;
    height:100%;
    position:absolute;
    z-index:1;
}

/* TEXT */
.field{
    position:absolute;
    z-index:5;
    font-size:14px;
}

/* POSITIONS */
.license-no{ top:120px; left:220px; }
.license-id{ top:143px; left:220px; }
.ward{ top:167px; left:220px; }
.street{ top:192px; left:220px; }
.issue { top:217px; left:220px; }

.renewal_year { top:241px; left:222px; }


.renewal_date { top:264px; left:220px; }

.valid { top:319px; left:500px; }



.org_name{
    position:absolute;
    top:353px;
    left:220px;
    z-index:10;
    font-size:15px;
}

.business_cat{
    position:absolute;
    top:391px;
    left:220px;
    z-index:10;
    font-size:15px;
}

.owner_name{
    position:absolute;
    top:423px;
    left:220px;
    z-index:10;
    font-size:15px;
}

.father{
    position:absolute;
    top:459px;
    left:220px;
    z-index:10;
    font-size:15px;
}

.mother{
    position:absolute;
    top:490px;
    left:220px;
    z-index:10;
    font-size:15px;
}

.address{
    position:absolute;
    top:527px;
    left:220px;
    z-index:10;
    font-size:15px;
}

.p_address{
    position:absolute;
    top:559px;
    left:220px;
    z-index:10;
    font-size:15px;
}

.par_address{
    position:absolute;
    top:595px;
    left:220px;
    z-index:10;
    font-size:15px;
}

.nid{
    position:absolute;
    top:630px;
    left:220px;
    z-index:10;
    font-size:15px;
}

.mobile{
    position:absolute;
    top:665px;
    left:220px;
    z-index:10;
    font-size:15px;
}


.tr_fee {
    position: absolute;
    top: 742px;
    left: 448px;
    width: 80px;
    text-align: right;
    font-size: 14px;
}

.sign_fee{
    position:absolute;
    top:760px;
    left:448px;
    width:80px;
    text-align:right;
    font-size:14px;
}

.miscellaneous_fee{
    position:absolute;
    top:780px;
    left:448px;
    width:80px;
    text-align:right;
    font-size:14px;
}

.arrear{
    position:absolute;
    top:800px;
    left:448px;
    width:80px;
    text-align:right;
    font-size:14px;
}

.surcharge{
    position:absolute;
    top:827px;
    left:448px;
    width:80px;
    text-align:right;
    font-size:14px;
}

.total_fee{
    position:absolute;
    top:848px;
    left:448px;
    width:80px;
    text-align:right;
    font-size:14px;
    font-weight:bold;
}



.current_surcharge{
    position:absolute;
    top:735px;
    left:675px;
    width:70px;
    text-align:right;
    font-size:14px;
}

.vat{
    position:absolute;
    top:759px;
    left:675px;
    width:70px;
    text-align:right;
    font-size:14px;
}

.vat_arrear{
    position:absolute;
    top:783px;
    left:675px;
    width:70px;
    text-align:right;
    font-size:14px;
}

.income_tax{
    position:absolute;
    top:807px;
    left:675px;
    width:70px;
    text-align:right;
    font-size:14px;
}

.income_tax_arrear{
    position:absolute;
    top:826px;
    left:675px;
    width:70px;
    text-align:right;
    font-size:14px;
}

.total_fee_col{
    position:absolute;
    top:878px;
    left:400px;
    width:70px;
    text-align:center;
    font-size:14px;
    font-weight:bold;
}



/* QR */
.qr{
    position:absolute;
    bottom:260px;
    left:62px;
    z-index:20;
}

.qr img{
    width:113px;
    height:113px;
}

/* PRINT */
@media print{
    body{ background:#fff; }
    .print-btn{ display:none; }
    .page{ margin:0; }
}
</style>
</head>

<body>

<button class="print-btn" onclick="window.print()">প্রিন্ট করুন</button>

<div class="page">

<img src="assets/license_bg.jpg" class="bg">

<div class="qr">
<img src="<?= $qr_img ?>" alt="QR Code">
</div>

<!-- ID -->
<div class="field license-no">
   <?= bn($data['license_no']) ?>
</div>

<div class="field license-id">
   <?= bn($data['license_id']) ?>
</div>

<div class="field ward">
   <?= bn($data['ward_bengali']) ?>
</div>



<div class="field street">
   <?= $data['street_bengali'] ?>
</div>





<div class="field issue">
  <?= bn_date($data['issue_date']) ?>
</div>



<div class="field renewal_year">
     <?= bn($data['renewal_year']) ?>
</div>


<div class="field renewal_date">
     <?= bn_date($data['renewal_date']) ?>
</div>


<div class="field valid">
     <?= bn($data['valid']) ?>
</div>


<!-- INFO -->
 <div class="field org_name">
    <?= htmlspecialchars($data['organization_name_bengali']) ?>
</div>

 <div class="field business_cat">
    <?= htmlspecialchars($data['business_type_bengali']) ?>
</div>

 <div class="field owner_name">
    <?= htmlspecialchars($data['owner_name_bengali']) ?>
</div>

<div class="field father">
    <?= htmlspecialchars($data['father_name']) ?>
</div>

<div class="field mother">
    <?= htmlspecialchars($data['mother_name']) ?>
</div>

<div class="field address">
    <?= htmlspecialchars($data['address_bengali']) ?>
</div>

<div class="field p_address">
    <?= htmlspecialchars($data['present_address']) ?>
</div>

<div class="field par_address">
    <?= htmlspecialchars($data['parmanent_address']) ?>
</div>

<div class="field nid">
<?= bn($data['nid']) ?>
</div>

<div class="field mobile">
<?= bn($data['phone_number']) ?>
</div>


<div class="field tr_fee">
<?= bn($data['license_fee']) ?>
</div>

<div class="field sign_fee">
<?= bn($data['signboard_fee']) ?>
</div>

<div class="field miscellaneous_fee">
<?= bn($data['miscellaneous_fee']) ?>
</div>


<div class="field sign_fee">
<?= bn($data['signboard_fee']) ?>
</div>


<div class="field arrear">
<?= bn($data['arrear']) ?>
</div>

<div class="field surcharge">
<?= bn($data['surcharge']) ?>
</div>

<div class="field total_fee">
<?= bn($data['total_fee']) ?>
</div>


<div class="field current_surcharge">
<?= bn($data['current_surcharge']) ?>
</div>

<div class="field vat">
<?= bn($data['vat']) ?>
</div>

<div class="field vat_arrear">
<?= bn($data['vat_arrear']) ?>
</div>


<div class="field income_tax">
<?= bn($data['income_tax']) ?>
</div>

<div class="field income_tax_arrear">
<?= bn($data['income_tax_arrear']) ?>
</div>

<div class="field total_fee_col">
<?= bn($data['total_fee']) ?>
</div>



</div>

<!-- QR -->


</div>
</div>

</body>
</html>