<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Admin_panel extends MBELINK_Controller {
	
	public function __construct(){
		
		parent::__construct();
		date_default_timezone_set('Asia/Jakarta');
		
		$this->load->library(array('session'));
		$this->load->helper(array('rsonline'));
				 
		// User Login Information
		$this->data['user'] = user_info();
		$this->data['instansi'] = orgInfo();
		
		// Cek Sesi
		 if(! $this->session->userdata('userid')):
			redirect(base_url('login-system.html'), 'refresh');
		endif;
		
		//load Model
		$this->load->model('admin/admin_model');		
		$this->load->model('admin/admin_model','mastere');		
		
		function setTglSql($str_tgl){
			$tglaja=strtotime($str_tgl);
			$bulan=date('m', $tglaja);
			$tahun=date('Y', $tglaja);
			$tglok=$tahun.'-'.$bulan.'-01';
			
			return $tglok;
		}
		
		//inisialisasi
		$date=date_create(date('Y-m-d'));
		date_modify($date, '-1 day');
		$tglkemarin=date_format($date, 'Y-m-d');
		
		$this->data['enddate']=$tglkemarin;
		$this->data['grup']=$this->admin_model->getGroupUser();
	}
	
	public function index()
	{
		//$this->load->view('mutu/indikator_bor');
		//$this->data['tanggal']=setTglSql($this->input->post('tglawal'));
		
		
		$this->data['result']=$this->admin_model->getDataUser();
		$this->load->view('admin/data_user', $this->data);
				
	}
	
	public function ajax_data()
	{
		$list = $this->mastere->get_datatables();
		$data = array();
		$no = $_POST['start'];
		foreach ($list as $mastere) {
			$no++;
			$row = array();
			//$row[] = $no;
			$row[] = $mastere->id_user;
			$row[] = $mastere->passwd;
			$row[] = $mastere->nik;
			$row[] = $mastere->material;	
			$row[] = $mastere->bhp;	
			$row[] = $mastere->tarif_tindakandr;	
			$row[] = $mastere->tarif_tindakanpr;	
			$row[] = $mastere->kso;	
			$row[] = $mastere->menejemen;	
			$row[] = $mastere->total_byrdr;	
			$row[] = $mastere->total_byrpr;	
			$row[] = $mastere->total_byrdrpr;	
			//$row[] = $mastere->kd_pj;	
			$row[] = $mastere->nm_poli;	
			//$row[] = $mastere->status;	
			//$row[] = $mastere->png_jawab;	
			//$row[] = $mastere->nm_kategori;	
			$row[] = '<a href="ubah-tindakan.html?key='.base64_encode($mastere->kd_jenis_prw).'" class="btn btn-info btn-xs" >Edit</a> <button id="'.$mastere->kd_jenis_prw.'" class="btn btn-danger btn-xs delet_pasien"><i class="fa fa-close"></i></button>';
			

			$data[] = $row;
		}
				
		$output = array(
						"draw" => $_POST['draw'],
						"recordsTotal" => $this->mastere->count_all(),
						"recordsFiltered" => $this->mastere->count_filtered(),
						"data" => $data,
				);
		//output to json format
		echo json_encode($output);
	}
	
	function saveSettingUserGroup()
	{
		//print_r($_POST);
		$cekdata=$this->admin_model->cekGroupUser($this->input->post('idreg'),$this->input->post('id_grup'));
		
		if($cekdata){
			?>
			<script language="javascript">
				alert("User dan grup yang di maksud sudah ada");
				history.go(-1);
				//history.back();
				//////document.location ="data-pasien-rajal.html";
				///location.reload(true);
			</script>
			<?php  
		}else{
			$data=array(
				'nik' => $this->input->post('idreg'),
				'nama' => $this->input->post('nama'),
				'id_grup' => $this->input->post('id_grup'),
				'petugas' => $this->session->userdata('userid'),
				'tgl_update' => date('Y-m-d H:i:s'),
			);
		
			$insert=$this->admin_model->savePenambahanUserGroup($data);
			if($insert){
				
				?>
				<script language="javascript">
					alert("Penambahan Grup User Berhasil");	
					document.location ="data-user.html";
				</script>
				<?php 
				//redirect(base_url('data-user.html'));
			}else{
			?>
				<script language="javascript">
					alert("Penambahan Grup User Gagal");
					history.go(-1);				
				</script>
			<?php  
			}
		}
	}
	
	
	//json
	function getDataUserByNip()
	{
		$nip=$this->input->post('nip',TRUE);		
		//$nip='2018121734';		
				
		$rinci = $this->admin_model->getUserByNIP($nip);
		$this->output
			->set_content_type('application/json')
			->set_output(json_encode($rinci));
	}
	
	function getDataUserGrupByNip()
	{
		$nip=$this->input->post('nip',TRUE);		
		$id=$this->input->post('id',TRUE);		
		//$nip='2018121734';		
				
		$rinci = $this->admin_model->getUserGrupByNIP($nip,$id);
		$this->output
			->set_content_type('application/json')
			->set_output(json_encode($rinci));
	}
	
	//end json
	
	function daftarUserGrup()
	{
		$this->data['result']=$this->admin_model->getDataUserGrup();
		
		$this->load->view('admin/user_grup_list', $this->data);
	}
	
	function saveUpdateUserGrup()
	{
		print_r($_POST);
		if($_POST['id_grup']==''){
			$grup=$this->input->post('idgrupx');
		}else{
			$grup=$this->input->post('id_grup');
		}
		$nik=$this->input->post('idreg');
		$id=$this->input->post('idgrupx');
		
		$updt=array(
			'nama' => $this->input->post('nama'),
			'id_grup' => $grup,
			'petugas' => $this->session->userdata('userid'),
			'tgl_update' => date('Y-m-d H:i:s'),
		);
		$save=$this->admin_model->saveUpdateUserGroup($updt, $nik, $id);
			if($save){
				
				?>
				<script language="javascript">
					alert("Perubahan Grup User Berhasil");	
					document.location ="data-user-grup.html";
				</script>
				<?php 
				//redirect(base_url('data-user.html'));
			}else{
			?>
				<script language="javascript">
					alert("Perubahan Grup User Gagal");
					history.go(-1);				
				</script>
			<?php  
			}
		
	}
}
