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

$maindb = "visamain";
$trandb = "visatran";
$trandb1 = "category";
$formname = "Register";
$file = "register_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"];
$paidby = $_POST["paidby"];
$dtype = $_POST["dtype"];

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

if (!empty($manualno)) {
    $manualnocond = " AND a.`manualno`='$manualno' ";
}
if (!empty($paidby)) {
    $paidbycond = " AND a.`paidby`='$paidby' ";
}
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));

//    browse($partynamecond);
if (isset($_POST["view"])) {
    $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.`vouchno`, b.`passport`,b.`paxname`, b.`memcash` 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 $paidbycond $agentcond
                            ")or die(mysqli_error($conn));
//print table($getpmain);
//die();
    $get = mysqli_num_rows($getpmain);
    include("includes/linkshead.php");
    ?>
    </head>
    <style>
        @media print {
            a[href]:after {
                content: none !important;
            }
        }
        .tblhead > th{
            background-color: #F5F5F5;
            color: #C45254;
            width: 10%;
        }
        /*        @media print{
                                .pgbrk{
                                    page-break-before: always;
                                }
                }
                * {
                    box-sizing: border-box;
                    -moz-box-sizing: border-box;
                }
                .page {
                    width: 210mm;
                    min-height: 160mm;
                    padding: 4mm;
                    margin: 10mm auto;
                    border: 1px #D3D3D3 solid;
                    border-radius: 5px;
                    background: white;
        
                }
                .subpage {
                    padding: 0cm;
                    border: 1px solid;
                    height: 288mm;
                    outline: 2cm #ffffff solid;
                }
        
                @page {
                    size: A4;
                    margin: 0;
                }
                @media print {
                    html, body {
                        width: 210mm;
                        height: 297mm;
                    }
                    .page {
                        margin: 0;
                        border: initial;
                        border-radius: initial;
                        width: initial;
                        min-height: initial;
                        box-shadow: initial;
                        background: initial;
                        page-break-after: always;
                    }
                }
                .brkpg{
                    page-break-after: always;
                }*/

    </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="">Paidby</th>
                            <th class="">Pax Name</th>
                            <th class="">Visa No</th>
                            <th class="">Passport No</th> 
                            <th class="text-right">Mofa</th>
                            <th class="text-right">Stamping</th>
                            <!--<th>Hijri</th>-->
                            <th class="text-right">Immigration</th>
                            <th class="text-right">M/s.</th>
                            <th class="text-right">Misc.</th>
                            <th class="text-right">Total</th>
                            <th class="text-right">Discount</th>
                            <th class="text-right">Final Amt.</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'];
                            $paidby = $pmain['paidby'];
                            $newpaid = [
                                'C' => 'Cash',
                                'R' => 'Credit',
                                'U' => 'UPI / RTGS',
                            ];
                            $getpaid = $newpaid[$paidby];

                            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>$getpaid</td>";
                            Echo "<td>$pmain[paxname]</td>";
                            Echo "<td>$pmain[visano]</td>";
                            Echo "<td>$pmain[passport]</td>";
//                                Echo "<td>$pmain[hijridt]</td>";

                            Echo "<td class='text-right'>$pmain[mofa]</td>";
                            Echo "<td class='text-right'>$pmain[stamp]</td>";
                            Echo "<td class='text-right'>$pmain[imig]</td>";
                            Echo "<td class='text-right'>$pmain[mis]</td>";

                            Echo "<td class='text-right'>$pmain[misc]</td>";
                            Echo "<td class='text-right'>$memcash</td>";
                            Echo "<td class='text-right'>$pmain[disc]</td>";
                            Echo "<td class='text-right'>$pmain[tot]</td>";
                            Echo "</tr>";

                            $ftot = $ftot + $tot;
                            $mistot = $mistot + $mis;
                            $misctot = $misctot + $misc;
                            $moftot = $mofano + $moftot;
                            $stot = $stamp + $stot;
                            $ttot = $ttot + $total;
                            $dtot = $dtot + $disc;
                            $itot = $itot + $imig;
                            $ctr++;
                        }
                        Echo "<tr>";
//                            Echo "<td></b></td>";
                        Echo "<td></b></td>";
                        Echo "<td></b></td>";
                        Echo "<td></b></td>";
                        Echo "<td></b></td>";
                        Echo "<td></b></td>";
                        Echo "<td class='text-right'></b></td>";
                        Echo "<td></b></td>";

                        Echo "<td class='text-right text-danger'><b>Total</b></td>";
                        Echo "<td class='text-right text-danger'><b>$moftot</b></td>";
                        Echo "<td class='text-right text-danger'><b>$stot</b></td>";
                        Echo "<td class='text-right text-danger'><b>$itot</b></td>";

                        Echo "<td class='text-right text-danger'><b>$mistot</b></td>";

                        Echo "<td class='text-right text-danger'><b>$misctot</b></td>";
                        Echo "<td class='text-right text-danger'><b>$ttot</b></td>";
                        Echo "<td class='text-right text-danger'><b>$dtot</b></td>";
                        Echo "<td class='text-right text-danger'><b>$ftot</b></td>";
                        Echo "<td><b></b></th>";

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