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

$maindb = "folwmain";
$trandb = "folwtran";
$formname = "Follow Up Register";
$file = "followup_print_fr.php";
$entfile = "followup.php";

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

//$howknowcond = '';


    $howknow = $_POST['howknow'];
    $inter = $_POST['inter'];

    $howknowcond = "";
    $intercond = "";

    if (!empty($howknow)) {
        $howknowcond = " AND `howknow`='$howknow' ";
    }
    if (!empty($inter)) {
        $intercond = " AND `interest`='$inter' ";
    }

    $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`,'%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchno` !='' $intercond $howknowcond ORDER BY `vouchno`")or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    </head>
    <body>
        <div class="container">

            <?php include("includes/head_rep.php"); ?>

            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-bordered table-condensed">
                        <tr class="mySearchTr" >
                            <th>Sr.</th>
                            <th>Voucher</th>
                            <th>Date</th>
                            <th>Party Name</th>
                            <th>City</th>
                            <th>Mobile 1</th>
                            <th>Mobile 2</th>
                            <th>How Know About Us</th>
                            <th>Interest</th>
                        </tr>
                        <?php
                        $ctr = 0;
                        while ($pmain = mysqli_fetch_array($getpmain)) {

                            $interest = $pmain['interest'];

                            if ($interest == 'Y') {
                                $interest = 'Yes';
                            } else {
                                $interest = 'No';
                            }

                            $ctr++;
                            Echo "<tr class=' clickable-row2' data-href='$entfile?mode=edit&vouchno=$pmain[vouchno]' [target]='_blank'>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[vouchno]</td>";
                            Echo "<td>$pmain[mydt]</td>";
                            Echo "<td>$pmain[party]</td>";
                            Echo "<td>$pmain[city]</td>";
                            Echo "<td>$pmain[mobile1]</td>";
                            Echo "<td>$pmain[mobile2]</td>";
                            Echo "<td>$pmain[howknow]</td>";
                            Echo "<td>$interest</td>";
                            Echo "</tr>";


                            if ($_POST["dtype"] == 2) {

                                Echo "<tr style='height:10px;'>";
                                Echo "<td colspan='25'>";
                                Echo "<table style='width:100%;' id='tabital' class='table table-condensed  table-bordered'>";
                                Echo "<thead>";
                                Echo "<tr class='' style='background-color:#BABFC2'>";
                                Echo "<th style='width:5%;'>Sr</th>";
                                Echo "<th style='width:8%;'>Date</th>";
                                Echo "<th style='width:40%;'>Description</th>";
                                Echo "<th style='width:8%;'>Reminder Date</th>";
                                Echo "</tr>";
                                Echo "<thead>";

                                $ctr2 = 0;

                                $gettran = mysqli_query($conn, "SELECT *,DATE_FORMAT(`trandt`,'%d-%m-%Y') as trandt ,DATE_FORMAT(`remdate`,'%d-%m-%Y') as remdate FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' order by srno") or die(mysqli_error($conn));
                                while ($tran = mysqli_fetch_array($gettran)) {


                                    $ctr2++;
                                    Echo "<tr>";
                                    Echo "<td>$ctr2</td>";
                                    Echo "<td>$tran[trandt]</td>";
                                    Echo "<td>$tran[descrip]</td>";
                                    Echo "<td>$tran[remdate]</td>";
                                    Echo "</tr>";
                                }
                            }
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

    <?php include("includes/linksfoot.php"); ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
    
    $contents .= "Sr,Voucher,Date,Party Name,City,Mobile,How Know About Us,Interest\n";

                        $ctr = 0;
                        while ($pmain = mysqli_fetch_array($getpmain)) {

                            $interest = $pmain['interest'];

                            if ($interest == 'Y') {
                                $interest = 'Yes';
                            } else {
                                $interest = 'No';
                            }

                            $ctr++;
                            $contents .= "$ctr" . ",";
                            $contents .= "$pmain[vouchno]" . ",";
                            $contents .= "$pmain[mydt]" . ",";
                            $contents .= "$pmain[party]" . ",";
                            $contents .= "$pmain[city]" . ",";
                            $contents .= "$pmain[mobile1]" . ",";
                            $contents .= "$pmain[mobile2]" . ",";
                            $contents .= "$pmain[howknow]" . ",";
                            $contents .= "$interest" . "\n";


                            if ($_POST["dtype"] == 2) {
                                
                                $contents .= "Sr,Date,Description,Reminder Date\n";

                                $ctr2 = 0;

                                $gettran = mysqli_query($conn, "SELECT *,DATE_FORMAT(`trandt`,'%d-%m-%Y') as trandt ,DATE_FORMAT(`remdate`,'%d-%m-%Y') as remdate FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' order by srno") or die(mysqli_error($conn));
                                while ($tran = mysqli_fetch_array($gettran)) {


                                    $ctr2++;
                                    $contents .= "$ctr2" . ",";
                                    $contents .= "$tran[trandt]" . ",";
                                    $contents .= "$tran[descrip]" . ",";
                                    $contents .= "$tran[remdate]" . "\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;
    }
}
?>