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

class Profile extends CI_Controller {
	// Session Authoriesed
	public function __construct() {
		parent::__construct();
		$admin = $this->session->userdata('admin');
		$column['id'] = $admin['id'];
		$table = 'tbl_user';
		
	    // loading common model
	    $this->load->model('Common_model');
		$result = $this->Common_model->Result($column,$table);
		if($result['password'] == $admin['password'] && $result['email'] == $admin['username']){
		    $login = true;
		} else {
		    $login = false;
		}
		
		if (empty($admin) || $login==false) {
			$this->session->set_flashdata('msg','Your Session Has Been Expired');
			redirect(base_url().'master/login/index');
		}
	}

	public function index()
        {
            $page='profile';
            $page2='profile';
            $page3= $_SESSION['admin']['id'];
            
            $page_2 =  $page.'/'.$page2;
            // loading form validation
            $this->load->library('form_validation');
            
            // loading common model
            $this->load->model('Common_model');
            $column['id'] = 1;
            $table = 'tbl_settings';
            // get common data
            $data['data'] = $this->Common_model->Result($column,$table);
            
            // get page name
            $data['page'] = $page;
            
            // here we are checking view is available
            if(file_exists('./application/views/master/'.$page_2.'.php')){
                $column['id'] = $page3;
                if($page=='profile'){
                    $column['id'] = $page3;
                    $table = 'tbl_user';
                    // we are getting results from common modal
                    $result = $this->Common_model->Result($column,$table);
                    $data['user'] = $result;
                }
                
                // loading page view
                $this->load->view('master/'.$page_2,$data);
            }
            else
            {
                // loading 404 page
                redirect(base_url().'master/page/redirect/404');
            }
        }
	
	
	
	public function edit_profile_img($name){
        foreach ($_POST as $key => $value) {
            if($key == 'table'){$table = $value;}
            if($key == 'page'){$page = $value;}
            if($key == 'id'){$id = $value;}
        }
        
        if (!empty($_FILES[$name]['name'])) {
                
            $column['id'] = $id;
            $table='tbl_user';
            
            $result = $this->Common_model->Result($column,$table);
            $current_img = $result[$name];
            $current_img ='./uploads/master/user/'.$current_img;
            
            if(file_exists($current_img)){
                unlink($current_img);
            }
            
            
            $path = $_FILES[$name]['name'];
            $ext = pathinfo( $path, PATHINFO_EXTENSION );
            
            $logo_name = 'User-'.$column['id'].'.'.$ext;
            
            $config['upload_path'] = 'uploads/master/user';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['file_name'] = $logo_name;
            
            $this->load->library('upload',$config);
            if($this->upload->do_upload($name)){
                $logo = $this->upload->data();
                $data[$name] = $logo_name;
                
                // we are updating data from tbl_settings
                $result  = $this->Common_model->Edit($column,$data,$table);
                if($result == true){
                    $this->session->set_flashdata('success','SUCCESS!!  '.strtoupper("$name").' is Updated Successfully.');
                    $_SESSION['admin'][$name] = $logo_name;
    			    redirect(base_url('master/profile/'));
    			    exit();
                } else{
                    $this->session->set_flashdata('error','Something went Wrong.');
    			    redirect(base_url('master/profile/'));
    			    exit();
                }
            } else{
                $this->session->set_flashdata('error','Something went Wrong Image is Not Uploaded.');
			    redirect(base_url('master/profile/'));
			    exit();
            }
           
        }
        else {
		    $this->session->set_flashdata('error',' '.$name.'  can not be Empty.');
			redirect(base_url('master/profile/'));
		}
	}
	public function edit_profile_content($id){
        $data = [];
        foreach ($_POST as $key => $value) {
            if($key == 'table'){$table = $value;}
            if($key == 'page'){$page = $value;}
            if($key != 'status'){
                if (empty($value)) {
                    $this->session->set_flashdata('error',''.strtoupper("$key").' Can not be Empty.');
                    redirect(base_url('master/profile/'));
                    exit();
                }
            }
            if($key != 'table' && $key != 'page'){
                $data[$key] = $value;
            }
        }
        
        $column['id'] = $id;
            
        // we are updating data from tbl_p_type
        $result  = $this->Common_model->Edit($column,$data,$table);
        if($result == true){
            $this->session->set_flashdata('success','SUCCESS!! Data is Updated Successfully.');
            
            foreach ($_POST as $key => $value) {
                if($key != 'table' && $key != 'page'){
                    $data[$key] = $value;
                    $_SESSION['admin'][$key] = $value;
                }
            }
        
		    redirect(base_url('master/profile/'));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong .');
		    redirect(base_url('master/profile/'));
		    exit();
        }
           
	}
	public function edit_profile_password($id){
        $data = [];
        foreach ($_POST as $key => $value) {
            if($key == 'table'){$table = $value;}
            if($key == 'page'){$page = $value;}
            if($key != 'status'){
                if (empty($value)) {
                    $this->session->set_flashdata('error',''.strtoupper("$key").' Can not be Empty.');
                    redirect(base_url('master/profile/'));
                    exit();
                }
            }
            if($key != 'table' && $key != 'page'){
                $data[$key] = $value;
            }
        }
        
        
        $column2['id'] = $id;
        $table2 = 'tbl_user';
        
        $result2  = $this->Common_model->Result($column2,$table2);
        $result2['password'];
        
        $password3 = md5($data['password3']);
        
        if($result2['password']!=$password3){
            $this->session->set_flashdata('error','Old Password Not Matched.');
		    redirect(base_url('master/profile/'));
		    exit();
        }
        
        
        if(md5($data['password']) == md5($data['password2'])){
            $password = md5($data['password']);
            $password2 = $data['password'];
            unset($data);
            $data['password'] = $password;
            $data['password2'] = $password2;
        }
        else{
            $this->session->set_flashdata('error','Password & Confirm Password Not Matched.');
		    redirect(base_url('master/profile/'));
		    exit();
        }
        
        $column['id'] = $id;
            
        // we are updating data from tbl_p_type
        $result  = $this->Common_model->Edit($column,$data,$table);
        if($result == true){
            $this->session->set_flashdata('success','SUCCESS!! Password is Updated Successfully.');
            
            $_SESSION['admin']['password'] = $data['password'];
        
		    redirect(base_url('master/profile/'));
		    exit();
        } else{
            $this->session->set_flashdata('error','Something went Wrong .');
		    redirect(base_url('master/profile/'));
		    exit();
        }
           
	}
	
}

?>