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

$formname = 'Product';
$maindb = 'product';
$trandb = 'prodsub';

$file = 'product.php';
$grid = '';
$keyfld = "code";
$printfile = 'product_fr.php';
$trandbexist = 0;
$focusfld = "name";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$adduser = $_SESSION['username_' . $dbname];

$fseries = $_SESSION["series"];
$keycond = "AND series = '$fseries'";

date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");

mysqli_select_db($conn, $zdb);
include("includes/code_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {

    $a = 1;
    $prodcode = trim($_POST["prodcode"]);
    $name = trim($_POST["name"]);

    $gram = trim($_POST["gram"]);
    $packing = trim($_POST["packing"]);
    $colour = trim($_POST["colour"]);
    $barcode = trim($_POST["barcode"]);

    $unit = trim($_POST["unit"]);
    $prodgrp = trim($_POST["prodgrp"]);
    $hsncode = trim($_POST["hsncode"]);
    $gstperc = trim($_POST["gstperc"]);

    $cost = trim($_POST["cost"]);
    $sellprice = trim($_POST["rate"]);
    $mrp = trim($_POST["mrp"]);
    $msp = trim($_POST["msp"]);
    $cartqty = trim($_POST["cartqty"]);
    $type = trim($_POST["type"]);
    $approxkg = trim($_POST["approxkg"]);
    $phytest = trim($_POST["phytest"]);
    $nametoprint = trim($_POST["nametoprint"]);
    $miniqty = $_POST["miniqty"];
    $pkg = $_POST["pkg"];
    $image = $_POST["image"];

    $count = $_POST["items"];
    $count2 = $_POST["items2"];
    $mvouch = $qvouch;

    if (empty($name)) {
        browse("Name Cannot Be Empty");
        $a = 0;
    }

    if (isset($_POST["name"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE  `name` = '$name' and `code` != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
            header("refresh: 1; $file?code=$qvouch&mode=last");
        }
    }

    if ($a == 1) {


        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(code+0) AS maxcode FROM `$maindb` where code != ''  LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
            $mvouch = $qvouch;
        
        }



        $path = "images/Products/";

        #####################IMAGE#####################
        $filename1 = $mvouch . ".jpg";
        $img_file1 = $path . ($filename1);
        $image_ftype = pathinfo($img_file1, PATHINFO_EXTENSION);
        $size = $_FILES['image']['size'];

        if ($size < 30000000000000) {
            move_uploaded_file($_FILES["image"]["tmp_name"], $img_file1);
        } else {
            browse("Image size Should be less then 300kb try again upload image");
            header("refresh: 1; $file?mode=last");
        }

        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE");

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`code`, `prodcode`, `name`, `gram`, `packing`, `colour`, `barcode`, `unit`, `prodgrp`, `hsncode`,
                                                       `gstperc`, `cost`, `rate`, `nametoprint`,`mrp`, `msp`, `type`,`cartqty`, `adduser`, `adddate`, `addtime`,
                                                       `miniqty`, `image`,`pkg`, `series`)
                           VALUES('$qvouch', '$prodcode', '$name', '$gram', '$packing', '$colour', '$barcode', '$unit', '1',
                                  '$hsncode', '$gstperc', '$cost', '$sellprice', '$nametoprint','$mrp', '$msp', '$type', '$cartqty', '$adduser', '$adddate', '$addtime',
                                  '$miniqty', '$img_file1', '$pkg', '$fseries')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit&series=$fseries");
        } else {
            $mvouch = $qvouch;

            $editdate = date("Y-m-d");
            $edittime = date("h:i:sa");
            mysqli_query($conn, "UPDATE `$maindb` SET  `prodcode`='$prodcode', `name`='$name', `gram`='$gram', `packing`='$packing', `colour`='$colour',
                                               `barcode`='$barcode', `unit`='$unit', `prodgrp`='1', `hsncode`='$hsncode', `gstperc`='$gstperc',
                                               `cost`='$cost', `rate`='$sellprice', `nametoprint`='$nametoprint',`mrp`='$mrp', `msp`='$msp',
                                               `type`='$type', `cartqty`='$cartqty', `edituser`='$adduser', `editdate`='$editdate', `edittime`='$edittime',
                                               `miniqty`='$miniqty', `image`='$img_file1', `pkg`='$pkg' WHERE code = '$mvouch' $keycond ") or die(mysqli_error($conn));

            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit&series=$fseries");
        }

        mysqli_query($conn, "UNLOCK TABLES");
    }
}
if ($addflag != 1) {
    $getvouch = '';

    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }
    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch' $keycond ") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

} 
// } else {
//     $result2 = mysqli_query($conn, "SELECT * FROM `$maindb` ORDER BY $keyfld desc limit 0,1 $keycond ") or die(mysqli_error($conn));
//     if (!empty($result2)) {
//         $get2 = mysqli_fetch_array($result2);
//     }
// }

$senable = "";
$cenable = "";
if (isset($_GET["delete"])) {
    $chkresult = mysqli_query($conn, "SELECT itemcode FROM `transacn` WHERE itemcode='$_GET[$keyfld]'") or die(mysqli_error($conn));
    $fetget = mysqli_num_rows($chkresult);
    if ($fetget > 0) {
        browse('Item Exist in Transaction');
        header("refresh: 1; $file?$keyfld=$_GET[$keyfld]&mode=edit");
    } else {
        $delcond2 = " $keyfld = '$_GET[$keyfld]' $keycond";

        delnew($zdb, "$maindb", "$delcond2", "$file?mode=last&series=$fseries");
    }
}
$versionToken = mt_rand();
include("includes/linkshead.php");
?>

<script src="MyScript/product.js<?= '?version=' . $versionToken ?>" type="text/javascript"></script>
<script src="MyScript/product_search.js" type="text/javascript"></script>
</head>

<body>
    <form method='POST' enctype="multipart/form-data">

        <!-- Default box -->

        <div class="row">
            <div class="col-lg-5 pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php echo $formname; ?></h3>

                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 numeric text-right  pad">
                                <label class="label-control marg">Barcode</label>
                            </div>
                            <div class="col-lg-3 col-md-4 col-sm-4 col-xs-3 pad">
                                <input type="text" style="font-weight: bold;font-size:15px" class="form-control form barcode" name="barcode" value="<?php echo ($addflag != 1) ? $get["barcode"] : "$qvouch"; ?>" readonly>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 numeric text-right  pad">
                                <label class="label-control marg">Name</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form prodname focusfield" name="name" value="<?php echo ($addflag != 1) ? $get["name"] : ""; ?>" autofocus required>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 numeric text-right  pad">
                                <label class="label-control marg">Cost Price</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-2 col-xs-6 pad">
                                <input type="text" class="form-control form text-right cost" name="cost" value="<?php echo ($addflag != 1) ? $get["cost"] : "0"; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 numeric text-right  pad">
                                <label class="label-control marg">Selling Price</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-2 col-xs-6 pad">
                                <input type="text" class="form-control form rate text-right" name="rate" value="<?php echo ($addflag != 1) ? $get["rate"] : "0"; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 numeric text-right  pad">
                                <label class="label-control marg">Unit</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-2 col-xs-6 pad">
                                <input type="text" class="form-control form unit" name="unit" value="<?php echo ($addflag != 1) ? $get["unit"] : "PCS."; ?>" required>
                            </div>
                        </div>

<!--                        <div class="row">
                            <div class="col-lg-4 col-md-3 col-sm-3 col-xs-3 numeric text-right  pad">
                                <label class="label-control marg">Packing Per Bags/Bharti</label>
                            </div>
                            <div class="col-lg-3 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control text-right form pkg" name="pkg" value="<?php echo ($addflag != 1) ? $get["pkg"] : ""; ?>">
                            </div>
                        </div>-->
                    </div>

                </div>

                <div class="panel-footer">
                    <?php include("includes/buttons.php"); ?>
                </div>
            </div>

        </div>
    </form>

    <div class="modal fade bs-example-modal-lg searchModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="2">Name</option>
                                <option value="1">Code</option>
                                <!--                                <option value="3">Grammage</option>
                                                                <option value="4">Packing</option>
                                                                <option value="5">Barcode</option>
                                                                <option value="6">GST %</option>-->
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="2">Name</option>
                                <option value="1">Code</option>
                                <!--                                <option value="3">Grammage</option>
                                                                <option value="4">Packing</option>
                                                                <option value="5">Barcode</option>
                                                                <option value="6">GST %</option>-->
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="asc">Asc</option>
                                <option value="desc">Desc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search">
                        </div>
                    </div>

                    <div class="col-lg-12 sloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr class="mySearchTr">
                                    <th width="7%">Code</th>
                                    <th width="30%">Name</th>
                                    <th width="10%">Cost</th>
                                    <th width="10%">Rate</th>
                                    <!--<th width="10%">Barcode</th>-->

                                    <!--<th width="10%">GST %</th>-->
                                    <th width="8%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button" class="btn btn-primary sprev" value="Previous">
                            <input type="button" class="btn btn-primary snext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>

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

</html>