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

$maindb = "accmast";
$formname = "School Labels";
$file = "rpschool_fr.php";

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

if (isset($_POST["view"])) {

    $medium = $_POST[medium];
    $center = $_POST[center];
    $city = $_POST[city];
    $custcode = $_POST[custcode];

    $medmcond = "";
    $centcond = "";
    $citycond = "";
    $custcond = "";

    if (!empty($center)) {
        $centcond = "AND `centcode` = '$center' ";
    }

    if (!empty($medium)) {
        $medmcond = " AND `medmcode` = '$medium' ";
    }

    if (!empty($city)) {
        $citycond = " AND `city` = '$city' ";
    }

    if (!empty($custcode)) {
        $custcond = " AND `custcode` = '$custcode' ";
    }

    $cmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE  `grpcode` = '010'  $medmcond $centcond $citycond ")or die(mysqli_error($conn));
    $getno = mysqli_num_rows($cmain);


    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: 3px;
        }
        .fontsize{
            font-size:12px;
        }
        .page {
            width: 210mm;
            min-height: 290mm;
            padding: 1mm;
            margin: 10mm auto;
            border: 1px #D3D3D3 solid;
            border-radius: 5px;
            background: white;
        }
        .subpage {
            padding-right: 0.5cm;
            padding-left: 0.5cm;
            /* padding: 0cm; */
            /* border: 1px solid; */
            height: 278mm;
            outline: 2cm #ffffff solid;
        }
    </style>
    </head>
    <body> 

        <?php
        $ctr = 0;
        $counter = 0;

        if ($getno < 2) {
            $td2 = 2 - $getno;
        } else {
            $td2 = fmod($getno, 2) + 1;
        }

        while ($lbl = mysqli_fetch_array($cmain)) {
            $barcode = $lbl[barcode];
            $ctr++;
            $lblctr++;
            if ($lblctr == 1) {
                echo "<div class='page' style=''>";
                echo "<div class='subpage' style=''>";
            }
            ?>

            <?php if ($ctr == 1) {
                ?>
                <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:40px;">
                                    <table class="table-hover">
                                        <tr>
                                            <td class="padd fontsize"><b><?php echo $lbl["custcode"] . " - " . $lbl['name']; ?></b></td>
                                        </tr>    
                                        <tr>    
                                            <td class="padd fontsize"><?php echo $lbl["add1"] . " " . $lbl["add2"] . " " . $lbl["add3"]; ?></td>
                                        </tr>    
                                        <tr>
                                            <td class="padd fontsize"><?php echo $lbl['station']; ?></td>
                                        </tr>  
                                        <tr>
                                            <td class="padd fontsize"><?php echo $lbl["city"] . " " . $lbl["pin"]; ?></td>
                                        </tr>
                                        <?php if ($lbl['tel'] != '') {
                                            ?>
                                            <tr>
                                                <td class="padd fontsize"><?php echo "Tel" . " - " . $lbl['tel']; ?></td>
                                            </tr>
                                        <?php } ?>
                                        <?php if ($lbl['mobile'] != '') {
                                            ?>
                                            <tr>
                                                <td class="padd fontsize"><?php echo "Mob" . "&nbsp;" . "-" . "&nbsp;" . $lbl['mobile']; ?></td>
                                            </tr>
                                        <?php } ?>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </td>
                    <?php
                    if ($ctr == 2) {
                        echo "</tr>";
                        echo "</table>";
                        $ctr = 0;
                    }
                    if ($lblctr >= 8) {
                        $lblctr = 0;
                        echo "</div>";
                        echo "</div>";
                    }
                }

                $nstart = $getno + 1;
                if ($td2 > 0) {
                    for ($n = 1; $n <= $getno; $n++) {

                        $ctr++;
                        $lblctr++;


                        if ($ctr == 1) {
                            ?>
                        <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:40px;">
                                            <table>
                                                <tr>
                                                    <td><b></b></td>
                                                </tr>    
                                                <tr>    
                                                    <td></td>
                                                </tr>    
                                                <tr>    
                                                    <td><b></b></td>
                                                </tr>  
                                                <tr>  
                                                    <td></td>
                                                </tr>
                                                <tr>
                                                    <td></td>
                                                </tr>

                                                <tr>
                                                    <td>
                                                    </td>
                                                </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"])) {

    $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"];
    $topax = $_POST["topax"];
    $fromhaji = $_POST["fromhaji"];
    $tohaji = $_POST["tohaji"];
    $siscono = $_POST["siscono"];
    $mair = $_POST["airline"];
    $mjbdt = $_POST["arrvdate"];
    $mflt = $_POST["flight"];

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

    if (!empty($name)) {
        $ncond = " AND `name` LIKE '%$name%' ";
    }
    if (!empty($district)) {
        $dcond = " AND `district` = '$district' ";
    }
    if (!empty($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' GROUP BY `paxno`) ";
    }
    if (!empty($_POST["frompax"]) && !empty($_POST["topax"])) {
        $paxcond = " AND `paxid` BETWEEN '$frompax' AND '$topax'";
    }
    if (!empty($_POST["fromhaji"]) && !empty($_POST["tohaji"])) {
        $hajicond = " 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["siscono"])) {
        $compcond = " AND `company` = '$siscono'";
    }
    if (!empty($_POST["flight"])) {
        $fltcode = " AND `paxid` IN (SELECT `paxno` FROM `airline_detail` WHERE `flightno`= '$mflt') ";
    }
    if (!empty($_POST["arrvdate"])) {
        $jb_date = " AND `paxid` IN (SELECT `paxno` FROM `airline_detail` WHERE `dept_date`= '$mjbdt' AND `srno` = 2 ) ";
    }

    $sortby = " ORDER BY " . $_POST["sortby"];



    $get = mysqli_query($conn, "SELECT `paxid`,`hajino`, `visitingfor`, `surname`, `name`, `midname`, `suffix`, `address`,
                   `city`,`taluka`, `pin`, `district`, `state`, `telno`, `mobile` FROM `pax` WHERE `familyhead` = 'T'
                   $bcond $paxcond 
        $hajicond $ncond 
        $dcond $scond 
         $fcond $ccond
         $air_code $fltcode
         $jb_date $sortby LIMIT 2") or die(mysqli_error($conn));
    $getno = mysqli_num_rows($get);

    $get2 = mysqli_query($conn, "SELECT `paxid`,`hajino`, `visitingfor`, `surname`, `name`, `midname`, `suffix`, `address`,
                   `city`,`taluka`, `pin`, `district`, `state`, `telno`, `mobile` FROM `pax` WHERE `familyhead` = 'T'
                   $bcond $paxcond 
        $hajicond $ncond 
        $dcond $scond 
         $fcond $ccond
         $air_code $fltcode
         $jb_date $sortby ") or die(mysqli_error($conn));
    $getno2 = mysqli_num_rows($get2);


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

    $pdf = new FPDF('P', 'mm', 'A4');
    $pdf->AddPage();
    $py = 10;
    $ctr = 0;
    $set = 3;
    $y = 10;
    $add2y = 0;
    $add3y = 0;


    while ($lbl2 = mysqli_fetch_array($get2)) {

        $add1 = substr($lbl2['address'], 0, 40);
        $add2 = substr($lbl2['address'], 41, 80);
        $add3 = substr($lbl2['address'], 81, 120);

        if ($y >= 240) {
            $pdf->AddPage();
            $y = 10;
        }
        $barcode = $lbl2['barcode'];
        $ctr++;
        $lbl2ctr++;
        if (($set % 2) == 1) {
            $pdf->SetXY("20", $y);
            $y = $y + 7;
        } else {
            $pdf->SetXY("120", $y);
            $y = $y + 7;
        }
        $pdf->SetFont('Arial', 'B', 9);
        $pdf->Cell("80", "0", $lbl2["suffix"] . " " . $lbl2["surname"] . " " . $lbl2["name"] . " " . $lbl2["midname"], "0");
        $pdf->Ln(5);



        $pdf->SetFont('Arial', '', 9);
        if (($set % 2) == 1) {
            $pdf->SetXY("20", $y);
            $y = $y + 7;
        } else {
            $pdf->SetXY("120", $y);
            $y = $y + 7;
        }
        $pdf->Cell("80", "0", $add1, "0");
        $pdf->Ln(5);



        if (($set % 2) == 1) {
            $pdf->SetXY("20", $y);
            $y = $y + 7;
        } else {
            $pdf->SetXY("120", $y);
            $y = $y + 7;
        }
        if ($add2 != '') {
            $pdf->Cell("80", "0", $add2, "0");
            $pdf->Ln(5);
            $pdf->SetFont('Arial', 'B', 9);
            if (($set % 2) == 1) {
                $pdf->SetXY("20", $y);
                $y = $y + 7;
            } else {
                $pdf->SetXY("120", $y);
                $y = $y + 7;
            }
            $add2y = 7;
        }

        if ($add3 != '') {
            $pdf->Cell("80", "0", $add3, "0");
            $pdf->Ln(5);
            $pdf->SetFont('Arial', 'B', 9);
            if (($set % 2) == 1) {
                $pdf->SetXY("20", $y);
                $y = $y + 7;
            } else {
                $pdf->SetXY("120", $y);
                $y = $y + 7;
            }
            $add3y = 7;
        }
        $pdf->SetFont('Arial', 'B', 9);
        $pdf->Cell("80", "0", "At " . "- " . $lbl2["city"] . "    " . "Pin" . "-" . " " . $lbl2["pin"], "0");
        $pdf->Ln(5);


        if (($set % 2) == 1) {
            $pdf->SetXY("20", $y);
            $y = $y + 7;
        } else {
            $pdf->SetXY("120", $y);
            $y = $y + 7;
        }
        $pdf->SetFont('Arial', '', 9);

        $pdf->Cell("100", "0", "Tal" . " " . "-" . " " . $lbl2["taluka"] . "    " . "Dist" . " " . "-" . " " . $lbl2["district"], "0");
        $pdf->Ln(5);



        if (($set % 2) == 1) {
            $pdf->SetXY("20", $y);
            $y = $y + 7;
        } else {
            $pdf->SetXY("120", $y);
            $y = $y + 7;
        }
        $pdf->Cell("80", "0", $lbl2["state"] . "     " . "Mob" . " " . "-" . " " . $lbl2["mobile"], "0");
        $pdf->Ln(5);



        if (($set % 2) == 1) {
            $pdf->SetXY("20", $y);
            $y = $y + 7;
        } else {
            $pdf->SetXY("120", $y);
            $y = $y + 7;
        }
        $pdf->Cell("80", "0", "P" . " " . "-" . " " . $lbl2[paxid] . " " . "/" . " " . "H" . " " . "-" . " " . $lbl2[hajino] . " " . "/" . " " . $lbl2[visitingfor] . " " . "/" . " " . $myyear, "0");
        $pdf->Ln(5);



        if (($set % 2) == 1) {
            $pdf->SetXY("20", $y);
            $y = $y + 7;
        } else {
            $pdf->SetXY("120", $y);
            $y = $y + 7;
        }
        $pdf->Cell("80", "0", $myyear . "" . $lbl2[paxid], "0");
//                        $pdf->Cell("80", "0", "$barcode", "0");
//                        $pdf->Code128(80, 0, $barcode, 32, 6);
        $pdf->Ln(5);
        if (($set % 2) == 1) {
            $pdf->SetXY("20", $y);
            $y = $y + 7;
            $y = $y - 56;
            $y = $y - $add2y;
            $y = $y - $add3y;
        } else {
            $pdf->SetXY("120", $y);
            $y = $y + 30;
        }

        $set++;
    }

    $pdf->Output("Address Labels (Jeddah - Mumbai).pdf", 'D');
}
?>