<?php 
if(isset($list)){	
	$val=$list;
}
//print_mz($gdg_br);
if(!$view){
    $select='select2';
}
?>
<?php
$cekdup=($type=='New' ? ',ajax[ajaxcekcode]' : '');
?>
    
<div class="row">
	
    <div class="box col-md-12">
    <div class="box-inner">
    
    	<div class="box-content">
       <form id="form" method="post" enctype="multipart/form-data" action="<?php echo base_url($this->utama)?>/submit" class="form-horizontal formular" role="form">
		   
		   <?php echo form_hidden('id',isset($val['id']) ? $val['id'] : '')?>
		   <?php echo form_hidden('param','in')?>
			
		   <div class="form-group">
			   
			   <?php $nm_f="booked";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Booked?</label>
				   </div><div class="col-sm-9" id="">
				   <!--input type="text" name="<?php echo $nm_f?>"  id="<?php echo $nm_f?>" value="<?php echo $a= (isset($val[$nm_f]) ? $val[$nm_f] : '') ?>" class="col-sm-4"-->
				   <?php echo form_dropdown($nm_f,array('no'=>'No','yes'=>'Yes'),(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class=" select2" id="'.$nm_f.'" onchange="gantibooked(this.value)"'); ?>
			   </div>
		   </div>
		   <div class="form-group" id="booking_code">
			   
			   <?php $nm_f="booking_id";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Code Booking</label>
                           </div><div class="col-sm-9" >
				   <!--input type="text" name="<?php echo $nm_f?>"  id="<?php echo $nm_f?>" value="<?php echo $a= (isset($val[$nm_f]) ? $val[$nm_f] : '') ?>" class="col-sm-4"-->
				   <?php echo form_dropdown($nm_f,$opt_booking,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class="select2" id="'.$nm_f.'" onchange="gantibooking(this.value)" '); ?>
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="code";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">No SO</label>
				   </div><div class="col-sm-9">
				   <input type="text" name="<?php echo $nm_f?>"  id="<?php echo $nm_f?>_master_produk" value="<?php echo $a= (isset($val[$nm_f]) ? $val[$nm_f] : '') ?>" class="col-sm-4 validate[required<?php echo $cekdup?>]">
			   </div>
		   </div>
		   <!--div class="form-group">
			   
			   <?php $nm_f="brand";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Merk</label>
                           </div><div class="col-sm-9" id="brands">
				   <!--input type="text" name="<?php echo $nm_f?>"  id="<?php echo $nm_f?>" value="<?php echo $a= (isset($val[$nm_f]) ? $val[$nm_f] : '') ?>" class="col-sm-4"->
				   <?php echo form_dropdown($nm_f,$opt_brand,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class="select2" id="'.$nm_f.'" onchange="gantiproduk(this.value)" '); ?>
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="jenis_produk";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Jenis Produk</label>
				   </div><div class="col-sm-9" id="jenis_produks">
				   <!--input type="text" name="<?php echo $nm_f?>"  id="<?php echo $nm_f?>" value="<?php echo $a= (isset($val[$nm_f]) ? $val[$nm_f] : '') ?>" class="col-sm-4"->
				   <?php echo form_dropdown($nm_f,$opt_jenis,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class=" select2" id="'.$nm_f.'" onchange="gantijenis(this.value)"'); ?>
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="produk";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Produk</label>
				   </div><div class="col-sm-9" id="produks">
				   <?php echo form_dropdown($nm_f,$opt_produk,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class=" select2" id="'.$nm_f.'" onchange="caritgl()"'); ?>
			   </div>
		   </div>
           <div class="form-group">
			   
			   <?php $nm_f="part_number";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Part Number</label>
				   </div><div class="col-sm-9">
				   <input type="text" name="<?php echo $nm_f?>"  id="<?php echo $nm_f?>_master_produk" value="<?php echo $a= (isset($val[$nm_f]) ? $val[$nm_f] : '') ?>" class="col-sm-4 validate[required<?php echo $cekdup?>]"  readonly="readonly">
			   </div>
		   </div>
                   <div class="form-group">
			   
			   <?php $nm_f="gudang";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Lokasi Gudang</label>
				   </div><div class="col-sm-9" id="produks">
				   <?php echo form_dropdown($nm_f,$opt_gudang,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class=" select2" id="'.$nm_f.'" onchange="caritgl()" '); ?>
			   </div>
		   </div>
           
                   <div class="form-group">
			   
			   <?php $nm_f="tgl_tiba";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Tanggal Tiba</label>
				   </div><div class="col-sm-9" id="tgl_tiba_div">
				   <input type="text" name="<?php echo $nm_f?>"  id="<?php echo $nm_f?>" value="<?php echo $a= (isset($val[$nm_f]) ? $val[$nm_f] : '') ?>" class="col-sm-4 validate[required] date-picker">
			   </div>
		   </div>
            
           <div class="form-group">
			   
			   <?php $nm_f="lot_number";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Lot Number</label>
				   </div><div class="col-sm-9">
				   <input type="text" name="<?php echo $nm_f?>"  id="<?php echo $nm_f?>_master_produk" value="<?php echo $a= (isset($val[$nm_f]) ? $val[$nm_f] : '') ?>" class="col-sm-4 validate[required<?php echo $cekdup?>]">
			   </div>
		   </div>
                   <div class="form-group">
			   
			   <?php $nm_f="total";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Jumlah Keluar</label>
                           </div><div class="col-sm-9" id="jumlah_keluars">
                                       
                                       <?php echo form_input($nm_f,(isset($val[$nm_f]) ? $val[$nm_f] : ''),' class="col-sm-4 validate[required]" max="0" id="'.$nm_f.'" ') ?>
			   </div>
                       <div class="col-sm-2" id="max-stok" style="color:red">Jumlah Stok : 0</div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="satuan";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Satuan</label>
				   </div><div class="col-sm-9" id="satuanan">
				   <?php echo form_dropdown($nm_f,$opt_satuan,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class="" id="'.$nm_f.'" '); ?>
			   </div>
		   </div-->
                   <div class="form-group">
			   
			   <?php $nm_f="tgl";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Tanggal Keluar</label>
				   </div><div class="col-sm-9">
				   <input type="text" name="<?php echo $nm_f?>"  id="<?php echo $nm_f?>" value="<?php echo $a= (isset($val[$nm_f]) ? $val[$nm_f] : '') ?>" class="col-sm-4 validate[required] date-picker">
			   </div>
		   </div>
                   <div class="form-group">
			   
			   <?php $nm_f="petugas";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Petugas</label>
				   </div><div class="col-sm-9">
				   <input type="text" name="<?php echo $nm_f?>"  id="<?php echo $nm_f?>" value="<?php echo $a= (isset($val[$nm_f]) ? $val[$nm_f] : $this->session->userdata('webmaster_nama')) ?>" class="col-sm-4 validate[required]">
			   </div>
		   </div>
           <div class="form-group">
			   
			   <?php $nm_f="customer";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Customer</label>
				   </div><div class="col-sm-9" >
				   <?php echo form_dropdown($nm_f,$opt_customer,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class=" select2" id="'.$nm_f.'" '); ?>
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="sales";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Sales</label>
				   </div><div class="col-sm-9" >
				   <?php echo form_dropdown($nm_f,$opt_sales,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class=" select2" id="'.$nm_f.'" '); ?>
			   </div>
		   </div>
                   <div class="form-group">
			   
			   <?php $nm_f="keterangan";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Keterangan</label>
				   </div><div class="col-sm-9" >
				   <?php echo form_textarea($nm_f,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class="" id="'.$nm_f.'" '); ?>
			   </div>
		   </div>
           <fieldset id='area-produk'>
                       <legend style="font-size:15pt;">Produk</legend>
                       <div class="form-group">
                       <div class="col-sm-4">Produk</div>
                       <div class="col-sm-2">Gudang</div>
                       <div class="col-sm-2">Tgl Tiba</div>
                       <div class="col-sm-2">Lot Number</div>
                       <div class="col-sm-1">QTY</div>
                       </div>
                       <?php
                       $a=1;
                       foreach($detail as $det){
                           
            $a=$this->db->query("SELECT SUM(total) as stok,produk,gudang,tgl FROM `sv_stok_trx` WHERE produk='".$det['produk']."' AND gudang='".$det['gudang']."' GROUP BY produk,gudang,tgl")->result_array();
                
            $opt[]='-Tanggal Tiba-';
            foreach($a as $b){
                $opt[$b['tgl']]=$b['tgl'];
            }
                           ?>
                       <div class="input-produk form-group" id="input-produk-<?php echo $a ?>" style="border-bottom:1px #0044cc dashed;margin-bottom:20px;">
                       <?php $select='select2';?>
                                <?php $nm_f="brand[]"; echo str_replace('\n','',form_hidden_('id_detail[]',$det['id']));?>
                                <!--div class="col-sm-2" id="brand-<?php echo $a?>">
                                    <label for="<?php echo $nm_f?>">Produk</label><label>(Hapus)</label></div-->
                           <div class="col-sm-4"> 
                           <div class="col-sm-12 produk" style="margin-bottom:10px;">
                                    <?php echo form_dropdown_('brand[]',GetOptAll('master_brand','-Merk-',array('status'=>'Aktif')),$det['brand'],'class="'.$select.'" id="brand-'.$a.'" onchange="gantibrand(this.value,'.$a.')"')?></div>
                                <div class="col-sm-12 col-md-12 produk" id="jenis-<?php echo $a?>" style="margin-bottom:10px;">
                                    <?php $nm_f="jenis_produk[]"; echo form_dropdown_('jenis_produk[]',GetOptAll('master_jenis_produk','-Jenis Produk-',array('brand'=>'where/'.$det['brand'])),$det['jenis_produk'],'class="'.$select.' form-control" id="jenis_produk-'.$a.'" onchange="gantijenis(this.value,'.$a.')"')?>
                                </div>
                                    
                                <div class="col-sm-12 detailproduk" id="produkz-<?php echo $a?>" style="margin-bottom:10px;">
                                        <?php $nm_f="produk[]";?><?php echo form_dropdown_('produk[]',GetOptAll('master_produk','-Produk-',array('jenis_produk'=>'where/'.$det['jenis_produk'])),$det['produk'],'class="'.$select.'" id="produk-'.$a.'" onchange="gantiproduk(this.value,'.$a.')"')?>
                                    </div>
                                    <!--div class="col-sm-12" style="margin-bottom:10px;">Part Number : <span id="part_number_<?php echo $a ?>"><?php echo GetValue('part_number', 'master_produk',array('id'=>'where/'.$det['produk'])) ?></span>
                                    </div-->
                           </div>
                        <div class="col-sm-2">
                            
                                     <?php $nm_f='gudang[]'; echo form_dropdown($nm_f,GetOptAll('master_gudang','-Gudang-',array()),(isset($det['gudang']) ? $det['gudang'] : ''),'class="'.$select.'" form-control" id="gudang-'.$a.'" onchange="caritgl_baru('.$a.')" '); ?>
                        </div>
                           <div class="col-md-2" id="tgl_tiba_div_<?php echo $a ?>">
                              <?php $nm_f='tgl_tiba[]'; echo form_dropdown('tgl_tiba[]',$opt,$det['tgl_tiba'],'class="select2" onchange="caristok('.$id.')" id="tgl_tiba-'.$id.'"');?>
                           </div>
                       <div class="col-sm-2"><input type="text" name="lot_number[]" class="form-control" value="<?php echo $det['lot_number'] ?>">
                       </div>
                       <!--div class="col-sm-1"><?php $nm_f="satuan[]";?><?php echo form_dropdown_('satuan[]',GetOptAll('ref_satuan','-Produk-',array('status'=>'where/Aktif')),$det['satuan'],'class="'.$select.'" id="$nm_f"')?></div-->
                       <div class="col-sm-1"><input type="text" name="qty[]" class="form-control" id="total-<?php echo $a ?>"  value="<?php echo $det['total'] ?>">
                           <input type='hidden' name="satuan[]" id="satuan-<?php echo $a ?>" value="<?php echo $det['satuan'] ?>">
                           <div class="col-md-12" id="max-stok-<?php echo $a ?>"></div>
                       </div> <label style="cursor:pointer" onclick="deletedetail(<?php echo $det['id'] ?>,<?php echo $val['id'] ?>)">(Hapus)</label>
                       </div>
                       <?php
                       $a++;
                       }
                       ?>
           </fieldset><br/>
                    <div class="form-group">
                    <?php if(!$view){?>
                    <h5><a href="#area-produk" onclick="duplikatproduk()" id="tomboltambah">+ Tambah Produk</a></h5><?php }?>
			</div><br/>
    		<div class="form-group">
            <button type="submit" class="btn pull-right">Submit</button>
            
             </div>
			 </form>
    	</div>
    </div>
    </div>
</div>
<script>
    function caripart(){
        $('#part_number_master_produk').val('');
        var pr=$('#produk').val();
        $.post('<?php echo base_url() ?>stok_in/load_part',{p:pr},function(e){
            
            if(e!=0){
            $('#part_number_master_produk').val(e);
            
            //$('#exp').attr('readonly',true);
            }
        });
    }
    function gantibooked(){
        var bkd=$('#booked').val();
        var id_vs=$("input[name=id]").val();;
        if(bkd=='no' && !id_vs){
            $('#booking_id').val('');
            $('#booking_code').hide();
            
             $('#brands').empty();
             $('#jenis_produks').empty();
             $('#produks').empty();
            $('#brands').load('<?php echo base_url().$this->utama?>/loadbrand');
            
        }
        else if(bkd=='yes' && !id_vs){
            $('#booking_code').show();
             $('#brands').empty();
             $('#jenis_produks').empty();
             $('#produks').empty();
        }
    }
    
    function gantibooking(){
    
        var bkg=$('#booking_id').val();
        $.post('<?php echo base_url().$this->utama?>/loadjumlah',{v:bkg},function(e){
            $('#total').val('100');
        });
        //$('#jumlah_keluars').load('<?php echo base_url().$this->utama?>/loadjumlah',{v:bkg});
        $('#satuanan').load('<?php echo base_url().$this->utama?>/loadsatuan',{v:bkg});
        $('#brands').load('<?php echo base_url().$this->utama?>/loadbrandb',{v:bkg});
        $('#jenis_produks').load('<?php echo base_url().$this->utama?>/loadjenisb',{v:bkg});
        $('#produks').load('<?php echo base_url().$this->utama?>/loadprodukb',{v:bkg});
        caristok();
    }
    function cariexpired(){
        var pr=$('#produk').val();
        //alert(pr);
        $.post('<?php echo base_url() ?>stok_in/load_expired',{p:pr},function(e){
            
            if(e!=0){
            $('#exp').val(e);
            
            //$('#exp').attr('readonly',true);
            }
        });
        caritgl();
        caristok();
        
    }
    function gantibrand(v,id=null){
        $('#jenis-'+id).load('<?php echo base_url()?>stok_out/loadjenis_out',{v:v,id:id},function(e){
            $('#jenis_produk-'+id).css('width','100%').select2({allowClear:true});
        });
        caritgl_baru(id);
    }
    function gantijenis(v,id=null){
        $('#produkz-'+id).load('<?php echo base_url()?>stok_out/loadprodukz_out',{v:v,id:id},function(e){
            $('#produk-'+id).css('width','100%').select2({allowClear:true});
        });
        caritgl_baru(id);
    }
    function gantiproduk(v,id=null){
        $('#part_number_'+id).load('<?php echo base_url()?>stok_out/loadproduknumber',{v:v,id:id},function(e){
             
        });
        caritgl_baru(id);
    }
    function caritgl_baru(id){
        $('#tgl_tiba_div_'+id).empty();
        var pr=$('#produk-'+id).val();
        var gdg=$('#gudang-'+id).val();
        $('#tgl_tiba_div_'+id).load('<?php echo base_url().$this->utama?>/cari_tanggal/<?php echo $val['tgl_tiba'] ?>',{v:pr,g:gdg,i:id});
        caristok(id);
        caripart();
    }
//    function gantiproduk(v){
//        $('#jenis_produks').load('<?php echo base_url().$this->utama?>/loadjenis',{v:v});
//        caristok();
//    }
//    function gantijenis(v){
//        $('#produks').load('<?php echo base_url().$this->utama?>/loadproduk',{v:v});
//        caristok();
//    }
    function caritgl(){
        $('#tgl_tiba_div').empty();
        var pr=$('#produk').val();
        var gdg=$('#gudang').val();
        $('#tgl_tiba_div').load('<?php echo base_url().$this->utama?>/cari_tanggal/<?php echo $val['tgl_tiba'] ?>',{v:pr,g:gdg});
        caristok();
        caripart();
    }
    function caristok(id){
       // alert(id);
       // alert('caristok');
        
        var pr=$('#produk-'+id).val();
        var gdg=$('#gudang-'+id).val();
        var tgl=$('#tgl_tiba-'+id).val();
        var bkd=$('#booked').val();
        $.post('<?php echo base_url() ?>stok_out/cari_stok',{v:pr,t:tgl,g:gdg,b:bkd},function(e){
            //alert(e);
            var stoks=JSON.parse(e);
            if(stoks.stok==null){
            stoks.stok=0;    
            }
            $('#max-stok-'+id).empty();
            $('#max-stok-'+id).append('Jumlah Stok : '+stoks.stok+' '+stoks.st);
            $('#total-'+id).prop('max',stoks.stok);
            //alert(stoks.satuan);
            $('#satuan-'+id).val(stoks.satuan);
            $('#satuan-'+id).prop('readonly',true);
            console.log(e);
        });
    }
	$('#target').mask('9.99');
	$('#comission').mask('9.99');
        
$(document).ready(function(e){
    
                           $('.select2').css('max-width','100%').select2({});
    gantibooked();
        cariexpired();
        $("#form").validate({
            submitHandler: function(form) {
    form.submit();
  },
           ignore: [],      
            rules: {
                'title': {required:true},
                'jenis_produk': {required:true},
                'brand': {required:true},
                'produk': {required:true},
                'gudang': {required:true},
                'lot_number': {required:true},
                'total': {required:true},
                'satuan': {required:true},
                'tgl': {required:true},
                'petugas': {required:true},
                'customer': {required:true},
                'sales': {required:true},
                'keterangan': {required:true}
            }
            , 
            messages: {
                'title': 'Wajib diisi',
                'jenis_produk': 'Wajib diisi',
                'brand': 'Wajib diisi',
                'produk': 'Wajib diisi',
                'gudang': 'Wajib diisi',
                'lot_number': 'Wajib diisi',
                'total': 'Wajib diisi',
                'satuan': 'Wajib diisi',
                'tgl': 'Wajib diisi',
                'petugas': 'Wajib diisi',
                'customer': 'Wajib diisi',
                'sales': 'Wajib diisi',
                'keterangan': 'Wajib diisi'
            }
            });
            
    });
    function deletedetail(id,docs){
        var r = confirm("Anda Yakin Akan Menghapus Data Produk Ini?");
        if (r == true) {
             window.location.assign('<?php echo base_url() ?>stok_out/deletes/'+id+'/'+docs);
        }
    }
     var x = document.getElementById("form");
        function duplikatproduk(){
        var itungproduk=$('.input-produk').length;
        var idsekarang=itungproduk+1;
        if(itungproduk==0){ 
            var oi ='';
        }else{
            var oi='<a href="#" onClick="hapusdiv(this)">(HAPUS)</a>';
        }
//            $( "#area-produk" ).append('<div class="input-produk form-group" id="input-produk-'+idsekarang+'" style="border-bottom:0px #0044cc dashed;margin-bottom:20px;"><div class="col-sm-2 produk" id="brand-'+idsekarang+'"><select name="brand[]" class="required" id="brandz-'+idsekarang+'" onchange="gantibrand(this.value,'+idsekarang+')"><option value="">-Merk-</option><?php foreach($merk_br as $br){?><option value="<?php echo $br['id']?>"><?php echo $br['title']?></option><?php }?></select></div><div class="col-sm-2 produk" id="jenis-'+idsekarang+'"></div><div class="col-sm-4 detailproduk" id="produkz-'+idsekarang+'"></div><div class="col-sm-1" id="qty-'+idsekarang+'"><input type="text" class="form-control" name="qty[]"></div><div class="col-sm-1"><select name="satuan[]" class="form-control" id="satuanz-'+idsekarang+'" ><option value="">-Satuan-</option><?php foreach($gdg_br as $br){?><option value="<?php echo $br['id']?>"><?php echo $br['title']?></option><?php }?></select></div><div class="col-sm-1" id="amount-'+idsekarang+'"><input type="text" name="amount[]" class="form-control"></div><div class="col-sm-1"><label>'+oi+'</label></div></div>');
//            $('#brandz-'+idsekarang).css('width','100%').select2({allowClear:true});
//            $('#satuanz-'+idsekarang).css('width','100%').select2({allowClear:true});
            $('#area-produk').append('<div class="input-produk form-group" id="input-produk-'+idsekarang+'" style="border-bottom:1px #0044cc dashed;margin-bottom:20px;"><img src="<?php echo base_url().'assets/img/wait.gif'?>" width="100px"></div>');
            $('#tomboltambah').hide();
            setTimeout(function(){ $('#input-produk-'+idsekarang).load('<?php echo base_url()?>load/barang/'+idsekarang,function(e){
                    $('#tomboltambah').show();
            }); }, 200);
            
                
	}
</script>