<script src="<?php echo base_url();?>resources/js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="<?php echo base_url();?>resources/select2/js/select2.full.js"></script>
<script type="text/javascript" src="<?php echo base_url();?>resources/select2/js/i18n/id.js"></script>
<link href="<?php echo base_url();?>resources/select2/css/select2.min.css" type="text/css" rel="stylesheet">

<hr class="thick-line" style="clear:both;">
<div id="left">
	<?php
	echo form_open(base_url().$page_url.'/search');
	$attributes = array('name' => 'search', 'id' => 'search-box', 'type' => 'text','size' => '50','placeholder'=>"  Ketik kata kunci  ");
	echo form_input($attributes);
	echo "<span>  </span>";
	$attributes = array('value' => 'Search', 'id' => 'search-btn1','class' => 'btn', 'type' => 'submit', 'onClick' => 'load()');
	echo form_submit($attributes);
	echo form_close('');
	?>
</div>
<div id="left">
<input type="submit" onClick='searchDetilShow()' value="Search Detil" id="search-btn1" class="btn">
</div>
<?php
if($this->session->userdata('is_logged_in')==FALSE){
	redirect('login');
}
?>
<div id="pages">
    <div id="selector"></div>
</div>
<div class="cssTable">
	<table id="tablePerkaraAll">
		<col  width="3%" />
        <col  width="15%" />
        <col  width="30%" />
        <col  width="45%" />
        <col  width="7%" />
		<tbody>
			<tr>
				<td style="cursor:default;">No</td>
				<td onclick="sorting(1)">Tanggal Terima</td>
				<td onclick="sorting(2)">Dari</td>
				<td onclick="sorting(3)">Subject</td>
				<td style="cursor:default;">Link</td>
			</tr>
			<?php
				if($pesan_masuk_list!=''){
					if($pesan_masuk_list->num_rows>0){
						if($page_active<1){
							$con = 1;
						}else{
							$con = $page_active+1;
						}
						$modelClass =& get_instance();
						
						# set history URL REQUESTED, this will be used to back on previous page.
						# 
						$this->nativesession->set_flash_session('history', current_url());
		    			foreach ($pesan_masuk_list->result() as $row) {
		    				$date = new DateTime(str_replace("-","",$row->tanggal_kirim));
		    				$status = ($row->status<2)? '<p style="font-weight: bold;font-size:12px;color:#3498db;">'.$row->subjek.'</p>':$row->subjek;
		    				echo "<tr>";
			    				echo '<td>'.$con.'</td>';
			    				echo '<td>'.$date->format('d M Y h:i:s').'</td>';
			    				echo '<td>'.$row->dari_userid_nama.'</td>';
			    				echo '<td>'.$status.'</td>';
			    				echo '<td style="cursor:pointer;" align="center" onclick="show_edit(\''.base64_encode($modelClass->encrypt->encode($row->id)).'\')">[<a href="#">Lihat</a>]</td>';
		    				echo "</tr>";
		    				$con++;
		    			}
			    	}else{
		    			echo "<tr><td colspan='5' align='center'>Tidak Ada Pesan Masuk</td></tr>";
		    		}
				}else{
		    		echo "<tr><td colspan='5' align='center'>Tidak Ada Pesan Masuk</td></tr>";
		    	}
			?>
		</tbody>
	</table>
</div>
<div id="pages_bottom" style="width:100%;padding-left:5px;padding-top:5px;">
    <div id="selector_bottom"></div>
</div>
<div id="popup_pesan_masuk" class="popup">
	<div class="centering" style="width:835px;margin-top: 5%;">
		<div id='edit_data_umum' style="border: 1px solid #ffffff;">
			<div class="detil" style="padding: 2px;background-color: white;">
				<form id="frm_pesan_masuk" method="post" action="<?php echo base_url().'pesan_masuk/validate_input';?>">
					<table id="tableDetilPerkara" style="font-size:14px;">
						<col width="20%">
						<col width="80%">
						<tbody>
							<tr>
								<td colspan="2" style="background:white;" align="center">PESAN</td>
							</tr>
							<tr id="from_field">
								<td>Dari</td>
								<td style="padding:0px;padding-left:5px;">
									<input id='from' name='from' value="" class="standard-input" maxlength="250" style="width:600px">
								</td>
							</tr>
							<tr id="kepada_field">
								<td>Kepada <font color="red">*</font></td>
								<td style="padding:0px;padding-left:5px;">
									<input type="text" id='kepada' name='kepada' value="" class="standard-input" style="width:400px">
								</td>
							</tr>
							<tr id="tanggal_field">
								<td>Tanggal</td>
								<td style="padding:0px;padding-left:5px;">
									<input id='tanggal' name='tanggal' value="" class="standard-input" maxlength="250" style="width:600px">
								</td>
							</tr>
							<tr id="kepada_select_field">
								<td>Kepada <font color="red">*</font></td>
								<td style="padding:0px;padding-left:5px;">
									<select class="destination-multiple" name='to' multiple="multiple" style="width:400px; z-index: 9999;">
								    <?php
								    	if($user_list!=''){
								    		if($user_list->num_rows>0){
								    			$opt='';
								    			foreach ($user_list->result() as $row) {
								    				if($opt!=$row->group_id){
								    					if(!empty($opt)){
								    						echo '</optgroup>';
								    					}
								    					$opt = $row->group_id;
								    					echo '<optgroup label="'.$row->group_name.'">';
								    				}
								    				echo '<option value="'.$row->userid.'">'.$row->fullname.'</option>';
								    			}
								    		}
								    	}
									?>
									</select>
								</td>
							</tr>
							<tr>
								<td>Perihal <font color="red">*</font></td>
								<td style="padding:0px;padding-left:5px;">
									<input id='subject' name='subject' value="" class="standard-input" maxlength="250" style="width:600px">
								</td>
							</tr>
							<tr>
								<td colspan="2" align="center">
									<font color="white">ISI PESAN</font> 
								</td>
							</tr>
							<tr id="read_msg_field">
								<td colspan="2" style="background:#fff;color:#111;" id="message">
								</td>
							</tr>
							<tr id="new_msg_field">
								<td colspan="2" style="background:#fff;color:#111;" id="message">
									<?php echo $this->ckeditor->editor("pesan","");?>
								</td>
							</tr>
							
							<tr>
								<td colspan="2" style="background:white;" align="center">
									<input type="hidden" id="enc_message" name="enc_message" value="0">
									<input type="hidden" id="refresh" value="0">
									<input type="button" onClick='kembali_pesan_list();' value="Kembali" class="btn">
									<input type="button" onClick='delete_pesan();' value="Delete" id="btn_del" class="btn">
									<input type="button" id="btn_balas" onClick='new_message();' value="Balas" class="btn">
									<input type="submit" id="btn_kirim" value="Kirim" class="btn">
								</td>
							</tr>
						</tbody>
					</table>
				</form>
			</div>
		</div>
	</div>
</div>
<?php
$url = base_url().$page_url.'/sort/';
$keyword = (!empty($keyword))?'/'.$keyword : '';
?>
<script type="text/javascript">
function sorting(col){
	window.open('<?php echo $url;?>'+col+'/<?php echo $enc; ?><?php echo $keyword; ?>','_self')
}
<?php $enc = (!empty($enc)) ? "/".$enc : $enc;?>
function searchDetilShow(){
	window.open('<?php echo base_url();?>search','_self')
}

function show_edit(enc){
	$('#loading').fadeIn();
	if(enc!=''){
		$.post( "<?php echo base_url()?>pesan_masuk/info",{
	        enc:enc
	    }).done(function( response ){
	    	var json = jQuery.parseJSON(response);
	        if(json.st==1){
	        	$('#enc_message').val(json.idH);
	        	$('#from').val(json.from);
	        	$('#tanggal').val(json.tanggal);
	        	$('#message').html(json.pesan);
	        	$('#kepada').val(json.kepada);
	        	$('#subject').val(json.subject);
	        	$('#read_msg_field').show();
	        	$('#kepada_field').show();
	        	$('#from_field').show();
	        	$('#tanggal_field').show();
	        	$('#new_msg_field').hide();
	        	$('#btn_balas').show();
	        	$('#btn_kirim').hide();
	        	$('#btn_del').show();
	        	$(':input').not('.btn').not('#old_msg').attr('readonly', true);
	        	$('#kepada_select_field').hide();
	        	$('#popup_pesan_masuk').fadeIn();
	        	$("body").css({overflow: 'hidden'});
	        	CKEDITOR.instances['pesan'].setData('<br /><hr style="border: 1px dashed rgb(0, 0, 0);">'+json.pesan);
	        	var values = json.recipient+','+json.dari;
	        	var selectedValues = values.split(',');
	        	$(".destination-multiple").select2('val',selectedValues);
	        	$('#loading').fadeOut();
	        }else{
	        	$('#loading').fadeOut();
	        	message_show(json.msg);
	        	return;
	        }
	    },'json');
	}else{
		message_show('Invalid Argument. Please refresh your browser and clear the cache');
	}
    
}

function delete_pesan(){
	var r = confirm("Apakah Anda Yakin Akan Menghapus Pesan?");
    if (r == true) {
		var enc = $('#enc_message').val();
		if(enc!=''){
			$.post( "<?php echo base_url()?>pesan_masuk/manage_pesan",{
		        enc:enc
		    }).done(function( response ){
		    	var json = jQuery.parseJSON(response);
		        if(json.st==1){
		        	$('#refresh').val(1);
		        	message_show(json.msg);
		        	$('#btn_del').hide();
		        	$('#btn_balas').hide();
		        	$('#refresh').val(1);
		        }else{
		        	message_show(json.msg);
		        	$('#btn_del').hide();
		        	$('#btn_balas').hide();
		        	$('#refresh').val(1);
		        }
		    },'json');
		    
		}else{
			message_show('Invalid Argument. Please refresh your browser and clear the cache');
		}
	}
	return;
}
function new_message(){
	var tmp = $('#subject').val();
	$('#subject').val('Balas: '+tmp);
	$('#new_msg_field').show();
	$('#read_msg_field').hide();
	$('#tanggal').hide();
	$('#kepada_select_field').show();
	$('#kepada_field').hide();
	$('#from_field').hide();
	$('#btn_del').hide();
	$('#tanggal_field').hide();
	$('#btn_balas').hide();
	$('#btn_kirim').show();
	$(':input').attr('readonly', false);
	$("body").css({overflow: 'hidden'});
	$('#popup_pesan_keluar').fadeIn();

}

$('#frm_pesan_masuk').submit(function(event){
    var r = confirm("Apakah Anda Yakin Akan Mengirim Pesan ini?");
    if (r == true) {
    	$('<div id="popup_msg" class="popup"><div class="centering"><table class="pesan"><tr style="background-color: #5FB85C;"><td align="center" style="padding:5px;" id="test">Pesan</td><td id="tutup_windows" class="tutup_windows" align="center" style="padding:5px;" width="50">tutup</td></tr><tr><td style="padding:5px;" colspan="2"><div id="pesan-container"></div></td></tr></table></div></div>').appendTo( "body" );
		$('#tutup_windows').click(function(){
			$("body").css({ overflow: 'inherit' })
			$('#popup_msg').hide();
			$("#popup_msg").remove();
			window.open('<?php echo base_url();?>pesan_masuk_list','_self')
			return false;
		})
		var values = $(this).serializeArray();
		values.push({
			name: "destination",
			value: $(".destination-multiple").val()
		});
		values.push({
			name: "message",
			value: CKEDITOR.instances['pesan'].getData()
		});
		values = jQuery.param(values);
		$.post($(this).attr('action'),values, function(json){
			if(json.st==0){
				$('#refresh').val(1);
                $('#pesan-container').html(json.msg);
            }else{
                $('#pesan-container').html(json.msg);

            }
           	$('#popup_msg').show();
        },'json');
        $("body").css({ overflow:'hidden'})
        event.preventDefault();
    }
    event.preventDefault();
})

$(".destination-multiple").select2();
function kembali_pesan_list(){
	$('#enc_message').val('');
	$('#popup_pesan_masuk').fadeOut();
	$("body").css({ overflow: 'inherit' })
	if($('#refresh').val()==1){
		window.open('<?php echo $this->nativesession->get_flash_session("url_requested");?>','_self');
	}
}
</script>
