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

$formname = "Fast Moving Items";
$action = "fastmvitem_fr.php";

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

$bseries = $_SESSION["series_" . $dbname];

$zyear = right($zdb, 4);
$date = date("Y-m-d");
$mtodate = date("Y-m-d");


$mfrdate = date("Y-m-d", strtotime($date . ' - 1 days'));

$timestamp = strtotime($mfrdate);
$day = date('l', $timestamp);

if ($day == 'Sunday') {
    $mfrdate = date("Y-m-d", strtotime($date . ' - 2 days'));
}

if ($usertype != 1) {
    $disp = 'readonly';
} else {
    $disp = '';
}
//browse($zyear);
$mown = mysqli_query($conn, "SELECT `code`, name FROM $zroot.`series` where code = '$bseries' order by name") or die(mysqli_error($conn));
$trow = mysqli_fetch_array($mown);

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

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

        $("body").on('blur', '.itemcode', getItemName);
        $("body").on('blur change', '.itemname', getItemCode);

        function getItemName() {
            var itemcode = $(".itemcode").val();
            $.post('get_itemname.php', {itemcode: itemcode}, function (data) {
                if (data != '') {
                    $(".itemname").html(data).trigger("change");
                }
            });
        }

        function getItemCode() {
            var itemname = $(".itemname").val();
            $.post('get_itemname.php', {itemname: itemname}, function (data) {
                if (data != '') {
                    $(".itemcode").val(data);
                }
            });
        }

    });
</script>

</head>
<body>
    <form method='POST' action="<?= $action; ?>">
        <!-- Default box -->
        <div>
            <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 class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </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 marg">Branch</label>
                            </div>

                            <div class="col-lg-3 col-md-3 col-sm-5 col-xs-5 pad">
                                <?php if ($usertype > 1) { ?>
                                    <input type="text" value="<?php echo $bseries; ?>" class="form-control" name="series" id="series" readonly>
                                <?php } else { ?>
                                    <select class=' party form-control select2' name='series' style="height:33px;">
                                        <option value=""></option>
                                        <?php
                                        $mown = mysqli_query($conn, "SELECT `code`, name FROM $zroot.`series` where code !=  'WH' order by name") or die(mysqli_error($conn));
                                        while ($trow = mysqli_fetch_array($mown)) {
                                            echo "<option value='$trow[code]'>$trow[name]</option>";
                                        }
                                        ?>
                                    </select>
                                <?php } ?>
                            </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 itemgrp' name='itemgrp' class="itemgrp" style="height:33px;">
                                    <option value=""></option>
                                    <?php
                                    $getitemgrp = mysqli_query($conn, "SELECT code, `name` FROM `itemgrp` ORDER BY `name`") or die(mysqli_error($conn));
                                    while ($itemgrp = mysqli_fetch_array($getitemgrp)) {
                                        echo "<option value='$itemgrp[code]'>$itemgrp[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 ">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">
                                <select class='form-control select2 itemname' name='name' class="item" style="height:33px;">
                                    <option value=""></option>
                                    <?php
                                    $getitem = mysqli_query($conn, "SELECT code, `name` FROM `item` 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">From</label>
                            </div>
                            <div class="col-lg-5 col-md-3 col-sm-5 col-xs-5 pad">
                                <input type="date" class="form-control" name="fdate" value="<? 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</label>
                            </div>
                            <div class="col-lg-5 col-md-3 col-sm-5 col-xs-5 pad">
                                <input type="date" class="form-control" name="tdate" value="<? 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">Order By</label>
                                                    </div>
                                                    <div class="col-lg-3 col-md-5 col-sm-8 col-xs-8 pad">
                                                        <select name="orderby" class="form-control select2">
                                                            <option value="1">Amount</option>
                                                            <option value="2">Qty.</option>
                                                        </select>
                                                    </div>
                                                </div>-->
                    </div>
                    <div class="panel-footer">
                        <div class="text-center pad">
                            <div class="panel-footer">
                                <?php include("includes/but_rep.php"); ?>
                            </div>
                        </div>                       
                    </div>
                </div>
            </div>
        </div>
    </form>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
