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

$maindb = 'pax';
$trandb = 'visaent';
$formname = "Visa Arrival Details";
$file = "visaarrvdetl.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>";
}

$country = $_POST["country"];

$countrycond = "";
$countrycond2 = "";

if (!empty($country)) {
    $countrycond = "AND b.country = '$country' ";
    $countrycond2 = "AND a.country = '$country' ";
}

// $getpmain = mysqli_query($conn, "SELECT 'FIT' as source, a.vouchno, a.surname, a.name, a.midname, a.agent,
//                                         b.country, b.vtraveldt, b.rate, b.amount, b.type, b.validity, b.nopax 
//                                         FROM $maindb a, $trandb b 
//                                         WHERE  a.vouchno = b.vouchno 
//                                         AND date(b.`vtraveldt`) >= '$mfrmdate' AND date(b.`vtraveldt`) <= '$mtodate' and a.visaaprov = '1'
//                                         $countrycond 
//                                         order by b.vtraveldt, a.surname, a.name, a.midname
//                                         ") or die(mysqli_error($conn));

$getpmain = mysqli_query($conn, "SELECT a.* FROM 
                                        (SELECT 'FIT' as source, a.vouchno, CONCAT(a.surname, ' ', a.name, ' ', a.midname) AS custname,
                                                b.country, b.vtraveldt as traveldt, b.rate, b.amount, b.type, b.validity, b.nopax,b.groupcode 
                                                FROM $maindb a, $trandb b 
                                                WHERE a.vouchno = b.vouchno 
                                                AND date(b.`vtraveldt`) >= '$mfrmdate' AND date(b.`vtraveldt`) <= '$mtodate' 
                                                and a.visaaprov = '1' $countrycond 
                                        union all
                                        SELECT 'VISA' as source, a.vouchno, b.paxname as custname,  a.country, 
                                               b.traveldt, a.rate, a.totamt as amount, b.type, b.period as validity, a.nopax, a.visagrpcode as groupcode
                                               FROM visanewmain a, visanewtran b 
                                               WHERE a.vouchno = b.vouchno AND date(b.`traveldt`) >= '$mfrmdate' 
                                               AND date(b.`traveldt`) <= '$mtodate' $countrycond2) a
                                        order by traveldt, custname") 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-fluid">
            <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="width:2%">Sr. </td>
                            <td style="width:20%">Country</td>
                            <td style="width:15%">Pax Name</td>
                            <td style="width:4%" class='text-right'>No. Pax</td>
                            <td style="width:6%" class='text-right'>Rate</td>
                            <td style="width:7%" class='text-right'>Amount</td>
                            <td style="width:10%">Visa Type</td>
                            <td style="width:7%">Validity</td>
                            <td style="width:15%">Group Code</td>
                            <td style="width:10%">Visa Travel Date</td>
                            <td style="width:10%">Source</td>
                            <td style="width:10%">Voucher</td>
                        </tr>
                        <?php
                        $ctr = 0;
                        $pp = 0;
                        $tvtraveldt = '0000-00-00';

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

                            $tvtraveldt = $pmain['traveldt'];

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


                            if ($pmain['source'] == "FIT"){
                                $url = "fit_ent.php?mode=edit&paxid=$pmain[vouchno]";
                            }else{
                                $url = "visanew.php?mode=edit&vouchno=$pmain[vouchno]";
                            }

                            $fullname = $pmain['custname'];

                    
                            $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;color:#6A994E'>$fetveh[country]</td>";
                            echo "<td style='font-size:13px;'>$fullname</td>";
                            echo "<td style='font-size:14px;text-align:center'>$pmain[nopax]</td>";
                            echo "<td style='font-size:14px;' class='text-right'>$pmain[rate]</td>";
                            echo "<td style='font-size:14px;' class='text-right'>$pmain[amount]</td>";
                            echo "<td style='font-size:13px;'>$pmain[type]</td>";
                            echo "<td style='font-size:13px;'>$pmain[validity]</td>";
                            echo "<td style='font-size:13px;'>$pmain[groupcode]</td>";
                            echo "<td style='font-size:13px;'>" . format_date($pmain['traveldt']) . "</td>";
                            echo "<td style='font-size:13px;'>$pmain[source]</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. , Country, Pax Name, No. Pax, Rate, Amount, Visa Type, Validity, Group Code, Visa Travel Date, Source, Voucher \n";

    $ctr = 0;
    $pp = 0;
    $tvtraveldt = '0000-00-00';

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

        $tvtraveldt = $pmain['traveldt'];


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

        $fullname = $pmain['custname'];


        $ctr++;
        
        $contents .= "$ctr" . ",";
        $contents .= "$fetveh[country]" . ",";
        $contents .= "$fullname" . ",";
        $contents .= "$pmain[nopax]" . ",";
        $contents .= "$pmain[rate]" . ",";
        $contents .= "$pmain[amount]" . ",";
        $contents .= "$pmain[type]" . ",";
        $contents .= "$pmain[validity]" . ",";
        $contents .= "$pmain[groupcode]" . ",";
        $contents .= format_date($pmain['traveldt']) . ",";
        $contents .= "$pmain[source]" . "\n";
        $contents .= "$pmain[vouchno]" . "\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;
    }
}
?>