<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller {
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/Welcome
	 *	- or -
	 * 		http://example.com/index.php/Welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/Welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct() {
		parent::__construct();
		$this->load->model('Profile_model');
		 if (!$this->session->userdata('logged_in')) {
            redirect(base_url());
        }
		
 	}
	public function index(){

		$template['page'] = "User/profile";
		$template['page_title'] = "Edit Profile";
		$template['data'] = $this->Profile_model->get_logined_user_data($this->session->userdata('id'));
		$this->load->view('template', $template);
	}

	public function EditProfile(){
		if(isset($_POST) && !empty($_POST)){
			$data = $_POST;
			$temps = $this->Profile_model->get_logined_user_data($data['id']);
			if(!empty($_FILES)) {
                $fileName =$_FILES['file']['name'];
                $fileName = str_replace('%','a',$fileName);
                $fileName = 'joyride'.'_'.time().".".$fileName;
                $config = set_upload_options('./assets/uploads/profile_pic/'); 
                $config['file_name'] = $fileName;
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload('file')) {
                    $error = array('error' => $this->upload->display_errors('', '')); 
                    $res = array(
                            "status"=> "error",
                            "error"=> "Upload Error",
                            "message"=> "Sorry! Promocode Image not uploaded".$error['error']
                        );
                }   
                else {                          
                    $imagedata = $this->upload->data(); 
                    $data['profile_picture']= '/assets/uploads/profile_pic/'.$imagedata['file_name'];
                }
            }else{
            	$data['profile_picture'] = $temps['profile_picture'];
            }
			$result = $this->Profile_model->edit_profile($data);
			if($result){
				$this->session->set_flashdata('message', array('message' => 'User details updated successfully','class' => 'success'));
			}else{
				  $this->session->set_flashdata('message', array('message' => 'Sorry, User details not Updated','class' => 'danger'));
			}
			 redirect(base_url().'User');
		}
	}

	public function ChangePassword(){
		if(isset($_POST) && !empty($_POST)){
			$data = $_POST;
			$temps = $this->Profile_model->get_logined_user_data($data['id']);
			if($temps['password'] == md5($data['password_c'])){
				if($data['password_n'] == $data['password_cn']){
					$result = $this->Profile_model->ChangePassword($data);
					if($result){
						$this->session->set_flashdata('message', array('message' => 'Password updated successfully','class' => 'success'));
					}else{
						  $this->session->set_flashdata('message', array('message' => 'Sorry, Password not Updated','class' => 'danger'));
					}
					 redirect(base_url().'User');
				}else{
					$this->session->set_flashdata('message', array('message' => 'New Password and Confirm Password Doesnot match','class' => 'danger'));
				}
			}else{
				$this->session->set_flashdata('message', array('message' => 'Current Password Doesnot match','class' => 'danger'));
			}
			redirect(base_url().'User');
		}
	}


}