<?php error_reporting(E_ALL ^ E_NOTICE);
if(isset($list)){	
	$val=$list;
}
?>
<?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" style="display:none">
			   
			   <?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?>">Code</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_awal";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Lokasi Gudang Awal</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="gudang" 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="gudang_akhir";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Lokasi Gudang Akhir</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.'" '); ?>
			   </div>
		   </div>
			   </div>
		   </div-->
    		<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();
        if(bkd=='no'){
            $('#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'){
            $('#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();
        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();
    }
    function caristok(){
       // alert('caristok');
        
        caripart();
        var pr=$('#produk').val();
        var gdg=$('#gudang').val();
        var tgl=$('#tgl_tiba').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('caristok');
            var stoks=JSON.parse(e);
            if(stoks.stok==null){
            stoks.stok=0;    
            }
            $('#max-stok').empty();
            $('#max-stok').append('Jumlah Stok : '+stoks.stok);
            $('#total').prop('max',stoks.stok);
            //alert(stoks.satuan);
            $('#satuan').val(stoks.satuan);
            $('#satuan').prop('readonly',true);
            console.log(e);
        });
    }
	$('#target').mask('9.99');
	$('#comission').mask('9.99');
        
$(document).ready(function(e){
    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_awal': {required:true},
                'gudang_akhir': {required:true},
                'part_number': {required:true},
                'lot_number': {required:true},
                'total': {required:true},
                'satuan': {required:true},
                'tgl_tiba': {required:true},
                'tgl': {required:true},
                'petugas': {required:true}
            }
            , 
            messages: {
                'title': 'Wajib diisi',
                'jenis_produk': 'Wajib diisi',
                'brand': 'Wajib diisi',
                'produk': 'Wajib diisi',
                'gudang_awal': 'Wajib diisi',
                'gudang_akhir': 'Wajib diisi',
                'part_number': 'Wajib diisi',
                'lot_number': 'Wajib diisi',
                'total': 'Wajib diisi',
                'satuan': 'Wajib diisi',
                'tgl': 'Wajib diisi',
                'tgl_tiba': 'Wajib diisi',
                'petugas': 'Wajib diisi',
                'customer': 'Wajib diisi'
            }
            });
            
    });
</script>