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

$maindb = "transport";
$formname = "Transport Print";
$file = "transport_print.php";

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

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

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


    $namecond = "";
    $mobilecond = "";
    $citycond = "";

    if (!empty($name)) {
        $namecond = "AND `code`='$name' ";
    }
    if (!empty($mobile)) {
        $mobilecond = " AND `country` LIKE '%$mobile%' ";
    }
    if (!empty($city)) {
        $citycond = " AND `city` LIKE '%$city%' ";
    }
    $getpmain = mysqli_query($conn, "SELECT `name`, `add1`, `add2`, `add3`, `mobile`,`city`, `state` FROM `$maindb` where code!= '' $namecond $statecond $mobilecond $citycond")or die(mysqli_error($conn));

    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 table-hover">
                        <tr  style="background-color: #1ABC9C">
                            <th width="3%">Sr</th>
                            <th width="25%">Name</th>                        
                            <th width="10%">GST No.</th>                        
                            <th width="35%">Address</th>                        
                            <th width="10%">Mobile No.</th>                        
                            <th width="10%">State</th>                        
                            <th width="10%">City</th>                        
                        </tr>
                        <?php
                        $ctr = 1;
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            
                            $address = $pmain['add1'] . " " . $pmain['add2'] ." ". $pmain['add3'] ;
                            
                            Echo "<tr class='bgwhite'>";
                            Echo"<td>$ctr</td>";
                            Echo"<td>$pmain[name]</td>";
                            Echo"<td>$pmain[gstno]</td>";
                            Echo"<td>$address</td>";
                            Echo"<td>$pmain[mobile]</td>";
                            Echo"<td>$pmain[state]</td>";
                            Echo"<td>$pmain[city]</td>";
                            Echo "</tr>";
                            $ctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php"); ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {

    $name = $_POST["name"];
    $mobile = $_POST["mobile"];
    $city = $_POST["city"];


    $namecond = "";
    $mobilecond = "";
    $citycond = "";

    if (!empty($name)) {
        $namecond = "AND `code`='$name' ";
    }
    if (!empty($mobile)) {
        $mobilecond = " AND `country` LIKE '%$mobile%' ";
    }
    if (!empty($city)) {
        $citycond = " AND `city` LIKE '%$city%' ";
    }
    $getpmain = mysqli_query($conn, "SELECT `name`,`mobile`,`city`, `state` FROM `$maindb` where code!= '' $namecond $statecond $mobilecond $citycond")or die(mysqli_error($conn));

    $contents.= "Sr., Name, GST No., Mobile No., City, State\n";
    $ctr = 1;
    while ($pmain = mysqli_fetch_array($getpmain)) {

        $pname = $pmain["name"];
        $pmobile = $pmain["mobile"];
        $pcity = $pmain["city"];

        $contents.=$ctr . ",";
        $contents.=$pname . ",";
        $contents.=$pmain['gstno'] . ",";
        $contents.=$pmobile . ",";
        $contents.=$pcity . ",";
        $contents.=$pmain["state"] . "\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;
    }
}
?>