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

$maindb = "courout";
$formname = "Courier Out";
$file = "couroutreg_fr.php";
$entfile = "entry.php";

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

$frmdate = $_POST["frmdate"];
$todate = $_POST["todate"];
$paxname = $_POST["paxname"];
$passport = $_POST["passport"];
$medical = $_POST["medical"];
$license = $_POST["license"];
$photo = $_POST["photo"];
$recdfrom = $_POST["recdfrom"];
$recdby = $_POST["recdby"];
$dtype = $_POST["dtype"];

$paxnamecond = '';
$passportcond = '';
$medicalcond = '';
$licensecond = '';
$photocond = '';
$recdfromcond = '';
$recdbycond = '';

if (!empty($paxname)) {
    $paxnamecond = " AND `paxname` = '$paxname' ";
}

if (!empty($passport)) {
    $passportcond = " AND `passport` = '$passport' ";
}

if ($medical == 1) {
    $medicalcond = " AND `medical` = 1 ";
} elseif ($medical == 2) {
    $medicalcond = " AND `medical` = 0 ";
} else {
    $medicalcond = "";
}

if ($license == 1) {
    $licensecond = " AND `license` = 1 ";
} elseif ($license == 2) {
    $licensecond = " AND `license` = 0 ";
} else {
    $licensecond = "";
}

if ($photo == 1) {
    $photocond = " AND `photo` = 1 ";
} elseif ($photo == 2) {
    $photocond = " AND `photo` = 0 ";
} else {
    $photocond = "";
}

if (!empty($recdfrom)) {
    $recdfromcond = " AND `recdfrom` LIKE '%$recdfrom%' ";
}

if (!empty($recdby)) {
    $recdbycond = " AND `recdby` LIKE '%$recdby%' ";
}

if (empty($_POST["fromdate"])) {
    browse("From Date Cannot Be Empty");
    echo "<script>window.close();</script>";
}
if (empty($_POST["todate"])) {
    browse("To Date Cannot Be Empty");
    echo "<script>window.close();</script>";
}



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

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

$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt,DATE_FORMAT(`courdt`, '%d-%m-%Y') as courdt FROM `$maindb` 
                                 WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $paxnamecond  $passportcond $medicalcond 
                                 $licensecond $photocond $recdfromcond $recdbycond order by vouchno") or die(mysqli_error($conn));
$getno2 = mysqli_num_rows($getpmain);

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    </head>
    <style>
        .tblhead > th{
            background-color: #F5F5F5;
            color: #C45254;
            width: 10%;
        }
        

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

            <?php
            $ctr = 1;
            $ctr2 = 0;
            ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-bordered" >
                    <tr class="mySearchTr">
                            <th class="">Voucher</th>
                            <th class="">Date</th>
                            <th class="">Pax Name</th>
                            <th class="">Passport No</th>
                            <th class="">Medical</th>
                            <th class="">License</th>
                            <th class="">Photo</th>
                            <th class="">Old Passport</th>
                            <th class="">To</th>
                            <th class="">Send By</th>
                            <th class="">Courier Date</th>
                            <th class="">Tracking No</th>
                            <th class="">Remarks</th>
                        </tr>
                        <?php
                        $ftot = 0;
                        $mistot = 0;
                        $misctot = 0;
                        $stot = 0;
                        $moftot = 0;
                        $itot = 0;
                        $ttot = 0;
                        $dtot = 0;

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

                    if($pmain['medical'] == 1){
                        $medical = "Yes";
                    }else{
                        $medical = "No";
                    }

                    if($pmain['license'] == 1){
                        $license = "Yes";
                    }else{
                        $license = "No";
                    }

                    if($pmain['photo'] == 1){
                        $photo = "Yes";
                    }else{
                        $photo = "No";
                    }
                    
                    if($pmain['oldpass'] == 1){
                        $oldpass = "Yes";
                    }else{
                        $oldpass = "No";
                    }

                    echo "<tr class='clickable-row' data-href='$entfile?mode=edit&vouchno='$pmain[vouchno]' [target]='_blank'>";
                    echo "<td>$ctr</td>";
                    echo "<td>$pmain[vouchdt]</td>";
                    echo "<td>$pmain[paxname]</td>";
                    echo "<td>$pmain[passport]</td>";
                    echo "<td>$medical</td>";
                    echo "<td>$license</td>";
                    echo "<td>$photo</td>";
                    echo "<td>$oldpass</td>";
                    echo "<td>$pmain[recdfrom]</td>";
                    echo "<td>$pmain[recdby]</td>";
                    echo "<td>$pmain[courdt]</td>";
                    echo "<td>$pmain[trackid]</td>";
                    echo "<td>$pmain[remark1]  $pmain[remark2]</td>";
                    echo "</tr>";

                    $ctr++;

                        }

                        Echo "</table>";

                    }

if (isset($_POST["excel"])) {
                   
    $contents .="Voucher, Date, Pax Name, Passport No, Medical, License, Photo, To, Send By, Courier Date, Tracking No, Remarks\n";
    
$ftot = 0;
$mistot = 0;
$misctot = 0;
$stot = 0;
$moftot = 0;
$itot = 0;
$ttot = 0;
$dtot = 0;

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

if($pmain['medical'] == 1){
$medical = "Yes";
}else{
$medical = "No";
}

if($pmain['license'] == 1){
$license = "Yes";
}else{
$license = "No";
}

if($pmain['photo'] == 1){
$photo = "Yes";
}else{
$photo = "No";
}

$ctr++;
$contents .="$ctr" . ",";
$contents .="$pmain[vouchdt]" . ",";
$contents .="$pmain[paxname]" . ",";
$contents .="$pmain[passport]" . ",";
$contents .="$medical" . ",";
$contents .="$license" . ",";
$contents .="$Photo" . ",";
$contents .="$pmain[recdfrom]" . ",";
$contents .="$pmain[recdby]" . ",";
$contents .="$pmain[courdt]" . ",";
$contents .="$pmain[trackid]" . ",";
$contents .="$pmain[remark1]" . "$pmain[remark2]" . "\n";


}

$contents = strip_tags($contents);
if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
