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

$maindb = 'pax';
$trandb = 'transent';
$formname = "Transportation Arrival Details";
$file = "transarrvdetl.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];

$tourcode = $_SESSION['tourcode'];
$tourcond = "AND tourno = '$tourcode'";
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$mfrmdate = $_POST["frmdt"];
$mtodate = $_POST["todt"];

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

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

$fromplace = $_POST["fromplace"];
$toplace = $_POST["toplace"];
$type = $_POST["type"];
$vehicle = $_POST["vehicle"];

$fromplacecond = "";
$toplacecond = "";
$vehiclecond = "";

if ($type == "A") {
    $typecond = "AND b.picktype = 'A' ";
} else if ($type == "I") {
    $typecond = "AND b.picktype = 'I' ";
} else if ($type == "D") {
    $typecond = "AND b.picktype = 'D' ";
} else {
    $typecond = "";
}

if (!empty($fromplace)) {
    $fromplacecond = "AND b.fromplace = '$fromplace' ";
}

if (!empty($toplace)) {
    $toplacecond = "AND b.toplace = '$toplace' ";
}
if (!empty($vehicle)) {
    $vehiclecond = "AND b.vehicle = '$vehicle' ";
}

$getpmain = mysqli_query($conn, "SELECT 'FIT' as source, a.vouchno, a.surname, a.name, a.midname, a.agent, b.picktype, b.fromplace, b.toplace, 
                                        b.pickuptime, b.vehicle, b.noveh, b.nopax FROM $maindb a, $trandb b 
                                        WHERE  a.vouchno = b.vouchno AND date(b.`pickuptime`) >= '$mfrmdate' AND date(b.`pickuptime`) <= '$mtodate' and a.transaprov = '1'
                                        $typecond $fromplacecond $toplacecond $vehiclecond order by b.pickuptime,b.picktype,b.fromplace, a.surname, a.name, a.midname") or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }
    </style>

    </head>

    <body>
        <div class="container">
            <div class="row">
                <div class="col-lg-12 ">
                    <?php include("includes/head_rep.php"); ?>

                    <table class="table table-condensed table-bordered" style="width:100%;">

                        <tr class="" style="height: 30px;background-color:#D0F4DE">
                            <td style="font-size:16px;width:2%">Sr. </td>
                            <td style="font-size:16px;width:7%;">Type</td>
                            <td style="font-size:16px;width:15%;">From</td>
                            <td style="font-size:16px;width:15%;">To</td>
                            <td style="font-size:16px;width:9%;">Pickup</td>
                            <td style="font-size:16px;width:4%;">Time</td>
                            <td style="font-size:16px;width:15%;">Vehicle</td>
                            <td style="font-size:16px;width:4%;text-align:center">Pax</td>
                            <td style="font-size:16px;width:4%;text-align:center">No. Vehicle</td>
                            <td style="font-size:16px;width:20%">Name </td>
                            <td style="font-size:16px;width:5%;">Booking</td>
                        </tr>
                        <?php
                        $ctr = 0;
                        $ttotamt = 0;
                        $totsaleamt = 0;
                        $totpnl = 0;
                        $thotel = '';
                        $pp = 0 ;
                        $tpickdate = '00-00-0000';

                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $getagent = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE accode = '$pmain[agent]' ") or die(mysqli_error($conn));
                            $fetagent = mysqli_fetch_array($getagent);

                            if ($pmain['picktype'] == "D") {
                                $flttypen = "Departure";
                            } elseif ($pmain['picktype'] == "A") {
                                $flttypen = "Arrival";
                            } elseif ($pmain['picktype'] == "I") {
                                $flttypen = "Internal";
                            } else {
                                $flttypen = "";
                            }

                            $getveh = mysqli_query($conn, "SELECT `code`,`vehtype` FROM `vehicle` where code='$pmain[vehicle]'") or die(mysqli_error($conn));
                            $fetveh = mysqli_fetch_array($getveh);


                            $url = "fit_ent.php?mode=edit&paxid=$pmain[vouchno]";

                            $fullname = $pmain['surname'] . " " . $pmain['name'] . " " . $pmain['midname'];

                            $pickupdate = date("d-m-Y", strtotime($pmain['pickuptime']));
                            $pickuptime = date("h:i", strtotime($pmain['pickuptime']));

                            if ($pickupdate != $tpickdate) {
                                if ($pp > 0) {
                                    echo "<tr style='height: 30px;'>";
                                    echo "<td colspan='11' ></td>";
                                    echo "</tr>";
                                }
                                $tpickdate = $pickupdate;
                                $pp++ ;
                            }

                            $ctr++;
                            echo "<tr style='height: 30px;' class='clickable-row2' data-href='$url' data-target='_blank' >";
                            echo "<td style='font-size:12px;'>$ctr</td>";
                            echo "<td style='font-size:14px;'>$flttypen</td>";
                            echo "<td style='font-size:13px;'>$pmain[fromplace]</td>";
                            echo "<td style='font-size:13px;'>$pmain[toplace]</td>";
                            echo "<td style='font-size:14px;color:#0176c0'>$pickupdate</td>";
                            echo "<td style='font-size:14px;color:#0176c0'>$pickuptime</td>";
                            echo "<td style='font-size:14px;color:#6A994E'>$fetveh[vehtype]</td>";
                            echo "<td style='font-size:14px;text-align:center'>$pmain[nopax]</td>";
                            echo "<td style='font-size:14px;color:#0176c0;text-align:center'>$pmain[noveh]</td>";
                            echo "<td style='font-size:13px;'>$fullname</td>";
                            echo "<td style='font-size:13px;'>$pmain[vouchno]</td>";
                            echo "</tr>";
                        }
                        ?>

                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
    <?php
}



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


    $contents .= "Sr., Name, Company/Agent, Type, From, To, Pickup Time, Vehicle, Pax, No. of Vehicle";

    $ctr = 0;
    $ttotamt = 0;
    $totsaleamt = 0;
    $totpnl = 0;
    $thotel = '';
    while ($pmain = mysqli_fetch_array($getpmain)) {


        $getagent = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE accode = '$pmain[agent]' ") or die(mysqli_error($conn));
        $fetagent = mysqli_fetch_array($getagent);

        if ($pmain['picktype'] == "D") {
            $flttypen = "Departure";
        } elseif ($pmain['picktype'] == "A") {
            $flttypen = "Arrival";
        } elseif ($pmain['picktype'] == "I") {
            $flttypen = "Internal";
        } else {
            $flttypen = "";
        }

        $getveh = mysqli_query($conn, "SELECT `code`,`vehtype` FROM `vehicle` where code='$pmain[vehicle]'") or die(mysqli_error($conn));
        $fetveh = mysqli_fetch_array($getveh);



        $fullname = $pmain['surname'] . " " . $pmain['name'] . " " . $pmain['midname'];

        $pickuptime = date("d-m-Y h:i:s", strtotime($pmain['pickuptime']));

        $ctr++;

        $contents .= "$ctr" . ",";
        $contents .= "$fullname" . ",";
        $contents .= "$fetagent[name]" . ",";
        $contents .= "$flttypen" . ",";
        $contents .= "$pmain[fromplace]" . ",";
        $contents .= "$pmain[toplace]" . ",";
        $contents .= "$pickuptime" . ",";
        $contents .= "$fetveh[vehtype]" . ",";
        $contents .= "$pmain[nopax]" . ",";
        $contents .= "$pmain[noveh]" . "\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" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
?>