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

$maindb = "visamain";
$trandb = "visatran";
// $trandb1 = "category";
$formname = "Non-Submission List";
$file = "nonsubmission_fr.php";

$keycond = "";
$keyfld = "myvouch";

$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"];

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));

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

    $getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as vouchdt,
                                     b.`vouchno`, b.`passport`,b.`paxname`, b.`memcash`, b.mofano, b.mofano2, b.submission_date FROM `$maindb` a left join `$trandb` b on 
                                     a.vouchno = b.vouchno and a.visatype = b.visatype where b.`mofa_date` >= '$mfrmdate' AND b.`mofa_date` <= '$mtodate'
                                     AND b.mofano != ''  AND  b.submission_date = '0000-00-00'  ")or die(mysqli_error($conn));
//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
//            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="" >Sr</th>
                            <!--<th class="">Voucher</th>-->
                            <th class="">Date</th>
                            <th class="">File No</th>
                            <th class="">Agent</th>
                            <th class="">Pax Name</th>
                            <th class="">Visa No</th>
                            <th class="">Passport No</th> 
                            <th>Mofa</th>
                            <th>Mofa 2</th>
                            <th>Submission Date</th>
                            <th class="text-right">Amount</th>
                        </tr>
                        <?php
                        $ftot = 0;
                        $mistot = 0;
                        $misctot = 0;
                        $stot = 0;
                        $moftot = 0;
                        $itot = 0;
                        $ttot = 0;
                        $dtot = 0;
//                        $ftot = 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'];
                            $memcash = $pmain['memcash'];

                            Echo "<tr class='clickable-row' data-href='$entfile?mode=edit&b.vouchno='$pmain[vouchno]' [target]='_blank'>";
                            Echo "<td>$ctr</td>";
//                                Echo "<td>$pmain[visatype] $pmain[vouchno]</td>";
                            Echo "<td>$pmain[vouchdt]</td>";
                            Echo "<td><a target='_blank' href='entry.php?mode=edit&myvouch=$pmain[myvouch]'>$pmain[manualno]</a></td>";
                            Echo "<td>$pmain[broker]</td>";
                            Echo "<td>$pmain[paxname]</td>";
                            Echo "<td>$pmain[visano]</td>";
                            Echo "<td>$pmain[passport]</td>";
                            Echo "<td>$pmain[mofano]</td>";
                            Echo "<td>$pmain[mofano2]</td>";
                            Echo "<td>$pmain[submission_date]</td>";
                            Echo "<td class='text-right'>$pmain[tot]</td>";
                            Echo "</tr>";

                            $ftot = $ftot + $tot;
                            $ctr++;
                        }
                        Echo "<tr>";
                        Echo "<td></td>";
                        Echo "<td></td>";
                        Echo "<td></td>";
                        Echo "<td></td>";
                        Echo "<td></td>";
                        Echo "<td></td>";
                        Echo "<td></td>";
                        Echo "<td></td>";
                        Echo "<td></td>";
                        Echo "<td class='text-right text-danger'><b>Total</b></td>";
                        Echo "<td class='text-right text-danger'><b>$ftot</b></td>";
                        Echo "<td></td>";
                        Echo "</tr>";

                        Echo "</table>";
                        Echo "</td>";
                        Echo "</tr>";
                    }

                    if (isset($_POST["excel"])) {
                        $contents .= "Sr., Date, File No., Agent, Sponsor, Visa Type, Visa No., Visa Qty, Id no., Place, Hijri, Pax Name, Sex, Passport no, DOB, POB, POI, Stay Period, DOI, Stamp Date, Immigration status, Category, Mofa no., Mofa no.2, Mofa, Stamping, Immigration, M/s, Miscellaneous, Total, Discount, Final Amt\n";
                        $totamt = 0;

                        echo ucwords($columnHeader) . "\n" . $setData . "\n";
                        mysqli_select_db($conn, $zdb);
                        $getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as vouchdt, DATE_FORMAT(a.`hijridate`, '%d-%m-%Y')
                            as hijridt, b.* FROM `$maindb` a left join `$trandb` b on 
                            a.vouchno = b.vouchno and a.visatype = b.visatype where a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate'
                            $visatypecond $passportcond $manualnocond $visanocond $partynamecond $sponsercond $contactcond $mobilecond $agentcond
                            ")or die(mysqli_error($conn));

                        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'];
                            $vouchdt = $pmain['vouchdt'];
                            $manualno = $pmain['manualno'];
                            $broker = $pmain['broker'];
                            $sponsor = $pmain['sponsor'];
                            $visatype = $pmain['visatype'];
                            $visano = $pmain['visano'];
                            $visaqty = $pmain['visaqty'];
                            $idno = $pmain['idno'];
                            $place = $pmain['sector'];
                            $hijridate = $pmain['hijridate'];
                            $paxname = $pmain['paxname'];
                            $sex = $pmain['sex'];
                            $passport = $pmain['passport'];
                            $dob = $pmain['dob'];
                            $plbirthr = $pmain['plbirth'];
                            $plbirth = str_replace(",", "  ", "$plbirthr");
                            $poi = $pmain['issplace'];
                            $stayperiod = $pmain['periodstay'];
                            $doi = $pmain['issdate'];
                            $stampdate = $pmain['stampdate'];
                            $immgstatus = $pmain['delvstatus'];
                            $category = $pmain['category'];
                            $mofano = $pmain['mofano'];
                            $mofano2 = $pmain['mofano2'];
                            $mofa = $pmain['mofa'];

                            $ctr++;
                            $contents .= $ctr . ",";
                            $contents .= "$vouchdt" . ",";
                            $contents .= "$manualno" . ",";
                            $contents .= "$broker" . ",";
                            $contents .= "$sponsor" . ",";
                            $contents .= "$visatype" . ",";
                            $contents .= "$visano" . ",";
                            $contents .= "$visaqty" . ",";
                            $contents .= "$idno" . ",";
                            $contents .= "$place" . ",";
                            $contents .= "$hijridate" . ",";
                            $contents .= "$paxname" . ",";
                            $contents .= "$sex" . ",";
                            $contents .= "$passport" . ",";
                            $contents .= "$dob" . ",";
                            $contents .= "$plbirth" . ",";
                            $contents .= "$poi" . ",";
                            $contents .= "$stayperiod" . ",";
                            $contents .= "$doi" . ",";
                            $contents .= "$stampdate" . ",";
                            $contents .= "$immgstatus" . ",";
                            $contents .= "$category" . ",";
                            $contents .= "$mofano" . ",";
                            $contents .= "$mofano2" . ",";
                            $contents .= "$mofa" . ",";
                            $contents .= "$stamp" . ",";
                            $contents .= "$imig" . ",";
                            $contents .= "$mis" . ",";
                            $contents .= "$misc" . ",";
                            $contents .= "$total" . ",";
                            $contents .= "$disc" . ",";
                            $contents .= "$tot" . "\n";
                        }

                        $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;
                        }
                    }
?>