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

$maindb = "visamain";
$trandb = "visatran";
//$trandb1 = "category";
$formname = "Submission";
$file = "submission_fr.php";
// $mgrpcode = "010";
$entfile = "entry.php";
$keycond = "";
$keyfld = "myvouch";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
mysqli_query($conn, "set names 'utf8'");

//print table($getpmain);
//die;

if (isset($_POST["submit"])) {
    $count = $_POST["items"];
    $date = date("Y-m-d");

//    browse($count);
    for ($i = 0; $i <= $count; $i++) {


        $myvouch = $_POST["myvouch_$i"];
        $passportno = $_POST["passport_$i"];
        $rejctresn = $_POST["rejctresn_$i"];

        mysqli_query($conn, "UPDATE `visatran` SET `rejctresn` = '$rejctresn' WHERE `myvouch`='$myvouch' and `passport` = '$passportno' ")or die(mysqli_error($conn));
    }
    browse("Record Update Successfully");
    echo "<script>"
    . "window.close();"
    . "</script>";
}


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

    $frmdate = $_POST["frmdate"];
    $todate = $_POST["todate"];

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

    $visatypecond = '';
    $agentcond = '';
    $placecond = '';

    if (!empty($visatype)) {
        $visatypecond = " AND a.`visatype` = '$visatype' ";
    }
    if (!empty($place)) {
        $placecond = " AND a.`sector` = '$place' ";
    }
    if (!empty($agent)) {
        $agentcond = " AND a.`broker` = '$agent' ";
    }
    if (!empty($stamptype)) {
        if ($stamptype == 1) {
            $stamcond = " and `stampdate` != '0000-00-00'";
        } else {
            $stamcond = " and `stampdate` = '0000-00-00'";
        }
    }
    if (empty($_POST["frmdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; submission_sc.php");
    }
    if (empty($_POST["todate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; submission_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.vouchno,a.myvouch, a.manualno, a.broker, a.visano, a.idno,  DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as vouchdt,
                         b.vouchno, b.passport, b.paxname, b.mofano, b.mofano2, b.category, b.delvstatus, b.rejctresn, b.memcash,
                          DATE_FORMAT(b.`stampdate`,'%d-%m-%Y') as stampdate, DATE_FORMAT(b.`submission_date`,'%d-%m-%Y') as subdate
                         FROM `$maindb` a left join `$trandb` b on a.myvouch = b.myvouch where b.`submission_date` >= '$mfrmdate' AND b.`submission_date` <= '$mtodate'
                         $visatypecond $agentcond $placecond $stamcond ORDER BY a.broker,a.myvouch")or die(mysqli_error($conn));

    $get = mysqli_num_rows($getpmain);
    include("includes/linkshead.php");
    ?>
    </head>
    <!--    <script>
        jQuery(document).ready(function($) {
    $(".clickable-row").click(function() {
        window.location = $(this).data("href");
    });
    });
    </script>-->
    <style>
        .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>
        <form action='' method='POST'>

            <div class="container" style="width:80%">
                <?php
                include("includes/head_rep.php");

                if ($stamptype == 2) {
                    ?>
                    <input style="padding-right: 15px;float:right;width:17%;"  type='submit' id="update" name='submit' value='SAVE' class="btn btn-success">

                <?php } ?>

                <div class="row">
                    <div class="col-lg-12 pad">
                        <table class="table table-condensed table-bordered">
                            <tr  class="mySearchTr" >
                                <th class="" style="width: 3%">Sr</th>
                                <th class="" style="width: 5%">File No</th>
                                <th class="" style="width: 9%">Agent</th>
                                <th class="" style="width: 6%">Passport No</th> 
                                <th class="" style="width: 12%">Pax Name</th>
                                <th class="" style="width: 5%">Mofa</th>
                                <th class="" style="width: 7%">Visa No</th>
                                <th class="" style="width: 7%">ID No</th>
                                <th class="" style="width: 5%">Category</th>
    <!--                                <th class="" style="width: 4%">DL</th>
                                <th class="" style="width: 4%">MED</th>
                                <th class="" style="width: 4%">Exit</th>-->
                                <th class="" style="width: 4%">ECR/<br>ECNR</th>
                                <th class="" style="width: 4%">Visa Stamping</th>
                                <th class="" style="width: 5%;text-align:right">Amount</th>
                                <th class="" style="width: 6%">Remarks</th>
                                <?php
                                if ($stamptype == 2) {
                                    ?>
                                    <th class="" style="width: 15%">Reject Reason</th>
                                <?php } ?>
                            </tr>
                            <?php
                            $ctr = 1;
                            $ctr2 = 0;
                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $myvouch = $pmain['myvouch'];

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

                                if ($pmain['mofano2'] > 0) {
                                    $mofa = $pmain['mofano2'];
                                } else {
                                    $mofa = $pmain['mofano'];
                                }
                                $bgcolr = "";
                                if ($pmain['stampdate'] == "00-00-0000") {
                                    $bgcolr = "background-color:#ECEBA3";
                                }

                                if ($pmain['dl'] == 'Y') {
                                    $dl = "YES";
                                } elseif ($pmain['dl'] == 'N') {
                                    $dl = "NO";
                                } else {
                                    $dl = "";
                                }

                                if ($pmain['med'] == 'Y') {
                                    $med = "YES";
                                } elseif ($pmain['med'] == 'N') {
                                    $med = "NO";
                                } else {
                                    $med = "";
                                }

                                if ($pmain['exit'] == 'Y') {
                                    $exit = "YES";
                                } elseif ($pmain['exit'] == 'N') {
                                    $exit = "NO";
                                } else {
                                    $exit = "";
                                }
                                echo "<tr class='clickable-row2' data-href='$entfile?myvouch=$myvouch&mode=edit' target='_blank' style='$bgcolr'>";
                                Echo "<td>$ctr</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[passport]</td>";
                                Echo "<td>$pmain[paxname]</td>";
                                Echo "<td class=''>$mofa</td>";
                                Echo "<td class=''>$pmain[visano]</td>";
                                Echo "<td class=''>$pmain[idno]</td>";
                                Echo "<td class=''>$pmain[category]</td>";
//                                Echo "<td>$dl</td>";
//                                Echo "<td>$med</td>";
//                                Echo "<td>$exit</td>";
                                Echo "<td class=''>$dlvstatus</td>";
                                Echo "<td>$pmain[stampdate]</td>";
                                Echo "<td style='text-align:right'>$pmain[memcash]</td>";
                                Echo "<td>$pmain[remtran]</td>";
                                if ($stamptype == 2) {
                                    Echo "<td><input type='text' name='rejctresn_$ctr2' class='form-control rejctresn' value='$pmain[rejctresn]' maxlength='30'>"
                                    . "<input type='hidden' name='myvouch_$ctr2' class='form-control passport' value='$pmain[myvouch]'>"
                                    . "<input type='hidden' name='passport_$ctr2' class='form-control passport' value='$pmain[passport]'></td>";
                                }

                                Echo "</tr>";

                                $totmemcash = $totmemcash + $pmain['memcash'];
                                $ctr++;
                                $ctr2++;
                            }
                            ?>
                            <input type = "text" style="display: none;" name = "items" value = "<? Echo $ctr2; ?>">
                            <?php
                            Echo "<tr>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td class=''></td>";
                            Echo "<td class=''></td>";
                            Echo "<td class=''></td>";
                            Echo "<td class=''></td>";
                            Echo "<td class=''></td>";
                            Echo "<td style='text-align:right;color:red;font-size:15px'>Total</td>";
                            Echo "<td style='text-align:right;color:red;font-size:15px'>". number_format($totmemcash,2)."</td>";
                            Echo "<td></td>";
                            Echo "<tr>";
                            ?>

                        </table>
                    </div>
                </div>
            </div>
        </form>

    </body>
    <?php
}

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

    $frmdate = $_POST["frmdate"];
    $todate = $_POST["todate"];

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

    $visatypecond = '';
    $agentcond = '';
    $placecond = '';

    if (!empty($visatype)) {
        $visatypecond = " AND a.`visatype` = '$visatype' ";
    }
    if (!empty($place)) {
        $placecond = " AND a.`sector` = '$place' ";
    }
    if (!empty($agent)) {
        $agentcond = " AND a.`broker` = '$agent' ";
    }
    if (!empty($stamptype)) {
        if ($stamptype == 1) {
            $stamcond = " and `stampdate` != '0000-00-00'";
        } else {
            $stamcond = " and `stampdate` = '0000-00-00'";
        }
    }
    if (empty($_POST["frmdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; submission_sc.php");
    }
    if (empty($_POST["todate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; submission_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.vouchno,a.myvouch, a.manualno, a.broker, a.visano, a.idno, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as vouchdt,
                         b.vouchno, b.passport, b.paxname, b.mofano, b.mofano2, b.category, b.delvstatus, b.memcash,
                          DATE_FORMAT(b.`stampdate`,'%d-%m-%Y') as stampdate, DATE_FORMAT(b.`submission_date`,'%d-%m-%Y') as subdate
                         FROM `$maindb` a left join `$trandb` b on a.myvouch = b.myvouch where b.`submission_date` >= '$mfrmdate' AND b.`submission_date` <= '$mtodate'
                         $visatypecond $agentcond $placecond $stamcond ORDER BY a.broker,a.myvouch")or die(mysqli_error($conn));

    $get = mysqli_num_rows($getpmain);

    $contents .= "Sr, File No, Agent, Passport No, Paxname, Mofa, Visa No, ID No, Category, ECR/ECNR, Visa Stamping, Remarks \n ";

    $ctr = 1;
    $ctr2 = 0;

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

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

        if ($pmain['mofano2'] > 0) {
            $mofa = $pmain['mofano2'];
        } else {
            $mofa = $pmain['mofano'];
        }
        if ($pmain['dl'] == 'Y') {
            $dl = "YES";
        } elseif ($pmain['dl'] == 'N') {
            $dl = "NO";
        } else {
            $dl = "";
        }

        if ($pmain['med'] == 'Y') {
            $med = "YES";
        } elseif ($pmain['med'] == 'N') {
            $med = "NO";
        } else {
            $med = "";
        }

        if ($pmain['exit'] == 'Y') {
            $exit = "YES";
        } elseif ($pmain['exit'] == 'N') {
            $exit = "NO";
        } else {
            $exit = "";
        }

        $contents .= $ctr . ",";
        $contents .= $pmain['manualno'] . ",";
        $contents .= $pmain['broker'] . ",";
        $contents .= $pmain['passport'] . ",";
        $contents .= $pmain['paxname'] . ",";
        $contents .= $mofa . ",";
        $contents .= $pmain['visano'] . ",";
        $contents .= $pmain['idno'] . ",";
        $contents .= $pmain['category'] . ",";
        $contents .= $dlvstatus . ",";
        $contents .= $pmain['stampdate'] . ",";
        $contents .= $pmain['memcash'] . ",";
        $contents .= $pmain['remtran'] . "\n";

        $totmemcash = $totmemcash + $pmain['memcash'];
        $ctr++;
    }
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$totmemcash" . ",";
    $contents .= "" . "\n";


    $contents = strip_tags($contents);
    if ($_POST["dtype"] == 1) {
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        print chr(255) . chr(254) . mb_convert_encoding($contents, 'UTF-16LE', 'UTF-8');
        // header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        // print $contents;
    } else {
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        print chr(255) . chr(254) . mb_convert_encoding($contents, 'UTF-16LE', 'UTF-8');
        // header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        // print $contents;
    }
}
?>
