<script type="text/javascript">
    $( document ).ready(function() {
        drawtabledata();
        $('#filter_begin').datepicker().on('changeDate', function (ev) { 
            drawtabledata();
        });
        $('#filter_end').datepicker().on('changeDate', function (ev) { 
            drawtabledata();
        });
    });
    function drawtabledata() {
        var filter_begin =  $("#filter_begin").val();
        var filter_end =  $("#filter_end").val();
        $('#edistockdiarytable').DataTable().destroy();
        var table = $('#edistockdiarytable').DataTable( {
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": "<?php echo base_url('inventory/stock/getstockdiary') ?>?filter_begin="+filter_begin+"&filter_end="+filter_end,
            "lengthMenu": [[20, 50, 100], [20, 50, 100]],
            "language": {
                "lengthMenu": "<?php echo $this->lang->line("DISPLAY"); ?> _MENU_ <?php echo $this->lang->line("RECORDSPERPAGE"); ?>",
                "zeroRecords": "<?php echo $this->lang->line("NOTHINGFOUND"); ?>",
                "info": "<?php echo $this->lang->line("SHOWINGPAGE"); ?> _PAGE_ <?php echo $this->lang->line("OF"); ?> _PAGES_",
                "infoEmpty": "<?php echo $this->lang->line("NORECORDSAVAILABLE"); ?>",
                "infoFiltered": "(<?php echo $this->lang->line("FILTEREDFROM"); ?> _MAX_ <?php echo $this->lang->line("TOTALRECORDS"); ?>)",
                "search": "<?php echo $this->lang->line("SEARCH"); ?> : ",
                "paginate": {
                    "previous": "<?php echo $this->lang->line("PREVIOUS"); ?>",
                    "next":"<?php echo $this->lang->line("NEXT"); ?>"
                }
            },
            "columnDefs" : [],
        });
    }
</script>

<script type="text/javascript">
  function myFunction(id) {
   // id = $(this).attr('id');
    $.ajax({
        url: base_url + 'inventory/stock/editstockdiary/'+ id,
        success:function(data){
            var json = $.parseJSON(data);
            for (var i=0;i<json.length;++i) {
                $("#id").val(json[i].ID);
                $("#DATE").val(json[i].DATE);
                $("#REASON").val(json[i].REASON);
                $("#LOCATION").val(json[i].LOCATION);
                $("#CODE").val(json[i].CODE);
                $("#PRODUCT").val(json[i].PRODUCT);
                $("#UNITS").val(json[i].UNITS);
                $("#PRICE").val(json[i].PRICE);
                $("#SUPPLIER").val(json[i].SUPPLIER);
            }
        }
    });
}


 function confirmFunction(id) {
    $("#okid").val(id);
}

  function deleteFunction() {
     id = $("#okid").val();
    $.ajax({
        url: base_url + 'inventory/stock/deletestockdiary/'+ id,
        success:function(data){
         location.reload(true);
          $("#confirmmodal").hide();
         $("#modal-success").show();

        }
    });
}
</script>
<style>
    .select2-container {
        width: 100% !important;
    }
</style>
            <!-- Page Content -->
            <div class="page-content">
        
                <!-- Page Header -->
                <div class="page-header position-relative">
                     <div class="header-title">
                        <h1>
                           <?php echo $this->lang->line("UPDATESTOCK"); ?>
                        </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-xs-12 col-md-12">
                            <div class="widget">
                              
    

<div id="modal-success" class="modal modal-message modal-success" style="display: none;">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <i class="glyphicon glyphicon-check"></i>
                </div>
                <div class="modal-title"><?php echo $this->lang->line("DELETESUCCESS"); ?></div>

                
                <div class="modal-footer">
                    <button type="button" class="btn btn-success" data-dismiss="modal"><?php echo $this->lang->line("OK"); ?></button>
                </div>
            </div> <!-- / .modal-content -->
        </div> <!-- / .modal-dialog -->
    </div>
                    
                              
                                <div class="widget-body">
                                    <div class="table-toolbar">
                                       <div class="form-group" style="width: 5%;float: left;"> 
                                           
                                              <a class="btn btn-success shiny new" href="<?php echo base_url(); ?>inventory/stock/createnew/"
                                   class="btn btn-default">
                                    <i class="fa fa-plus"></i>
                                </a>
                                       </div>
                                       <div class="form-group" style="float: left;">
                                            <div class="input-group" style="width: 175px;float: left;margin-left: 15px;">
                                                <input class="form-control date-picker" name="filter_begin" id="filter_begin"
                                                   type="text" placeholder="<?php echo $this->lang->line("FROM"); ?>"
                                                   data-date-format="yyyy-mm-dd" readonly value="<?php echo date("d-m-Y",strtotime("-1 month")); ?>" />
                                                <span class="input-group-addon">
                                                    <i class="fa fa-calendar"></i>
                                                </span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="input-group" style="width: 175px;padding-left: 15px;">
                                                <input class="form-control date-picker" name="filter_end" id="filter_end" type="text"
                                                    placeholder="<?php echo $this->lang->line("TO"); ?>"
                                                    data-date-format="yyyy-mm-dd" readonly value="<?php echo date("d-m-Y"); ?>" />
                                                    <span class="input-group-addon">
                                                        <i class="fa fa-calendar"></i>
                                                    </span>
                                            </div>
                                        </div>
                                        
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
        <form id="frmstockdiary" action="" method="post">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title"><?php echo $this->lang->line("UPDATESTOCK"); ?></h4>
      </div>
      <div class="modal-body">
       <div class="row">
           <div class="alert alert-success" id="successmessagealert" style="display: none;">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <?php echo $this->lang->line("SAVEDSUCCESSFULLY"); ?>
        </div>
            <div class="col-md-12">
                <input type="hidden" id="id" name="id">
                <div class="form-group">
                    <label><?php echo $this->lang->line("DATE"); ?></label>
                    <input type="text" class="form-control date-picker" name="DATENEW" id="DATENEW"
                        placeholder="<?php echo $this->lang->line("SELECT")." ".$this->lang->line("DATE"); ?>"
                        data-date-format="dd-mm-yyyy" readonly value="<?php echo date("d-m-Y"); ?>" />
                </div>
                <div class="form-group">
                    <label><?php echo $this->lang->line("SELECTREASON"); ?></label>
                    <select id="REASON" name="REASON" class="form-control" onchange="reasonselect(this.value)">
                        <option value=""><?php echo $this->lang->line("SELECTREASON"); ?></option>
                        <option role="option" value="1"><?php echo $this->lang->line("INPURCHASE"); ?></option>
                        <option role="option" value="2"><?php echo $this->lang->line("INREFUND"); ?></option>
                        <option role="option" value="4"><?php echo $this->lang->line("INMOVEMENT"); ?></option>
                        <option role="option" value="5"><?php echo $this->lang->line("INPRODUCTION"); ?></option>
                        <option role="option" value="-1"><?php echo $this->lang->line("OUTSALE"); ?></option>
                        <option role="option" value="-2"><?php echo $this->lang->line("OUTREFUND"); ?></option>
                        <option role="option" value="-3"><?php echo $this->lang->line("OUTBREAK"); ?></option>
                        <option role="option" value="-4"><?php echo $this->lang->line("OUTMOVEMENT"); ?></option>
                        <option role="option" value="-5"><?php echo $this->lang->line("OUTPRODUCTION"); ?></option>
                        <option role="option" value="1000"><?php echo $this->lang->line("CROSSING"); ?></option>
                        <option role="option" value="999"><?php echo $this->lang->line("REQUEST"); ?></option>
                    </select>
                </div>
                <div class="form-group">
                    <label><?php echo $this->lang->line("SELECTWAREHOUSE"); ?></label>
                   <select role="select" id="LOCATION" name="LOCATION" class="form-control">
                       <option value=""><?php echo $this->lang->line("SELECTWAREHOUSE"); ?></option>
                        <?php foreach($locations as $locations_list) { ?>
                            <option value="<?php echo $locations_list->ID;?>"><?php echo $locations_list->NAME;?></option>
                        <?php } ?>
                   </select>
                </div>
                <div class="form-group">
                    <label id="lbllocationto" style="display: none;"><?php echo $this->lang->line("TOWAREHOUSE"); ?></label>
                      <select role="select" id="LOCATION1" name="LOCATION1" class="form-control" style="display: none;">
                     <option value=""><?php echo $this->lang->line("TOWAREHOUSE"); ?></option>
                    <?php foreach($locations as $locations_list) { ?>
                         <option value="<?php echo $locations_list->ID;?>"><?php echo $locations_list->NAME;?></option>
                         <?php } ?>
                   </select>
                </div>
                <div class="form-group">
                    <label><?php echo $this->lang->line("CODE"); ?></label>
                    <input type="text" class="form-control" name="CODE" id="CODE" placeholder="<?php echo $this->lang->line("CODE"); ?>">
                </div>
                <div class="form-group">
<!--                    <label><?php // echo $this->lang->line("SELECTPRODUCT"); ?></label>
                    <select role="select" id="PRODUCT" name="PRODUCT" class="form-control">
                       <option value=""><?php // echo $this->lang->line("SELECTPRODUCT"); ?></option>
                            <?php // foreach($product as $product_list) { ?>
                         <option value="<?php // echo $product_list->ID;?>"><?php // echo $product_list->NAME;?></option>
                         <?php // } ?>
                   </select>-->
                    <label><?php echo $this->lang->line("SELECTPRODUCT"); ?></label>
                    <div class="input-group" style="width: 100%;">
                        <a href="#" data-toggle="modal" data-target="#MYMODALPRODUCT">
                            <div class="form-control" id="txtitemname"></div>
                        </a>
                        <span class="input-group-btn">
                            <a href="#" class="btn btn-sky shiny" data-toggle="modal" data-target="#MYMODALPRODUCT">
                                <i class="fa fa-search"></i>
                                <?php echo $this->lang->line("BROWSE"); ?></a>
                        </span>
                        <input type="hidden" name="PRODUCT" id="PRODUCT" />
                    </div>
                </div>
                <div class="form-group">
                    <label><?php echo $this->lang->line("UNITS"); ?></label>
                    <input type="text" class="form-control" name="UNITS" id="UNITS" placeholder="<?php echo $this->lang->line("UNITS"); ?>">
                </div>
                <div class="form-group">
                    <label><?php echo $this->lang->line("PRICE"); ?></label>
                    <input type="text" class="form-control" name="PRICE" id="PRICE" placeholder="<?php echo $this->lang->line("PRICE"); ?>">
                </div>
            </div>
        </div>
      </div>
      <div class="modal-footer">
          <button type="submit" class="btn btn-success shiny" id="saveandnewbtn"> <?php echo $this->lang->line("SAVEANDNEW"); ?> </button>
        <button type="button" class="btn btn-danger shiny" data-dismiss="modal"><?php echo $this->lang->line("CLOSE"); ?></button>
      </div>
    </form>
    </div>
</div>
</div>


 <div id="confirmmodal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title"><?php echo $this->lang->line("AREYOUSURETODELETE"); ?></h4>
      </div>
      <div class="modal-footer">
        <input type="hidden" id="okid" value="">
        <button type="button" class="btn btn-success" onclick="return deleteFunction()"><?php echo $this->lang->line("OK"); ?></button>
        <button type="button" class="btn btn-danger" data-dismiss="modal"><?php echo $this->lang->line("CLOSE"); ?></button>
      </div>
    </div>
</div>

</div>
                                        
                                
                                    <div class="table-responsive">
                                    <table class="table table-striped table-hover table-bordered editable" id="edistockdiarytable">
                                        <thead>
                                            <tr role="row">
                                                <th style="width: 70px;">
                                                    <?php echo $this->lang->line("ID"); ?>
                                                </th>
                                                <th style="width: 120px;">
                                                    <?php echo $this->lang->line("DATE"); ?>
                                                </th>
                                                <th style="width: 120px;">
                                                    <?php echo $this->lang->line("REASON"); ?>
                                                </th>
                                                <th style="width: 140px;">
                                                    <?php echo $this->lang->line("LOCATION"); ?>
                                                </th>
                                                <th style="width: 100px;">
                                                    <?php echo $this->lang->line("CODE"); ?>
                                                </th>
                                                <th style="width: 200px;">
                                                    <?php echo $this->lang->line("PRODUCT"); ?>
                                                </th>
                                                  <th style="width: 50px;">
                                                    <?php echo $this->lang->line("UNITS"); ?>
                                                </th>
                                                <th style="width: 100px;">
                                                    <?php echo $this->lang->line("PRICE"); ?>
                                                </th>
                                                <th style="width: 150px;">
                                                    <?php echo $this->lang->line("SUPPLIER"); ?>
                                                </th>
                                            </tr>
                                        </thead>
                                    </table>
                                 </div>
                                </div>
                            </div>
                        </div>
                    </div>
                   
                </div>
                <!-- /Page Body -->
            </div>
            <!-- /Page Content -->
        </div>
        <!-- /Page Container -->
        <!-- Main Container -->

        <div id="MYMODALPRODUCT" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-body">
                <div class="widget-body no-padding" id="productlistselect" style="box-shadow: none;">
                    <table class="table table-bordered table-hover table-striped" id="productsdatatablelist" style="padding: 6px !important;">
                        <thead class="bordered-darkorange">
                            <tr role="row">
                                <th><?php echo $this->lang->line("CATEGORY"); ?></th>
                                <th><?php echo $this->lang->line("CODE"); ?></th>
                                <th><?php echo $this->lang->line("NAME"); ?></th>
                                <th><?php echo $this->lang->line("PRICEBUY"); ?></th>
                                <th><?php echo $this->lang->line("PRICESELL"); ?></th>
                                <th><?php echo $this->lang->line("STOCKVALUE"); ?></th>
                            </tr>
                        </thead>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
        
    </div>
    <script src="<?php echo base_url();?>assets/js/dropzone/dropzone.min.js"></script>
    <script src="<?php echo base_url();?>assets/js/select2/select2.js"></script>
<!--    <script src="<?php echo base_url();?>assets/js/validation/bootstrapValidator.js"></script>-->
    <script src="<?php echo base_url()?>assets/js/jquery.validate.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#frmstockdiary').validate({ // initialize the plugin
                rules: {
                    REASON: {
                        required: true
                    },
                    LOCATION: {
                        required: true
                    },
                    CODE: {
                        required: true
                    },
                    PRODUCT: {
                        required: true
                    },
                    UNITS: {
                        required: true
                    },
                    PRICE: {
                        required: true
                    },
                    LOCATION1: {
                        required: true
                    }
                },
                submitHandler: function (form) {
                    $("#saveandnewbtn").prop("disabled",true);
                    var REASON     =   $("#REASON").val();
                    var LOCATION   =   $("#LOCATION").val();
                    var LOCATION1  =   $("#LOCATION1").val();
                    var PRODUCT    =   $("#PRODUCT").val();
                    $.ajax({
                        method: "POST",
                        url: "<?php echo base_url('inventory/stock/addstockdiary'); ?>",
                        data:$("#frmstockdiary input").serialize()+"&REASON="+REASON+"&LOCATION="+LOCATION+"&LOCATION1="
                        +LOCATION1+"&PRODUCT="+PRODUCT
                    }).done(function (msg) {
                        if(msg=='Done'){
                            $("#successmessagealert").show();
                            var table = $('#edistockdiarytable').DataTable();
//                            table.ajax.reload();
                            $('#frmstockdiary')[0].reset();
                            $("#saveandnewbtn").prop("disabled",false);
                        }
                    });
                    return false;
                }
            });
        });

        $(document).on('keydown', '#CODE', function (e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            if (e.type == 'keydown') {
                if (code == 13) {
                    e.preventDefault();
                    $(".loading-container").removeClass("loading-inactive");
                    $.ajax({
                        method: "GET",
                        url: base_url+"inventory/Products/getproductsfromcode/",
                        data: {term: $(this).val()},
                        dataType: "json"
                    }).done(function (msg) {
                        $(".loading-container").addClass("loading-inactive");
                        if(!$.isArray(msg) ||  !msg.length) {
                            resetform();
                        } else {
                            $.each(msg, function (index, element) {
                                $('#PRODUCT').val(element.ID);
                                $('#txtitemname').html(element.NAME);
                                var REASON = $("#REASON").val();
                                if(REASON>0) {
                                    $('#PRICE').val(element.PRICEBUY);
                                } else {
                                    $("#PRICE").val(element.PRICESELL);
                                }
                            });
                        }
                    });
                }
            }
        });
        
        function reasonselect(reasonId) {
            if(reasonId=='1000'){
                $("#LOCATION1").show();
                $("#lbllocationto").show();
            } else {
                $("#LOCATION1").hide();
                $("#lbllocationto").hide();
            }
        }
//        $("#PRODUCT").select2();
        $(document.body).on("change","#PRODUCT",function(){
        if($(this).val()!='') {
            $.ajax({
                method: "GET",
                url: base_url+"inventory/Products/getproductsfromid/",
                data: {id: $(this).val()},
                dataType: "json"
            }).done(function (msg) {
                $(".loading-container").addClass("loading-inactive");
                $.each(msg, function (index, element) {
                    $('#CODE').val(element.CODE);
                    $('#PRICE').val(element.PRICEBUY);
                });
            });
        } else {
            $('#CODE').val('');
            $('#PRICE').val('');
        }
    });
    function resetform() {
        $("#frmstockdiary")[0].reset();
//        $("#PRODUCT").select2('val', '');
    }
    
    $('#myModal').on('shown.bs.modal', function () {
        $('#REASON').focus();
    });
    
    $(document).ready(function() {
        var table = $('#productsdatatablelist').DataTable( {
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": base_url+"inventory/products/breakdownproductlistall",
            "oLanguage": {
                "sEmptyTable":     "<?php echo $this->lang->line("NOTHINGFOUND"); ?>",
                "sInfo":           "<?php echo $this->lang->line("SHOWINGPAGE"); ?> _START_ <?php echo $this->lang->line("TO"); ?> _END_ <?php echo $this->lang->line("OF"); ?> _TOTAL_ <?php echo $this->lang->line("ENTRIES"); ?>",
                "sInfoEmpty":      "<?php echo $this->lang->line("SHOWINGPAGE"); ?> 0 <?php echo $this->lang->line("TO"); ?> 0 <?php echo $this->lang->line("OF"); ?> 0 <?php echo $this->lang->line("ENTRIES"); ?>",
                "sInfoPostFix":    "",
                "sInfoThousands":  ",",
                "sLengthMenu":     "<?php echo $this->lang->line("SHOW"); ?> _MENU_ <?php echo $this->lang->line("ENTRIES"); ?>",
                "sLoadingRecords": "<?php echo $this->lang->line("LOADING"); ?>",
                "sProcessing":     "<?php echo $this->lang->line("PROCESSING"); ?>",
                "sSearch":         "",
                "sZeroRecords":    "<?php echo $this->lang->line("NORECORDSAVAILABLE"); ?>",
                "oPaginate": {
                    "sFirst":    "<?php echo $this->lang->line("FIRST"); ?>",
                    "sLast":     "<?php echo $this->lang->line("LAST"); ?>",
                    "sNext":     "<?php echo $this->lang->line("NEXT"); ?>",
                    "sPrevious": "<?php echo $this->lang->line("PREVIOUS"); ?>"
                }
            },
            "columnDefs" : [ 
                {"targets": [ 5 ],"sortable" : false}
            ],
        });
    });
    
    function selectitemname(pricebuy='',id='',barcode='',prname='',pricesell='') {
        $('#PRODUCT').val(id);
        $('#CODE').val(barcode);
        $('#UNITS').focus();
        $('#txtitemname').html(prname);
        var REASON = $("#REASON").val();
        if(REASON>0) {
            $("#PRICE").val(pricebuy);
        } else {
            $("#PRICE").val(pricesell);
        }
        $('#MYMODALPRODUCT').modal('hide');
        $("#UNITS").focus();
    }
    
    </script>