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

$maindb = "visamain";
$trandb = "visatran";
//$trandb1 = "category";
$formname = "Immigration Update List";
$file = "immiglist_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'");

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

$agent = $_POST["agent"];

$agentcond = '';

if (!empty($agent)) {
    $agentcond = " AND a.`broker` = '$agent' ";
    $agcond = " AND b.`broker` = '$agent' ";
}

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

$gdate = mysqli_query($conn, "SELECT a.*,DATE_FORMAT(a.`appdt`, '%d-%m-%Y') as shdt FROM `$trandb` a LEFT JOIN $maindb b ON a.vouchno=b.vouchno  WHERE a.appdt != '0000-00-00' AND a.`appdt` >= '$mfrmdate' AND a.`appdt` <= '$mtodate' $agcond ORDER BY appdt") or die(mysqli_error($conn));

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

    include("includes/linkshead.php");
?>
    </head>
    <style>
        .tblhead>th {
            background-color: #F5F5F5;
            color: #C45254;
        }
    </style>

    <body>
        <form action='' method='POST'>
            <div class="container" style="width:80%">
                <?php
                include("includes/head_rep.php");
                ?>

                <div class="row">
                    <div class="col-lg-12 pad">
                        <table class="table table-condensed table-bordered">
                            <?php
                            while ($fdt = mysqli_fetch_array($gdate)) {
                            ?>
                                <tr class="mySearchTr">
                                    <th class="" colspan="8" style="text-align: center">Date - <?php echo $fdt['shdt'] ?></th>
                                </tr>
                                <tr class="mySearchTr">
                                    <th class="" style="width: 3%">Sr</th>
                                    <th class="" style="width: 9%">File No</th>
                                    <th class="" style="width: 12%">Pax Name</th>
                                    <th class="" style="width: 9%">Passport</th>
                                    <th class="" style="width: 7%">Visa No</th>
                                    <th class="" style="width: 5%">Agent</th>
                                    <th class="" style="width: 7%">Job Id</th>
                                    <th class="" style="width: 5%">E.N. No</th>
                                </tr>
                            <?php
                                $getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as vouchdt,
                            b.*,DATE_FORMAT(b.`appdt`, '%d-%m-%Y') as approvedt FROM `$maindb` a left join `$trandb` b on a.myvouch = b.myvouch where b.appdt='$fdt[appdt]' $agentcond ORDER BY a.vouchdt") or die(mysqli_error($conn));
                                $ctr = 1;
                                $ctr2 = 0;
                                while ($pmain = mysqli_fetch_array($getpmain)) {
                                    $myvouch = $pmain['myvouch'];

                                    echo "<tr class='clickable-row2' data-href='$entfile?myvouch=$myvouch&mode=edit' target='_blank' style='$bgcolr'>";
                                    echo "<td>$ctr</td>";
                                    echo "<td>$pmain[manualno]</td>";
                                    echo "<td>$pmain[paxname]</td>";
                                    echo "<td>$pmain[passport]</td>";
                                    echo "<td class=''>$pmain[visano]</td>";
                                    echo "<td>$pmain[broker]</td>";
                                    echo "<td class=''>$pmain[idno]</td>";
                                    echo "<td class=''>$pmain[enno]</td>";
                                    echo "</tr>";

                                    $ctr++;
                                    $ctr2++;
                                }
                                echo "<tr>";
                                echo "<td colspan='8' style='height:20px'></td>";
                                echo "<tr>";
                            }
                            ?>

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

    </body>
<?php
}

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


    while ($fdt = mysqli_fetch_array($gdate)) {
        $contents .= "Date - $fdt[shdt] \n";

        $contents .= "Sr., File No, Pax Name, Passport, Visa No, Agent, Job Id, E.N. No \n";

        $getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as vouchdt,
                            b.*,DATE_FORMAT(b.`appdt`, '%d-%m-%Y') as approvedt FROM `$maindb` a left join `$trandb` b on a.myvouch = b.myvouch where b.appdt='$fdt[appdt]' $agentcond ORDER BY a.vouchdt") or die(mysqli_error($conn));
        $ctr = 1;
        $ctr2 = 0;
        while ($pmain = mysqli_fetch_array($getpmain)) {
            $myvouch = $pmain['myvouch'];

            $contents .= "$ctr" . ",";
            $contents .= "$pmain[manualno]" . ",";
            $contents .= "$pmain[paxname]" . ",";
            $contents .= "$pmain[passport]" . ",";
            $contents .= "$pmain[visano]" . ",";
            $contents .= "$pmain[broker]" . ",";
            $contents .= "$pmain[idno]" . ",";
            $contents .= "$pmain[enno]" . "\n";
            
            $ctr++;
            $ctr2++;
        }
        
        $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;
    }
}
