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

$formname = 'Change of Rate';
$maindb = 'product';
$file = 'openstock.php';
$trandbexist = 0;
$grid = '';
$keyfld = "code";
$focusfield = "name";
$zroot = $_SESSION["zdbroot"];

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

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

$grate = mysqli_query($conn, "SELECT `rateperc` FROM $zroot.`company`") or die(mysqli_error($conn));
$frate = mysqli_fetch_array($grate);
$rateperc = $frate['rateperc'];


mysqli_select_db($conn, $zdb);

if (isset($_POST["submit"])) {
    $a = 1;
    if ($a == 1) {

        $a = 1;
        $count = $_POST["count"];
        $mvouch = $qvouch;
        $cnt = 0;
        for ($i = 0; $i <= $count; $i++) {
            $cost = $_POST["cost_$i"];
            $selling = $_POST["selling_$i"];
            $msp = $_POST["msp_$i"];
            $mrp = $_POST["mrp_$i"];
            $stkunit = $_POST["stkunit_$i"];
            $minstk = $_POST["minstk_$i"];
            //            $opening = $_POST["opening_$i"];
            $barcode = $_POST["barcode_$i"];

            mysqli_query($conn, "UPDATE `$maindb` SET `cost`='$cost', `rate`='$selling',`mrp`='$mrp', stkunit='$stkunit',minstk='$minstk' WHERE `barcode`='$barcode'") or die(mysqli_error($conn));
        }

        updmess();
        header("Refresh: 1; openstock.php");
    }
}

$senable = "";
$cenable = "";

include("includes/linkshead.php");
?>
<script>
    $(document).ready(function() {
        $(".savetabl").hide();
        // $('body').on('click', '#proceed', getProducts);
        $("body").on("change blur input", '.cost', calCost);

        $("#proceed").click(function() {
            var itemtype = $('#itemtype').val();
            var pouchflag = $('#pouchflag').val();

            $.ajax({
                type: "GET",
                url: "opnstksrch.php",
                data: {itemtype: itemtype, pouchflag: pouchflag},
                success: function(result) {
                    if (result != '') {
                        $(".savetabl").show();
                        $(".proctabl").hide();
                    } else {
                        $(".proctabl").show();
                    }
                    $("#data").html(result);
                    // Reinitialize select2
                    $('.select2').select2();
                }
            });
        });
        
        function calCost() {
            var tr = $(this).closest("tr");
            var cost = tr.find(".cost").val();
            var rateperc = $(".rateperc").val();
            var newrate = parseFloat(cost) + parseFloat((cost * rateperc) / 100);

            tr.find(".rate").val(newrate.toFixed(2));
        }
        //        $('#submit').click(function () {
        //
        //            $('#bill').submit();
        //        });

    });
</script>
<style>
    .mySearchTr th {
        position: sticky;
        top: 0;
        background-color: #004e98;
        z-index: 10;
        border-bottom: 2px solid #ddd;
    }
    td, th {
    padding: 4px;
}
input[type=text]{
  border-bottom: none;
}
</style>
</head>

<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title"><?php echo $formname; ?></h3>
                    </div>
                </div>
                        
                <div class="row proctabl" >
                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                        <label class="label-control marg">Item Type</label>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                        <select name="itemtype" id="itemtype" class="form-control pad select2">
                            <option value=""></option>
                            <option value="Organic">Organic</option>
                            <option value="Other">Conventional</option>
                        </select>
                        <input type="hidden" class="form-control form rateperc" value="<?php echo $rateperc; ?>">                        
                    </div>
                </div>
                <div class="row proctabl">
                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                        <label class="label-control marg">Pouch</label>
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                        <select name="pouchflag" id="pouchflag" class="form-control pad select2">
                            <option value=""></option>
                            <option value="1">Yes</option>
                            <option value="2">No</option>
                        </select>
                    </div>
                </div>
                <div class='panel-footer'>
                    <div style="overflow: auto; ">
                        <table class="table-condensed proctabl" style="margin: 0 auto;">
                            <tr>
                                <td>
                                    <input id="proceed" type='button' name='proceed' value='Proceed' class="btn btn-success">

                                    <!-- <button id='proceed' type='proceed' name='proceed' value='Proceed' class="btn btn-pad btn-square " style="background-color: greenyellow">Proceed
                                                                        </button> -->
                                </td>
                                <td>
                                    <input class="btn btn-danger" type="button" onClick="window.location.href = '<?= 'default.php' ?>'" value="Exit   " />
                                    <!-- <button id='exit' type="button" class="btn btn-pad btn-square " style="background-color:#cccccc; "  onClick="window.location.href = '<?= 'default.php' ?>'">
                                        Exit
                                    </button> -->
                                </td>
                                <!--                                    <td class=" buttonform tooltip-bottom" id="scancel" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Cancel Record">
                                            <button id='cancel' type="button" class="btn btn-pad btn-square " style="background-color:#39CCCC; "  onClick="window.location.href = '<?= $file . "?mode=last" ?>'">
                                                <i class="fa fa-times" style="color:#ffffff"></i>
                                            </button>
                                        </td>-->
                            </tr>
                        </table>
                        <table class="table-condensed savetabl" style="margin: 0 auto;">
                            <tr>
                                <td>
                                    <input id="submit" type='submit' name='submit' value='Save   ' class="btn btn-success">
                                </td>
                                <td>
                                    <input class="btn btn-danger" type="button" onClick="window.location.href = '<?= $file ?>'" value="Exit   " />
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
                <div class="col-lg-12" id="data" style="overflow-x: scroll;height:500px;">

                </div>
            </div>
        </div>
    </form>


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

</html>