<?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='';?>
<style>
    input[type="text"]
{
    background: transparent;
    border: none;
}
    select
{
    background: transparent;
    display: none;
}
    textarea
{
    background: transparent;
    display: none;
}
    </style>
<?php 
}else{
    $disabled='';
    $select='select2';
}
?>
    
<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="#">View</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 $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="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 $val[$nm_f]?>
				  
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="sales";?>
			   <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_sales,(isset($val[$nm_f]) ? $val[$nm_f] : ''),"class=' $select' id='$nm_f'")?>
				  <?php echo GetValue('nama_lengkap', 'master_sales',array('id'=>'where/'.$val[$nm_f]));?>
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?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)'")?>
				  <?php echo GetValue('title', 'ref_activity',array('id'=>'where/'.$val[$nm_f]));?> - 
				  <?php echo GetValue('notes', 'ref_activity',array('id'=>'where/'.$val[$nm_f]));?>
				  
			   </div>
		   </div>
		   <div class="form-group">
			   
			   <?php $nm_f="pelanggan";?>
			   <div class="col-sm-2">
				   <label for="<?php echo $nm_f?>">Customer</label>
				   </div><div class="col-sm-9">
				  <?php echo GetValue('title', 'master_pelanggan',array('id'=>'where/'.$val[$nm_f]));?>
			   </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 GetValue('title', 'master_pelanggan_divisi',array('id'=>'where/'.$val[$nm_f]))=='0'?'':GetValue('title', 'master_pelanggan_divisi',array('id'=>'where/'.$val[$nm_f]));?>
			   </div>
		   </div>
		   
		   
		   
		   <div class="form-group">
			   
			   <?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 $val[$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 GetValue('title','ref_activity_status',array('id'=>'where/'.$val[$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-->
		   <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 $val[$nm_f] ?>
				   <!--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'>
    		<div class="form-group">
                    <?php if(!$view){?>
                    <h3><a href="#area-produk" onclick="duplikatproduk()">+ Tambah Produk</a></h3><?php }?>
			</div>
		   <legend>Produk</legend>
                   <div class="col-sm-12" style="margin-bottom:10px;">
                        <div class="col-sm-2 produk">
                            <b>Brand</b></div>
                                <div class="col-sm-2 produk" id="jenis-<?php echo $a?>">
                                    <b>Jenis Produk</b>
                                </div>
                                    
                                <div class="col-sm-4 detailproduk" id="produkz-<?php echo $a?>">
                                <b>Produk</b></div><br/>
                   </div>
                        
                        <?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:1px #0044cc dashed;margin-bottom:20px;">
                                <?php $nm_f="brand[]"; echo str_replace('\n','',form_hidden_('id_detail[]',$det['id']));?>
                                <div class="col-sm-2 produk">
                                    <?php 
                                    echo GetValue('title','sv_master_brand',array('id'=>'where/'.$det['brand']));?></div>
                                <div class="col-sm-2 produk" id="jenis-<?php echo $a?>">
                                    <?php $nm_f="jenis_produk[]"; 
                                    
                                    echo GetValue('title','master_jenis_produk',array('id'=>'where/'.$det['jenis_produk']));?>
                                </div>
                                    
                                <div class="col-sm-4 detailproduk" id="produkz-<?php echo $a?>">
                                        <?php $nm_f="produk[]";?><?php 
                                    echo GetValue('title','master_produk',array('id'=>'where/'.$det['produk']));?></div></div><?php $a++; } ?>
                        <?php }?>
                   </fieldset><br/><br/>
                   <?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 class="btn pull-right">Back</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){
        getLocation();
        <?php if($val['id']==''){?>
        duplikatproduk();
        <?php }?>
    });
    function carinilai(v){
        $.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){
        
        //var that = $(this);
       // that.parent().next().find('.detailproduk').load('<?php echo base_url()?>activity_sales/loadproduk',{v:v});
        //alert('ok');
        $('#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 gantijenis(v,id=null){
        
        //var that = $(this);
       // that.parent().next().find('.detailproduk').load('<?php echo base_url()?>activity_sales/loadproduk',{v:v});
        //alert('ok');
        $('#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; 
    //alert(idsekarang);
		$( "#area-produk" ).append('<div class="input-produk form-group" id="input-produk-'+idsekarang+'" style="border-bottom:1px #0044cc dashed;margin-bottom:20px;"><div class="col-sm-2"><label for="produk">Produk</label>&nbsp;&nbsp;&nbsp;<label><a href="#" onClick="hapusdiv(this)">(HAPUS)</a></label></div><div class="col-sm-2 produk" id="brand-'+idsekarang+'"><select name="brand[]" id="brandz-'+idsekarang+'" onchange="gantibrand(this.value,'+idsekarang+')"><option value="">-Brand-</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> 