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

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$zroot = $_SESSION["zdbroot"];

$maindb = 'product';
$grid = '';
$formname = 'Product Listing';

ini_set('display_errors', 1);
error_reporting(E_ERROR);


$pouchflag = isset($_POST['pouchflag']) ? 1 : 0;

$pouchflagcond = "";
if ($pouchflag == 1) {
    $pouchflagcond = "AND pouchflag = 1";
}

$cond = "";

if (!empty($_POST["name"])) {
    $pname = trim($_POST["name"]);
    $cond = "AND `code` = '$pname'";
}

$tcond = "";

if (!empty($_POST["type"])) {
    $type = trim($_POST["type"]);
    $tcond = "AND `type` = '$type'";
}

$get = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name`!='' $cond $tcond $pouchflagcond ORDER by `name`") or die(mysqli_error($conn));

$getno = mysqli_num_rows($get);

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>

    </head>

    <body>
        <div class="container-fluid">
            <?php include('includes/head_rep.php') ?>
            <div class="row">
                <div class="col-lg-12">
                    <table class="table table-condensed table-bordered ">
                        <tr class="mySearchTr">
                            <th width="5%">Sr.</th>
                            <th width="10%">Barcode</th>
                            <th width="25%">Name</th>
                            <th width="6%">Type</th>
                            <th width="10%">Country Origin</th>
                            <th width="6%">Best Before</th>
                            <th width="10%">Batch</th>
                            <th width="3%">Unit</th>
                            <th width="5%">Stock Unit</th>
                            <th width="6%" style="text-align: right">Cost</th>
                            <th width="6%" style="text-align: right">Rate</th>
                            <th width="15%" style="text-align: right">MRP / Retail</th>
                        </tr>
                        <tbody>
                            <?php
                            $ctr = 1;
                            while ($pmain = mysqli_fetch_array($get)) {

                                $gunit = mysqli_query($conn, "SELECT * FROM $zroot.`unit` WHERE `code`='$pmain[stkunit]'") or die(mysqli_error($conn));
                                $funit = mysqli_fetch_array($gunit);

                                echo "<tr>";
                                echo "<td>$ctr</td>";
                                echo "<td>$pmain[barcode]</td>";
                                echo "<td>$pmain[name]</td>";
                                echo "<td>$pmain[type]</td>";
                                echo "<td>$pmain[origin]</td>";
                                echo "<td>$pmain[bbf] months</td>";
                                echo "<td>$pmain[batch]</td>";
                                echo "<td>$pmain[unit]</td>";
                                echo "<td>$funit[name]</td>";
                                echo "<td style='text-align:right'>$pmain[cost]</td>";
                                echo "<td style='text-align:right'>$pmain[rate]</td>";
                                echo "<td style='text-align:right'>$pmain[mrp]</td>";
                                echo "</tr>";
                                $ctr++;
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

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

    </html>
    <?php
}
if (isset($_POST["excel"])) {


    $contents .= "Sr., Barcode, Name, Type, Country Origin, Best Before, Batch, Unit \n";

    $ctr = 1;
    while ($pmain = mysqli_fetch_array($get)) {
        $contents .= "$ctr" . ",";
        $contents .= "$pmain[barcode]" . ",";
        $contents .= "$pmain[name]" . ",";
        $contents .= "$pmain[type]" . ",";
        $contents .= "$pmain[origin]" . ",";
        $contents .= "$pmain[bbf]" . ",";
        $contents .= "$pmain[batch]" . ",";
        $contents .= "$pmain[unit]" . "\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;
    }
}
?>