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

$maindb = "staff";
$formname = "Staff Print";
$file = "staff_print_fr.php";

$zroot = $_SESSION["zdbroot"];
$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];
    $atype = $_POST[atype];


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

    if (!empty($name)) {
        $namecond = " AND `code`='$name' ";
    }
    if (!empty($atype)) {
        $atypecond = " AND `active`='$atype' ";
    }

    if (!empty($city)) {
        $citycond = " AND `city`='$city' ";
    }
    $getpmain = mysqli_query($conn,"SELECT *,DATE_FORMAT(`joindt`,'%d-%m-%Y') as `joindt` FROM `$maindb` WHERE code !=''  $namecond $citycond")or die(mysqli_error($conn));

    include("includes/linkshead.php");
    ?>
    <style>
        .padd {
            background-color: white;
            padding: 0px;
        }


        </style>

    </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">
                    <a href="<? Echo $file; ?>" class="btn btn-primary" onclick="window.close();">Go Back</a>
                </div>-->
            </div>
            <div class="row">
                <div class="col-lg-12 padd">
                    <table class="table table-condensed table-bordered">
                        <tr  style="background-color: #64CCFC">
                            <th>Sr</th>
                            <th>Customer Name</th>
                            <th>Address</th>
                            <th>City</th>
                            <th>Pin</th>
                            <th>Mobile</th>
                            <th>Date Of Join</th>
                            <th>Designation</th>
                            <th class='text-right'>Salary</th>
                        </tr>
                        <?php
                        $ctr = 1;
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $getname = mysqli_query($conn,"SELECT name,accode From accmast WHERE accode = '$pmain[accode]'")or die(mysqli_erorr());
                            $showname = mysqli_fetch_array($getname);

                            $getdesig = mysqli_query($conn,"SELECT desig, code From $zroot.desig WHERE code = '$pmain[desig]'")or die(mysqli_erorr());
                            $showdesig = mysqli_fetch_array($getdesig);
//                            
                            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[joindt]</td>";
                            Echo "<td>$showdesig[desig]</td>";
                            Echo "<td class='text-right'>$pmain[salary]</td>";
                            Echo "</tr>";
                            $ctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php"); ?>
    </body>
    </html>
    <?
}

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


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

    if (!empty($iname)) {
        $inamecond = " AND `code`='$iname' ";
    }
    if (!empty($name)) {
        $namecond = " AND `accode`='$name' ";
    }

    if (!empty($city)) {
        $citycond = " AND `city`='$city' ";
    }
    $getpmain = mysqli_query($conn,"SELECT * FROM `$maindb` WHERE code !='' $inamecond $namecond $citycond")or die(mysqli_error($conn));

    include 'includes/head_rep_excel.php';

    $contents = "Sr.No,Name,Open Qty,Address,City,Pin,Mobile,Purchase Date,\n";


    $ctr = 1;
    while ($pmain = mysqli_fetch_array($getpmain)) {
        $piname = $pmain[name];
        $popenqty = $pmain[openqty];
        $paddress = $pmain[add1] . " " . $pmain[add2] . " " . $pmain[add3];
        $pcity = $pmain[city];
        $ppin = $pmain[pin];
        $pmobile = $pmain[mobile];
        $ppurchdt = $pmain[purchdt];

        $contents .= $ctr . ",";
        $contents .= $piname . ",";
        $contents .= $popenqty . ",";
        $contents .= $paddress . ",";
        $contents .= $pcity . ",";
        $contents .= $ppin . ",";
        $contents .= $pmobile . ",";
        $contents .= $ppurchdt . "\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;
    }
}
?>
