<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

mysqli_query($conn, "SET NAMES 'utf8'");
mysqli_query($conn, 'SET CHARACTER SET utf8');

header('Content-Type: text/html;charset=UTF-8');
ini_set('default_charset', 'utf-8');

$maindb = 'visamain';
$trandb = 'visatran';
$file = 'pax_pdf.php';
$keyfld = "myvouch";

$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
mysqli_select_db($conn, $zdb);
$mht = 'height:20px;';
$admin = $_SESSION["admin_" . $dbname];

$myvouch = $_REQUEST["myvouch"];
$ppno = $_REQUEST["passport"];
$prnamt = $_REQUEST["prnamt"];


$result = mysqli_query($conn, "SELECT *, date_format(`vouchdt`,'%d-%m-%Y') as mydt FROM `$maindb` WHERE $keyfld='$myvouch'") or die(mysqli_error($conn));
$get = mysqli_fetch_array($result);

$qry = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `myvouch`='$get[myvouch]' AND passport='$ppno' ORDER BY srno") or die(mysqli_error($conn));
$row = mysqli_fetch_array($qry);

if ($get['visatype'] == 'W') {
    $vtype = "Work";
} elseif ($get['visatype'] == 'F') {
    $vtype = "Family";
} elseif ($get['visatype'] == 'R') {
    $vtype = "Residence";
} elseif ($get['visatype'] == 'V') {
    $vtype = "Visit";
} else {
    $vtype = "";
}

$getpaid = $get['paidby'];
$fetpaid = [
    'C' => 'Cash',
    'R' => 'Credit',
    'U' => 'UPI / RTGS',
];
$paidby = $fetpaid[$getpaid];
$getcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company` WHERE `code`= '1'")or die(mysqli_error($conn));
$comp = mysqli_fetch_array($getcomp);

require_once("library/TCPDF/tcpdf.php");

$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// $pdf->AliasNbPages();
$pdf->AddPage();

$pdf->SetFont('freeserif', 'B', 15);

$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(193, 6, $comp['pname'], 0, 0, 'C');

// $pdf->Image('images/logoeng.png', 0, 5, 180, 30, 'PNG');
$pdf->ln(15);
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(190, 6, "$comp[add1]" . "$comp[add2]", 0, 0, 'C');
$pdf->Ln();
$pdf->Cell(190, 6, "$comp[add3] " . "$comp[city] " . " $comp[pin]" . " " . "Tel: $comp[tel] " . "Mob: +$comp[mobile]", 0, 0, 'C');
$pdf->Ln(20);

$pdf->Image($row['imagepath'], 10, 40, 30, 30, 'JPG');
$pdf->SetFont('freeserif', 'B', 15);
$pdf->Cell(70, $ln, "", 0, 0, 'L');
$pdf->Cell(20, $ln, "File No.:", 0, 0, 'L');
$pdf->Cell(50, $ln, " $get[manualno]", 0, 0, 'L');

$getinv = mysqli_query($conn, "Select * from invtran where passport='$row[passport]'") or die(mysqli_error($conn));
if (mysqli_num_rows($getinv) > 0) {
    $fetinv = mysqli_fetch_array($getinv);
    $invno = $fetinv['vouchno'];
    $pdf->ln(7);
    $pdf->SetFont('freeserif', 'B', 12);
    $pdf->Cell(171, $ln, "Invoice No.:", 0, 0, 'R');
    $pdf->Cell(25, $ln, "$invno", 0, 0, 'C');
    $pdf->ln(6);
} else {
    $pdf->ln(13);
}

$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(171, $ln, "Date:", 0, 0, 'R');
$pdf->Cell(25, $ln, " $get[mydt]", 0, 0, 'R');
$pdf->ln(7);

$ln = 7;
$pdf->SetFont('freeserif', '', 12);
$pdf->Cell(20, $ln, "Visa Type:", 'LT', 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(30, $ln, "$vtype", 'T', 0, 'L');
$pdf->SetFont('freeserif', '', 12);
$pdf->Cell(14, $ln, "Agent:", 'T', 0, 'L');
$pdf->SetFont('freeserif', 'B', 11);
$pdf->Cell(132, $ln, "$get[broker]", 'TR', 0, 'L');
$pdf->Ln();

$pdf->SetFont('freeserif', '', 12);
$pdf->Cell(27, $ln, "Sponsor Name:", 'L', 0, 'L');
$pdf->SetFont('freeserif', 'B', 11);
$pdf->Cell(104, $ln, "$get[sponsor]", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(17, $ln, "Visa No.:", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(48, $ln, "$get[visano]", 'R', 0, 'L');
$pdf->Ln();

$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(13, $ln, "ID No:", 'L', 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(30, $ln, "$get[idno]", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(13, $ln, "Email:", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(75, $ln, "$get[email]", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(12, $ln, "Place:", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(53, $ln, "$get[sector]", 'R', 0, 'L');
$pdf->Ln();

$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(17, $ln, "Visa Qty:", 'LB', 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(26, $ln, "$get[visaqty]", 'B', 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(10, $ln, "Hijri:", 'B', 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(27, $ln, "$get[hijridate]", 'B', 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(16, $ln, "Paid By:", 'B', 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(35, $ln, "$paidby", 'B', 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(22, $ln, "Created By:", 'B', 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(43, $ln, "$get[createby]", 'RB', 0, 'L');
$pdf->Ln(10);

$DOB = date("d-m-Y", strtotime($row['dob']));
$getsex = $row['sex'];
$fetsex = [
    'F' => 'Female',
    'M' => 'Male',
];
$sex = $fetsex[$getsex];
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(21, $ln, "Pax Name:", 'LT', 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(110, $ln, "$row[paxname]", 'T', 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(8, $ln, "Sex:", 'T', 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(57, $ln, " $sex", 'TR', 0, 'L');
$pdf->Ln();

$getdelv = $row['delvstatus'];
$fetdelv = [
    'E' => 'ECR',
    'N' => 'ECNR',
];
$delvstatus = $fetdelv[$getdelv];
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(15, $ln, "Passport:", 'L', 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(36, $ln, " $row[passport]", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(24, $ln, "Immigration:", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(24, $ln, "$delvstatus", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(12, $ln, "DOB:", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(39, $ln, "$DOB", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(17, $ln, "Religion:", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 11);
$pdf->Cell(29, $ln, "$row[religion]", 'R', 0, 'L');
$pdf->ln();

$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(12, $ln, "POB:", 'L', 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(87, $ln, "$row[plbirth]", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(11, $ln, "POI:", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(40, $ln, "$row[issplace]", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(18, $ln, "Duration:", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(28, $ln, "$row[periodstay]", 'R', 0, 'L');
$pdf->ln();
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(10, $ln, "DOI:", 'L', 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$issuedt = date("d-m-Y", strtotime($row['issdate']));
$pdf->Cell(41, $ln, "$issuedt", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(11, $ln, "DOE:", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$doe = date("d-m-Y", strtotime($row['doe']));
$pdf->Cell(37, $ln, "$doe", 0, 0, 'L');
if ($row['stampdate'] != '0000-00-00') {
    $stampdt = date("d-m-Y", strtotime($row['stampdate']));
} else {
    $stampdt = "";
}
if ($row['releasedt'] != '0000-00-00') {
    $releasedt = date("d-m-Y", strtotime($row['releasedt']));
} else {
    $releasedt = "";
}
if ($row['releasedt'] != '0000-00-00') {
    $submissiondt = date("d-m-Y", strtotime($row['submission_date']));
} else {
    $submissiondt = "";
}
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(23, $ln, "Stamp Date:", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(28, $ln, " $stampdt", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(18, $ln, "Released:", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(28, $ln, "$releasedt", 'R', 0, 'L');
$pdf->ln();
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(22, $ln, "Submission:", 'L', 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(29, $ln, " $submissiondt", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(18, $ln, "Category:", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(64, $ln, " $row[category]", 0, 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(21, $ln, "Mofa Date:", 0, 0, 'L');
$pdf->SetFont('freeserif', 'B', 12);
$pdf->Cell(42, $ln, "$row[mofa_date]", 'R', 0, 'L');
$pdf->ln();
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(21, $ln, "Mofa No 1:", 'LB', 0, 'L');
$pdf->SetFont('freeserif', 'B', 14);
$pdf->Cell(77, $ln, "$row[mofano]", 'B', 0, 'L');
$pdf->SetFont('aefurat', '', 12);
$pdf->Cell(21, $ln, "Mofa No 2:", 'B', 0, 'L');
$pdf->SetFont('freeserif', 'B', 14);
$pdf->Cell(77, $ln, "$row[mofano2]", 'RB', 0, 'L');
$pdf->Ln(10);

$pdf->SetFont('aefurat', '', 12);
if ($get['remark'] != '') {
    $pdf->Cell(125, $ln, "REMARKS", 0, 0, 'L');
    $pdf->Ln(8);
    $pdf->SetFont('aefurat', 'B', 12);
    $pdf->Cell(125, $ln, "$get[remark] $get[remark2]", 0, 0, 'L');
    $pdf->Ln(8);
} else {
    $pdf->Cell(125, $ln, "", 0, 0, 'L');
    $pdf->Ln(8);
    $pdf->Cell(125, $ln, "", 0, 0, 'L');
    $pdf->Ln(8);
}

if ($prnamt != 'N') {
    
    $pdf->SetFont('aefurat', '', 12);
    $pdf->Cell(125, $ln, "", 0, 0, 'L');
    $pdf->Cell(26, $ln, "Mofa Amt", 1, 0, 'R');
    $pdf->SetFont('freeserif', 'B', 12);
    $pdf->Cell(45, $ln, "$row[mofamt_mod]", 1, 0, 'R');
    $pdf->Ln(8);

    $pdf->Cell(125, $ln, "", 0, 0, 'L');
    $pdf->SetFont('aefurat', '', 12);
    $pdf->Cell(26, $ln, "Stamp Amt", 1, 0, 'R');
    $pdf->SetFont('freeserif', 'B', 12);
    $pdf->Cell(45, $ln, "$row[stampamt_mod]", 1, 0, 'R');
    $pdf->Ln(8);

    $pdf->Cell(125, $ln, "", 0, 0, 'L');
    $pdf->SetFont('aefurat', '', 12);
    $pdf->Cell(26, $ln, "Immigration", 1, 0, 'R');
    $pdf->SetFont('freeserif', 'B', 12);
    $pdf->Cell(45, $ln, "$row[imigamt_mod]", 1, 0, 'R');
    $pdf->Ln(8);
}
if ($row['rejctresn'] != "") {
    $pdf->SetTextColor(255, 0, 0);

    $pdf->Cell(125, $ln, "Reject Reason :", 0, 0, 'L');
} else {
    $pdf->Cell(125, $ln, "", 0, 0, 'L');
}
if ($prnamt != 'N') {

    $pdf->SetTextColor(0, 0, 0);

    $pdf->SetFont('aefurat', '', 12);
    $pdf->Cell(26, $ln, "M/s", 1, 0, 'R');
    $pdf->SetFont('freeserif', 'B', 12);
    $pdf->Cell(45, $ln, "$row[mis_mod]", 1, 0, 'R');
    $pdf->Ln(8);
}
if ($row['rejctresn'] != "") {

    $pdf->SetTextColor(255, 0, 0);

    $pdf->Cell(125, $ln, "$row[rejctresn]", 0, 0, 'L');
} else {
    $pdf->Cell(125, $ln, "", 0, 0, 'L');
}
if ($prnamt != 'N') {

    $pdf->SetTextColor(0, 0, 0);

    $pdf->SetFont('aefurat', '', 12);
    $pdf->Cell(26, $ln, "Miscellaneous", 1, 0, 'R');
    $pdf->SetFont('freeserif', 'B', 12);
    $pdf->Cell(45, $ln, "$row[misc_mod]", 1, 0, 'R');
    $pdf->Ln(8);

    $pdf->Cell(125, $ln, " ", 0, 0, 'R');
    $pdf->SetFont('aefurat', '', 12);
    $pdf->Cell(26, $ln, "Total", 1, 0, 'R');
    $pdf->SetFont('freeserif', 'B', 12);
    $pdf->Cell(45, $ln, "$row[memcash]", 1, 0, 'R');
    $pdf->Ln();
}

ob_end_clean();
$pdf->Output("PP No. $row[passport]" . ".pdf", "I");
