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

$maindb = "alldata";
$formname = "All Data Print";
$file = "alllisting.php";
$mgrpcode = "";

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

if (isset($_POST["view"])) {
    $name = $_POST[name];
    $area = $_POST[area];
    $city = $_POST[city];
    $pin = $_POST[pin];
    $address = $_POST[address];
    $member = $_POST[member];
    $brno = $_POST[brno];
    $groupmast = $_POST[groupmast];
    $orderby = $_POST[orderby];

    $namecond = "";
    $membercond = "";
    $areacond = "";
    $brnocond = "";
    $citycond = "";
    $pincond = "";
    $addresscond = "";
    $groupmastcond = "";
    $orderbycond = "";

    if (!empty($name)) {
        $namecond = " AND `accode`='$name' ";
    }
    if (!empty($member)) {
        $membercond = " AND `name` LIKE '%$member%' ";
    }
    if (!empty($address)) {
        $addresscond = "  AND ( `add1` LIKE '%$address%' or  `add2` LIKE '%$address%' or  `add3` LIKE '%$address%' )";
    }
    if (!empty($brno)) {
        $brnocond = " AND `brno` LIKE '%$brno%' ";
    }
    if (!empty($pin)) {
        $pincond = " AND `pin` LIKE '%$pin%' ";
    }
    if (!empty($city)) {
        $citycond = " AND `city` LIKE '%$city%' ";
    }
    if (!empty($orderby)) {
        if ($orderby == "1") {
            $orderbycond = " order by brno";
        } else {
            $orderbycond = " order by name";
        }
    }
    $getpmain = mysqli_query($conn,"SELECT `region`, `memno`, `brno`, `name`, `add1`, `add2`, `add3`, `city`, `pin`, `mobile` FROM `$maindb` WHERE `name`!=''  $namecond $addresscond $brnocond $membercond $pincond $citycond  $orderbycond")or die(mysqli_error($conn));

    include("includes/linkshead.php");
    ?>
    <script src="MyScript/item.js" type="text/javascript"></script>
    </head>
    <body>
        <div class="container">
            <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-1">
                    <a onclick="window.close()"  class="btn btn-primary">Go Back</a>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-hover">
                        <tr class="mySearchTr"  style="">
                            <th>Sr</th>
                            <th>BR. No.</th>
                            <th>Name</th>
                            <th>Address</th>
                            <th>Area</th>
                            <th>Email</th>
                            <th>Pin</th>
                            <th>Tel</th>
                        </tr>
                        <?php
                        $ctr = 1;
//                        browse($ctr);
                        while ($pmain = mysqli_fetch_array($getpmain)) {

                            Echo "<tr>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[brno]</td>";
                            Echo "<td>$pmain[name]</td>";
                            Echo "<td>$pmain[add1] $pmain[add2] $pmain[add3] </td>";
                            Echo "<td>$pmain[station]</td>";
                            Echo "<td>$pmain[email]</td>";
                            Echo "<td>$pmain[pin]</td>";
                            Echo "<td>$pmain[tel]</td>";
                            Echo "</tr>";
                            $ctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php"); ?>
    </body>
    </html>
    <?
}
if (isset($_POST["excel"])) {
    $name = $_POST[name];
    $area = $_POST[area];
    $city = $_POST[city];
    $brno = $_POST[brno];
    $pin = $_POST[pin];
    $member = $_POST[member];
    $address = $_POST[address];

    $groupmast = $_POST[groupmast];
    $orderby = $_POST[orderby];

    $namecond = "";
    $areacond = "";
    $citycond = "";
    $brnocond = "";
    $addresscond = "";
    $membercond = "";
    $pincond = "";
    $groupmastcond = "";
    $orderbycond = "";
    
    if (!empty($name)) {
        $namecond = " AND `accode`='$name' ";
    }
    

    if (!empty($address)) {
        $addresscond = " AND ( `add1` LIKE '%$address%' or  `add2` LIKE '%$address%' or  `add3` LIKE '%$address%' )";
    }

    if (!empty($city)) {
        $citycond = " AND `city` LIKE '%$city%' ";
    }
    if (!empty($brno)) {
        $brnocond = " AND `brno` LIKE '%$brno%' ";
    }
    if (!empty($member)) {
        $membercond = " AND `name` LIKE '%$member%' ";
    }
    if (!empty($pin)) {
        $pincond = " AND `pin` LIKE '%$pin%' ";
    }

    if (!empty($orderby)) {
        if ($orderby == "1") {
            $orderbycond = " order by brno";
        } else {
            $orderbycond = " order by name";
        }
    }
//        browse($namecond);

        $getpmain = mysqli_query($conn,"SELECT `region`, `memno`, `brno`, `name`, `add1`, `add2`, `add3`, `city`, `pin`, `mobile` FROM `$maindb` WHERE `name`!=''  $namecond $addresscond $brnocond $membercond $pincond $citycond  $orderbycond")or die(mysqli_error($conn));


    $contents.= "Sr, BR.No., Name, Address, Pin, Mobile \n";

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

        $contents.= $ctr . ",";
        $contents.= $pmain[brno] . ",";
        $contents.= $pmain[name] . ",";
        $contents.= $pmain[add1] . " " . [add2] . " " . [add3] . ",";
        $contents.= $pmain[pin] . ",";
        $contents.= $pmain[mobile] . "\n";
        $ctr++;
    }
    $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;
    }
}
?>
