<?php error_reporting(E_ALL ^ E_NOTICE);
if(isset($list)){	
	$val=$list->row_array();
}
?>
<?php
$cekdup=($type=='New' ? ',ajax[ajaxcekcode]' : '');
if($view){
    $disabled='disabled="disabled"';
    $select='';
}else{
    $disabled='';
    $select='select2';
}
if(isset($poquo)){
    $display='display:none';
    $display2='';
}else{
    $display='';
    $display2='display:none';
}   
?>
<style>
    
    .select2-selection__clear{
        display:none!important;
    }
</style>
    
<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>
    <fieldset <?php echo $disabled ?> > 
        
    <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 (isset($title) ? $title :$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" onsubmit="sambit()">
		   
		   <?php echo form_hidden('id',isset($val['id']) ? $val['id'] : '')?>
			
		   <div class="form-group">
			   
			   <?php $nm_f="tanggal";?>
			   <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($nm_f,(isset($val[$nm_f]) ? $val[$nm_f] : date('Y-m-d')),"class='col-sm-4 validate[required]' id='$nm_f' data-date-format='yyyy-mm-dd' readonly='readonly'")?>
				  
			   </div>
		   </div>
		   <div class="form-group" style="<?php echo $display2 ?>">
			   
			   <?php $nm_f="sales";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Solution Force</label>
				   </div><div class="col-sm-9">
                                       <?php echo form_dropdown($nm_f,getsales(),(isset($val[$nm_f]) ? $val[$nm_f] : webmastermarketing()),"class='select2' id='$nm_f' onchange='gantisales()'")?>
				  
			   </div>
		   </div>
		   <div class="form-group" style="<?php echo $display?>">
			   
			   <?php $nm_f="jenis_aktivitas";?>
			   <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_dropdown($nm_f,$opt_aktivitas,(isset($val[$nm_f]) ? $val[$nm_f] : ''),"class=' $select' id='$nm_f' onchange='carinilai(this.value)'")?>
				  
			   </div>
		   </div>
		   <div class="form-group" id="nilai_po" style="display:none">
			   
			   <?php $nm_f="nilai_po";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>" id='labelpo'>Nilai PO</label>
				   </div><div class="col-sm-9">
                                       <?php echo form_input($nm_f,(isset($val[$nm_f]) ? $val[$nm_f] : ''),"class='currenz' id='$nm_f' onchange=''")?>
				  
			   </div>
		   </div>
		   <div class="form-group" id="nilai_quo" style="display:none">
			   
			   <?php $nm_f="nilai_quo";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Nilai Quotation</label>
				   </div><div class="col-sm-9">
                                       <?php echo form_input($nm_f,(isset($val[$nm_f]) ? $val[$nm_f] : ''),"class='currenz' id='$nm_f' onchange=''")?>
				  
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="pelanggan";?>
			   <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_dropdown($nm_f,$opt_pelanggan,(isset($val[$nm_f]) ? $val[$nm_f] : ''),"class=' $select' id='$nm_f' onchange='gantidivisi(this.value)'")?>
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="pelanggan_divisi";?>
			   <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" id="divisipelanggan">
                                       <?php echo form_dropdown($nm_f,array(),(isset($val[$nm_f]) ? $val[$nm_f] : ''),"class=' $select' id='$nm_f'")?>
			   </div>
		   </div>
		   
		   
		   
		   <div class="form-group" style="display:none">
			   
			   <?php $nm_f="nilai";?>
			   <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($nm_f,(isset($val[$nm_f]) ? $val[$nm_f] : ''),"class='col-sm-4 validate[required]' id='$nm_f'")?>  
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="status";?>
			   <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_dropdown($nm_f,$opt_aktivitas_status,(isset($val[$nm_f]) ? $val[$nm_f] : ''),"class=' $select' id='$nm_f'")?>
			   </div>
		   </div>
		   <!--div class="form-group">
			   <?php $nm_f="tag";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Tag</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]">
			   </div>
		   </div-->
		  
		   <fieldset id='area-produk'>
                       <legend style="font-size:15pt;">Produk</legend>
                        <?php if($val['id']>0){
                            $a=0;
                            foreach($detail->result_array() as $det){
                            ?>
            <div class="input-divisi form-group" id="input-divisi-<?php echo $a?>" style="border-bottom:0px #0044cc dashed;margin-bottom:20px;">
                                <?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-2 produk">
                                    <?php echo form_dropdown_('brand[]',GetOptAll('master_brand','-Merk-',array('status'=>'Aktif')),$det['brand'],'class="'.$select.'" id="$nm_f" onchange="gantibrand(this.value,'.$a.')"')?></div>
                                <div class="col-sm-2 produk" id="jenis-<?php echo $a?>">
                                    <?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.'" id="$nm_f" onchange="gantijenis(this.value,'.$a.')"')?>
                                </div>
                                    
                                <div class="col-sm-4 detailproduk" id="produkz-<?php echo $a?>">
                                        <?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="$nm_f"')?></div></div><?php $a++; } ?>
                        <?php }?>
                   </fieldset><br/>
                    <div class="form-group">
                    <?php if(!$view){?>
                    <h5><a href="#area-produk" onclick="duplikatproduk()">+ Tambah Produk</a></h5><?php }?>
			</div><br/>
                   <div class="form-group">
			   
			   <?php $nm_f="catatan";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Catatan</label>
				   </div><div class="col-sm-9">
                                       <?php echo form_textarea($nm_f,(isset($val[$nm_f]) ? $val[$nm_f] : ''),'class="required"');?>
				   <!--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]"-->
			   </div>
		   </div>
                   <?php //echo form_textarea('location','',"id='location' ");?>
		   <div class="form-group" style="display:none">
			   
			   <?php $nm_f="location";?>
			   <div class="col-sm-3">
				   <label for="<?php echo $nm_f?>">Input Location</label>
                           </div><div class="col-sm-9"><div  id="mapholder"></div>
                   <?php $os=getBrowserOS(); ?>            
                 <?php echo form_hidden('longitude',(isset($val['longitude']) ? $val['longitude'] : ''));?>
                 <?php echo form_hidden('latitude',(isset($val['latitude']) ? $val['latitude'] : ''));?>
                 <?php echo form_hidden('platform',(isset($val['platform']) ? $val['platform'] : $os['os']));?>
				   <!--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]"-->
			   </div>
		   </div>
                   <?php //print_mz($opt_jenis_produk)?>
    		<div class="form-group">
            <button type="submit" class="btn pull-right">Submit</button>
            
             </div>
			 </form>
    	</div>
    </div>
    </div>
        <!--
        <div class="input-divisi" id="input-divisi-<?php echo $a?>" style="border-bottom:1px #0044cc dashed;margin-bottom:20px;"><div class="col-sm-2" id="brand-<?php echo $a?>"><label for="<?php echo $nm_f?>">Produk</label></div><div class="col-sm-2 produk"><?php echo form_dropdown_('brand[]',GetOptAll('master_produk','-Produk-',array('brand'=>'where/'.$det['brand'])),$det['produk'],'class="'.$select.'" id="$nm_f"')?></div><div class="col-sm-2 produk" id="jenis-<?php echo $a?>"></div><div class="col-sm-4 detailproduk" id="produk-<?php echo $a?>"></div></div>
        -->
</fieldset>
</div>
<script>
	$('#target').mask('9.99');
	$('#comission').mask('9.99');
</script>
<script>
          
    
    
    $(document).ready(function(e){
        $("#form").validate({
            submitHandler: function(form) {
    form.submit();
  },errorPlacement: function (error, element) {
    if (element.parent('.input-group').length) { 
        error.insertAfter(element.parent());      // radio/checkbox?
    } else if (element.hasClass('select2')) {     
        error.insertAfter(element.next('span'));  // select2
    } else {                                      
        error.insertAfter(element);               // default
    }
},
           ignore: [],        
            rules: {
                'brand[]': {required:true},
                'jenis_produk[]': {required:true},
                'produk[]': {required:true},
                'tanggal': {required:true},
                'jenis_aktivitas': {required:true},
                'pelanggan': {required:true},
                'pelanggan_divisi': {required:true},
                'status': {required:true},
                'catatan': {required:true},
                //pob: {required:true},
                //id_prop: {required:true},
                //id_kab: {required:true}, 
               // kecamatan: {required:true},
               // tempat_nongkrong: {required:true},
            }
            , 
            messages: {
                'brand[]' : 'Wajib diisi',
                'jenis_produk[]' : 'Wajib diisi',
                'produk[]' : 'Wajib diisi',
                'tanggal': 'Wajib diisi',
                'jenis_aktivitas': 'Wajib diisi',
                'pelanggan_divisi': 'Wajib diisi',
                'pelanggan': 'Wajib diisi',
                //pelanggan_divisi: {required:true},
                'status': 'Wajib diisi',
                'catatan': 'Wajib diisi',
            } 
            });  
    $('.currenz').maskMoney({thousands:".",decimal:",",precision:0});
    
        carinilai();
        getLocation();
        gantisales();
        <?php if($val['id']==''){?>
        duplikatproduk();
        <?php }?>
        });   
   
       
    function carinilai(){
        var v=$('#jenis_aktivitas').val();
        //alert(v);
        if(v=='24' || v=='25' || v=='37' || v=='42'){
            if(v=='37' || v=='42'){
                $('#labelpo').html('Nilai PO (New)');
            }else{
                $('#labelpo').html('Nilai PO');
            }
            $('#nilai_po').show();
            $('#nilai_quo').hide();
            $("#form").validate(); //sets up the validator
            $("input[id*=nilai_po").rules("add", "required");
            $("input[id*=nilai_quo").rules("remove", "required");
        }else if(v=='8' || v=='31'){
            $('#nilai_quo').show();
            $('#nilai_po').hide();
            $("#form").validate(); //sets up the validator
            $("input[id*=nilai_po").rules("remove", "required");
            $("input[id*=nilai_quo").rules("add", "required");
        }else{
            $('#nilai_po').hide();
            $('#nilai_quo').hide();
            $("#form").validate(); //sets up the validator
            $("input[id*=nilai_po").rules("remove", "required");
            $("input[id*=nilai_quo").rules("remove", "required");
        } 
        $.post('<?php echo base_url()?>activity_sales/carinilai',{v:v},function(e){
            $('#nilai').val(e);
        });
    }
    function gantidivisi(v,id=null){
        
        //var that = $(this);
       // that.parent().next().find('.detailproduk').load('<?php echo base_url()?>activity_sales/loadproduk',{v:v});
        //alert('ok');
        $('#divisipelanggan').load('<?php echo base_url()?>activity_sales/loadivisi',{v:v});
    }
    function gantibrand(v,id=null){
        $('#jenis-'+id).load('<?php echo base_url()?>activity_sales/loadjenis',{v:v,id:id},function(e){
            $('#jenis_produk-'+id).css('width','100%').select2({allowClear:true});
        });
    }
    function gantisales(){
        var zz=$('#sales').val();
        $.post("<?php echo base_url()?>activity_sales/load_sales",{s:zz,t:'<?php echo $this->uri->segment(2)?>'},function(e){
            $('#jenis_aktivitas').val(e); 
            setTimeout(function(e){carinilai()},400);
        }); 
    }
    function gantijenis(v,id=null){
        $('#produkz-'+id).load('<?php echo base_url()?>activity_sales/loadprodukz',{v:v,id:id},function(e){
            $('#produk-'+id).css('width','100%').select2({allowClear:true});
        });
    }
        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"><label for="produk">Produk</label>&nbsp;&nbsp;&nbsp;<label>'+oi+'</label></div><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($opt_brand 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>');
            $('#brandz-'+idsekarang).css('width','100%').select2({allowClear:true});
                
	}
        function hapusdiv(z){
             $(z).closest('.input-produk').remove();
        }
function getLocation() {
    if (navigator.geolocation) {
        var a=navigator.geolocation.getCurrentPosition(showPosition);
        
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}
function showPosition(position) {
    var a = "Latitude: " + position.coords.latitude + 
    "\nLongitude: " + position.coords.longitude;
    
    var latlon = position.coords.latitude + "," + position.coords.longitude;
    <?php if($val['id']==''){?>
    var lat=position.coords.latitude;
    var long=position.coords.longitude;
    
        $( "input[name='latitude']" ).val(lat);
        $( "input[name='longitude']" ).val(long);
    <?php }else{?>
    var lat='<?php echo $val['latitude']?>';
    var long='<?php echo $val['longitude']?>';
    <?php }?>
    var img_url = "http://maps.googleapis.com/maps/api/staticmap?center="+latlon+"&zoom=14&size=400x300&sensor=false";
    
   // document.getElementById("mapholder").innerHTML = "<img src='"+img_url+"'>";
    document.getElementById("mapholder").innerHTML = '<iframe width="300" height="170" frameborder="0" scrolling="no" marginheight="0"marginwidth="0" src="https://maps.google.com/maps?q='+lat+','+long+'&hl=es;z=14&amp;output=embed"></iframe><br /><small><a href="https://maps.google.com/maps?q='+lat+','+long+'&hl=es;z=14&amp;output=embed" style="color:#0000FF;text-align:left" target="_blank">See map bigger</a></small>';
       // $('#location').val(a);
}
</script> 