<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MiraUniversalUser
 *
 * @author zmq163
 */
class MiraUniversalUser extends CI_Controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('Master_universal_user');
        $this->load->library('session');
        $this->load->helper('url');
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function updateUserProfilePicture() {
        $user_logi_id = $this->input->post('user_login_id');
        $image_name = addslashes($_FILES['profile_pic_upload']['name']);
        $image_data = file_get_contents(addslashes($_FILES['profile_pic_upload']['tmp_name']));

        $this->Master_universal_user->updateUserProfilePictureModel(array('user_login_id' => $user_logi_id, 'image_name' => $image_name, 'image' => $image_data));
        redirect('profile');
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getUserProfile() {

        $return['profile'] = $this->Master_universal_user->getUserProfileModel($this->session->userdata('create_region_id'));
        $this->session->set_userdata('page_name', 'mira_user_details_profile');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function updatePasswordForm() {
        if ($this->session->userdata('password_message') != '') {
            $return['status'] = $this->session->userdata('password_status');
            $return['message'] = $this->session->userdata('password_message');
            $this->session->unset_userdata(array('password_message' => '', 'password_message' => ''));
        }
        $return['autoId'] = $this->Master_universal_user->getUserProfileModel($this->session->userdata('create_region_id'))['0']['auto_id'];
        $this->session->set_userdata('page_name', 'mira_user_update_password');
        $this->load->view('mira_welcome_page', $return);
        //print_r($this->session->userdata('create_region_id'));
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function updatePasswordSubmit() {
        $message = NULL;
        $status = NULL;
        $updatePasswordData = array();
        $updatePasswordData['oldPassword'] = $this->input->post('old-password');
        $updatePasswordData['newPassword'] = $this->input->post('new-password');
        $updatePasswordData['confirmPassword'] = $this->input->post('confirm-password');
        $updatePasswordData['autoId'] = $this->input->post('auto_id');
        if ($updatePasswordData['newPassword'] == $updatePasswordData['confirmPassword']) {
            $responseUpdate=  $this->Master_universal_user->updatePasswordSubmitModel($updatePasswordData);
            if($responseUpdate){
                $status=$responseUpdate;
              $message = 'Successfully upadet !!';  
            }  else {
                $status=$responseUpdate;
              $message = 'Password update fail. Try again!!';
            }
        } else {
            $status = FALSE;
            $message = 'Password and confirm password mismatch.';
        }

        $this->session->set_userdata('password_status', $status);
        $this->session->set_userdata('password_message', $message);

        redirect('password');
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function universalLogin() {
        if (isset($_POST['user_id']) and isset($_POST['password'])) {
            $user_login = $this->Master_universal_user->universalLoginModel($_POST['user_id'], $_POST['password']);

            if ($user_login and $user_login['status']) {

                if ($user_login['user_level'] != 0) {

                    $this->session->set_userdata('app_total_level', $user_login['app_total_level']);
                    $this->session->set_userdata('logo_url', $user_login['logo_url']);
                    $this->session->set_userdata('level_name', $user_login['app_level']);
                    $this->session->set_userdata('right', $user_login['right']);
                    $this->session->set_userdata('app_id', $user_login['app_id']);
                    $this->session->set_userdata('create_region_id', $user_login['create_region_id']);
                }

                $this->session->set_userdata('level', $user_login['user_level']);
                $this->session->set_userdata('is_user_loged', TRUE); // This is used for user login or not.
                $this->session->set_userdata('status', TRUE);
                $this->session->set_userdata('message', 'Successfully Login');
                $this->session->set_userdata('asharegionId', 0);
                $this->session->set_userdata('anmregionId', 0);
                $this->session->set_userdata('start_date', '');
                $this->session->set_userdata('end_date', '');
                $this->session->set_userdata('search_by', 0);
                redirect('success');
            } else {
                $this->session->set_userdata('status', FALSE);
                $this->session->set_userdata('message', 'OOPS!: User id or psssword incorecet');
                redirect('login');
            }
        } else {
            $this->session->set_userdata('status', FALSE);
            $this->session->set_userdata('message', 'OOPS!: User id or psssword incorecet');
            redirect('login');
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function universalLogout() {
        $this->session->unset_userdata('level_name');
        $this->session->unset_userdata('right');
        $this->session->unset_userdata('level');
        $this->session->unset_userdata('is_user_loged');
        $this->session->unset_userdata('app_id');
        $this->session->unset_userdata('create_region_id');
        $this->session->set_userdata('status', TRUE);
        $this->session->set_userdata('message', 'Success:Logout successfully!');
        redirect('login');
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function loginSuccess() {
        if (!$this->session->userdata('is_user_loged')) {
            redirect('login');
        }
        $return_data['status'] = $this->session->userdata('status');
        $return_data['message'] = $this->session->userdata('message');
        $this->session->set_userdata('alert_message_info', 'success_alert_message');
        $this->session->set_userdata('page_name', 'mira_success_page');
        $this->load->view('mira_welcome_page', $return_data);
    }

    /**
     * This function used for logn page view
     * with login error
     * 
     * 
     */
    public function userLoginView() {
        $this->session->set_userdata('alert_message_info', 'login_alert_message');
        $this->session->set_userdata('page_name', 'mira_login_page');
        $return_data['status'] = $this->session->userdata('status');
        $return_data['message'] = $this->session->userdata('message');
        $this->load->view('mira_welcome_page', $return_data);
        
        // echo "Home Page";
    }

}
