<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_production", 'id'=>"add_production")); ?>
                                    
                <?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">Machine:</label>
                            <select class="form-control machine_id"  name="machine_id" required="required" id="machine_id">
                                <option>Select Machine</option>
                                <?php foreach ($get_machine as $machine) { ?>
                                  <option value="<?php echo $machine['production_id']; ?>"><?php echo machine_name($machine['machine']); ?></option>
                               <?php } ?>
                            </select>
                         </div>
                        </div>


                        <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" required="required"  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">Products:</label>
                            <!-- <select class="form-control product_id"  name="product_id" required="required" id="product_id">
                                <option>Select Product</option>
                                <?php foreach ($get_products_semi as $product) { ?>
                                  <option value="<?php echo $product['id']; ?>"><?php echo $product['name']; ?></option>
                               <?php } ?>
                            </select> -->
                             <input type="text"  id="product_id" name="product_id" required="required"  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">Material:</label>
                                <!-- <select class="form-control material_id"  name="material_id" required="required" id="material_id">
                                    <option>Select Material</option>
                                    <?php foreach ($get_raw as $material) { ?>
                                      <option value="<?php echo $material['id']; ?>"><?php echo $material['name']; ?></option>
                                   <?php } ?>
                                </select> -->
                                 <input type="text"  id="material_id" name="material_id" required="required"  class="form-control material_id">
                             </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 value="">Select Option</option>
                                          
                                            <option value="1">IN</option>
                                          <option value="0">Out</option>
                                          
                                        </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="text"  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">Unit:</label>
                                        <select class="form-control" name="unit" required="required" id="unit">
                                       
                                            <option value="">Select Option</option>
                                            <?php foreach($get_unit as $get_units){ ?>
                                            <option value="<?php echo $get_units['id']; ?>"><?php echo $get_units['names']; ?></option>
                                            <?php } ?>
                                        </select>
                                     </div>
                                  </div>

                           <div class="col-lg-3 col-md-6 col-sm-12">
                           <div class="form-group">
                                <label class="form-label">IN Stock Balance:</label>
                                  <input type="text"  id="total_qty" readonly="readonly" 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">Quantity:</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 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'>

    


          /*$(".product_id").chosen();

           $(".material_id").chosen();
*/
            //$(".machine_id").chosen();


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

        
                var product_id = $('#product_id').val();
    
                var material_id = $('#material_id').val();

                 var machine_id = $('#machine_id').val();
                //alert(id);

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

    });


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

                 var machine_id = $('#machine_id').val();
                //alert(id);

                $.ajax({
                            type:'POST',
                            url:'<?php echo base_url('Stock/get_product_from_production'); ?>',
                            data:{machine_id:machine_id},
                            dataType:'json',
                            success:function(data){
                            
                                    /*$('#code').val(data.codes);
                                    $('#type').val(data.type);*/
                                    $('#product_id').val(data.product);
                                    $('#job').val(data.job); 
                                     $('#material_id').val(data.material_id); 

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

                     });


        
   /* var add_production = $('#add_production');   
    
    add_production.on('submit',function(e){
  $("#dis_submit").attr('disabled', true);
        e.preventDefault();
        
        var add_fg = add_production.serialize();
        // var inout = $('#inout').val();
      //if(inout==1){
          if (confirm('Are you sure you want to Add.?')) {
            $.ajax({
                type:'POST',
                url:'<?php echo base_url('Stock/return_production_balance'); ?>',
                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(' 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);}
            });
        //}
        }
    });*/



  var add_production = $('#add_production');   
    
    add_production.on('submit',function(e){
  $("#dis_submit").attr('disabled', true);
        e.preventDefault();
       
        var add_fg = add_production.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/return_production_balance'); ?>',
                data:add_fg,
                dataType:'json',
                success:function(data){
                
                   
                    console.log('first'+data);
                    //if(data.status === 'true'){
                        console.log('second'+data);
                        $('#fg_stock_success').show().html(' Stock Added Successfully');
                        var id = data;
                       //setTimeout(function(){location.href="<?php //echo base_url('Stock/waste_printer/'); ?>"+id});
                         window.location.href="<?php echo base_url('Stock/fg_stock'); ?>";
                    //}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/production_out_stock'); ?>',
                data:add_fg,
                dataType:'json',
                success:function(data){
                
                    console.log('first'+data.status);
                    if(data.status === 'true'){
                        console.log('second'+data.status);
                      window.location.href="<?php echo base_url('Stock/fg_stock'); ?>";
                    }else{
                        console.log('third'+data);          
                    }
                },
                error:function(data){console.log(data.responseText);}
            });

        }
        }
        }
    });
    
  </script>