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

$maindb = "crm";

$formname = "CRM";
$file = "crmlist_fr.php";

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

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

$category = $_POST["category"];
$catgcond = "";

if (!empty($category)) {
    $catgcond = " AND `category` = '$category' ";
}

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 = $_POST["frmdate"];
$mtodate = $_POST["todate"];

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

if (isset($_POST["view"])) {
    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(vouchdt, '%d-%m-%Y') as myvouchdt FROM `$maindb` 
                                            where `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $catgcond ") 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="" style="width: 5%">Sr.</th>
                            <th class="" style="width: 7%">Date</th>
                            <th class="" style="width: 10%">Pax Name</th>
                            <th class="" style="width: 15%">Email Id.</th> 
                            <th class="" style="width: 10%">Mobile</th>
                            <th class="" style="width: 7%">Passport</th>
                            <th class="" style="width: 10%">Category</th>
                            <th class="" style="width: 15%">Education</th>
                            <th class="" style="width: 20%">Remarks</th>
                        </tr>
                        <?php
                        while ($pmain = mysqli_fetch_array($getpmain)) {

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

if (isset($_POST["excel"])) {
    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(vouchdt, '%d-%m-%Y') as myvouchdt FROM `$maindb` 
                                            where `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $catgcond ") or die(mysqli_error($conn));

    $contents .= "Sr., Date, Paxname, Email ID, Mobile, Passport, Category, Education, Remarks \n";

    $ctr = 0;

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

        $ctr++;
        $contents .=$ctr . ",";
        $contents .=$pmain[myvouchdt] . ",";
        $contents .=$pmain[name] . ",";
        $contents .=$pmain[emailid] . ",";
        $contents .=$pmain[mobile] . ",";
        $contents .=$pmain[passport] . ",";
        $contents .=$pmain[category] . ",";
        $contents .=$pmain[edu] . ",";
        $contents .=$pmain[remark] . "\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;
    }
    
}
    ?>