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

$maindb = "visamain";
$trandb = "visatran";
//$trandb1 = "category";
$formname = "Submission";
$file = "submission_fr.php";
// $mgrpcode = "010";
$entfile = "entry.php";
$keycond = "";
$keyfld = "myvouch";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
mysqli_query($conn, "set names 'utf8'");
$frmdate = $_POST["frmdate"];
$todate = $_POST["todate"];

$manualno = $_POST["manualno"];
$visatype = $_POST["visatype"];
$place = $_POST["place"];
$visano = $_POST["visano"];
$passport = $_POST["passport"];
$partyname = $_POST["partyname"];
$sponser = $_POST["sponser"];
$contact = $_POST["contact"];
$mobile = $_POST["mobile"];
$agent = $_POST["agent"];
$dtype = $_POST["dtype"];

$visatypecond = '';
$agentcond = '';
$placecond = '';

if (!empty($visatype)) {
    $visatypecond = " AND a.`visatype` = '$visatype' ";
}
if (!empty($place)) {
    $placecond = " AND a.`sector` = '$place' ";
}
if (!empty($agent)) {
    $agentcond = " AND a.`broker` = '$agent' ";
}
if (empty($_POST["frmdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; submission_sc.php");
}
if (empty($_POST["todate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; submission_sc.php");
}

$mfrmdate = trim($_POST["frmdate"]);
$mtodate = trim($_POST["todate"]);

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));


$getpmain = mysqli_query($conn, "SELECT a.vouchno,a.myvouch, a.manualno, a.broker, a.visano, a.idno, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as vouchdt,
                         b.vouchno, b.passport, b.paxname, b.mofano, b.mofano2, b.category, b.delvstatus, DATE_FORMAT(a.`meddate`,'%d-%m-%Y') as mymeddate, 
                          DATE_FORMAT(b.`stampdate`,'%d-%m-%Y') as stampdate, DATE_FORMAT(b.`submission_date`,'%d-%m-%Y') as subdate
                         FROM `$maindb` a left join `$trandb` b on a.myvouch = b.myvouch where b.`submission_date` >= '$mfrmdate' AND b.`submission_date` <= '$mtodate'
                         $visatypecond $agentcond $placecond ORDER BY a.broker,a.myvouch")or die(mysqli_error($conn));

$get = mysqli_num_rows($getpmain);
//print table($getpmain);
//die;
if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    </head>
    <!--    <script>
        jQuery(document).ready(function($) {
    $(".clickable-row").click(function() {
        window.location = $(this).data("href");
    });
    });
    </script>-->
    <style>
        .tblhead > th{
            background-color: #F5F5F5;
            color: #C45254;
            width: 10%;
        }
        /*        @media print{
                                .pgbrk{
                                    page-break-before: always;
                                }
                }
                * {
                    box-sizing: border-box;
                    -moz-box-sizing: border-box;
                }
                .page {
                    width: 210mm;
                    min-height: 160mm;
                    padding: 4mm;
                    margin: 10mm auto;
                    border: 1px #D3D3D3 solid;
                    border-radius: 5px;
                    background: white;
        
                }
                .subpage {
                    padding: 0cm;
                    border: 1px solid;
                    height: 288mm;
                    outline: 2cm #ffffff solid;
                }
        
                @page {
                    size: A4;
                    margin: 0;
                }
                @media print {
                    html, body {
                        width: 210mm;
                        height: 297mm;
                    }
                    .page {
                        margin: 0;
                        border: initial;
                        border-radius: initial;
                        width: initial;
                        min-height: initial;
                        box-shadow: initial;
                        background: initial;
                        page-break-after: always;
                    }
                }
                .brkpg{
                    page-break-after: always;
                }*/

    </style>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>

            <?php
//            if ($_POST["dtype"] == 1) {
            $ctr = 1;
            $ctr2 = 0;
            ?>
            <!--<div class="page">-->
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-bordered" >
                        <tr  class="mySearchTr" >
                            <th class="" style="width: 3%">Sr</th>
                            <th class="" style="width: 7%">File No.</th>
                            <th class="" style="width: 8%">Agent</th>
                            <th class="" style="width: 7%">Passport No</th> 
                            <th class="" style="width: 13%">Pax Name</th>
                            <th class="" style="width: 5%">Mofa</th>
                            <th class="" style="width: 7%">Visa No.</th>
                            <th class="" style="width: 7%">ID No.</th>
                            <th class="" style="width: 5%">Category</th>
                            <th class="" style="width: 5%">DL</th>
                            <th class="" style="width: 6%">Medical Expiry</th>
                            <th class="" style="width: 6%">SETTING</th>
                            <th class="" style="width: 5%">ECR / ECNR</th>
                            <th class="" style="width: 5%">Visa Stamping</th>
                            <th class="" style="width: 10%">Remarks</th>
                        </tr>
                        <?php
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $myvouch=$pmain['myvouch'];
                            $mymeddate=$pmain['mymeddate'];

                            if ($pmain['delvstatus'] == 'E') {
                                $dlvstatus = "ECR";
                            } elseif ($pmain['delvstatus'] == 'N') {
                                $dlvstatus = "ECNR";
                            } else {
                                $dlvstatus = "";
                            }

                            if ($pmain['mofano2'] > 0) {
                                $mofa = $pmain['mofano2'];
                            } else {
                                $mofa = $pmain['mofano'];
                            }

                            if ($pmain['dl'] == 'Y') {
                                $dl = "YES";
                            } elseif ($pmain['dl'] == 'N') {
                                $dl = "NO";
                            } else {
                                $dl = "";
                            }

//                            if ($pmain['med'] == 'Y') {
//                                $med = "YES";
//                            } elseif ($pmain['med'] == 'N') {
//                                $med = "NO";
//                            } else {
//                                $med = "";
//                            }

                            if ($pmain['exit'] == 'Y') {
                                $exit = "YES";
                            } elseif ($pmain['exit'] == 'N') {
                                $exit = "NO";
                            } else {
                                $exit = "";
                            }
                            echo "<tr class='clickable-row2' data-href='$entfile?myvouch=$myvouch&mode=edit' target='_blank'>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[manualno]</td>";
                            Echo "<td>$pmain[broker]</td>";
                            Echo "<td>$pmain[passport]</td>";
                            Echo "<td>$pmain[paxname]</td>";
                            Echo "<td class=''>$mofa</td>";
                            Echo "<td class=''>$pmain[visano]</td>";
                            Echo "<td class=''>$pmain[idno]</td>";
                            Echo "<td class=''>$pmain[category]</td>";
                            Echo "<td>$dl</td>";
                            Echo "<td>$mymeddate</td>";
                            Echo "<td>$exit</td>";
                            Echo "<td class=''>$dlvstatus</td>";
                            Echo "<td>$pmain[stampdate]</td>";
                            Echo "<td>$pmain[remtran]</td>";
                            Echo "</tr>";

                            $ctr++;
                        }
                        ?>
                </div>
            </div>
        </div>
    </body>
    <?php
}

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

    $contents .= "Sr, File No, Agent, Passport No, Paxname, Mofa, Visa No, ID No, Category, DL, MED, Exit, ECR/ECNR, Visa Stamping, Remarks \n ";

    $ctr = 1;
    $ctr2 = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {

        if ($pmain['delvstatus'] == 'E') {
            $dlvstatus = "ECR";
        } elseif ($pmain['delvstatus'] == 'N') {
            $dlvstatus = "ECNR";
        } else {
            $dlvstatus = "";
        }

        if ($pmain['mofano2'] > 0) {
            $mofa = $pmain['mofano2'];
        } else {
            $mofa = $pmain['mofano'];
        }
        if ($pmain['dl'] == 'Y') {
            $dl = "YES";
        } elseif ($pmain['dl'] == 'N') {
            $dl = "NO";
        } else {
            $dl = "";
        }

        if ($pmain['med'] == 'Y') {
            $med = "YES";
        } elseif ($pmain['med'] == 'N') {
            $med = "NO";
        } else {
            $med = "";
        }

        if ($pmain['exit'] == 'Y') {
            $exit = "YES";
        } elseif ($pmain['exit'] == 'N') {
            $exit = "NO";
        } else {
            $exit = "";
        }

        $contents .= $ctr . ",";
        $contents .= $pmain['manualno'] . ",";
        $contents .= $pmain['broker'] . ",";
        $contents .= $pmain['passport'] . ",";
        $contents .= $pmain['paxname'] . ",";
        $contents .= $mofa . ",";
        $contents .= $pmain['visano'] . ",";
        $contents .= $pmain['idno'] . ",";
        $contents .= $pmain['category'] . ",";
        $contents .= $dl . ",";
        $contents .= $med . ",";
        $contents .= $exit . ",";
        $contents .= $dlvstatus . ",";
        $contents .= $pmain['stampdate'] . ",";
        $contents .= $pmain['remtran'] . "\n";
        $ctr++;
    }
    $contents = strip_tags($contents);
    if ($_POST["dtype"] == 1) {
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        print chr(255) . chr(254).mb_convert_encoding($contents, 'UTF-16LE', 'UTF-8');
        // header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        // print $contents;
    } else {
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        print chr(255) . chr(254).mb_convert_encoding($contents, 'UTF-16LE', 'UTF-8');
        // header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        // print $contents;
    }
}
?>
