<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
	
	class Dashboard extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->model('dashboard/Dashboard_model', 'dashboard');
			$this->load->library('form_validation');
			//$this->load->library('Dompdf_gen','dompdf');
			$this->load->model('login/M_login', 'login');
			$this->auth->cek_auth(); //ngambil auth dari library
		}
		
		public function index()
		{	
			$id_log  = $this->session->userdata('id_admin');
			$level_log = $this->session->userdata('level');
			$akses_log = $this->session->userdata('akses');
			$user   = $this->login->user($id_log);
			$set	= $this->dashboard->pengaturan();
			
			if($level_log != 1){
				redirect('beranda');
			}
			
			$tanggal = date('Y-m-d');
			
			$data = array(
			'user'			=> $user,
			'set'			=> $set,
			'judul_halaman' => 'Data Antrian '.$set->title,
			'pengunjung'	=> $this->dashboard->antrian_pengunjung($tanggal),
			'jlhnotif' 		=> $this->dashboard->notif_count(),
			);
			
			$this->template->load('template', 'dashboard/antrian', $data);
		}	
		
		function cari_data_akhir()
		{
			$date = date('Y-m-d');
            $q = $this->db->query("SELECT count(*) as total from ptsp_pengunjung where waktu_jawab IS NULL");
            $row = $q->num_rows();
            if($row>0){
                foreach($q->result() as $dt){
                    $d['no_akhir'] = $dt->total;
				}
                echo json_encode($d);
				}else{
                $d['no_akhir'] = '';
                echo json_encode($d);
			}
		}
		
		public function pengunjung()
		{
			$id_log  = $this->session->userdata('id_admin');
			$level_log = $this->session->userdata('level');
			$akses_log = $this->session->userdata('akses');
			$user   = $this->login->user($id_log);
			$set	= $this->dashboard->pengaturan();
			
			if($level_log != 1){
				redirect('beranda');
			}
			
			
			$data = array(
			'user'			=> $user,
			'set'			=> $set,
			'judul_halaman' => 'Data Pengunjung '.$set->title,
			'pengunjung'	=> $this->dashboard->pengunjung(),
			);
			
			$this->template->load('template', 'dashboard/pengunjung', $data);
		}			
		
		
		public function jamlayanan(){
			$id_log  = $this->session->userdata('id_admin');
			$level_log = $this->session->userdata('level');
			$akses_log = $this->session->userdata('akses');
			$user   = $this->login->user($id_log);
			$set	= $this->dashboard->pengaturan();
			
			if($level_log != 1){
				redirect('beranda');
			}
			$jl   = $this->db->query("SELECT * FROM jam_layanan WHERE id = '1'")->row();
			
			$data = array(
			'user'			=> $user,
			'set'			=> $set,
			'judul_halaman' => 'Jam Layanan '.$set->title,
			'jl'			=> $jl,
			'jam_mulai'		=> set_value('jam_mulai',$jl->jam_mulai),
			'jam_selesai'	=> set_value('jam_selesai',$jl->jam_selesai),
			'status'		=> set_value('status',$jl->status),
			);
			//print_r($data);
			$id 		= $this->input->post('id');
			$jam_mulai 	= $this->input->post('jam_mulai');
			$jam_selesai= $this->input->post('jam_selesai');
			$status     = $this->input->post('status');
			
			if($this->input->post('simpan')=='yes'){
				$id = $this->input->post('id');
				$d = array(
				'jam_mulai'   => $jam_mulai,
				'jam_selesai' => $jam_selesai,
				'status'	  => $status,
				);
				$this->dashboard->update_jam_layanan($id,$d);
				$this->session->set_flashdata('message', '<div class="alert alert-success text-center">Tersimpan !<br/><b>'.$jam_mulai.' s/d '.$jam_selesai.'</b></div>');
				redirect('dashboard/jamlayanan');
				}else{
				$this->template->load('template', 'dashboard/jamlayanan', $data);
			}			
		}
		
		
		public function kontak(){
			$id_log  = $this->session->userdata('id_admin');
			$level_log = $this->session->userdata('level');
			$akses_log = $this->session->userdata('akses');
			$user   = $this->login->user($id_log);
			$set	= $this->dashboard->pengaturan();
			
			if($level_log != 1){
				redirect('beranda');
			}
			$kontak1  = $this->db->query("SELECT * FROM ptsp_hubungi LIMIT 0,1")->result();
			$kontak2  = $this->db->query("SELECT * FROM ptsp_hubungi_kesekretariatan LIMIT 0,3")->result();
			
			$data = array(
			'user'			=> $user,
			'set'			=> $set,
			'judul_halaman' => 'Kontak Layanan '.$set->title,
			'kontak'		=> $kontak1,
			);
			//print_r($data);
			
			if($this->input->post('simpan')=='chat'){
				$id 			= $this->input->post('id1',TRUE);
				$link_layanan	= $this->input->post('link_layanan1',TRUE);
				$d = array(
				'link_layanan'  => $link_layanan,
				);
				$this->dashboard->update_ptsp_hubungi($id,$d);
				$this->session->set_flashdata('message', '<div class="alert alert-success text-center">Tersimpan !<br/>Link <b>Layanan Chat dan Call</b> diperbaharui</div>');
				redirect('dashboard/kontak');
				}else if($this->input->post('simpan')=='call'){
				$id 			= $this->input->post('id2',TRUE);
				$link_layanan	= $this->input->post('link_layanan2',TRUE);
				$d = array(
				'link_layanan'  => $link_layanan,
				);
				$this->dashboard->update_ptsp_hubungi($id,$d);
				$this->session->set_flashdata('message', '<div class="alert alert-success text-center">Tersimpan !<br/>Link <b>Layanan Call Kepaniteraan</b> diperbaharui</div>');
				redirect('dashboard/kontak');
				}else if($this->input->post('simpan')=='vcall'){
				$id 			= $this->input->post('id3',TRUE);
				$link_layanan	= $this->input->post('link_layanan3',TRUE);
				$d = array(
				'link_layanan'  => $link_layanan,
				);
				$this->dashboard->update_ptsp_hubungi($id,$d);
				$this->session->set_flashdata('message', '<div class="alert alert-success text-center">Tersimpan !<br/>Link <b>Layanan Video Call Kepaniteraan </b> diperbaharui</div>');
				redirect('dashboard/kontak');
				}else{
				$this->template->load('template', 'dashboard/kontak', $data);
			}			
		}
		
		public function kontak2(){
			$id_log  = $this->session->userdata('id_admin');
			$level_log = $this->session->userdata('level');
			$akses_log = $this->session->userdata('akses');
			$user   = $this->login->user($id_log);
			$set	= $this->dashboard->pengaturan();
			
			if($level_log != 1){
				redirect('beranda');
			}
			$kontak1  = $this->db->query("SELECT * FROM ptsp_hubungi LIMIT 0,3")->result();
			$kontak2  = $this->db->query("SELECT * FROM ptsp_hubungi_kesekretariatan LIMIT 0,3")->result();
			
			$data = array(
			'user'			=> $user,
			'set'			=> $set,
			'judul_halaman' => 'Kontak Layanan '.$set->title,
			'kontak'		=> $kontak2,
			);
			//print_r($data);
			
			if($this->input->post('simpan')=='chat'){
				$id 			= $this->input->post('id1',TRUE);
				$link_layanan	= $this->input->post('link_layanan1',TRUE);
				$d = array(
				'link_layanan'  => $link_layanan,
				);
				$this->dashboard->update_ptsp_hubungi_kesekretariatan($id,$d);
				$this->session->set_flashdata('message', '<div class="alert alert-success text-center">Tersimpan !<br/>Link <b>Layanan Chat Kesekretariatan</b> diperbaharui</div>');
				redirect('dashboard/kontak2');
				}else if($this->input->post('simpan')=='call'){
				$id 			= $this->input->post('id2',TRUE);
				$link_layanan	= $this->input->post('link_layanan2',TRUE);
				$d = array(
				'link_layanan'  => $link_layanan,
				);
				$this->dashboard->update_ptsp_hubungi_kesekretariatan($id,$d);
				$this->session->set_flashdata('message', '<div class="alert alert-success text-center">Tersimpan !<br/>Link <b>Layanan Call Kesekretariatan</b> diperbaharui</div>');
				redirect('dashboard/kontak2');
				}else if($this->input->post('simpan')=='vcall'){
				$id 			= $this->input->post('id3',TRUE);
				$link_layanan	= $this->input->post('link_layanan3',TRUE);
				$d = array(
				'link_layanan'  => $link_layanan,
				);
				$this->dashboard->update_ptsp_hubungi_kesekretariatan($id,$d);
				$this->session->set_flashdata('message', '<div class="alert alert-success text-center">Tersimpan !<br/>Link <b>Layanan Video Call Kesekretariatan </b> diperbaharui</div>');
				redirect('dashboard/kontak');
				}else{
				//$this->template->load('template', 'dashboard/kontak', $data);
				redirect('dashboard/kontak');
			}			
		}
		
		public function pengaturan() 
		{
			
			$id_log  = $this->session->userdata('id_admin');
			$level_log = $this->session->userdata('level');
			$akses_log = $this->session->userdata('akses');
			$user   = $this->login->user($id_log);
			$set	= $this->dashboard->pengaturan();
			
			$row = $this->dashboard->pengaturan();
			
			if ($row) {
				$data = array(
				'user'			=> $user,
				'set'			=> $set,
				'judul_halaman' => 'Kontak Layanan '.$set->title,
                'button' => '<i class="fa fa-edit"> </i> Update Data',
                'action' => site_url('dashboard/update_action'),
				'id' => set_value('id', $row->id),
				'title' => set_value('title', $row->title),
				'deskripsi_singkat' => set_value('deskripsi_singkat', $row->deskripsi_singkat),
				'deskripsi_lengkap' => set_value('deskripsi_lengkap', $row->deskripsi_lengkap),
				//'logo' => set_value('logo', $row->logo),
				'nama_satker' => set_value('nama_satker', $row->nama_satker),
				'kode_satker' => set_value('kode_satker', $row->kode_satker),
				'website' => set_value('website', $row->website),
				'nama_pengadilan' => set_value('nama_pengadilan', $row->nama_pengadilan),
				'catatan' => set_value('catatan', $row->catatan),
				'versi' => set_value('versi', $row->versi),
				'map' => set_value('map', $row->map),
				'alamat' => set_value('alamat', $row->alamat),
				'email' => set_value('email', $row->email),
				'telp' => set_value('telp', $row->telp),
				);
				$this->template->load('template', 'dashboard/pengaturan_form', $data);
				} else {
				$this->session->set_flashdata('message', 'Record Not Found');
				redirect(site_url('pengaturan'));
			}
		}
		
		public function update_action() 
		{
			$this->_rules_pengaturan();
			
			if ($this->form_validation->run() == FALSE) {
				$this->pengaturan($this->input->post('id', TRUE));
				} else {
				$data = array(
				'title' => $this->input->post('title',TRUE),
				'deskripsi_singkat' => $this->input->post('deskripsi_singkat',TRUE),
				'deskripsi_lengkap' => $this->input->post('deskripsi_lengkap',TRUE),
				//'logo' => $this->input->post('logo',TRUE),
				'nama_satker' => $this->input->post('nama_satker',TRUE),
				'kode_satker' => $this->input->post('kode_satker',TRUE),
				'website' => $this->input->post('website',TRUE),
				'nama_pengadilan' => $this->input->post('nama_pengadilan',TRUE),
				'catatan' => $this->input->post('catatan',TRUE),
				//'versi' => $this->input->post('versi',TRUE),
				'map' => $this->input->post('map',TRUE),
				'alamat' => $this->input->post('alamat',TRUE),
				'email' => $this->input->post('email',TRUE),
				'telp' => $this->input->post('telp',TRUE),
				);
				$data_user['instansi_nama'] = $this->input->post('nama_pengadilan',TRUE);
				$data_user['kodesatker'] = $this->input->post('kode_satker',TRUE);
				$data_user['namasatker'] = $this->input->post('nama_pengadilan',TRUE);
				$this->dashboard->update_user('1',$data_user);
				$this->dashboard->update_pengaturan($this->input->post('id', TRUE), $data);
				$this->session->set_flashdata('message', 'Update Record Success');
				redirect(site_url('dashboard/pengaturan'));
			}
		}	
		
		public function _rules_pengaturan() 
		{
			$this->form_validation->set_rules('title', 'title', 'trim|required');
			$this->form_validation->set_rules('deskripsi_singkat', 'deskripsi singkat', 'trim|required');
			$this->form_validation->set_rules('deskripsi_lengkap', 'deskripsi lengkap', 'trim|required');
			//$this->form_validation->set_rules('logo', 'logo', 'trim|required');
			$this->form_validation->set_rules('nama_satker', 'nama satker', 'trim|required');
			$this->form_validation->set_rules('kode_satker', 'kode satker', 'trim|required');
			$this->form_validation->set_rules('website', 'website', 'trim|required');
			$this->form_validation->set_rules('nama_pengadilan', 'nama pengadilan', 'trim|required');
			$this->form_validation->set_rules('catatan', 'catatan', 'trim|required');
			$this->form_validation->set_rules('versi', 'versi', 'trim|required');
			$this->form_validation->set_rules('map', 'map', 'trim|required');
			$this->form_validation->set_rules('alamat', 'alamat', 'trim|required');
			$this->form_validation->set_rules('email', 'email', 'trim|required');
			$this->form_validation->set_rules('telp', 'telp', 'trim|required');
			
			$this->form_validation->set_rules('id', 'id', 'trim');
			$this->form_validation->set_error_delimiters('<span class="text-danger">', '</span>');
		}
		
		function password()
		{
			
			$id_log  = $this->session->userdata('id_admin');
			$level_log = $this->session->userdata('level');
			$akses_log = $this->session->userdata('akses');
			$user   = $this->login->user($id_log);
			$set	= $this->dashboard->pengaturan();
			
			
			if (empty($tahun)) {
				$tahun = date('Y');
				} else {
				$tahun = $this->input->get('tahun');
			}
			
			$segment    = $this->uri->segment(4);
			
			$data  = array(
            'user'  => $user,
			'set'			=> $set,
            'judul_halaman' => 'Form Ubah Password / Kata Sandi',
			);
			
			$ke         = $this->uri->segment('3');
			//var post
			$p1         = md5($this->input->post('p1', TRUE));
			$p2         = md5($this->input->post('p2', TRUE));
			$p3         = md5($this->input->post('p3', TRUE));
			if ($ke == "simpan") {
				$cek_password_lama  = $this->db->query("SELECT password FROM user WHERE id = '$id_log'")->row();
				if ($cek_password_lama->password != $p1) {
					$this->session->set_flashdata('k_password', '<div class="alert btn-warning">Password Lama tidak sama</div>');
					redirect('dashboard/password');
					} else if ($p2 != $p3) {
					$this->session->set_flashdata('k_password', '<div id="alert" class="alert btn-danger">Password Baru dan Konfirmasi Password tidak cocok</div>');
					redirect('dashboard/password');
					} else if ($p1 == $p3) {
					$this->session->set_flashdata('k_password', '<div id="alert" class="alert btn-danger">Password Lama dan Password Baru sama, silahkan coba dengan menggunakan password lain</div>');
					redirect('dashboard/password');
					} else {
					$this->db->query("UPDATE user SET password = '$p3' WHERE id = '$id_log'");
					$this->session->set_flashdata('k_password', '<div id="alert" class="alert btn-success">Password berhasil diperbaharui</div>');
					redirect('dashboard/password');
				}
				} else {
				$this->template->load('template', 'dashboard/v_password', $data);
			}
		}
		
		function foto()
		{
			$id_log  = $this->session->userdata('id_admin');
			$level_log = $this->session->userdata('level');
			$akses_log = $this->session->userdata('akses');
			$user   = $this->login->user($id_log);
			$set	= $this->dashboard->pengaturan();
			
			if (empty($tahun)) {
				$tahun = date('Y');
				} else {
				$tahun = $this->input->get('tahun');
			}
			$data       = array(
            'user'  => $user,
            'judul_halaman' => 'Form Ubah Foto / Change Photo',
			);
			//ambil variabel URL
			$mau_ke                 = $this->uri->segment(3);
			//upload config 
			
			$tahun = date('Y');
			$bulan = date('m');
			$tanggal = date('d');
			$nmfile = 'foto_' . $tanggal . '_' . $bulan . '_' . $tahun . '_' . time();
			$config['upload_path']  = './gambar';
			$config['allowed_types'] = 'jpg|jpeg|png|JPG|JPEG|PNG|gif|GIF';
			$config['max_size']     = '512';
			$config['max_width']    = '1024';
			$config['max_height']   = '1024';
			$config['file_name']    = $nmfile; //nama yang terupload nantinya
			
			$this->load->library('upload', $config);
			
			if ($mau_ke == "act_edit") {
				if (!$this->upload->do_upload('foto')) {
					$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Foto gagal diupdate dengan pesan : " . $this->upload->display_errors() . "</div>");
					redirect('dashboard/foto');
					} else {
					/* untuk edit ganti file */
					$this->db->where('id', $id_log);
					$hapus_foto = $this->db->get('user')->row();
					/* batas ganti file */
					$up_data  = $this->upload->data();
					unlink("./gambar/" . $hapus_foto->foto);
					$this->db->query("UPDATE user SET foto = '" . $up_data['file_name'] . "' WHERE id = '$id_log'");
					$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Foto telah diperbaharui</div>");
					redirect('dashboard/foto');
				}
				} else {
				$this->template->load('template', 'dashboard/v_foto', $data);
			}
		}
		
		
	} // end controller home																																																																																