  <?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 id='validate' class='alert alert-danger solid' style='display:none;'></div>

     <div id='so_po_pending' class='alert alert-danger 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" 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">Machine:</label>
                            <select class="form-control machine"  name="machine" required="required" id="machine">
                                <option>Select Machine</option>
                                <?php foreach ($get_machine as $machine) { ?>
                                  <option value="<?php echo $machine['machine']; ?>"><?php echo machine_name($machine['machine']); ?></option>
                               <?php } ?>
                            </select>
                         </div>
                        </div>
                        <input type="hidden" id="so_id"   name="so_id" class="form-control so_id">
                        <div class="col-lg-3 col-md-6 col-sm-12">
                        <div class="form-group">
                            <label class="form-label">Job </label>
                            <input type="text" id="job"  name="job" readonly="readonly"  class="form-control job">
                        </div>
                    </div>

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

					

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


                                      <div class="col-lg-3 col-md-6 col-sm-12">
                        <div class="form-group">
                            <label class="form-label">Material Weight </label>
                            <input type="text" id="m_weight"  name="m_weight"  class="form-control m_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">Waste Weight </label>
                            <input type="text" id="w_weight"  name="w_weight"  class="form-control w_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">Recived QTY PCS:</label>
                                  <input type="text" id="qty" name="qty" required="required" readonly="readonly"  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">Number of Bags </label>
                            <input type="text" id="qty_b"  name="qty_b"  class="form-control qty_b"  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">Production Remaining QTY:</label>
                                  <input type="text" id="p_qty"  readonly="readonly" name="p_qty" required="required"  class="form-control p_qty">
                             </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>


                            <?php
                           
                            /*$timezone = date("e");
                            
                            if ($time < "19") {
                                $shift= "Morning";
                            } else
                         
                            if ($time >= "12" && $time < "17") {
                                $shift= "Good afternoon";
                            } else
                           
                            if ($time >= "17" && $time < "19") {
                                $shift= "Good evening";
                            } else
                            
                            if ($time >= "19") {
                                $shift= "Good night";
                            }*/
                            ?>

                            <div class="col-lg-3 col-md-6 col-sm-12">
                           <div class="form-group">
                                <label class="form-label">Stock Date:</label>
                                  <input type="date" name="insert_date" id="insert_date" class="form-control required">

                             </div> 
                             
                           </div>

                           
                             <div class="col-lg-3 col-md-6 col-sm-12">
                           <div class="form-group">
                                <label class="form-label">Shift:</label>
                               <select class="form-control shift"  name="shift" required="required" id="shift">
                                 <option value="1">Morning Shift</option>
                                 <option value="2">Night Shift</option>
                              
                            </select>
                            </div> 
                             
                           </div>

                          <div class="col-lg-3 col-md-6 col-sm-12">
                           <div class="form-group">
                                <label class="form-label">Moving to:</label>
                               <select class="form-control moving_to"  name="moving_to" required="required" id="moving_to">
                                 <option value="0">Semi Stock</option>
                                  <option value="1">Packing Depart</option>
                                 
                            </select>
                            </div> 
                             
                           </div>

                     <input type="hidden" id="type" name="type" readonly="readonly"  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").chosen();*/


         /*$("#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);}
                        });
                   });*/


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

                  $("#dis_submit").attr('disabled', false);
    
		                var machine =  this.value;
		                //alert(id);

		                $.ajax({
		                            type:'POST',
		                            url:'<?php echo base_url('Stock/get_semi_production_production_id'); ?>',
		                            data:{machine:machine},
		                            dataType:'json',
		                            success:function(data){
		                            
		                                    $('#job').val(data.job);
		                                    $('#product_id').val(data.product_name);
		                                    
		                                    $('#type').val(data.type); 
                                        $('#so_id').val(data.so_id);
		                                    
                                        $('#emp_id').val(data.emp_id);

                                        $('#qty_b').val(data.get_semi_standard);

                                        $('#total_qty').val(data.total_qty.toFixed(2));
                                        $('#p_qty').val(data.p_qty.toFixed(2));
		                                  	
		                            },
		                            error:function(data){console.log(data.responseText);}
		                        });
		                   });



                   $("#m_weight").keyup(function() {

                //alert('ok');
    
                    var m_weight  = $('#m_weight').val();
                     var machine = $('#machine').val();
                    var  job = $('#job').val();
                      var  so_id = $('#so_id').val();
                      var Product = $('#product_id').val();
                    //alert(id);

                    $.ajax({
                                type:'POST',
                                url:'<?php echo base_url('Stock/calculation_recived_qty'); ?>',
                                data:{m_weight:m_weight,machine:machine,job:job,so_id:so_id,Product:Product},
                                dataType:'json',
                                success:function(data){
                                  console.log(data);
                                  if(data.recived_qty =='false'){

                                        $('#qty').val('');

                                  }else{
                                        //$('#qty').val(data.recived_qty);
                                      $('#qty').val(data.recived_qty);
                                      $('#qty_b').val(data.semi_standard.toFixed(2));
                                  }
                                
                                        
                                },
                                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 add_fg = add_fg_stocks.serialize();
         var inout = $('#inout').val();
    
             if (confirm('Are you sure you want to Add Stock IN.?')) {
            $.ajax({
                type:'POST',
                url:'<?php //echo base_url('Stock/fg_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('Semi Stock added Successfully');
                       setTimeout(function(){location.href="<?php echo base_url('Stock/fg_stock/'); ?>"} , 2000);
                    }/*else if(data.status === 'complete'){
                        $('#validate').show().html('No Production Qty...!');         
                    }else if(data.status === 'so_po_pending'){
                        $('#so_po_pending').show().html('Knidly Complete QC Inspections...!');         
                    }*/
                /*},
                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 add_fg = add_fg_stocks.serialize();
          var inout = $('#inout').val();
          var machine = $('#machine').val();
          var  job = $('#job').val();
          var  product_id = $('#product_id').val();
          var  qty = $('#qty').val();
          var moving_to = $('#moving_to').val();

           if(qty == ''){

             alert('PC Weight Not Inserted Or Semi Standard Not Define..!');
          }else{
     
             if (confirm('Are you sure you want to Add Stock IN.?')) {
            $.ajax({
                type:'POST',
                url:'<?php echo base_url('Stock/semi_add_stock'); ?>',
                data:add_fg,
                dataType:'json',
                success:function(data){
                
                    console.log('first'+data.status);
                    if(data){
                        var id = data;
                        $('#fg_stock_success').show().html('Semi Stock added Successfully');
                       //setTimeout(function(){location.href="<?php //echo base_url('Stock/fg_stock/'); ?>"} , 2000);
                       setTimeout(function(){location.href="<?php echo base_url('Stock/semi_in_print/'); ?>"+id+'/'+moving_to});

                    }else{
                        console.log('third'+data);          
                    }
                },
                error:function(data){console.log(data.responseText);}
            });
        }

         }
        
    });
    
  </script>