<?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"];
$agent = $_POST["agent"];

if (empty($_POST["frmdate"])) {
    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>";
}
$agentcond = "";
if (!empty($agent)) {
    $agentcond = " AND a.`broker` = '$agent' ";
}

$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 vouchdt,
                                     b.`vouchno`, b.`passport`,b.`paxname`, b.`memcash`, b.mofano, b.mofano2, b.submission_date,b.memcash 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' $agentcond order by a.broker  ") or die(mysqli_error($conn));
//print table($getpmain);
//die();
$get = 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
            //            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 = 2000;

                        echo "<tr class='clickable-row2' data-href='entry.php?mode=edit&myvouch=$pmain[myvouch]' [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[manualno]</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'>$memcash</td>";
                        echo "</tr>";

                        $ftot = $ftot + $memcash;
                        $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"])) {

                    $ctr = 1;
                    $ctr2 = 0;
                    $contents .= "Sr., Date, File No., Agent, Pax Name, Visa No, Passport No, Mofa, Mofa 2, Submission Date, Amount \n";

                    $ftot = 0;
                    $mistot = 0;
                    $misctot = 0;
                    $stot = 0;
                    $moftot = 0;
                    $itot = 0;
                    $ttot = 0;
                    $dtot = 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 = 2000;

                        $contents .= "$ctr" . ",";
                        $contents .= "$pmain[vouchdt]" . ",";
                        $contents .= "$pmain[manualno]" . ",";
                        $contents .= "$pmain[broker]" . ",";
                        $contents .= "$pmain[paxname]" . ",";
                        $contents .= "$pmain[visano]" . ",";
                        $contents .= "$pmain[passport]" . ",";
                        $contents .= "$pmain[mofano]" . ",";
                        $contents .= "$pmain[mofano2]" . ",";
                        $contents .= "$pmain[submission_date]" . ",";
                        $contents .= "$memcash" . "\n";

                        $ftot = $ftot + $memcash;
                        $ctr++;
                    }
                    $contents .= "" . ",,,,,,,,,";
                    $contents .= "Total" . ",";
                    $contents .= "$ftot" . ",";
                    $contents .= "" . "\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;
                    }
                }
                    ?>