<script>$(function() {
  $('.selectpicker').selectpicker();
});</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css" rel="stylesheet" />


 <?php echo form_open(base_url(''),array('class'=>"form-horizontal add_fg_stock", 'id'=>"add_fg_stock")); ?>
                                    
                <?php if($this->session->flashdata('fg_stock_success')){ ?>
        <div class="alert alert-success solid" style='text-align:center;'><?php echo $this->session->flashdata('fg_stock_success'); ?></div>
    <?php } ?> 

     <div id='fg_stock_success' class='alert alert-success solid' style='display:none;'></div>   
<div class="row">
         <div class="col-lg-6 col-md-6 col-sm-12">
            <div class="form-group">
               
                <input type="hidden" name="created" id="created" value="<?php echo date('Y-m-d H:i:s'); ?>" class="form-control required">
            </div>
        </div>

        <div class="col-lg-6 col-md-6 col-sm-12">
            <div class="form-group">
               
                <input type="hidden" value="<?php echo date('Y-m-d H:i:s'); ?>" name="modified" id="modified" class="form-control required">
            </div>
        </div>
        <input type="hidden" value="<?php echo date('Y-m-d'); ?>" name="insert_date" id="insert_date" class="form-control required">
          <input type="hidden" name="user_id" id="user_id" value="<?php echo $this->session->userdata('id');?>">

           <div class="col-lg-3 col-md-6 col-sm-12">
           <div class="form-group ">
                            <label class="form-label ">Products:</label>
                            <select class="form-control selectpicker" data-live-search="true" name="product_id" required="required" id="product_id">
                                <option>Select Product</option>
                                <?php foreach ($get_products as $product) { ?>
                                  <option value="<?php echo $product['id']; ?>"><?php echo $product['name']; ?></option>
                               <?php } ?>
                            </select>
                         </div> 
                        </div>
                         
                         <div class="col-lg-3 col-md-6 col-sm-12">
                        <div class="form-group">
                            <label class="form-label">Product Code</label>
                            <input type="text" id="code"  disabled="disabled"  placeholder="Product Code" class="form-control code">
                        </div>
                    </div>

                    <div class="col-lg-3 col-md-6 col-sm-12">
                           <div class="form-group">
                                <label class="form-label">Pack QTY:</label>
                                  <input type="number" id="qty1" name="qty1" required="required" placeholder="0 X 0"  class="form-control qty1">
                             </div> 
                             
                           </div>


                           <div class="col-lg-3 col-md-6 col-sm-12">
                           <div class="form-group">
                                <label class="form-label">Pack QTY:</label>
                                  <input type="number" id="qty2" name="qty2" required="required" placeholder="0 X 0" class="form-control qty2">
                             </div> 
                             
                           </div>

                         
                           <div class="col-lg-3 col-md-6 col-sm-12">
                           <div class="form-group">
                                <label class="form-label">Total:</label>
                                  <input type="text" id="total" readonly="readonly" name="total" class="form-control total">
                             </div> 
                             
                           </div>

                           <div class="col-lg-3 col-md-6 col-sm-12">
                           <div class="form-group">
                                <label class="form-label">Batch #:</label>
                                  <input type="number" id="batch" name="batch" required="required"  class="form-control batch">
                             </div> 
                             
                           </div>
                           
                           
                           <div class="col-lg-3 col-md-6 col-sm-12">
                           <div class="form-group">
                                <label class="form-label">QC Ref #:</label>
                                  <input type="number" id="qc_ref" name="qc_ref" required="required"  class="form-control qc_ref">
                             </div> 
                             
                           </div>

                           <div class="col-lg-3 col-md-6 col-sm-12">
                           <div class="form-group">
                                <label class="form-label">Qty:</label>
                                  <input type="number" id="qty"  name="qty" required="required"  class="form-control qty">
                             </div> 
                             
                           </div>

                           <div class="col-lg-3 col-md-6 col-sm-12">
                           <div class="form-group">
                                <label class="form-label">Carton Weight(kg):</label>
                                  <input type="text" id="carton_weight"  name="carton_weight" required="required"  class="form-control carton_weight" onkeypress="return isNumberKey(event)">
                             </div> 
                             
                           </div>
                           
                           <div class="col-lg-3 col-md-6 col-sm-12">
                           <div class="form-group">
                                <label class="form-label">Total IN Stock:</label>
                                  <input type="text" id="total_qty" name="total_qty" required="required"  class="form-control total_qty">
                             </div> 
                             
                           </div>

                           <div class="col-lg-3 col-md-6 col-sm-12">
                                   <div class="form-group">
                                                    <label class="form-label">IN/OUT:</label>
                                                    <select class="form-control" required="required" id="inout">
                                                        <option>Select Option</option>
                                                        
                                                        <option value="1">IN</option>
                                                      <option value="0">OUT</option>
                                                    </select>
                                                 </div>
                                                </div>

                     <input type="hidden" id="type" name="type" readonly="readonly" value="5"  class="form-control type" >
                         

                        <div class="col-lg-6 col-md-6 col-sm-12">
                            <div class="form-group">
                                <label class="form-label">Remarks:</label>
                                <textarea  class="form-control remarks" name="remarks" id="remarks"></textarea>  
                             </div>
                         </div>

                 </div>

                  <?php form_close();?>
                <button type="submit" id="dis_submit" class="btn btn-primary">Save <i class="icon-arrow-right14 position-right"></i></button>


      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <style>
        .redborder{
            border:1px solid red;
        }
        </style>

    <script type='text/javascript'>

function isNumberKey(evt)
       {
          var charCode = (evt.which) ? evt.which : evt.keyCode;
          if (charCode != 46 && charCode > 31 
            && (charCode < 48 || charCode > 57))
             return false;

          return true;
       }
         

         $("#product_id").change(function() {

                
    
                var product_id =  this.value;
                //alert(id);

                $.ajax({
                            type:'POST',
                            url:'<?php echo base_url('Stock/get_product_code'); ?>',
                            data:{product_id:product_id},
                            dataType:'json',
                            success:function(data){
                            
                                    $('#code').val(data.codes);
                                    $('#type').val(data.type);
                                    $('#total_qty').val(data.total_qty);
                                      
                                  
                            },
                            error:function(data){console.log(data.responseText);}
                        });

    });


        
    var add_fg_stocks = $('#add_fg_stock');   
    
    add_fg_stocks.on('submit',function(e){
  $("#dis_submit").attr('disabled', true);
        e.preventDefault();
        var qty1 = $('#qty1').val();
        var qty2 = $('#qty2').val();
        var total = $('#total').val();
        var add_fg = add_fg_stocks.serialize();
         var inout = $('#inout').val();
      if(inout==1){
          if (confirm('Are you sure you want to Add Stock IN.?')) {
            $.ajax({
                type:'POST',
                url:'<?php echo base_url('Stock/fg_stock'); ?>',
                data:add_fg,
                dataType:'json',
                success:function(data){
                
                    console.log('first'+data.id);
                     if(data.id !=''){
                        console.log('second'+data.id);
                        $('#fg_stock_success').show().html('FG Stock Added Successfully');
                       setTimeout(function(){location.href="<?php echo base_url('Stock/fg_print/'); ?>"+data+"/"+qty1+"/"+qty2+"/"+total} , 2000);

                       //setTimeout(function(){location.href="<?php //echo base_url('Stock/fg_stock/'); ?>"} , 2000);
                    }else{
                        console.log('third'+data);          
                    }
                },
                error:function(data){console.log(data.responseText);}
            });
        }
        }else if(inout == 0){

            var total_qty = parseInt($("#total_qty").val());
            var qty = parseInt($("#qty").val());
           
            if (total_qty < qty) {
                alert("Quantity Greater Then  Quantity..!");
               
            }else{

            if (confirm('Are you sure you want to Add Stock Out.?')) {
            $.ajax({
                type:'POST',
                url:'<?php echo base_url('Stock/out_stock1'); ?>',
                data:add_fg,
                dataType:'json',
                success:function(data){
                
                    console.log('first'+data.status);
                    if(data.status === 'true'){
                        console.log('second'+data.status);
                        $('#fg_stock_success').show().html('FG Stock added Successfully');
                       setTimeout(function(){location.href="<?php echo base_url('Stock/fg_stock/'); ?>"} , 2000);
                    }else{
                        console.log('third'+data);          
                    }
                },
                error:function(data){console.log(data.responseText);}
            });

         }
     }
        }
    });



    $("#qty2").keyup(function() {
  //var dept_cost_hrs = 3.00;
  var qty1 = $('#qty1').val();
  var qty2 = $('#qty2').val();
  
   var total = qty1*qty2;

 //var output=parseInt(total).toLocaleString(); 
  $('#total').val(total);

});

    
  </script>