<?php
function table_data($data)
{
?>
<div class="col-sm-3 pad marg" style="padding-left: 15px;" >
    <!-- <div class="col-sm-3" style="padding: 0px;"> -->
        <table class="table table-condensed table-bordered">
            <tbody>
            <tr class="mySearchTr ">
                <th class="" style="width: 20%;font-size:20px;">SR.</th>
                <th class="" style="width: 80%;font-size:20px;">MOFA NO</th>
            </tr>
            <?php
            $ctr = 0;
            $ctr2 = 0;
            foreach ($data as $pmain) {
                $ctr++;
                $ctr2++;

                $myvouch = $pmain['myvouch'];

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

            ?>

                <!--<tr class='clickable-row2' data-href='$entfile?myvouch=$myvouch&mode=edit' target='_blank'>-->
                <tr style="height: 28px;" class="pad">
                    <td style="font-size:18px;"><?= $pmain['sr_no']; ?></td>
                    <td style="font-size:18px;" class="text-center"><?= $mofa; ?></td>
                </tr>
            <?php
            }
            ?>
            </tbody>
        </table>
    </div>
<?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>";
}



$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, a.sector, 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);

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

    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: 12%">Pax Name</th>
                                <!-- <th class="" style="width: 5%">Place</th> -->
                                <th class="" style="width: 6%">Passport No</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: 9%">Agent</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 = "";
                                }
                                if ($pmain['sector'] == "New DELHI" ){
                                    $sectorcolor = "style='color:red'";
                                }else{
                                    $sectorcolor = "";
                                }
                                
                                echo "<tr class='clickable-row2' data-href='$entfile?myvouch=$myvouch&mode=edit' target='_blank' style='$bgcolr'>";
                                echo "<td>$ctr</td>";
                                // <a target='_blank' href='entry.php?mode=edit&myvouch=$pmain[myvouch]'></a>
                                echo "<td>$pmain[manualno]</td>";
                                echo "<td>$pmain[paxname]</td>";
                                // echo "<td $sectorcolor>$pmain[sector]</td>";
                                echo "<td>$pmain[passport]</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>$pmain[broker]</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></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"])) {


    $contents .= "Sr, File No, Paxname, Passport No, Mofa, Visa No, ID No, Category, Agent, 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['paxname'] . ",";
        // $contents .= $pmain['sector'] . ",";
        $contents .= $pmain['passport'] . ",";
        $contents .= $mofa . ",";
        $contents .= $pmain['visano'] . ",";
        $contents .= $pmain['idno'] . ",";
        $contents .= $pmain['category'] . ",";
        $contents .= $pmain['broker'] . ",";
        $contents .= $dlvstatus . ",";
        $contents .= $pmain['stampdate'] . ",";
        $contents .= $pmain['remtran'] . "\n";

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


    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
        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;
    }

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

if (isset($_POST["view2"])) {
    
    if ($frmdate !== $todate) {
        browse('Please select Specific Date');
        header("refresh: 1; menu.php?action=submission"); 
 }
    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;
        }
    </style>

    <body>
        <div class="container">
            <thead>
                <?php
                include("includes/head_rep2.php");
                ?>
            </thead>
            <div class="row">
                <?php
                $counter = 0;
                $num_rows = 25;
                $rows = array();
                $sr_no = 0;

                while ($pmain = mysqli_fetch_array($getpmain)) {
                    $counter++;
                    $sr_no++;
                    $pmain['sr_no'] = $sr_no;

                    $rows[] = $pmain;
                    if (fmod($counter, $num_rows) == 0) {
                        $counter = 0;
                        table_data($rows);
                        $rows = array();
                    }
                }
                table_data($rows);
                ?>
            </div>
            <div class="row" style="padding: 0px;">
                <div class="col-lg-6">
                    <h5>I RECEIVED ALL THE PASSPORT :</h5>
                </div>
            </div>
            <div class="row" style="padding: 0px;">
                <div class="col-lg-8">
                    <h5>NAME OF THE REPRESENTATIVE & SIGNATURE __________________</h5>
                </div>
            </div>
        </div>
    </body>
<?php

}
