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

class ChangePassword extends CI_Controller {

	function __construct() {
		error_reporting(0);
		parent::__construct();
		$this->load->model('CommonModel');
		$this->load->helper('Security');

	}


	public function index()
	{
		if(check_login())
		{
			$data['content'] = 'Change/index';
			$this->load->view('layout_home', $data);
		}
	}

	public function Hod()
	{
		if(isHod())
		{
			$data['content'] = 'Change/hod';
			$this->load->view('layout_hod', $data);
		}
	}

	public function Controller()
	{
		if(isController())
		{
			$data['content'] = 'Change/controller';
			$this->load->view('layout_controller', $data);
		}
	}

	public function check()
	{

		if(check_login())
		{

			$data = $this->input->post();

			$data = $this->security->xss_clean($data);

	        $md5_convert_data = md5($this->db->escape_like_str($data['old']));
	        $salt_key = get_encript_id($this->db->escape_like_str($data['old']));
	        $password = $md5_convert_data . $salt_key;

	        $user_exsist = $this->db->select('*')->where('email', $this->session->userdata('admin_user_id'))->where('password', $password)->get('admin')->result_array();

	        if (!empty($user_exsist) && count($user_exsist) > 0) {
	            echo 'true';
	        } else {
	        	echo 'false';
	        }

		}

	}


	public function change_pass()
	{

		if(check_login())
		{


			$data = $this->input->post();

			// echo "<pre>".json_encode($data, 128);
			// print_r($data); die;
			if($data['new_pass'] != $data['con_new_pass']){
				echo "failed";
			}
			else{
			$md5_convert_data = md5($data['new_pass']);
			$salt_key = get_encript_id($data['new_pass']);
			$password = $md5_convert_data . $salt_key;

			$upd = [ "password" => $password, "password_change" => 1 ];
			// $this->CommonModel->edit_data('admin', 'admin_id', $this->session->userdata('admin_id'), $upd);
			$sql_upd=" UPDATE `admin` SET `password`='".$password."' WHERE `admin_id`='".$this->session->userdata('admin_id')."' ";
			$this->db->query($sql_upd);
			$this->session->set_userdata('password_change', 1);
			echo "success";
			
		}
		}

	}

	public function update_details()
	{

		if(check_login())
		{


			$data = $this->input->post();

			
			
			// $this->CommonModel->edit_data('admin', 'admin_id', $this->session->userdata('admin_id'), $upd);
			$sql_upd=" UPDATE `admin` SET `fullname`='".$data['full_name']."',`email`='".$data['email']."' WHERE `admin_id`='".$this->session->userdata('admin_id')."' ";
			$this->db->query($sql_upd);
			$this->session->set_userdata('password_change', 1);
			echo "success";
			
		
		}

	}

	public function UpdateHod()
	{

		if(check_login())
		{

			$data = $this->input->post();

			// echo "<pre>".json_encode($data, 128);

			if($data['new'] != $data['newC']){
				$this->session->set_flashdata('fail', 'New password and Confirm password aren\'t same.');
				redirect(base_url('ChangePassword/Hod'));
			}

			$md5_convert_data = md5($data['new']);
			$salt_key = get_encript_id($data['new']);
			$password = $md5_convert_data . $salt_key;

			$upd = [ "password" => $password, "password_change" => 1 ];
			$upduser = [ "password" => $data['new']];
			$this->CommonModel->edit_data('admin', 'admin_id', $this->session->userdata('admin_id'), $upd);
			$hod_details=$this->db->where('admin_id',$this->session->userdata('admin_id'))->get('tbl_hod')->result_array();
			$this->CommonModel->edit_data('tbl_hod', 'hod_id', $hod_details[0]['hod_id'], $upduser);
			$this->session->set_userdata('password_change', 1);
			$this->session->set_flashdata('success', 'Password successfully changed.');
			// ---------------------------Insert Audit trail--------------------
            $audit_trail_table='audit_trail';
	        $audit_trail_array=array(
	                                    'user_name'=>$this->session->userdata('admin_name'),
	                                    'action'=>'Action -> "Change Password"',
	                                    'action_time'=>date('Y-m-d H:i:s')
	                                );
        	$this->CommonModel->insert($audit_trail_table,$audit_trail_array);
            // ----------------------------End----------------------------------
			redirect(base_url('ChangePassword/Hod'));

		}

	}

	public function UpdateController()
	{

		if(check_login())
		{

			$data = $this->input->post();

			// echo "<pre>".json_encode($data, 128);

			if($data['new'] != $data['newC']){
				$this->session->set_flashdata('fail', 'New password and Confirm password aren\'t same.');
				redirect(base_url('ChangePassword/Controller'));
			}

			$md5_convert_data = md5($data['new']);
			$salt_key = get_encript_id($data['new']);
			$password = $md5_convert_data . $salt_key;

			$upd = [ "password" => $password, "password_change" => 1 ];
			$upduser = [ "password" => $data['new']];
			$this->CommonModel->edit_data('admin', 'admin_id', $this->session->userdata('admin_id'), $upd);
			$controller_details=$this->db->where('admin_id',$this->session->userdata('admin_id'))->get('tbl_exam_controller')->result_array();
			$this->CommonModel->edit_data('tbl_exam_controller', 'exam_controller_id', $controller_details[0]['exam_controller_id'], $upduser);
			$this->session->set_userdata('password_change', 1);
			$this->session->set_flashdata('success', 'Password successfully changed.');
			// ---------------------------Insert Audit trail--------------------
            $audit_trail_table='audit_trail';
	        $audit_trail_array=array(
	                                    'user_name'=>$this->session->userdata('admin_name'),
	                                    'action'=>'Action -> "Change Password"',
	                                    'action_time'=>date('Y-m-d H:i:s')
	                                );
        	$this->CommonModel->insert($audit_trail_table,$audit_trail_array);
            // ----------------------------End----------------------------------
			redirect(base_url('ChangePassword/Controller'));

		}
	}
}
