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

class User extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        

		$this->load->model("user_model");
		$this->load->model("generic_model");
		$this->load->model("setting_model");
    }

	public function login()
	{
	    $this->user_model->check_login("login");

        if($this->input->post())
		{
		    
			$username = $this->input->post('username',true);
			$password = $this->input->post('password',true);
			echo $this->user_model->login($username,$password);
			
			$this->session->set_flashdata('login_error', 'Invalid username or password.');
		
		}
		
	}

    public function change_pass()
	{
	    //$this->user_model->check_permissions("user/change_pass");
	    $data['menu']= $this->load_model->menu();
        $data['base_url']   = base_url();
        $data['userInfo']=$this->user_model->userInfo("first_name,last_name");
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view('change_pass',$data);
        $this->load->view('footer',$data);
	}
	
	
	public function check_pass($val)
	{
	    $vals = md5($val);
	    $admin=$this->user_model->userInfo("id");
	   
	            $this->db->select("password"); 
                $this->db->from('admin');
                $this->db->where('admin.id ',$admin['id']);
                $this->db->where("admin.is_delete","0");
                $password = $this->db->get()->row()->password;
    	
    	if($password == $vals)
    			{
    				 
    				 $data =  1;
    			}
    		else{
    		   
    		    $data =  0;
    		    }
    		echo json_encode($data);
	}
	
	
	public function update_password()
    {
            // $this->user_model->check_permissions("user/update_password");   
            $opass= $this->input->post('old_pass');
            $md=md5($opass);
            $data['user_data']=$this->user_model->userInfo("id,password");
            $mydata=$data['user_data'];
            $id=$mydata['id'];
            $user_password=$mydata['password'];
       
            $npass=$this->input->post('new_pass');
            $new_pass_md=md5($npass);
            $cpass=$this->input->post('con_pass');
           
          if($md != $user_password){
             
                $this->session->set_flashdata('err-msg', 'The password you entered is not correct !');
                 redirect('User/change_pass');

            }else{
                $data = array('password' =>$new_pass_md);
                $this->db->where('id',$id);
                $this->db->update('admin', $data);
                $this->session->set_flashdata('msg', 'Your password has been Updated !');
                 redirect('User/change_pass');
            }
          }


	public function index()
	{
		$this->user_model->check_login("login");
        $data['base_url'] = base_url();
        $data['settings']=$this->setting_model->getSettingsSingle();
		$this->load->view('user/login',$data);
	
	}

	public function logout()
	{
        $this->user_model->logout();
	}

	public function sabeeh()
	{
        $abc = fopen("abc.txt","w+");
fwrite($abc,"testing");
fclose($abc);
	}

}
