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

$maindb = "crm";
$maindb2 = "visamain";
//$trandb = "visatran";
//$trandb1 = "category";
$formname = "CRM";
$file = "crm_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"];

$myvouch = $_POST["myvouch"];
$vouchdt = $_POST["vouchdt"];
$name = $_POST["name"];
$mobile = $_POST["mobile"];
$emailid = $_POST["emailid"];
$passport = $_POST["passport"];
$category = $_POST["category"];
$edu = $_POST["edu"];
$remark = $_POST["remark"];

$myvouchcond = '';
$vouchdtcond = '';
$namecond = '';
$mobilecond= '';
$emailidcond = '';
$passportcond = '';
$categorycond = '';
$educond = '';
$remarkcond = '';
//$dtypecond = "";
// $mtypecond = "";

if (!empty($category)) {
    $categorycond = " AND a.`category`='$category' ";
}

if (empty($_POST["frmdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; crm_sc.php");
}
if (empty($_POST["todate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; crm_sc.php");
}


$myvouch = 'myvouch';
$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.`mobile` as mobileno, a.`category` as category1,a.`remark` as remarks, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') FROM `$maindb` a ,$maindb2 b  WHERE 
                                 a.myvouch = b.myvouch  AND a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $categorycond")or die(mysqli_error($conn));

//print table($getpmain);
//die();
    $get = mysqli_num_rows($getpmain);
    include("includes/linkshead.php");
    ?>



    </head>
    <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>
        <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="text-center" style="width: 5%">Sr</th>
                            <th class="text-center" style="width: 10%">Date</th>
                            <th class="text-center" style="width: 10%">Name</th>
                            <th class="text-center" style="width: 7%">Mobile No</th> 
                            <th class="text-center" style="width: 10%">Email ID </th>
                            <th class="text-center" style="width: 10%">Passport No</th>
                            <th class="text-center" style="width: 10%">Category</th>
                            <th class="text-center" style="width: 10%">Education</th>
                            <th class="text-center" style="width: 10%">Remarks</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)) {

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

                            Echo "<tr class='clickable-row' data-href='$entfile?mode=edit&b.vouchno='$pmain[vouchno]' [target]='_blank'>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[vouchdt]</td>";
                            Echo "<td>$pmain[name]</td>";
                            Echo "<td>$pmain[mobileno]</td>";
                            Echo "<td>$pmain[emailid]</td>";
                            Echo "<td class=''>$pmain[passport]</td>";
                            Echo "<td class='text-right'>$pmain[category1]</td>";
                            Echo "<td class=''>$pmain[edu]</td>";
                            Echo "<td class=''>$pmain[remarks]</td>";
                            Echo "</tr>";
                            $ctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>
    </body>
    <?php
}



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

    $getpmain = mysqli_query($conn,"SELECT *,a.`mobile` as mobileno, a.`category` as category1,a.`remark` as remarks, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') FROM `$maindb` a ,$maindb2 b  WHERE 
                                 a.myvouch = b.myvouch  AND a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate'
                                 $categorycond ")or die(mysqli_error($conn));

$contents .= "Sr, Date, Name, Mobile No, Email ID, Passport No, Category, Education, Remarks \n";
    
    $ctr = 0;
            $ctr2 = 0;

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

                            $ctr++;
        $contents .=$ctr . ",";
        $contents .=$pmain[vouchdt] . ",";
        $contents .=$pmain[name] . ",";
        $contents .=$pmain[mobileno] . ",";
        $contents .=$pmain[emailid] . ",";
        $contents .=$pmain[passport] . ",";
        $contents .=$pmain[category1] . ",";
        $contents .=$pmain[edu] . ",";
        $contents .=$pmain[remarks] . "\n";
                        }

//    $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.*, DATE_FORMAT(b.`submission_date`, '%d-%m-%Y') as subdate 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));
//
//
//
//    $contents .= "Sr, Date, Pax Name, Passport No, Agent, Mofa, Job Id, Category, Mobile Number, Immigration Clear Date \n";
//
//    $ctr = 0;
//    $ctr2 = 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'];
//
//        if ($pmain['delvstatus'] == 'E') {
//            $dlvstatus = "ECR";
//        } elseif ($pmain['delvstatus'] == 'N') {
//            $dlvstatus = "ECNR";
//        } else {
//            $dlvstatus = "";
//        }
//
//        $ctr++;
//        $contents .=$ctr . ",";
//        $contents .=$pmain[subdate] . ",";
//        $contents .=$pmain[paxname] . ",";
//        $contents .=$pmain[passport] . ",";
//        $contents .=$pmain[broker] . ",";
//        $contents .=$pmain[mofano] . ",";
//        $contents .=$pmain[jobid] . ",";
//        $contents .=$pmain[category] . ",";
////        $contents .=$pmain[sponsor] . ",";
//        $contents .=$pmain[mobileno] . ",";
//        $contents .=$dlvstatus . ",";
//        $contents .=$pmain[imgdate] . "\n";
//    }
//
//    if ($pmain['mofano2'] > 0) {
//        $mofa = $pmain['mofano2'];
//    } else {
//        $mofa = $pmain['mofano'];
//    }
//
//    $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;
    }
}