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

$maindb = "accmast";
$formname = "Bank Print";
$file = "bank_print_fr.php";
$mgrpcode = '001';

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

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

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



    if (!empty($name)) {
        $namecond = " and `accode`='$name' ";
    }
    if (!empty($city)) {
        $citycond = " and `city`='$city' ";
    }
    $getpmain = mysqli_query($conn,"SELECT * FROM `$maindb` WHERE `grpcode`='001' $namecond ")or die(mysqli_error($conn));
//    $getpmain = mysqli_query($conn,"SELECT `name`,`add1`,`add2`,`add3`,`city`,`pin`,`mobile`,`tel`,`email`,`contpers`,`openbal` FROM `$maindb` WHERE `grpcode`='$mgrpcode' $namecond $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-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">
                                <button id='cancel' type="reset" class="btn btn-primary" style="background-color:#64CCFC;" onclick="window.close()">
                                    Go Back
                                </button>
                            </div>
                        </div>-->
            <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>City</th>
                            <th>Pin</th>
                            <th>Mobile</th>
                            <th>Tel</th>
                            <th>Email</th>
                            <th>Contact</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[city]</td>";
                            echo "<td > $pmain[pin]</td>";
                            echo "<td > $pmain[mobile]</td>";
                            echo "<td > $pmain[tel]</td>";
                            echo "<td > $pmain[email]</td>";
                            echo "<td > $pmain[contpers]</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"];
    $city = $_POST["city"];

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



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

//    $getpmain = mysqli_query($conn,"SELECT `name`,`add1`,`add2`,`add3`,`city`,`pin`,`mobile`,`tel`,`email`,`contpers`,`openbal` FROM `$maindb` WHERE `grpcode`='$mgrpcode' $namecond $citycond")or die(mysqli_error($conn));
    $contents.="Sr.No,Name,Address,City,Pin,Mobile,Tel,Email,Contact,Opening Bal,\n";



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

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