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

$maindb = "medent";
$trandb = "visatran";
//$trandb1 = "category";
$formname = "Medical List";
$file = "medical_print_fr.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

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

//$manualno = $_POST["manualno"];
$visatype = $_POST["visatype"];
$visano = $_POST["visano"];
$fileno = $_POST["fileno"];
$mofano1 = $_POST["mofano1"];
$mofano2 = $_POST["mofano2"];
$paxname = $_POST["paxname"];
$passport = $_POST["passport"];
$type = $_POST["type"];
//$sponser = $_POST["sponser"];
//$contact = $_POST["contact"];
//$mobile = $_POST["mobile"];
$agent = $_POST["agent"];
$dtype = $_POST["dtype"];
$status = $_POST["fitness"];

$visatypecond = '';
$agentcond = '';
$statuscond = '';

if (!empty($passport)) {
    $passportcond = " AND `passport` = '$passport' ";
}
if (!empty($agent)) {
    $agentcond = " AND `broker` = '$agent' ";
}
if (!empty($status)) {
    $statuscond = " AND `fitness` = '$status' ";
}
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));


$getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as vouchdt FROM `$maindb` WHERE 
                                  vouchno = '$vouchno' AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'
                                  $agentcond $statuscond $passportcond ")or die(mysqli_error($conn));
$get = mysqli_num_rows($getpmain);
//print table($getpmain);
//die();


if (isset($_POST["view"])) {
//        $contents .="Sponsor \n";

    $get = mysqli_num_rows($getpmain);
    include("includes/linkshead.php");
    ?>
    </head>
    <body>
        <div class="container">
            <?php
            include("includes/head_rep.php");
            $ctr = 1;
            $ctr2 = 0;
            ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-bordered" >
                        <tr  class="mySearchTr" >
                            <th class="" style="width: 1%">Sr</th>
                            <th class="" style="width: 4%">File No</th>
                            <th class="" style="width: 5%">Date</th>
                            <th class="" style="width: 15%">Paxname</th> 
                            <th class="" style="width: 5%">Mobile No. </th>
                            <th class="" style="width: 4%">Passport</th>
                            <th class="" style="width: 10%">Agent</th>
                            <th class="" style="width: 12%">Doctor</th>
                            <th class="" style="width: 4%">Fit / Unfit</th>

                        </tr>
                        <?php
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            if ($pmain['visatype'] == 'E') {
                                $visatype = "ECR";
                            } elseif ($pmain['delvstatus'] == 'N') {
                                $visatype = "ECNR";
                            } else {
                                $visatype = "";
                            }

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

                            if ($pmain['fitness'] == 1) {
                                $fitness = "FIT";
                                $fitcolor = 'blue';
                            } elseif ($pmain['fitness'] == 2) {
                                $fitness = "UNFIT";
                                $fitcolor = 'red';
                            }
//
                            // convert to UTF-16 and add BOM
//$contents = chr(255) . chr(254) . mb_convert_encoding($contents, "UTF-16LE", "UTF-8");
// add encoding in headers
//header("Content-Encoding: UTF-16LE, UTF-8");
//header("Content-type: text/csv; charset=UTF-16LE, UTF-8");


                            Echo "<tr class='clickable-row' data-href='$entfile?mode=edit&b.vouchno='$pmain[vouchno]' [target]='_blank'>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[manualno]</td>";
                            Echo "<td>$pmain[vouchdt]</td>";
                            Echo "<td>$pmain[paxname]</td>";
                            Echo "<td>$pmain[mobile]</td>";
                            Echo "<td>$pmain[passport]</td>";
                            Echo "<td>$pmain[broker]</td>";
                            Echo "<td>$pmain[doctor]</td>";
                            Echo "<td style='color:$fitcolor'>$fitness</td>";

                            $ctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>
    </body>
    <?php
}

if (isset($_POST["excel"])) {
    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as vouchdt FROM `$maindb` WHERE 
                                  vouchno = '$vouchno' AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'
                                  $agentcond $statuscond $passportcond ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
    
    $contents .= "Sr., File No.,Date, Paxname,  Mobile No., Passport, Agent, Doctor, Fit/Unfit \n";

    $ctr = 0;

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

        $ctr++;
        $contents .=$ctr . ",";
        $contents .=$pmain[manualno] . ",";
        $contents .=$pmain[vouchdt] . ",";
        $contents .=$pmain[paxname] . ",";
        $contents .=$pmain[mobile] . ",";
        $contents .=$pmain[passport] . ",";
        $contents .=$pmain[broker] . ",";
//        $contents .=$pmain[doctor] . ",";
        $contents .=$pmain[doctor] . "\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;
    }
}
?>