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

$maindb = 'expensep';
$formname = "Narraion Expense Print";
$file = "narrmast_print_fr.php";

$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"];

    $namecond = "";
    $areacond = "";


    if (!empty($name)) {
        $namecond = " and `code`='$name' ";
    }
    if (!empty($area)) {
        $areacond = " and `area` like '%$area%'";
    }
    $getpmain = mysqli_query($conn,"SELECT * FROM `$maindb` WHERE `code` != '' $namecond  $areacond")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-condensed table-hover">
                        <tr  style="background-color: #64CCFC">
                            <th>Sr.No</th>
                            <th>Name</th>
                            <th>Address</th>
                            <th>area</th>
                            <th>Pin</th>
                            <th>Mobile</th>
                            <th>Tel</th>
                            <th>Email</th>
                            <th>Owner</th>
                            <th class='text-right'>Opening Bal</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[area]</td>";
                            echo "<td > $pmain[pin]</td>";
                            echo "<td > $pmain[mobile]</td>";
                            echo "<td > $pmain[tel]</td>";
                            echo "<td > $pmain[email]</td>";
                            echo "<td > $pmain[owner]</td>";
                            echo "<td class='text-right'> $pmain[openbal]</td>";
                            Echo "</tr>";
                            $ctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

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

    $namecond = "";
    $areacond = "";


    if (!empty($name)) {
        $namecond = " and `code`='$name' ";
    }
    if (!empty($city)) {
        $citycond = " and `area`='$area' ";
    }
    $getpmain = mysqli_query($conn,"SELECT * FROM `$maindb` WHERE `code` != '' $namecond ")or die(mysqli_error($conn));
    include 'includes/head_rep_excel.php';

    $contents.="Sr.No,Name,Address,Area,Pin,Mobile,Tel,Email,Owner,Opening Bal,\n";

    $ctr = 1;
    while ($pmain = mysqli_fetch_array($getpmain)) {
        $pname = $pmain[name];
        $paddress = $pmain[add1] . " " . $pmain[add2] . " " . $pmain[add3];
        $parea = $pmain[area];
        $ppin = $pmain[pin];
        $pmobile = $pmain[mobile];
        $ptel = $pmain[tel];
        $pemail = $pmain[email];
        $powner = $pmain[owner];
        $popenbal = $pmain[openbal];

        $contents.=$ctr . ",";
        $contents.=$pname . ",";
        $contents.=$paddress . ",";
        $contents.=$parea . ",";
        $contents.=$ppin . ",";
        $contents.=$pmobile . ",";
        $contents.=$ptel . ",";
        $contents.=$pemail . ",";
        $contents.=$powner . ",";
        $contents.=$popenbal . "\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;
    }
}
?>