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

$maindb = "visamain";
$trandb = "visatran";
//$trandb1 = "category";
$formname = "Service ";
$file = "service_fr.php";
// $mgrpcode = "010";
//$entfile = "entry.php";

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

//    $name = $_POST[name];
//    $manualno = $_POST["manualno"];
$frmdate = $_POST["frmdate"];
$todate = $_POST["todate"];
//$plbirthr = $_POST["plbirth"];

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

$manualnocond = '';
$visatypecond = '';
$visanocond = '';
$passportcond = '';
$partynamecond = '';
$sponsercond = '';
$contactcond = '';
$mobilecond = '';
$agentcond = '';
$dtypecond = "";
// $mtypecond = "";

if (!empty($manualno)) {
    $manualnocond = " AND a.`manualno`='$manualno' ";
}
if (!empty($passport)) {
    $passportcond = " AND b.`passport` ='$passport' ";
}
if (!empty($partyname)) {
    $partynamecond = " AND a.`partyname` LIKE '$partyname%' ";
}
if (!empty($sponser)) {
    $sponsercond = " AND a.`sponser` LIKE '$sponser%' ";
}
if (!empty($visano)) {
    $visanocond = " AND a.`visano` = '$visano' ";
}
if (!empty($visatype)) {
    $visatypecond = " AND a.`visatype` = '$visatype' ";
}
if (!empty($contact)) {
    $contactcond = " AND a.`contact` = '$contact' ";
}
if (!empty($mobile)) {
    $mobilecond = " AND a.`mobile` = '$mobile' ";
}
if (!empty($agent)) {
    $agentcond = " AND a.`broker` = '$agent' ";
}
if (empty($_POST["frmdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; register_sc.php");
}
if (empty($_POST["todate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; register_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.*, DATE_FORMAT(a.vouchdt, '%d-%m-%Y') as mydt , b.*, DATE_FORMAT(b.`imigdate`, '%d-%m-%Y') as imigdate FROM `$maindb` a, `$trandb` b WHERE a.vouchno = b.myvouch and a.visatype = b.visatype and
                                 a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $visatypecond $agentcond ") or die(mysqli_error($conn));

//    browse($partynamecond);
if (isset($_POST["view"])) {

    //print table($getpmain);
    //die();
    $get = mysqli_num_rows($getpmain);
    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="" style="width: 5%">Sr</th>
                            <th class="" style="width: 10%">File Date</th>
                            <th class="" style="width: 10%">Staff</th>
                            <th class="" style="width: 7%">Visa Type</th>
                            <th class="" style="width: 10%">File No.</th>
                            <th class="" style="width: 10%">Direct/Indirect</th>
                            <th class="" style="width: 10%">Branch</th>
                            <th class="text-right" style="width: 10%">Visa Number</th>
                            <th class="" style="width: 10%">Agent Name</th>
                            <th class="" style="width: 10%">Qty.</th>
                        </tr>
                        <?php

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

                            if ($pmain['mofano2'] > 0) {
                                $mofa = $pmain['mofano2'];
                            } else {
                                $mofa = $pmain['mofano'];
                            }
                           
                            if ($pmain['stampstatus'] == 'Y' ) {
                                $vistatus = 'Yes';
                            } elseif ($pmain['stampstatus'] == 'N' ) {
                                $vistatus = 'No';
                            }

                            echo "<tr>";
                            echo "<td>$ctr</td>";
                            echo "<td>$pmain[mydt]</td>";
                            echo "<td>$pmain[adduser]</td>";
                            echo "<td>$vistatus</td>";
                            echo "<td>$pmain[manualno]</td>";
                            echo "<td class=''>$pmain[enttype]</td>";
                            echo "<td class=''>$pmain[sector]</td>";
                            echo "<td class='text-right'>$pmain[visano]</td>";
                            echo "<td class=''>$pmain[broker]</td>";
                            echo "<td class=''>$pmain[visaqty]</td>";
                            echo "</tr>";
                            $ctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>
    </body>
    <?php
}



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

    $contents .= "Sr, Date, Pax Name, Passport No, Agent, Mofa, Job Id, Category, Mobile Number, Immigration Clear Date \n";

    $ctr = 0;
    $ctr2 = 0;

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

        $imig = $pmain['imig'];
        $stamp = $pmain['stamp'];
        $mis = $pmain['mis'];
        $mofano = $pmain['mofa'];
        $misc = $pmain['misc'];
        $total = $pmain['total'];
        $disc = $pmain['disc'];
        $tot = $pmain['tot'];

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

        $ctr++;
        $contents .= $ctr . ",";
        $contents .= $pmain['subdate'] . ",";
        $contents .= $pmain['paxname'] . ",";
        $contents .= $pmain['passport'] . ",";
        $contents .= $pmain['broker'] . ",";
        $contents .= $pmain['mofano'] . ",";
        $contents .= $pmain['jobid'] . ",";
        $contents .= $pmain['category'] . ",";
        //        $contents .=$pmain[sponsor] . ",";
        $contents .= $pmain['mobileno'] . ",";
        $contents .= $dlvstatus . ",";
        $contents .= $pmain['imgdate'] . "\n";
    }

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

    $gtot = $totamt + $totamt1 + $totamt2 + $totamt3;

    $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;
    }
}
//C:\Users\Admin\DownloadsC