<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pass extends CI_Controller {
	function __construct(){
		parent::__construct();	
			$this->load->library('session');
			$this->load->library('form_validation');
			//$this->load->model('m_pass');
			$this->load->model('m_pass');
			if($this->session->userdata('username') =='' OR $this->session->userdata('token') !=md5('100%Madiun'.date('Ymd')) ){
				redirect(base_url("login"));
			}			
	}
	public function index()
	{
	$this->load->view('admin/pass');			
	}	
	function gantipass()
	{
		$iduser = $this->session->userdata('iduser');	
		$passlama =md5($this->security->xss_clean($this->input->post('passlama')));	
		$passbaru =$this->security->xss_clean($this->input->post('passbaru'));	
		$ulangipass =$this->security->xss_clean($this->input->post('ulangipass'));	
		if(isset($_POST['button'])){	
		$res= $this->m_pass->cekpasslama($iduser);
		$passlamaok = $res->password;
			if($passlama!=$passlamaok){
					$data['alert'] = 'danger';
					$data['pesan'] = 'Password lama salah';				
					$this->load->view('admin/pass', $data);			
			}elseif($passbaru!=$ulangipass) {
					$data['alert'] = 'danger';
					$data['pesan'] = 'Password baru tidak sama';				
					$this->load->view('admin/pass', $data);					
			}else{
					$data_save = array(
											'password' => md5($passbaru),
									);			
					$where = array(
									'iduser' => $iduser
							);
					$this->m_pass->update($where,$data_save,'user');			
					$data['alert'] = 'success';
					$data['pesan'] = 'Password berhasil diperbarui';				
					$this->load->view('admin/pass', $data);			
			}
		}else{
				$data['alert'] = '';
				$data['pesan'] = '';				
				$this->load->view('admin/pass', $data);		
		}
	}		
}