<?php
if($this->session->userdata('is_logged_in')){
	if($this->session->userdata('idgroup')>2){
		redirect('login');
	}
}else{
	redirect('login');
}
?>
<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');
	echo form_submit($attributes);
	echo form_close('');
	?>
</div>
<div id="left" style="margin-left:50px;">
	<input type="submit" value="Tambah Template" class="btn" onclick="show_edit('')">
</div>
<div id="pages">
    <div id="selector"></div>
</div>
<div class="cssTable">
	<table id="tablePerkaraAll">
		<col  width="3%" />
        <col  width="30%" />
        <col  width="10%" />
        <col  width="5%" />
		<tbody>
			<tr>
				<td>No</td>
				<td onclick="sorting(1)">Nama</td>
				<td onclick="sorting(2)">Aktif</td>
				<td>Link</td>
			</tr>
			<?php
				if($users_list!=''){
					if($users_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 ($users_list->result() as $row) {
		    				echo "<tr>";
			    				echo '<td>'.$con.'</td>';
			    				echo '<td>'.$row->nama.'</td>';
			    				echo '<td>'.$row->aktif_text.'</td>';
			    				echo '<td style="cursor:pointer;" align="center" onclick="show_edit(\''.base64_encode($modelClass->encrypt->encode($row->id)).'\')">[<a href="#">edit</a>]</td>';
		    				echo "</tr>";
		    				$con++;
		    			}
			    	}else{
		    			echo "<tr><td colspan='6' align='center'>Data Tidak diTemukan</td></tr>";
		    		}
				}else{
		    		echo "<tr><td colspan='6' align='center'>Data Tidak diTemukan</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_template_keterangan_saksi" class="popup">
	<div class="centering" style="width:90%;margin-top: 5%;z-index:-100">
		<div id='edit_template_amar' style="border: 1px solid #ffffff;">
			<div class="detil" style="padding: 2px;background-color: white;">
				<form id="frm_template_amar" style="background:white;margin:0;" method="post" action="<?php echo base_url().'manage_template_keterangan_saksi/validate_input';?>">
				<table id="tableDetilPerkara">
					<col width="15%">
					<col width="85%">
					<tbody>
						<tr>
							<td colspan="2" style='text-align:center;background:#fff;color:#5fb85c;border:2px solid #fff;margin-bottom:10px;font-size: 20px;'>REFERENSI TEMPLATE KETERANGAN SAKSI</td>
						</tr>
						<tr>
							<td>Nama Amar<font color="red">*</font></td>
							<td style="padding:0px;padding-left:5px;">
								<input type="text" id='nama' name='nama' class="standard-input" maxlength="50" readonly="readonly">
							</td>
						</tr>
						<tr>
							<td>Aktif</td>
							<td style="padding:0px;padding-left:5px;">
								<span class="custom-dropdown custom-dropdown--emerald" style="width:80px">
									<select style="width:80px" name="aktif" id="aktif" class="custom-dropdown__select custom-dropdown__select--white" disabled>
										<option value="1"> Ya</option>
										<option value="0"> Tidak</option>
									</select>
								</span>
							</td>
						</tr>
						<tr>
							<td>Template</td>
							<td>
								<table>
									<tr>
										<td>Pertanyaan</td>
										<td>Jawaban</td>
									</tr>
									<tbody class="template_content">
									</tbody>
								</table>
								<input type="button" id="add_row" value="Add Row" class="btn">
							</td>
						</tr>
						<tr>
							<td colspan="2" style="background:white;" align="center">
							<input type="hidden" id='idH' name="idH">
							<input type="button" onClick='kembali_template_amar();' id="refresh" value="Kembali" class="btn">
							<?php
							if($this->session->userdata('idgroup')<2 OR $this->session->userdata('idgroup')==900){
							?>
								<input type="button" id="edit_btn" onClick='edit();' value="Edit" class="btn">
								<input type="submit" id="simpan_btn" value="Simpan" class="btn" disabled>
							<?php
							}?>
						</td>
						</tr>
					</tbody>
				</table>
			</form>
		</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')
}

$('#add_row').click(function(){
	var table = $(".template_content");
	var table_len = $('.template_content').length;
	table.append("<tr>\
		<td height='30'><input name='tanya[]' value=''></td>\
		<td height='30'><input name='jawab[]' value=''></td>\
		<td height='30'><a href='#' class='hapus' style='color:red'>Hapus</a></td>\
		</tr>");
});

$(document).on('click','.hapus',function(){
	$(this).closest('tr').remove();
});

function edit(){
	$(':input').prop('readonly', false);
	$(':input').attr('readonly', false);
	$('.custom-dropdown__select').prop('disabled',false);
	$('.custom-dropdown__select').attr('disabled',false);
	$('.hapus').show();
	$('#simpan_btn').prop('disabled',false);
	$('#add_row').prop('disabled',false);
	$('#edit_btn').prop('disabled',true);
}
function show_edit(enc){
	if(enc!=''){
		$.post( "<?php echo base_url()?>template_keterangan_saksi/info",{
	        enc:enc
	    }).done(function( response ){
	    	$('.template_content').empty();
	    	var json = jQuery.parseJSON(response);
	        if(json.st==1){
	        	$('#idH').val(json.idH);
	        	$('#nama').val(json.nama);
	        	$('#aktif').val(json.aktif);
	        	$('.template_content').append(json.table);
	        	$('#sub_title').html('INFO TEMPLATE AMAR');
	        	$('#edit_btn').prop('disabled',false);
	        	$('#edit_btn').attr('disabled',false);
	        	$('.custom-dropdown__select').attr('disabled',true);
	        	$('#add_row').prop('disabled',true);
	        	$(':input').not('.btn').not('.btn_small').attr('readonly', true);
	        }else{
	        	message_error_show_parent('Error, Please refresh your browser and clear the cache')
	        }
	    },'json');
	}else{
		$('#sub_title').html('TAMBAH TEMPLATE AMAR');
		$('#idH').val(0);
		$('#edit_btn').hide();
		edit();
	}
    $('#popup_template_keterangan_saksi').fadeIn();
    $("body").css({overflow: 'hidden'});
}

function kembali_template_amar(){
	$('#popup_template_keterangan_saksi').fadeOut();
	$('.template_content').empty();
	if($('#refresh').val()==1){
		window.open('<?php echo base_url().'template_keterangan_saksi_list';?>','_self');
	}
	$(':input').not('.btn').not('.btn_small').val('');
	$('.hidden').hide();
	$('#edit_btn').show();
	$('#simpan_btn').prop('disabled',true);
	$('#add_row').prop('disabled',true);
	$("body").css({ overflow: 'inherit' })
}


$('#frm_template_amar').SubmitHandling({
   	refresh:'1',
    setRefresh:1,
    divRef:'#frm_template_amar',
    clicked:1,
    btnClicked:'#refresh',
    whenStatus:1
});
</script>
