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

class Login extends CI_Controller {
      function __construct(){
	      ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
            parent::__construct();
			$this->load->library('session');
			$this->load->model('m_login');
			$this->load->model('m_home');
			$this->load->model('m_pesan');
			$this->load->library(array('form_validation', 'Recaptcha'));
      }
	public function index()
	{ 
	//template
	$headeractive = 'login';
	$cek = $this->m_home->getmenu($headeractive);
	$title = $cek->title;
	$description = $cek->description;
	$data['title'] = $title;
	$data['description'] =$description;
	$data['headeractive'] = $headeractive;	
	$data = array(
		'widget' =>$this->recaptcha->getWidget(),
		'script' =>$this->recaptcha->getScriptTag(),
		);		
	$this->load->view('login',$data);	
	}

	function cek()
	{
		if(isset($_POST['button'])){
							//cek login mhs
			$recaptcha = $this->input->post('g-recaptcha-response');
			if(!empty($recaptcha)){
					$response = $this->recaptcha->verifyResponse($recaptcha);
				if(isset($response['success']) AND $response['success'] === true){		
				$username = $this->input->post('username');
				$password = $this->input->post('password');
				$where = array(
					'username' => $username,
					'password' => md5($password)
					);
				$cek = $this->m_login->cek_login("user",$where)->num_rows();
				if($cek > 0){
				$user = $this->m_login->get_user($username);
				$iduser = $user->iduser;
				$nama = $user->nama;
				$level = $user->level;		
				$data_session = array(
				'username' => $username,
				'iduser' => $iduser,
				'nama' => $nama,
				'level' => $level,
				'token' => md5('100%Madiun'.date('Ymd'))
				);
				$this->session->set_userdata($data_session);
				/*
				//cek jatuh tempo 
				$cekdata = $this->m_pesan->pesan_lama();	
				foreach($cekdata->result() as $row){
				$jttempo = date('Y-m-d', strtotime('+3 days', strtotime( $row->tglpesan )));
				$jtsekarang = date('Y-m-d');
				//$tempo = $jttempo - $jthari;
				if($jtsekarang >= $jttempo){
					$idpesan = $row->idpesan;

					$data_save = array(
											'status' => '2'
									);			
					$where = array(
									'idpesan' => $idpesan,
									'disetujui' => 0
							);
					$this->m_pesan->update($where,$data_save,'pesan');						
				}
				}
				*/
				redirect(base_url("admin/utama"));	
				}else{
				//template
				$headeractive = 'login';
				$cek = $this->m_home->getmenu($headeractive);
				$title = $cek->title;
				$description = $cek->description;
				$data['title'] = $title;
				$data['description'] =$description;
				$data['headeractive'] = $headeractive;					
				$data['alert'] = 'danger';
				$data['pesan'] = 'Ada kesalahan username / password';
				$data = array(
					'widget' =>$this->recaptcha->getWidget(),
					'script' =>$this->recaptcha->getScriptTag(),
					);
				$this->load->view('login', $data);											
				}

		}else{
				//template
				$headeractive = 'login';
				$cek = $this->m_home->getmenu($headeractive);
				$title = $cek->title;
				$description = $cek->description;
				$data['title'] = $title;
				$data['description'] =$description;
				$data['headeractive'] = $headeractive;					
				$data['alert'] = 'danger';
				$data['pesan'] = 'Ada kesalahan username / password';
				$data = array(
					'widget' =>$this->recaptcha->getWidget(),
					'script' =>$this->recaptcha->getScriptTag(),
					);				
				$this->load->view('login', $data);			
			
		}
		}else{
				//template
				$headeractive = 'login';
				$cek = $this->m_home->getmenu($headeractive);
				$title = $cek->title;
				$description = $cek->description;
				$data['title'] = $title;
				$data['description'] =$description;
				$data['headeractive'] = $headeractive;					
				$data['alert'] = 'danger';
				$data['pesan'] = 'Ada kesalahan username / password';
				$data = array(
					'widget' =>$this->recaptcha->getWidget(),
					'script' =>$this->recaptcha->getScriptTag(),
					);				
				$this->load->view('login', $data);	

		}
	}	
	}

	public function logout(){
	$this->session->sess_destroy();
    redirect('login');
	}	
}
