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


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

$zyear = right($zdb, 4);
$mfrdate = getzdates($zyear, 'S');
$mtodate = getzdates($zyear, 'E');

$formname = "Stock";
$action = "stock_fr.php";

$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

include("includes/linkshead.php");
?>
<script>
    $(document).ready(function () {

        function httpGet()
        {
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.open("GET", "item_api.php", false); // false for synchronous request
            xmlHttp.send(null);
            return xmlHttp.responseText;
        }

//        $(".itemname").change(function () {
//            var item = $(".itemname").val();
//            $(".itemcode").val(item);
//        });
//
//        $(".itemcode").change(function () {
//            var itemcode = $(".itemcode").val();
//            $(".itemname").val(itemcode).trigger("change");
//        });
        
        
        var key_name = "item";
        var key_time = localStorage.getItem("item_time");
        if (!key_time) {
            var d = new Date();
            var h = d.getHours()
            d.setMinutes(d.getMinutes() + 5)
            key_time = d.getHours() + ":" + d.getMinutes()
            localStorage.setItem("item_time", key_time);
        }
        var mill_sec = 3000;
        function addMinutes(date) {
            return new Date(date.getTime() + mill_sec * 100);
        }
        var d = new Date();
        ddd = addMinutes(d)
        console.log("Next burst:",key_time)

        function burstItemStorage() {
            var d = new Date();
            var timing = key_time.split(":")
            
            d.setHours(timing[0])
            d.setMinutes(timing[1])
            console.log(ddd, "f1")
            console.log(d, "f2")
            console.log(ddd > d)
            if (ddd > d) {
                console.log("removed cached")
                localStorage.removeItem(key_name)
                localStorage.removeItem(key_time)
            }
        }
        burstItemStorage()
        var item_data = localStorage.getItem(key_name);
        console.log(item_data, "asdfasfasss")
        var msg = "fetched from cached"
        if (!item_data) {
            item_data = httpGet()
            localStorage.setItem(key_name, item_data)
            msg = "cached data"
        }
        console.log(msg)
        item_data = JSON.parse(item_data)

        $("#itemnames").empty(); // first empty datalist 
        var options = [];
        var counter = 0;
        item_data.forEach(function (arrayItem) {
            options[counter] = new Option(arrayItem.name)
            counter++;
        });
//        options[0] = new Option('landan');
//        options[1] = new Option('york');
//        options[2] = new Option('liverPool');
        $("#itemnames").append(options);

    });
</script>
</head>
<body>
    <form method='POST' action="<?= $action; ?>">
        <!-- Default box -->

        <div class="col-lg-5 col-md-12 col-sm-12 col-xs-12  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-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control ">Barcode</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <input type="text" name="itemcode" class="form-control itemcode" value="<?php echo $itemcode; ?>" autofocus="autofocus" >
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control ">Item Name</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <input type="text" name="itemname" class="form-control itemcode" value="<?php echo $itemname; ?>" autofocus="autofocus" >
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control ">Item Name</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <input list="itemnames" name="itemname" id="itemname">


                            <datalist id="itemnames">

                            </datalist>                            
                            <select class='form-control select2 item' name='name' class="item" style="width:297px;height:33px;">
                                <option value=""></option>
                                <?php
                                $getitem = mysqli_query($conn, "SELECT code, `name` FROM `product` ORDER BY `name`") or die(mysqli_error($conn));
                                while ($iget = mysqli_fetch_array($getitem)) {
                                    echo "<option value='$iget[code]'>$iget[name]</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control ">Item Group</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <select class='form-control select2' name='itemgrp'  class="itemgrp" style="width:297px;height:33px;" >
                                <option value=""></option>
                                <?php
                                $getitemgrp = mysqli_query($conn, "SELECT code, `name` FROM `prodgrp` ORDER BY `name`") or die(mysqli_error($conn));
                                while ($igetgrp = mysqli_fetch_array($getitemgrp)) {
                                    echo "<option value='$igetgrp[code]'>$igetgrp[name]</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control ">Godown</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <select class='form-control select2' name='godown'  class="godown" style="width:297px;height:33px;" >
                                <option value=""></option>
                                <?php
                                $getgdw = mysqli_query($conn, "SELECT code,gcode, `name` FROM `godown` ORDER BY `name`") or die(mysqli_error($conn));
                                while ($igdw = mysqli_fetch_array($getgdw)) {
                                    echo "<option value='$igdw[code]'>$igdw[gcode]</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control ">From Date</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <input type="date" id="mfrdt" name="frmdt" class="form-control" value="<?php echo $mfrdate; ?>" />  
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control ">To Date</label>
                        </div>
                        <div class="col-lg-5 col-md-5 col-sm-8 col-xs-8 pad">
                            <input type="date" id="mtodt" name="todt" class="form-control" value="<?php echo $mtodate; ?>" />  
                        </div>
                    </div>


                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control">Print Zero Stock</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                            <select name="prnzero" class="form-control select2" style="height:33px;">
                                <option value="Y">Yes</option>
                                <option value="N">No</option>
                            </select>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-ms-3 col-xs-3 text-right pad">
                            <label class="label-control">Print Value</label>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-8 col-lg-8 pad">                          
                            <select name="prnval" class="form-control select2" style="height:33px;">
                                <option value="1">No</option>
                                <option value="2">Yes</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="panel-footer">
                    <?php include("includes/but_rep.php"); ?>
                </div>
            </div>
        </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
