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

$maindb = "address";
$formname = "Address Book Print";
$file = "addbook_print.php";
$mgrpcode = "010";

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

$name = $_POST['name'];
$state = $_POST["state"];
$city = $_POST['city'];

$namecond = "";
$statecond = "";
$citycond = "";

// if (!empty($name)) {
//     $namecond = " AND `name` like '%$name%' ";
// }
// if (!empty($country)) {
//     $countrycond = " AND `country` LIKE '%$country%' ";
// }
// if (!empty($city)) {
//     $citycond = " AND `city` LIKE '%$city%' ";
// }
if (!empty($name)) {
    $namecond = " AND `accode` = '$name' ";
}

if (!empty($city)) {
    $citycond = " AND `city` = '$city' ";
}

if (!empty($state)) {
    $statecond = " AND `state` = '$state' ";
}

$getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `grpcode`='$mgrpcode' $namecond $citycond $statecond group by name order by name")or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    <script src="MyScript/item.js" type="text/javascript"></script>
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="">
                <div class="col-lg-12 pad">
                    <table class="table table-bordered table-condensed table-hover">
                        <tr  style="background-color: #1ABC9C">
                            <th>Sr</th>
                            <th>Name</th>
                            <th>Address</th>
                            <th>Country</th>
                            <th>City</th>
                            <th>Email</th>
                            <th>Pin</th>
                            <th>Tel</th>
                        </tr>
                        <?php
                        $ctr = 1;
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            Echo "<tr>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[name]</td>";
                            Echo "<td>$pmain[add1] $pmain[add2] $pmain[add3] </td>";
                            Echo "<td>$pmain[country]</td>";
                            Echo "<td>$pmain[city]</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>
    <?php
}


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


    $contents .= "Sr, Name, Address, Country, City, Email, Pin, Tel, Fax \n";
    $ctr = 1;
    while ($pmain = mysqli_fetch_array($getpmain)) {

        $ctr;
        $pname = $pmain['name'];
        $padd = $pmain['add1'] . $pmain['add2'] . $pmain['add3'];
        $add = str_replace(',', ' ', $padd);
        $pcountry = $pmain['country'];
        $pcity = $pmain['city'];
        $pemail = $pmain['email'];
        $ppin = $pmain['pin'];
        $ptel = $pmain['tel'];
        $ctr++;

        $contents .= $ctr . ",";
        $contents .= $pname . ",";
        $contents .= $add . ",";
        $contents .= $pcountry . ",";
        $contents .= $pcity . ",";
        $contents .= $pemail . ",";
        $contents .= $ppin . ",";
        $contents .= $ptel . "\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;
    }
}
?>
