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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
$zroot = $_SESSION["zdir"];



##############################Print Start Here##################################
if (isset($_POST["view"])) {

    $name = trim($_POST["name"]);
    $district = trim($_POST["district"]);
    $state = trim($_POST["state"]);
    $for = trim($_POST["for"]);
    $class = trim($_POST["class"]);
    $batch = $_POST["batch"];
    $frompax = $_POST["frompax"];
    $fromhaji = $_POST["fromhaji"];
    $topax = $_POST["topax"];
    $tohaji = $_POST["tohaji"];
    $mair = $_POST["airline"];
    $mjbdt = $_POST["arrvdate"];
    $siscomp = trim($_POST["siscono"]);
    $sortby = $_POST["sortby"];



    $ncond = "";
    $vcond = "";
    $jb_date = "";
    $air_code = "";
    $hajicond = "";
    $paxcond = "";
    $bcond = "";
    $ccond = "";
    $fcond = "";
    $scond = "";
    $dcond = "";

    if (!empty($_POST["name"])) {
        $ncond = " AND `name` LIKE '%$name%' ";
    }

    if (!empty($_POST["district"])) {
        $dcond = " AND `district` = '$district' ";
    }

    if (!empty($_POST["state"])) {
        $scond = " AND `state` = '$state' ";
    }

    if (!empty($_POST["for"])) {
        $fcond = " AND `visitingfor` = '$for' ";
    }

    if (!empty($_POST["class"])) {
        $ccond = " AND `class` LIKE '%$class%' ";
    }

    if (!empty($_POST["batch"])) {
        $bcond = " AND `paxid` IN (SELECT `paxno` FROM `airline_detail` WHERE `batchno`= '$batch') ";
    }

    if (!empty($_POST["frompax"]) && !empty($_POST["topax"])) {
        $paxcond = " AND `paxid` BETWEEN '$frompax' AND '$topax'";
    }

    if (!empty($_POST["fromhaji"]) && !empty($_POST["tohaji"])) {

        $paxcond = " AND `hajino` BETWEEN '$fromhaji' AND '$tohaji'";
    }


    if (!empty($_POST["airline"])) {
        $air_code = " AND `paxid` IN (SELECT `paxno` FROM `airline_detail` WHERE `airline_code`= '$mair') ";
    }


    if (!empty($_POST["arrvdate"])) {
        $jb_date = " AND `paxid` IN (SELECT `paxno` FROM `airline_detail` WHERE `dept_date`= '$mjbdt' AND `srno` = 2 ) ";
    }

    if (!empty($_POST["siscono"])) {
        $vcond = " AND `company` = '$siscomp' ";
    }

    $tourdb = DBSUFFIX . $_SESSION["cono"] . "_" . $_SESSION["tourname"];

    $get = mysqli_query($conn, "SELECT `passportno`, `paxid`, `hajino`, `surname`, `name`, `midname`, `suffix`, `gender`, 
                        `mehram_no`, `mehram_relation_code` FROM `pax` WHERE `name` IS NOT NULL $bcond $paxcond 
                        $hajicond $ncond $dcond $scond $fcond $ccond $vcond $air_code $jb_date ORDER BY $sortby ")or die(mysqli_error($conn));
    $getno = mysqli_num_rows($get);

    include("includes/linkshead.php");
    ?>
    <link href="styles/rep_format.css" rel="stylesheet">
    <style>

        body{
            font-size: 15px;

        }
        td,th{
            padding:0px;
            font-size:15px;
        }
        table{
            width:100%;
            /*border : 1px solid black;*/
        }
        .center{
            text-align: center;
        }
        .left{
            text-align: left;
        }
        .right{
            text-align: right;
        }
        .padd{
            padding: 5px;
        }
        .fontsize{
            font-size:12px;
        }
        .page {
            width: 210mm;
            min-height: 280mm;
            padding: 1mm;
            margin: 10mm auto;
            border: 1px #D3D3D3 solid;
            border-radius: 5px;
            background: white;

        }
    </style>
    </head>
    <body> 

        <?php
        $ctr = 0;
        while ($pax = mysqli_fetch_array($get)) {

            if ($getno < 2) {
            $td2 = 2 - $getno;
        } else {
            $td2 = fmod($getno, 2) + 1;
        }
            
            
            $ctr++;
            $lblctr++;
            if ($lblctr == 1) {
                echo "<div class='page' style=''>";
            }
            ?>

            <?php if ($ctr == 1) {
                ?>
                <br>
                <br>
                <br>
                <br>
                <br>
                <br>
                <table width="100%">
                    <tr>
                        <td width="50%">
                        <?php } else { ?>  
                        <td width="50%">
                        <?php } ?>
                        <table>
                            <tr>
                                <td width="50%" style="padding-left:30px;">
                                    <table class="table-hover">
                                        <tr>    
                                            <td class="padd fontsize"><b>ID NO.&nbsp;:&nbsp;<?php echo $pax[paxid]; ?></b></td>
                                        </tr>    
                                        <tr>    
                                            <td class="padd fontsize"><b>PASSPORT NO.&nbsp;:&nbsp;<?php echo $pax[passportno]; ?></b></td>
                                        </tr>  
                                        <tr>  
                                            <td class="padd fontsize"><b><?php echo $pax[surname] . " " . $pax[name] . " " . $pax[midname]; ?>(SELF)</b></td>
                                        </tr>
                                        <?php
                                        if ($pax[gender] == "M") {

                                            $getmeh = mysqli_query($conn, "SELECT `surname`, `relation`, `midname`, `name`, `mehram_relation_code`
                                                                   FROM `pax` WHERE `mehram_no` = '$pax[paxid]' ORDER BY `paxid` LIMIT 0,4 ");
                                            $mehr = mysqli_num_rows($getmeh);

                                            if ($mehr > 0) {
                                                while ($meh = mysqli_fetch_array($getmeh)) {

                                                    $getrel = mysqli_query($conn, "SELECT `code`,`name` FROM $zroot.`relation` WHERE `code` = '$meh[relation]' LIMIT 0,1")or die(mysqli_error($conn));
                                                    $rel = mysqli_fetch_array($getrel);
                                                    if ($meh[chkmarried] == "T") {
                                                        echo "<tr>";
                                                        echo "<td class='padd' fontsize><i>$meh[surname]  $meh[name] $meh[midname]  ($rel[name])</i></td>";
                                                        echo "</tr>";
                                                    } else {
                                                        echo "<tr>";
                                                        echo "<td class='padd' fontsize><i>$meh[surname] $meh[name]  $meh[midname] ($rel[name])</i></td>";
                                                        echo "</tr>";
                                                    }
                                                }
                                            }
                                        } else {

                                            $getmeh = mysqli_query($conn, "SELECT `surname`, `midname`, `name`, `mehram_relation_code` 
                                                                   FROM `pax` WHERE `paxid` = '$lbl[mehram_no]' ORDER BY `paxid` 
                                                                   LIMIT 0, 1 ")or die(mysqli_error($conn));
                                            while ($meh = mysqli_fetch_array($getmeh)) {
                                                $getrel = mysqli_query($conn, "SELECT `name` FROM `relation` WHERE `code` = '$meh[relation]' LIMIT 0,1");
                                                $rel = mysqli_fetch_array($getrel);

                                                echo "<tr>";
                                                echo "<td class='padd' fontsize><i>$meh[surname] $meh[name] $meh[midname] ($rel[name])</i></td>";
                                                echo "</tr>";
                                            }
                                        }
                                        ?>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </td>
                    <?php
                    if ($ctr == 2) {
                        echo "</tr>";
                        echo "</table>";
                        $ctr = 0;
                    }
                    if ($lblctr >= 8) {
                        $lblctr = 0;
                        echo "</div>";
                    }
                }
                 $nstart = $getno + 1;
                if ($td2 > 0) {
                    for ($n = 1; $n <= $getno; $n++) {

                        $ctr++;
                        $lblctr++;
                        
                        if ($ctr == 1) {
                ?>
                <br>
                <br>
                <br>
                <br>
                <br>
                <br>
                <table width="100%">
                    <tr>
                        <td width="50%">
                        <?php } else { ?>  
                        <td width="50%">
                        <?php } ?>
                        <table>
                            <tr>
                                <td width="50%" style="padding-left:30px;">
                                    <table>
                                        <tr>    
                                            <td></td>
                                        </tr>    
                                        <tr>    
                                            <td></td>
                                        </tr>  
                                        <tr>  
                                            <td></td>
                                        </tr>
                                        <?php
                                        if ($pax[gender] == "M") {

                                                        echo "<tr>";
                                                        echo "<td class='padd' fontsize><i></i></td>";
                                                        echo "</tr>";
                                        } else {

                                                echo "<tr>";
                                                echo "<td class='padd' fontsize></td>";
                                                echo "</tr>";
                                        }
                                        ?>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </td>
                </body>
                </html>
                <?php
              if ($ctr == 2) {
                                echo "</tr>";
                                $ctr = 0;
                                echo "</table>";
                                echo "<div>&nbsp; </div> ";
                            }
                            if ($lblctr >= 8) {
                                $lblctr = 0;
                                echo "</div>";
                            }
                        }
                    }
                }
            
            if (isset($_POST["subpdf"])) {

                /* Setting Up Condition for the Report */
                $ncond = "";
                if (!empty($_POST["name"])) {
                    $name = trim($_POST["name"]);
                    $ncond = " AND `name` LIKE '%$name%' ";
                }

                $dcond = "";
                if (!empty($_POST["district"])) {
                    $district = trim($_POST["district"]);
                    $dcond = " AND `district` = '$district' ";
                }

                $scond = "";
                if (!empty($_POST["state"])) {
                    $state = trim($_POST["state"]);
                    $scond = " AND `state` = '$state' ";
                }

                $fcond = "";
                if (!empty($_POST["for"])) {
                    $for = trim($_POST["for"]);
                    $fcond = " AND `visitingfor` = '$for' ";
                }

                $ccond = "";
                if (!empty($_POST["class"])) {
                    $class = trim($_POST["class"]);
                    $ccond = " AND `class` LIKE '%$class%' ";
                }

                $bcond = "";
                if (!empty($_POST["batch"])) {
                    $batch = $_POST["batch"];
                    $bcond = " AND `paxid` IN (SELECT `paxno` FROM `airline_detail` WHERE `batchno`= '$batch') ";
                }


                $paxcond = "";
                if (!empty($_POST["frompax"]) && !empty($_POST["topax"])) {
                    $frompax = $_POST["frompax"];
                    $topax = $_POST["topax"];
                    $paxcond = " AND `paxid` BETWEEN '$frompax' AND '$topax'";
                }

                $hajicond = "";
                if (!empty($_POST["fromhaji"]) && !empty($_POST["tohaji"])) {
                    $fromhaji = $_POST["fromhaji"];
                    $tohaji = $_POST["tohaji"];
                    $paxcond = " AND `hajino` BETWEEN '$fromhaji' AND '$tohaji'";
                }

                $air_code = "";
                if (!empty($_POST["airline"])) {
                    $mair = $_POST["airline"];
                    $air_code = " AND `paxid` IN (SELECT `paxno` FROM `airline_detail` WHERE `airline_code`= '$mair') ";
                }

                $jb_date = "";
                if (!empty($_POST["arrvdate"])) {
                    $mjbdt = $_POST["arrvdate"];
                    $jb_date = " AND `paxid` IN (SELECT `paxno` FROM `airline_detail` WHERE `dept_date`= '$mjbdt' AND `srno` = 2 ) ";
                }


                $vcond = "";
                if (!empty($_POST["siscono"])) {
                    $siscomp = trim($_POST["siscono"]);
                    $vcond = " AND `company` = '$siscomp' ";
                }

                $tourdb = DBSUFFIX . $_SESSION["cono"] . "_" . $_SESSION["tourname"];
                mysqli_select_db($conn, $tourdb);
                $sortby = $_POST["sortby"];
                $get = mysqli_query($conn, "SELECT `passportno`, `paxid`, `hajino`, `surname`, `name`, `midname`, `suffix`, `gender`, `mehram_no`, `mehram_relation_code` FROM `pax` WHERE `name` IS NOT NULL $bcond $paxcond $hajicond $ncond $dcond $scond $fcond $ccond $vcond $air_code $jb_date ORDER BY $sortby ") or die(mysqli_error($conn));

                require_once('library/fpdf/fpdf.php');


                $pdf = new FPDF('P', 'mm', 'A4');
                $pdf->AddPage();
                $a = 1;
                $ctr = 0;
                $py = 10;
                while ($lbl = mysqli_fetch_assoc($get)) {
                    $ctr++;
                    // image
                    if ($a == 1) {

                        $pdf->SetXY("20", $py);
                        $pdf->SetFont('Arial', '', 9);
                        $pdf->Cell("80", "0", $lbl["paxid"], "0");
                        $pdf->Ln(5);
                        $pdf->SetX("20");
                        $pdf->Cell("80", "0", "ID. NO.: ", "0");
                        $pdf->Ln(5);
                        $pdf->SetX("20");
                        $pdf->Cell("80", "0", "PASSPORT NO.: " . $lbl["passportno"], "0");
                        $pdf->Ln(5);
                        $pdf->SetX("20");
                        $pdf->SetFont('Arial', 'B', 9);
                        $pdf->Cell("100", "0", $lbl["surname"] . " " . $lbl["name"] . " " . $lbl["midname"] . " (SELF)", "0");

                        if ($lbl["gender"] === "M") {
                            $tourdb = DBSUFFIX . $_SESSION["cono"] . "_" . $_SESSION["tourname"];
                            mysqli_select_db($conn, $tourdb);
                            $getmeh = mysqli_query($conn, "SELECT `surname`, `relation`, `midname`, `name`, `mehram_relation_code` FROM `pax` WHERE `mehram_no` = '$lbl[paxid]' ORDER BY `paxid` LIMIT 0,4 ");
                            if (mysqli_num_rows($getmeh)) {
                                while ($meh = mysqli_fetch_array($getmeh)) {
                                    mysqli_select_db($conn, MYSQLDB);
                                    $getrel = mysqli_query($conn, "SELECT `name` FROM `relation` WHERE `code` = '$meh[relation]' LIMIT 0,1");
                                    $rel = mysqli_fetch_array($getrel);
                                    mysql_free_result($getrel);
                                    $pdf->SetFont('Arial', 'I', 8);
                                    $pdf->Ln(5);
                                    $pdf->SetX("20");
                                    $pdf->Cell("80", "0", $meh["surname"] . " " . $meh["name"] . " " . $meh["midname"] . " (" . $rel["name"] . ")", "0");
                                    unset($rel);
                                }
                                unset($meh);
                            }
                            mysql_free_result($getmeh);
                        } else {
                            $tourdb = DBSUFFIX . $_SESSION["cono"] . "_" . $_SESSION["tourname"];
                            mysqli_select_db($conn, $tourdb);
                            $getmeh = mysqli_query($conn, "SELECT `surname`, `midname`, `name`, `mehram_relation_code` FROM `pax` WHERE `paxid` = '$lbl[mehram_no]' ORDER BY `paxid` LIMIT 0,1 ") or die(mysqli_error($conn));
                            if (mysqli_num_rows($getmeh)) {
                                $meh = mysqli_fetch_array($getmeh);
                                mysqli_select_db($conn, MYSQLDB);
                                $getrel = mysqli_query($conn, "SELECT `name` FROM `relation` WHERE `code` = '$lbl[mehram_relation_code]' LIMIT 0,1");
                                $rel = mysqli_fetch_array($getrel);
                                mysql_free_result($getrel);
                                $pdf->Ln(5);
                                $pdf->SetX("20");
                                $pdf->SetFont('Arial', 'BI', 8);
                                $pdf->Cell("80", "0", $meh["surname"] . " " . $meh["name"] . " " . $meh["midname"] . " (" . $rel["name"] . ")", "0");
                                unset($rel);
                                unset($meh);
                            }
                            mysql_free_result($getmeh);
                        }

//            $pdf->SetFont('Arial', 'I', 6);
//            $pdf->Ln(5);
//            $pdf->SetX("20");
//            $pdf->Cell("100", "0", "H. No.:" . $lbl["hajino"] . " | P. No.:" . $lbl["paxid"], "0");
                        $a = 50;
                    } else {

                        $pdf->SetFont('Arial', '', 9);
                        $pdf->SetXY("130", $py);
                        $pdf->Cell("80", "0", $lbl["paxid"], "0");
                        $pdf->Ln(5);
                        $pdf->SetX("130");
                        $pdf->Cell("80", "0", "ID. NO.: ", "0");
                        $pdf->Ln(5);
                        $pdf->SetX("130");
                        $pdf->Cell("80", "0", "PASSPORT NO.: " . $lbl["passportno"], "0");
                        $pdf->Ln(5);
                        $pdf->SetX("130");
                        $pdf->SetFont('Arial', 'B', 9);
                        $pdf->Cell("100", "0", $lbl["surname"] . " " . $lbl["name"] . " " . $lbl["midname"] . " (SELF)", "0");
                        if ($lbl["gender"] === "M") {
                            $tourdb = DBSUFFIX . $_SESSION["cono"] . "_" . $_SESSION["tourname"];
                            mysqli_select_db($conn, $tourdb);
                            $getmeh = mysqli_query($conn, "SELECT `surname`, `relation`, `midname`, `name`, `mehram_relation_code` FROM `pax` WHERE `mehram_no` = '$lbl[paxid]' ORDER BY `paxid` LIMIT 0,4 ");
                            if (mysqli_num_rows($getmeh)) {
                                $pdf->SetFont('Arial', 'I', 8);
                                while ($meh = mysqli_fetch_array($getmeh)) {
                                    mysqli_select_db($conn, MYSQLDB);
                                    $getrel = mysqli_query($conn, "SELECT `name` FROM `relation` WHERE `code` = '$meh[relation]' LIMIT 0,1");
                                    $rel = mysqli_fetch_array($getrel);
                                    mysql_free_result($getrel);
                                    $pdf->Ln(5);
                                    $pdf->SetX("130");
                                    $pdf->Cell("80", "0", $meh["surname"] . " " . $meh["name"] . " " . $meh["midname"] . " (" . $rel["name"] . ")", "0");
                                    unset($rel);
                                }
                                unset($meh);
                            }
                            mysql_free_result($getmeh);
                        } else {
                            $tourdb = DBSUFFIX . $_SESSION["cono"] . "_" . $_SESSION["tourname"];
                            mysqli_select_db($conn, $tourdb);
                            $getmeh = mysqli_query($conn, "SELECT `surname`, `midname`, `name`, `mehram_relation_code` FROM `pax` WHERE `paxid` = '$lbl[mehram_no]' ORDER BY `paxid` LIMIT 0,1 ");
                            if (mysqli_num_rows($getmeh)) {
                                $meh = mysqli_fetch_array($getmeh);
                                mysqli_select_db($conn, MYSQLDB);
                                $getrel = mysqli_query($conn, "SELECT `name` FROM `relation` WHERE `code` = '$lbl[mehram_relation_code]' LIMIT 0,1");
                                $rel = mysqli_fetch_array($getrel);
                                mysql_free_result($getrel);
                                $pdf->Ln(5);
                                $pdf->SetX("130");
                                $pdf->SetFont('Arial', 'I', 8);
                                $pdf->Cell("80", "0", $meh["surname"] . " " . $meh["name"] . " " . $meh["midname"] . " (" . $rel["name"] . ")", "0");
                                unset($rel);
                                unset($meh);
                            }
                            mysql_free_result($getmeh);
                        }

//            $pdf->SetFont('Arial', 'I', 6);
//            $pdf->Ln(5);
//            $pdf->SetX("130");
//            $pdf->Cell("100", "0", "H. No.:" . $lbl["hajino"] . " | P. No.:" . $lbl["paxid"], "0");
                        $a = 1;
                        $py += 60;
                    }

                    if ($ctr >= 10) {
                        $pdf->AddPage();
                        $pdf->SetXY(0, 0);
                        $ctr = 0;
                        $py = 10;
                    }

                    // Other Details
                }
                $pdf->Output("Sticker Print.pdf", 'D');


                unset($pdf);
                unset($lbl);
                mysql_free_result($get);
            }
            ?>
    