  <script src="<?php echo base_url();?>assets/choosen.js"></script>
<link rel="stylesheet" href="<?php echo base_url();?>assets/style.css">



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

     <div id='qty_not_match' 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">

                <input type="hidden" value="<?php echo date('Y-m-d'); ?>" name="insert_date" id="insert_date" 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">FG Product:</label>
                        <select class="form-control fg_product" name="fg_product" required="required" id="fg_product">
                          <option value="">Select Option</option>
                                  
                            <?php foreach($fg_product as $fg_products){ ?>
                             <option value="<?php echo $fg_products['id'] ?>"><?php echo $fg_products['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">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">Batch #:</label>
                                  <input type="text" id="batch"  name="batch"  required="required"  class="form-control batch">

                                  <span style="color:red;" id="demo" style='display:none;'></span>

                                   <span style="color:green;" id="demo1" style='display:none;'></span> -
                             </div> 
                             
                           </div>
                           




                             <div  class="col-lg-4 col-md-6 col-sm-12" id="fg_data">
                              
                             </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> -->

                          

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

                       

                 </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'>

        
            $(".fg_product").chosen();




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

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

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


                   
               // $("#batch").click(function() {
            $("#batch").keyup(function(){
                
    
		                var batch =  this.value;
		                //alert(batch);

		                $.ajax({
		                            type:'POST',
		                            url:'<?php echo base_url('Stock/match_batch'); ?>',
		                            data:{batch:batch},
		                            dataType:'json',
		                            success:function(data){
		                            console.log(data);
		                                  if(data.status ==='false') {

                                            
                                            $('#demo1').show().html(' Batch Available...!'); 

                                            $("#dis_submit").attr('disabled', false);
                                             
                                            $('#demo').hide(); 
                                          }else if(data.status ==='true'){

                                            $('#demo1').hide(); 
                                             $('#demo').show().html(' Batch Exists...!');  

                                             $("#dis_submit").attr('disabled', true);



                                          }
		                                    
		                                  
		                            },
		                            error:function(data){console.log(data.responseText);}
		                        });
		                   });

        
    var add_fg_stocks = $('#add_fg_stock');   
    
    add_fg_stocks.on('submit',function(e){
 
        e.preventDefault();
       
        var add_fg = add_fg_stocks.serialize();
     

            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.?')) {

                 $("#dis_submit").attr('disabled', true);

                 $("#dis_submit").attr('disabled', true);
            $.ajax({
                type:'POST',
                url:'<?php echo base_url('Stock/out_stock'); ?>',
                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 === 'true1'){
                         $('#batch_exists').show().html('Batch Exists in Data Base ...!');
                    }else if(data.status === 'qty_not_match'){

                      $('#qty_not_match').show().html('Stock Not Available ...!');
                      
                    }
                },
                error:function(data){console.log(data.responseText);}
            });

        }
        }
       
    });

    
  </script>