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

$maindb = "leadenq";
//$trandb = "saletran";
$formname = "Lead Register";
$file = "leadreg.php";

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

if (isset($_POST["view"])) {
$name = $_POST[name];
$gtype = $_POST[gtype];


$staffcond = "";
$mtypecond = "";
//    browse($gtype);
if (!empty($name)) {
$staffcond = " AND `username`='$name' ";
}


$mycheck1 = array();
    $count = 1;

    if (!empty($_POST['gtype'])) {
        foreach ($_POST['gtype'] as $check1) {
            $mycheck1[$count++] .= "OR `status` = '" . $check1 . "' ";
        }
    }
    
    foreach ($mycheck1 as $g => $values) {
        $mcheck1 .= $values;
    }

    $typecond = '';
    if (!empty($_POST['gtype'])) {
        $typecond = " AND (`status` = '' " . $mcheck1 . " )";
    }
//if (!empty($gtype)) {
//$mtypecond = " AND `status`='$gtype' ";
//}

//browse($mcheck1);


if (empty($_POST["frmdt"])) {
browse("From Date Cannot Be Empty");
header("refresh:1; leadreg.php");
}
if (empty($_POST["todt"])) {
browse("To Date Cannot Be Empty");
header("refresh:1; leadreg.php");
}
$mfrmdate = trim($_POST["frmdt"]);
$mtodate = trim($_POST["todt"]);



// print "SELECT * FROM `$maindb` WHERE `vouchno`!='' $partyname $broker $transpor $item $godown $mtype";
if ($usertype == 1){
    $getpmain = mysqli_query($conn,"SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt,DATE_FORMAT(`deptdate`, '%d-%m-%Y') as deptdate
                         FROM $maindb where `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'
                         $staffcond $typecond order by vouchno")or die(mysqli_error($conn));
    }else{
        $getpmain = mysqli_query($conn,"SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt,DATE_FORMAT(`deptdate`, '%d-%m-%Y') as deptdate
                         FROM $maindb where `username`='$user' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'
                         $typecond order by vouchno")or die(mysqli_error($conn));
    }
include("includes/linkshead.php");
?>
<!--<script src="MyScript/item.js" type="text/javascript"></script>-->    
</head>
<body>
    <div class="container" style="width:100%">
        <div class="row">
<!--                            <div class="col-lg-6">
                                <label class="label-control marg">Date - </label>
                                <label class="label-control marg"><?php // Echo Date("d-m-Y");  ?></label>
                            </div>-->

            <div class="col-lg-5">
                <label class="label-control marg">Print - </label>
                <label class="label-control marg"><?php Echo $formname; ?></label>
            </div>

            <div class="col-lg-3" style="float: right; margin-top: 5px;">
                <a onclick="window.close()" class="btn btn-primary">GO Back</a>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12 pad">
                <table style="width:100%;background: white" class="table-condensed table-hover table-bordered">
                    <thead>
                        <tr style="height:10px;background-color: #64CCFC;">
                            <th style="width:3%">Sr</th>
                            <?php 
                            if($usertype == 1){ ?>
                            <th style="width:10%">Staff</th>
                            <?php }
                            ?>
                            <th style="width:4%">Book No</th>
                            <th style="width:9%">Enq Date</th>
                            <th style="width:9%">Name</th>
                            <th style="width:9%">Last Name</th>
                            <th style="width:9%">Destination</th>
                            <th style="width:9%">Source</th>
                            <th style="width:8%">Travel Date</th>
                            <th style="width:7%">Mobile</th>
                            <th style="width:8%">Email-ID</th>
                            <th style="width:7%">Status</th>
                            <th style="width:5%">Que to Visa</th>
                            <th style="width:22%">Remarks</th>
                            <!--<th class="numeric">New Remark</th>-->                            
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $ctr = 0;

                        while ($pmain = mysqli_fetch_array($getpmain)) {
                        $ctr++;
                        if($pmain[quotvisa] == "Y"){
                            $quotvisa = "YES";
                        }else{
                            $quotvisa = "No";
                        }
                        Echo "<tr>";
                        
                        Echo "<td style='width:3%'>$ctr</td>";
                        if($usertype == 1){
                            Echo "<td style='width:10%'>$pmain[username]</td>";
                        }
                        Echo "<td style='width:4%'>$pmain[vouchno]</td>";
                        Echo "<td style='width:9%'>$pmain[mydt]</td>";
                        Echo "<td style='width:9%'>$pmain[name]</td>";
                        Echo "<td style='width:9%'>$pmain[lastname]</td>";
                        Echo "<td style='width:9%'>$pmain[destination]</td>";
                        Echo "<td style='width:9%'>$pmain[source]</td>";
                        Echo "<td style='width:8%'>$pmain[deptdate]</td>";
                        Echo "<td style='width:7%'>$pmain[mobile]</td>";
                        Echo "<td style='width:8%'>$pmain[email]</td>";
                        Echo "<td style='width:7%'>$pmain[status]</td>";
                        Echo "<td style='width:5%'>$quotvisa</td>";
                        Echo "<td style='width:22%'>$pmain[remarks]  $pmain[remarks1]</td>";
//                        Echo "<td style='width:10%'>$pmain[remarks1]</td>";
                        Echo "</tr>";
                        }

//                            
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

<?php include("includes/linksfoot.php"); ?>
</body>
</html>
<?php
}

if (isset($_POST["excel"])) {
$name = $_POST[name];
$gtype = $_POST[gtype];


$staffcond = "";
$mtypecond = "";
//    browse($gtype);
if (!empty($name)) {
$staffcond = " AND `username`='$name' ";
}


$mycheck1 = array();
    $count = 1;

    if (!empty($_POST['gtype'])) {
        foreach ($_POST['gtype'] as $check1) {
            $mycheck1[$count++] .= "OR `status` = '" . $check1 . "' ";
        }
    }
    
    foreach ($mycheck1 as $g => $values) {
        $mcheck1 .= $values;
    }

    $typecond = '';
    if (!empty($_POST['gtype'])) {
        $typecond = " AND (`status` = '' " . $mcheck1 . " )";
    }




if (empty($_POST["frmdt"])) {
browse("From Date Cannot Be Empty");
header("refresh:1; leadreg.php");
}
if (empty($_POST["todt"])) {
browse("To Date Cannot Be Empty");
header("refresh:1; leadreg.php");
}
$mfrmdate = trim($_POST["frmdt"]);
$mtodate = trim($_POST["todt"]);



// print "SELECT * FROM `$maindb` WHERE `vouchno`!='' $partyname $broker $transpor $item $godown $mtype";
$getpmain = mysqli_query($conn,"SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM $maindb where `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'
             $staffcond $typecond")or die(mysqli_error($conn));
if($usertype == 1){
    $contents.="Sr,Staff,Book No ,Enq Date,Name,Last Name,Destination,Source,Travel Date,Mobile,Email-ID,Status,Que to Visa,Remark,New Remark\n";
}else{
    $contents.="Sr,Book No ,Enq Date,Name,Last Name,Destination,Source,Travel Date,Mobile,Email-ID,Status,Que to Visa,Remark,New Remark\n";
}

$contents.="\n";
$ctr = 0;


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


$ctr++;

$exctr = $ctr;
$exvouchno = $pmain[vouchno];
$exusername = $pmain[username];
$exvouchdt = $pmain[vouchdt];
$exname = $pmain[name];
$exlastname = $pmain[lastname];
$exdestination = $pmain[destination];
$exsource = $pmain[source];
$exdeptdate = $pmain[deptdate];
$mobile = $pmain[mobile];
$email = $pmain[email];
$exstatus = $pmain[status];
$exquotvisa = $pmain[quotvisa];
$exremarks = $mqty[remarks];
$exremarks1 = $pmain[remarks1];

$contents.=$exctr . ",";
if($usertype == 1){
    $contents.=$exusername . ",";
}
$contents.=$exvouchno . ",";
$contents.=$exvouchdt . ",";
$contents.=$exname . ",";
$contents.=$exlastname . ",";
$contents.=$exdestination. ",";
$contents.=$exsource. ",";
$contents.=$exdeptdate . ",";
$contents.=$mobile . ",";
$contents.=$email . ",";
$contents.=$exstatus . ",";
$contents.=$exquotvisa . ",";
$contents.=$exremarks . ",";
$contents.=$exremarks1 . "\n";

}


$contents = strip_tags($contents);

//if ($_POST["dtype"] == 1) {
header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
print $contents;
//}
}
?>

