<?php error_reporting(E_ALL^E_NOTICE);
if(isset($list)){	
	$val=$list->row_array();
}
?>
<?php
$cekdup=($type=='New' ? ',ajax[ajaxcekcode]' : '');
?>
    
<div class="row">
	<ul class="breadcrumb">
        <li>
            <a href="#">Home</a>
        </li>
        <li>
            <a href="<?php echo base_url($this->utama)?>"><?php echo ucfirst($this->title)?></a>
        </li>
        <li>
            <a href="#"><?php echo $type?></a>
        </li>
    </ul>
    <div class="box col-md-12">
    <div class="box-inner">
    <div class="box-header well" data-original-title="">
        <h2><i class="<?php echo GetValue('icon','sv_menu',array('filez'=>'where/'.$this->utama))?>"></i> <?php echo $this->title;?></h2>
    </div>
    	<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'] : '')?>
			
		   
		   <!--div class="form-group">
			   
			   <?php $nm_f="code";?>
			   <div class="col-sm-2">
				   <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_pelanggan" value="<?php echo $a= (isset($val[$nm_f]) ? $val[$nm_f] : '') ?>" class="col-sm-4 ">
                                   <?php //validate[required<?php echo $cekdup]?>
			   </div>
		   </div-->
		   <div class="form-group">
			   
			   <?php $nm_f="title";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Nama Customer</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 ">
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="perusahaan";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Bidang / Industri</label>
				   </div><div class="col-sm-9">
				   <?php echo form_dropdown_($nm_f,GetOptAll('ref_bidang_pelanggan','-Bidang Customer-',array('status'=>'Aktif')), (isset($val[$nm_f]) ? $val[$nm_f] : ''),'class="select2" id="$nm_f"')?>
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="alamat";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Alamat</label>
				   </div><div class="col-sm-9">
                                       <?php echo form_textarea($nm_f,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class=""');?>
				   <!--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 "-->
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="telp1";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Telp</label>
				   </div><div class="col-sm-9">
                                       <?php echo form_input($nm_f,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class=""');?> /
				   <!--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 $nm_f="telp2";?>
                                       <?php echo form_input($nm_f,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class=""');?>
				   <!--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 "-->
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="fax";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Fax</label>
				   </div><div class="col-sm-9">
                                       <?php echo form_input($nm_f,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class=""');?> 
				   <!--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 "-->
				   <!--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 "-->
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="email";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Email</label>
				   </div><div class="col-sm-9">
                                       <?php echo form_input($nm_f,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class=""');?> 
				   <!--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 "-->
				   <!--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 "-->
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="situs";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Situs</label>
				   </div><div class="col-sm-9">
                                       <?php echo form_input($nm_f,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class=""');?> 
				   <!--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 "-->
				   <!--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 "-->
			   </div>
		   </div>
		   <!--div class="form-group">
			   
			   <?php $nm_f="twitter";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Twitter</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 ">
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="fanpage";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Fanpage</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 ">
			   </div>
		   </div-->
		   <div class="form-group">
			   
			   <?php $nm_f="status_pelanggan";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Status</label>
				   </div><div class="col-sm-9">
                                   <?php echo form_dropdown($nm_f,$opt_pelanggan,(isset($val[$nm_f]) ? $val[$nm_f] : ''),"class='select2' id='$nm_f'"); ?>
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="pelanggan_sejak";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>"><?php echo ucwords(str_replace('_',' ',$nm_f)) ?></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  date-picker" data-date-format='yyyy-mm-dd'>
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="catatan";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Note</label>
				   </div><div class="col-sm-9">
                                       <?php echo form_textarea($nm_f,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class=""');?>
				   <!--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 "-->
			   </div>
		   </div>
		   <fieldset id='area-divisi'>
		   <legend>Divisi</legend>
		   <?php if($type=='New'){?>
    		<!--div class="form-group input-divisi">
			   <?php $nm_f="divisi[]";
			   echo form_hidden('id_divisi[]','');?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Nama Divisi</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 ">
			   </div>
             </div-->
		   <?php }else{
			   $getdiv=GetAll('sv_master_pelanggan_divisi',array('pelanggan'=>'where/'.$val['id']));
                           $a=1;
			   foreach($getdiv->result_array() as $gd){
			   ?>
            <div class="input-divisi" id="input-divisi-<?php echo $a?>" style="border-bottom:1px #0044cc dashed;margin-bottom:20px;">
    		<div class="form-group">
			   <?php $nm_f="divisi[]";
                           $unique=($gd['unique']==''?$gd['id']:$gd['unique']);
			   echo form_hidden('id_divisi[]',$gd['id']);
			   echo form_hidden("unique[]",$unique);
			   ?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Nama Divisi</label>
				   </div><div class="col-sm-9">
				   <input type="text" name="<?php echo $nm_f?>"  id="<?php echo $nm_f?>" value="<?php echo isset($gd['title']) ? $gd['title'] : '' ?>" class="col-sm-4 "><label><a href="#" onClick="hapusdivz(this,<?php echo $gd['id']?>)">(HAPUS)</a></label>
                                   </div></div> 
                                   
                  
                   &nbsp;&nbsp;&nbsp;<a href="#" onclick="tambahkontak_<?php echo $unique ?>()">+ Tambah Kontak</a>
                   
    		<div class="form-group"><div class="col-md-3">&nbsp;</div>
                    <div class="col-md-9" id="area-kontak-<?php echo $unique ?>">
                        
                        <?php if($type!='New'){
                       $getkontak=GetAll('sv_master_pelanggan_divisi_kontak',array('divisi'=>'where/'.$gd['id']));
                           $a=1;
			   foreach($getkontak->result_array() as $gk){
                               
                           $unique=($gk['unique']==''?$gk['id']:$gk['unique']);
			   ?><div class="input-kontak-<?php echo $unique ?> inputankontak" id="input-kontak-<?php echo $unique ?>-'+now+'" style="border-bottom:1px #0044cc dashed;margin-bottom:20px;">
                        <div class="form-group">
			   <?php
			   echo form_hidden("id_kontak[$unique][]",$gk['id']);
			  // echo form_hidden("unique[$unique][]",($gd['unique']==''?$gd['id']:$gd['unique']));
                           //$unique=$gk['id'];
                           $nm_f="d_kontak[$unique][]";?>
                            
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Nama Kontak</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($gk['kontak']) ? $gk['kontak'] : '') ?>" class="col-sm-4 validate[required]"><label><label><a href="#" onClick="hapuskontakz(this,<?php echo $gk['id']?>)">(HAPUS)</a></label>
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="telp1";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>"><?php echo ucwords(str_replace('_',' ',$nm_f))?></label>
				   </div><div class="col-sm-9">
                                       <?php echo form_input('d_'.$nm_f."[$unique][]",(isset($gk[$nm_f]) ? $gk[$nm_f] : ''),"class='col-sm-4 validate[required]' id='$nm_f'")?>
				  
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="telp2";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>"><?php echo ucwords(str_replace('_',' ',$nm_f))?></label>
				   </div><div class="col-sm-9">
                                       <?php echo form_input('d_'.$nm_f."[$unique][]",(isset($gk[$nm_f]) ? $gk[$nm_f] : ''),"class='col-sm-4 validate[required]' id='$nm_f'")?>
				  
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="fax";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>"><?php echo ucwords(str_replace('_',' ',$nm_f))?></label>
				   </div><div class="col-sm-9">
                                       <?php echo form_input('d_'.$nm_f."[$unique][]",(isset($gk[$nm_f]) ? $gk[$nm_f] : ''),"class='col-sm-4 validate[required]' id='$nm_f'")?>
				  
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="email";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>"><?php echo ucwords(str_replace('_',' ',$nm_f))?></label>
				   </div><div class="col-sm-9">
                                       <?php echo form_input('d_'.$nm_f."[$unique][]",(isset($gk[$nm_f]) ? $gk[$nm_f] : ''),"class='col-sm-4 validate[required]' id='$nm_f'")?>
				  
			   </div>
		   </div>
		   <div class="form-group" >
			   
			   <?php $nm_f="catatan";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Note</label>
				   </div><div class="col-sm-9">
                                       <?php echo form_textarea('d_'.$nm_f."[$unique][]",(isset($gk[$nm_f]) ? $gk[$nm_f] : ''),'class="validate[required]"');?>
				   <!--input type="text" name="<?php echo $nm_f?>"  id="<?php echo $nm_f?>" value="<?php echo $a= (isset($gd[$nm_f]) ? $gd[$nm_f] : '') ?>" class="col-sm-4 validate[required]"-->
			   </div>
                   </div></div>
                           <?php }
                        }?>
                        
                        
                    </div></div>
			   <?php $a++;?></div>
                               <script>
	function tambahkontak_<?php echo  $unique ?>(){
            var now=$('.input-kontak-<?php echo  $unique ?>').length+1;
		$( "#area-kontak-<?php echo  $unique ?>" ).append( '<div class="input-kontak-<?php echo  $unique ?> inputankontak" id="input-kontak-<?php echo  $unique ?>-'+now+'" style="border-bottom:1px #0044cc dashed;margin-bottom:20px;"></div>');
                $('#input-kontak-<?php echo  $unique ?>-'+now).load('<?php echo base_url()?>master_pelanggan/load_kontak/<?php echo  $unique ?>',{v:now});
	}
    </script>
                               
                               
                               <?php } 
                               
                           }?>
			 </fieldset>
			 
    		<div class="form-group">
            <h5><a href="#area-divisi" onclick="duplikatdivisi()">+ Tambah Divisi</a></h5>
			</div>
    		<div class="form-group">
            <button type="submit" class="btn pull-right">Submit</button>
            
             </div>
			 
			 </form>
    	</div>
    </div>
    </div>
</div>
<script>
    $(document).ready(function(e){
       <?php if(!isset($val['id'])){?>
       duplikatdivisi();<?php }?>
        $("#form").validate({
            submitHandler: function(form) {
    form.submit();
  },
           ignore: [],        
            rules: {
                'title': {required:true},
                'perusahaan': {required:true},
                'alamat': {required:true},
                'telp1': {required:true},
                //'telp2': {required:true},
                //'fax': {required:true},
                //'email': {required:true},
                //'situs': {required:true},
                'status_pelanggan': {required:true},
                //pelanggan_sejak: {required:true},
               // 'catatan': {required:true},
                'divisi[]': {required:true},
                //id_prop: {required:true},
                //id_kab: {required:true}, 
               // kecamatan: {required:true},
               // tempat_nongkrong: {required:true},
            }
            , 
            messages: {
                'title': 'Wajib diisi',
                'perusahaan': 'Wajib diisi',
                'alamat': 'Wajib diisi',
                'telp1': 'Wajib diisi',
                //'telp2': 'Wajib diisi',
                //'fax': 'Wajib diisi',
                //'email': 'Wajib diisi',
                //'situs': 'Wajib diisi',
                'status_pelanggan': 'Wajib diisi',
                //'pelanggan_sejak' : 'Wajib diisi',
               // 'catatan': 'Wajib diisi',
                'divisi[]': 'Wajib diisi',
            }
            });    
    });
	$('#target').mask('9.99'); 
	$('#comission').mask('9.99');
	function duplikatdivisi(){
            var now=$('.input-divisi').length+1;
            
		$( "#area-divisi" ).append( '<div class="input-divisi" id="input-divisi-'+now+'" style="border-bottom:1px #0044cc dashed;margin-bottom:20px;"></div>' );
                $('#input-divisi-'+now).load('<?php echo base_url()?>master_pelanggan/load_div',{v:now});
	}
	function hapusdiv(z){
		//alert('ok');<a href="#area-divisi" onclick="deletethis()">- Delete</a></div></div>
		//var oke = $(this).closest('.input-divisi').val();
		//alert(oke);
		//return false;
                $(z).closest('.input-divisi').remove();
	}
	function hapusdivz(z,idz){
		//alert('ok');<a href="#area-divisi" onclick="deletethis()">- Delete</a></div></div>
		//var oke = $(this).closest('.input-divisi').val();
		//alert(oke);
		//return false;
                var strconfirm = confirm("Hapus Divisi?");
    if (strconfirm == true) {
        $.post('<?php echo base_url()?>master_pelanggan/hapusdiv',{v:idz},function(e){
            if(e=='Y'){
                $(z).closest('.input-divisi').remove();}
        });
    }
	}
	function hapuskontak(z){
		//alert('ok');<a href="#area-divisi" onclick="deletethis()">- Delete</a></div></div>
		//var oke = $(this).closest('.input-divisi').val();
		//alert(oke);
		//return false;
                $(z).closest('.inputankontak').remove();
	}
	function hapuskontakz(z,idz){
		//alert('ok');<a href="#area-divisi" onclick="deletethis()">- Delete</a></div></div>
		//var oke = $(this).closest('.input-divisi').val();
		//alert(oke);
		//return false;
                var strconfirm = confirm("Hapus Divisi?");
    if (strconfirm == true) {
        $.post('<?php echo base_url()?>master_pelanggan/hapuskontak',{v:idz},function(e){
            if(e=='Y'){
                $(z).closest('.inputankontak').remove();}
        });
    }
	}
</script> 