<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
	
	class Login extends CI_Controller
	{
		
		public function __construct()
		{
			parent::__construct();
			$this->load->model('login/M_login','login');
			$this->load->library('form_validation');
		}
		
		public function index()
		{
			$session_1  = $this->session->userdata('isLoginPenggunaPTSP');
			if ($session_1 == FALSE) {
				$data = array(
                'title' => 'Halaman Utama',
                'login' => site_url('proses_pengguna'),
				);
				$this->template->load('template_login','login/login_pengguna',$data);
				} else { //jika session true maka di redirect ke halaman dashboard
				redirect('home','refresh');
			}
		}
		
		function proses_pengguna()
		{
			$username = $this->input->post('username', TRUE);
			//$password = $this->input->post('password', TRUE);
			$password = '123456';
			$status   = '1';
			
			if(empty($username) OR empty($password)){
				redirect('login');
			}
			
			$query = $this->login->sys_pengguna($username, md5($password));
			if (count($query) >= 1) {
				$cek = $this->login->cek_pengguna($username, $query->password);
				if (count(array($cek)) >= 1) {
					$this->session->set_userdata(array(
                    'isLoginPenggunaPTSP'  	 => TRUE,
                    'jk'   			     	 => $cek->jk,
                    'nama'   			     => $cek->nama,
                    'email'   			     => $cek->email,
                    'nik'     			     => $cek->nik,
                    'id'           			 => $cek->id,
					));
					redirect('home', 'refresh');  //redirect ke halaman home
					} else {
					$this->session->set_flashdata('message', '<div class="alert alert-danger text-center">Gagal Login! / akun diblok, silahkan hubungi admin </div>');
					redirect(site_url('login/index'));
				}
				} else {
				$this->session->set_flashdata('message', '<div class="alert alert-danger text-center">Gagal Login! / Nomor HP belum terdaftar, silahkan daftarkan nomor HP / Whatsapp terlebih dahulu</div>');
				redirect(site_url('login'));
			}
		}
		
		function daftar(){
			
			$data = array(
			'title' => 'Halaman Pendaftaran',
			'login' => site_url('login/proses_daftar'),
			'id' => set_value('id'),
			'nik' => set_value('nik'),
			'hp' => set_value('hp'),
			'email' => set_value('email'),
			'nama' => set_value('nama'),
			'tgllahir' => set_value('tgllahir'),
			'jk' => set_value('jk'),
			'pekerjaan' => set_value('pekerjaan'),
			'alamat' => set_value('alamat'),
			'password' => set_value('password'),
			'status' => set_value('status'),
			'tgldaftar' => set_value('tgldaftar'),
			'file_ktp' => set_value('file'),
			);
			$this->load->view('login_pendaftaran', $data);
			
			
		}
		
		public function proses_daftar() 
		{
			$this->_rules();
			if ($this->form_validation->run() == FALSE) {
				$this->daftar();
				} else {
				$nik 		= $this->input->post('nik',TRUE);
				$tgllahir 	= substr($nik, 6, 2);
				$blnlahir   = substr($nik, 8, 2);
				$thnlahir   = substr($nik, 10, 2);
				$unik		= substr($nik, 12, 4);
				if (intval($tgllahir) > 40) {
					$tgl_lahir = intval($tgllahir) - 40;
					if(intval($tgl_lahir) >=1 AND intval($tgl_lahir) <= 9){
						$tgl_lahir = '0'.$tgl_lahir;
						}else{
						$tgl_lahir = $tgl_lahir;
					}
					$jk 		 = 'P';
					} else {
					$tgl_lahir = intval($tgllahir);
					$jk 		 = 'L';
				}
				
				if (intval($thnlahir) >= 0 AND intval($thnlahir) <= 40) {
					$thn_lahir = intval($thnlahir) + 2000;
					} else {
					$thn_lahir = intval($thnlahir) + 1900;
				}
				
				$password   = '123456';
				
				$data = array(
				'nik' 		=> $this->input->post('nik',TRUE),
				'hp' 		=> $this->input->post('hp',TRUE),
				'email' 	=> $this->input->post('email',TRUE),
				'nama' 		=> $this->input->post('nama',TRUE),
				'tgllahir' 	=> $thn_lahir.'-'.$blnlahir.'-'.$tgl_lahir,
				//'jk' 		=> $jk,
				'jk' 		=> $this->input->post('jk',TRUE),
				'pekerjaan' => $this->input->post('pekerjaan',TRUE),
				'alamat' 	=> $this->input->post('alamat',TRUE),
				'password' 	=> md5('123456'),
				'status' 	=> '1',
				'tgldaftar' => date('Y-m-d H:i:s'),
				//'file_ktp' 	=> $this->input->post('file_ktp',TRUE),
				'file_ktp' 	=> 'no_file.jpg',
				);
				$this->login->insert($data);
				//print_r($data);
				$this->session->set_flashdata('message', '<div class="alert alert-success text-center">Akun anda berhasil didaftarkan, silahkan masuk menggunakan<br/>No. HP / Whatsapp</b></div>');
				$username = $this->input->post('hp', TRUE);
				$password = $password;
				
				$query = $this->login->sys_pengguna($username, md5($password));
				if (count($query) == 1) {
					$cek = $this->login->cek_pengguna($username, $query->password);
					if (count($cek) == 1) {
						$this->session->set_userdata(array(
						'isLoginPenggunaPTSP'  => TRUE,
						'jk'   			     	 => $cek->jk,
						'nama'   			     => $cek->nama,
						'email'   			     => $cek->email,
						'nik'     			     => $cek->nik,
						'id'           			 => $cek->id,
						));
						redirect('home', 'refresh');  //redirect ke halaman home
						} else {
						$this->session->set_flashdata('message', '<div class="alert alert-danger text-center">Gagal Login! / Nomor HP belum terdaftar, silahkan daftar nomor HP terlebih dahulu </div>');
						redirect(site_url('login/index'));
					}
					} else {
					$this->session->set_flashdata('message', '<div class="alert alert-danger text-center">Gagal Login!, silahkan hubungi admin</div>');
					redirect(site_url('login'));
				}
			}
		}	
		
		public function email_sudah_ada($email){
			$this->db->where_not_in('id',$this->input->post('id'));
			$this->db->where('email',$email);
			if($this->db->count_all_results('pengguna') > 0){
				$this->form_validation->set_message('email_sudah_ada', ' <b>{field}</b> sudah digunakan');
				return false;
				}else{
				return true;
			}
		}
		
		public function hp_sudah_ada($hp){
			$this->db->where_not_in('id',$this->input->post('id'));
			$this->db->where('hp',$hp);
			if($this->db->count_all_results('pengguna') > 0){
				$this->form_validation->set_message('hp_sudah_ada', ' <b>{field}</b> sudah digunakan');
				return false;
				}else{
				return true;
			}
		}	
		
		public function nik_sudah_ada($nik){
			$this->db->where_not_in('id',$this->input->post('id'));
			$this->db->where('nik',$nik);
			if($this->db->count_all_results('pengguna') > 0){
				$this->form_validation->set_message('nik_sudah_ada', ' <b>{field}</b> sudah digunakan');
				return false;
				}else{
				return true;
			}
		}
		
		public function _rules() 
		{
			//$this->form_validation->set_rules('nik', 'NIK', 'trim|required|numeric|min_length[16]|max_length[16]|callback_nik_sudah_ada');
			$this->form_validation->set_rules('hp', 'Nomor HP', 'trim|required|numeric|callback_hp_sudah_ada|min_length[10]|max_length[14]');
			//$this->form_validation->set_rules('email', 'alamat email', 'trim|valid_email|required|callback_email_sudah_ada');
			$this->form_validation->set_rules('nama', 'nama', 'trim|required');
			$this->form_validation->set_rules('jk', 'Jenis Kelamin', 'trim|required');
			$this->form_validation->set_rules('id', 'id', 'trim');
			$this->form_validation->set_error_delimiters('<span class="badge badge-warning text-danger">', '</span>');
		}
		
		function admin(){
			$session  = $this->session->userdata('isLoginPTSP');
			if ($session == FALSE) {
				$data = array(
                'title' => 'Halaman Utama',
                'login' => site_url('proses'),
				);
				$this->template->load('template_login','login/login',$data);
				} else { //jika session true maka di redirect ke halaman dashboard
				redirect('dashboard','refresh');
			}			
		}
		
		function do_login()
		{
			$username = $this->input->post('username', TRUE);
			$password = $this->input->post('password', TRUE);
			$status   = '1';
			
			$query = $this->login->sys_users($username, md5($password));
			if (count(array($query)) == 1) {
				$cek = $this->login->cek_user($username, $query->password);
				if (count($cek) == 1) {
					$this->session->set_userdata(array(
                    'isLoginPTSP'  => TRUE,
                    'username'     => $cek->username,
                    'id_admin'     => $cek->id,
                    'level'        => $cek->level,
                    'akses'        => $cek->akses,
					));
					redirect('dashboard', 'refresh');  //redirect ke halaman dashboard
					} else {
					$this->session->set_flashdata('message', '<div class="alert alert-danger text-center">Gagal Login! / akun diblok, silahkan hubungi admin </div>');
					redirect(site_url('admin'));
				}
				} else {
				$this->session->set_flashdata('message', '<div class="alert alert-danger text-center">Gagal Login! / akun belum dibuat / username atau password salah, silahkan hubungi admin PT Palangka Raya</div>');
				redirect(site_url('admin'));
			}
		}
		
		function logout()
		{
			$this->session->sess_destroy();
			//$this->db->query('DELETE FROM ci_sessions  WHERE  timestamp < NOW() - INTERVAL 12 HOUR');
			$this->db->query('OPTIMIZE TABLE ci_sessions_ptsp');
			redirect('beranda', 'refresh');
		}
		
		function logout_all()
		{
			$userid   = $this->session->userdata('id_admin');
			$username = $this->session->userdata('username');
			$hapus 	  = $username . '";id|s:';
			//$query = $this->db->query("SELECT * FROM ci_sessions")->row();
			//echo date('Y-m-d H:i:s',strtotime($query->timestamp));
			$this->session->sess_destroy();
			$this->db->query("DELETE FROM ci_sessions WHERE ci_sessions.data LIKE '%$hapus%'");
			$this->db->query('OPTIMIZE TABLE ci_sessions_ptsp');
			redirect('admin', 'refresh');
		}
		
		function logout_all_pengguna()
		{
			$userid   = $this->session->userdata('id');
			$nik	  = $this->session->userdata('nik');
			$hapus    = $nik . '";id|s:';
			//$query = $this->db->query("SELECT * FROM ci_sessions")->row();
			//echo date('Y-m-d H:i:s',strtotime($query->timestamp));
			$this->session->sess_destroy();
			$this->db->query("DELETE FROM ci_sessions WHERE ci_sessions.data LIKE '%$hapus%'");
			$this->db->query('OPTIMIZE TABLE ci_sessions_ptsp');
			redirect('beranda', 'refresh');
		}
		
		function tidak_ditemukan()
		{
			$this->load->view('template/404');
		}
	} // end controller home														