       <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src='select2/dist/js/select2.min.js' type='text/javascript'></script>

<!-- CSS -->
<link href='select2/dist/css/select2.min.css' rel='stylesheet' type='text/css'>

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


<?php if($id==1) {?>


            
                       <?php echo form_open(base_url(''),array('class'=>"form-horizontal add_material_bom", 'id'=>"add_material_bom")); ?>
                                    
                                        <?php if($this->session->flashdata('material_success')){ ?>
                                <div class="alert alert-success solid" style='text-align:center;'><?php echo $this->session->flashdata('material_success'); ?></div>
                            <?php } ?>         
                            
                         
                             <div class="card">
                               <input type="hidden" name="product_id" id="product_id" value="<?php echo $segment; ?>" class="form-control required">
                               <input type="hidden" name="main_product" id="main_product" value="<?php echo $main_product; ?>" class="form-control required">

                                <div class="card-body">
                                    <div id='material_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>
                                          
                                            <div class="col-lg-4 col-md-6 col-sm-12">
                                                <div class="form-group">
                                                    <label class="form-label">BOM Componenet:</label>
                                                     <select data-live-search="true" name="material_id" id="material_id" class="form-control material_id">

                                                        <option value="">Select Material</option>
                                                      <?php foreach ($get_materials as $get_materials) { ?>
                                                                
                                                     <option value="<?php echo $get_materials['id']; ?>"><?php echo $get_materials['name']; ?>/ <?php echo $get_materials['color']; ?></option>

                                                        <?php } ?>
                                                  </select>
                                                 </div>
                                                </div>

                                                <div class="col-lg-2 col-md-6 col-sm-12">
                                                 <div class="form-group">
                                                    <label class="form-label">Grade/Remarks:</label>
                                                    <input type="text"  readonly="readonly"   class="form-control grade">
                                                 </div>
                                                </div>


                                                 <div class="col-lg-2 col-md-6 col-sm-12">
                                                <div class="form-group">
                                                    <label class="form-label">Item Code:</label>
                                                    <input type="text" readonly="readonly"  class="form-control code">
                                                 </div>
                                                </div>
                                                

                                                  <div class="col-lg-2 col-md-6 col-sm-12">
                                                <div class="form-group">
                                                    <label class="form-label">Weight/Qty:</label>
                                                      <input type="text" name="weight" id="weight" required  class="form-control">
                                                 </div>
                                                </div>

                                                <div class="col-lg-2 col-md-6 col-sm-12">
                                                  <div class="form-group">
                                                    <label class="form-label">Unit:</label>
                                                     <select name="unit" id="unit" class="form-control" required>
                                                            <option value="">Select Unit</option>
                                                            <?php foreach ($get_units as $get_units) { ?>
                                                                
                                                                 <option value="<?php echo $get_units['id']; ?>"><?php echo $get_units['names']; ?></option>

                                                            <?php } ?>
                                                           
                                                            
                                                            </select>
                                                       </div>

                                                </div>
                                        
                                       </div>
                                         <button type="submit" id="dis_submit" class="btn btn-primary">Submit <i class="icon-arrow-right14 position-right"></i></button>
                                     </div>
                                    </div>
                                    
                          
                           
                               
                                
                                            
                                 <?php form_close();?>
                           
                            <?php }else if($id==2){?>


                                    
            
                       <?php echo form_open(base_url(''),array('class'=>"form-horizontal add_products_bom", 'id'=>"add_products_bom")); ?>
                                    
                                        <?php if($this->session->flashdata('products_bom_success')){ ?>
                                <div class="alert alert-success solid" style='text-align:center;'><?php echo $this->session->flashdata('products_bom_success'); ?></div>
                            <?php } ?>         
                            
                         
                             <div class="card">
                               
                                <div class="card-body">
                                    <div id='products_bom_success' class='alert alert-success solid' style='display:none;'></div>
                                       <div class="row">
                                        <input type="hidden" name="product_id" id="product_id" value="<?php echo $segment; ?>" class="form-control required">
                                            <input type="hidden" name="main_product" id="main_product" value="<?php echo $main_product; ?>" class="form-control required">
                                             <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>
                                          
                                            <div class="col-lg-4 col-md-6 col-sm-12">
                                                <div class="form-group">
                                                    <label class="form-label">BOM Component:</label>
                                               
                                                        <select data-live-search="true" name="child_product" id="child_product" class="form-control child_product">
                                                        <option value="">Select Products</option>
                                                      <?php foreach ($get_products as $get_products) { ?>
                                                                
                                                      <option value="<?php echo $get_products['id']; ?>"><?php echo $get_products['name']; ?>/<?php echo $get_products['pro_code']; ?>/
                                                        <?php if($get_products['product_type']==1){ ?>
                                                         Molded SFG
                                                         <?php }else if($get_products['product_type']==2){ ?>

                                                          Printed SFG

                                                         <?php }elseif ($get_products['product_type']==3) { ?>
                                                            Assembled SFG
                                                             
                                                         <?php  }elseif ($get_products['product_type']==4) { ?>

                                                            Sealed SFG
                                                         <?php  } ?>
                                                        <?php } ?>
                                                  </select>
                                                 </div>
                                                </div>

                                                <div class="col-lg-2 col-md-6 col-sm-12">
                                                 <div class="form-group">
                                                    <label class="form-label">Grade/Remarks:</label>
                                                    <input type="text"  readonly="readonly"   class="form-control grade">
                                                 </div>
                                                </div>


                                                 <div class="col-lg-2 col-md-6 col-sm-12">
                                                <div class="form-group">
                                                    <label class="form-label">Item Code:</label>
                                                    <input type="text"  readonly="readonly"  class="form-control code">
                                                 </div>
                                                </div>
                                                

                                                  <div class="col-lg-2 col-md-6 col-sm-12">
                                                <div class="form-group">
                                                    <label class="form-label">Weight/Qty:</label>
                                                      <input type="text" required="required" name="weight" id="weight"  class="form-control weight">
                                                 </div>
                                                </div>

                                                <div class="col-lg-2 col-md-6 col-sm-12">
                                                  <div class="form-group">
                                                    <label class="form-label">Unit:</label>
                                                     <select required="required" name="unit" id="unit" class="form-control">
                                                            <option value="">Select Unit</option>
                                                            <?php foreach ($get_units as $get_units) { ?>
                                                                
                                                                 <option value="<?php echo $get_units['id']; ?>"><?php echo $get_units['names']; ?></option>

                                                            <?php } ?>
                                                           
                                                            
                                                            </select>
                                                       </div>

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


                                <?php } ?>
<script type='text/javascript'>

     $(".material_id").chosen();

     $(".child_product").chosen();
    
        
    var material = $('#add_material_bom');   
    
    material.on('submit',function(e){
 $("#dis_submit").attr('disabled', true);
        e.preventDefault();
        
        var add_material_bom = material.serialize();
         var id = $('#product_id').val();
        var main_product = $('#main_product').val();
            $.ajax({
                type:'POST',
                url:'<?php echo base_url('Masterlist/boms_details_add_materials'); ?>',
                data:add_material_bom,
                dataType:'json',
                success:function(data){
                    console.log('first'+data.status);
                    if(data.status === 'true'){
                        console.log('second'+data.status);
                        $('#material_success').show().html('Bom added Successfully');
                       setTimeout(function(){location.href="<?php echo base_url('Masterlist/boms_details_materials/'); ?>"+id+"/"+main_product} , 3000);
                    }else{
                        console.log('third'+data);          
                    }
                },
                error:function(data){console.log(data.responseText);}
            });
       // }
    });


    var products_bom = $('#add_products_bom');   
    
    products_bom.on('submit',function(e){
 $("#dis_submit").attr('disabled', true);
        e.preventDefault();
        
        var add_products_bom = products_bom.serialize();
         
        var id = $('#product_id').val();
        var main_product = $('#main_product').val();
            $.ajax({
                type:'POST',
                url:'<?php echo base_url('Masterlist/boms_details_add_child'); ?>',
                data:add_products_bom,
                dataType:'json',
                success:function(data){
                    console.log('first'+data.status);
                    if(data.status === 'true'){
                        console.log('second'+data.status);
                        $('#products_bom_success').show().html('Bom added Successfully');
                       setTimeout(function(){location.href="<?php echo base_url('Masterlist/boms_details_materials/'); ?>"+id+"/"+main_product} , 3000);
                    }else{
                        console.log('third'+data);          
                    }
                },
                error:function(data){console.log(data.responseText);}
            });
       // }
    });

    function delete_data(id) { 
        var id = id;
        var pro_id = $('#product_id').val();
         $.ajax({
                type:'POST',
                url:'<?php echo base_url('Masterlist/bom_delete_material/'); ?>'+id,
                data:id,
                dataType:'json',
                success:function(data){
                    console.log('first'+data.status);
                    
                        console.log('second'+data.status);
                        //debugger;
                        $('#material_bom_danger').show().html('Bom Component Deleted Successfully');
                       setTimeout(function(){location.href="<?php echo base_url('Masterlist/boms_details_materials/'); ?>"+pro_id} , 3000);
                    
                },
                error:function(data){console.log(data.responseText);}
            });
        } 

            $("#material_id").change(function() {
                
                    var id =  this.value;

                    $.ajax({
                                type:'POST',
                                url:'<?php echo base_url('Masterlist/boms_details_materials'); ?>',
                                data:{id:id},
                                dataType:'json',
                                success:function(data){
                                    //console.log(data[0].material_code);
                                    $('.grade').val(data[0].grade);
                                    $('.code').val(data[0].material_code);
                                   
                                   
                                },
                                error:function(data){console.log(data.responseText);}
                            });

                });



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

                
    
                var id =  this.value;

                $.ajax({
                            type:'POST',
                            url:'<?php echo base_url('Masterlist/boms_details'); ?>',
                            data:{id:id},
                            dataType:'json',
                            success:function(data){
                                //console.log(data[0].material_code);
                                $('.grade').val(data[0].remarks);
                                $('.code').val(data[0].pro_code);
                               
                               //$('.weight').val(data[0].weight);
                               
                            },
                            error:function(data){console.log(data.responseText);}
                        });

    });

</script>