<script type="text/javascript">
    var mydata = new Array();
    var data = <?php echo $fields; ?>;
    // alert(data);
    $(document).ready(function () {
        $("button").button();
        $("#fields").empty();
        fields = "";
        for (var k in data) {
            if (data.hasOwnProperty(k)) {
                if (k == "pricesell")
                {
                    fields = fields + "<div class='form-group'><label for='" + k + "' class='col-sm-2 control-label no-padding-right'>" + k + "</label><div class='col-sm-10'><input type='text' class='form-control' name='"+ k +"' id='" + k + "' value='" + Encode(data[k]) + "'></div></div>";
                } else
                {
                    fields = fields + "<div class='form-group'><label for='" + k + "' class='col-sm-2 control-label no-padding-right'>" + k + "</label><div class='col-sm-10'><input type='text' name='"+ k +"' class='form-control' id='" + k + "' value='" + data[k] + "'></div></div>";
                }
            }
        }
        $("#fields").append(fields);
    });

</script>
<script type="text/javascript">
    function Encode(message)
    {
        var alphabet, coded, i, ch, index;
        //012..9
        key = 'ABCDEFGHIJK';
        alphabet = "0123456789";

        coded = "";
        for (i = 0; i < message.length; i++) {
            ch = message.charAt(i);
            index = alphabet.indexOf(ch);
            if (index == -1) {
                coded = coded + ch;
            } else {
                coded = coded + key.charAt(index);
            }
        }
        return coded;
    }
    
</script>
<!-- Page Content -->
<div class="page-content">

    <!-- Page Header -->
    <div class="page-header position-relative">
        <div class="header-title">
            <h1><?php echo $product[0]->NAME; ?></h1>
        </div>
        <!--Header Buttons-->
        <div class="header-buttons">
            <a class="sidebar-toggler" href="#">
                <i class="fa fa-arrows-h"></i>
            </a>
            <a class="refresh" id="refresh-toggler" href="">
                <i class="glyphicon glyphicon-refresh"></i>
            </a>
            <a class="fullscreen" id="fullscreen-toggler" href="#">
                <i class="glyphicon glyphicon-fullscreen"></i>
            </a>
        </div>
        <!--Header Buttons End-->
    </div>
    <!-- /Page Header -->
    <!-- Page Body -->
    <div class="page-body">
        <div class="row">
            <div class="col-lg-6 col-sm-6 col-xs-12">
                <div class="widget">
                    <div class="widget-header bordered-bottom bordered-palegreen">
                        <span class="widget-caption">Current Stock</span>
                    </div>
                    <div class="widget-body">
                        <form name="frmcurrentstock" id="frmcurrentstock" action="<?php echo base_url('inventory/products/reorderlevel/'); ?>" method="post">
                            <input type="hidden" name="prodId" value="<?php echo $productid; ?>" />
                        <div class="form-horizontal form-bordered" role="form">
                            <?php
                            if ($this->session->flashdata('success_msg')) {
                            ?>
                                <div class="alert alert-success">
                                    <a href="#" class="close" data-dismiss="alert">&times;</a>
                                    <?php echo $this->session->flashdata('success_msg'); $this->session->unset_userdata('success_msg');?>
                                </div>
                            <?php
                            }
                            ?>
                            <?php
                            foreach ($stock as $stock_item) { ?>
                            <input type="hidden" name="LOCATION[]" id="LOCATION" value="<?php echo $stock_item["LOCATIONID"]; ?>" />
                                <div class="form-title"><?php echo $stock_item['LOCATION']; ?></div>
                                <div class="row">
                                    <div class="col-sm-4">
                                        <label for="units">Stock</label>
                                        <span class="input-icon icon-right">
                                            <input type="text" class="form-control text-align-center" readonly="true" id="units" value="<?php echo $stock_item['UNITS']; ?>" placeholder="">
                                        </span>
                                    </div>
                                    <div class="col-sm-4">
                                        <label for="minimum">Min</label>
                                        <span class="input-icon icon-right">
                                            <input type="text" class="form-control text-align-center" name="minimum[]" id="minimum" value="<?php echo $stock_item['STOCKSECURITY']; ?>" placeholder="">
                                        </span>
                                    </div>
                                    <div class="col-sm-4">
                                        <label for="maximum">Max</label>
                                        <span class="input-icon icon-right">
                                            <input type="text" class="form-control text-align-center " name="maxmimum[]" id="maxmimum" value="<?php echo $stock_item['STOCKMAXIMUM']; ?>" placeholder="">
                                        </span>
                                    </div>
                                </div>
                                <br>
                            <?php } ?>
                                <div class='form-group'>
                            <div class='col-sm-12'>
                                <button type='submit' class='btn btn-blue'>Submit</button>
                            </div>
                        </div>
                        </div>
                        </form>
                    </div>
                </div>
                
                <div class="widget">
                    <div class="widget-header bordered-bottom bordered-blue">
                        <span class="widget-caption">Product Tariffs</span>
                    </div>
                    <div class="widget-body">
                        <?php
                            if ($this->session->flashdata('success_msg1')) {
                            ?>
                                <div class="alert alert-success">
                                    <a href="#" class="close" data-dismiss="alert">&times;</a>
                                    <?php echo $this->session->flashdata('success_msg1'); $this->session->unset_userdata('success_msg1');?>
                                </div>
                            <?php
                            }
                            ?>
                        <form name="frmtariff" id="frmtariff" action="<?php echo base_url('inventory/products/savetariff/'); ?>" method="post">
                        <div class="form-horizontal form-bordered" role="form">
                            <input type="hidden" name="prodId" value="<?php echo $productid; ?>" />
                            <?php
                            if($tarifflist){
                                foreach($tarifflist as $tariffrec){
                                    
                            ?>
                                <div class="form-title"><?php echo $tariffrec->NAME; ?></div>
                                <div class="row">
                                    <div class="col-sm-6">
                                        <label for="minimum">Price</label>
                                        <input type="hidden" name="tariffid[]" value="<?php echo $tariffrec->ID; ?>" />
                                        <span class="input-icon icon-right">
                                            <input type="text" class="form-control text-align-center" id="price_<?php echo $tariffrec->NAME; ?>" value="" placeholder="" name="price[]" onchange="calcsellwithtax(this.value,'<?php echo $tariffrec->NAME; ?>')" />
                                        </span>
                                    </div>
                                    <div class="col-sm-6">
                                        <label for="maximum">Price+Tax</label>
                                        <span class="input-icon icon-right">
                                            <input type="text" class="form-control text-align-center " id="pricetax_<?php echo $tariffrec->NAME; ?>" value="" placeholder="" />
                                        </span>
                                    </div>
                                </div>
                                <br>
                            <?php
                                }
                            }
                            ?>
                                <div class='form-group'>
                                    <div class='col-sm-12'>
                                        <button type='submit' class='btn btn-blue'>Submit</button>
                                    </div>
                                </div>
                        </div>
                            </form>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-6 col-sm-6 col-xs-12">
                <div class="widget">
                    <div class="widget-header bordered-bottom bordered-blue">
                        <span class="widget-caption">Label</span>
                    </div>
                    <div class="widget-body">
                        <form class="form-horizontal" role="form" action="<?= site_url() . 'inventory/prodlabel/printlabel'; ?>" method="post" name="prodlabel" >

                            <input id="productid" name="productid" value="<?= $productid ?>" type="hidden"/>
                            <div id="fields"></div>

                            <div class='form-group'>
                                <div class='col-sm-offset-2 col-sm-10'>
                                    <button type='submit' class='btn btn-blue'>Submit</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    function calcsellwithtax(sellPrice='',tarfname=''){
        $.ajax({    //create an ajax request to load_page.php
            type: "POST",
            data:  {tax_id:'<?php echo $product[0]->TAXCAT; ?>'},
            dataType: "JSON",
            url: base_url+"settings/taxes/taxCalcultion",                   
            success: function(response){ 
                if(response!=false) {
                   var taxRate = response[0].MAXRATE;
                    var sellPricewithTax = (parseFloat(sellPrice)*taxRate)+parseFloat(sellPrice);
                } else {
                   var sellPricewithTax = sellPrice;
                }
                $("#pricetax_"+tarfname).val(parseFloat(sellPricewithTax).toFixed(2));
              }
        });
     }
    
</script>