<?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 MiraUserRegionMapping
 *
 * @author zmq163
 */
class MiraUserRegionMapping extends CI_Controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('Region_model');
        $this->load->model('User_model');
        $this->load->library('session');
        $this->load->helper('url');
        if (!$this->session->userdata('is_user_loged')) {
            redirect('login');
        }
    }

    public function getUserRightRegionCode() {

        print_r(json_encode($this->Region_model->getUserRightRegionCodeModel($_POST['region_code'], $this->session->userdata('app_id'))));
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getUserDetailRight() {
        $app_id = $this->session->userdata('app_id');
        $super_region_code = $this->session->userdata('create_region_id');
        $return['user'] = $this->User_model->getUserDetailRightModel($app_id, $_POST['region_level'], $super_region_code);
        print_r(json_encode($return));
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function submitUpdateUserRight() {
        $data_array_right = NULL;
        $region_code = $_POST['region_code'];
        $app_id = $this->session->userdata('app_id');
        for ($i = 0; $i < $_POST['count_number']; $i++) {
            if (isset($_POST['right' . $i])) {
                $data_array_right[] = $_POST['right' . $i];
            }
        }
        if ($this->Region_model->submitUpdateUserRightModel($app_id, $region_code, $data_array_right)) {
            redirect('update_user_right');
        } else {
            print_r(json_encode($_POST));
        }

        //
//        $app_id=$this->session->userdata('app_id');
//        $super_region_code=$this->session->userdata('create_region_id');
//        $return['user']=$this->User_model->getUserDetailRightModel($app_id,$_POST['region_level'],$super_region_code);
//        print_r(json_encode($return));
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getForUpdateUserRight() {
        $this->load->model('Mira_applicatio_model');
        $return['right'] = $this->Mira_applicatio_model->getCurrentUserRightAssignedModel($this->session->userdata('app_id'), $this->session->userdata('create_region_id'), $this->session->userdata('level'));
        $return['level'] = $this->Mira_applicatio_model->getApplicationHirecharchyLevel($this->session->userdata('app_id'), $this->session->userdata('level'));
        $this->session->set_userdata('page_name', 'mira_user_right_update_list_view');
        $this->load->view('mira_welcome_page', $return);
        //print_r($return['right']);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function updateLoginStatus() {
        $return['date'] = date('d-m-Y');
        if ($this->User_model->updateLoginStatusModel($_POST['auto_id'], $_POST['region_id'], $_POST['status'],  $this->session->userdata('app_id'))) {
            $return['status'] = TRUE;
        } else {
            $return['status'] = FALSE;
        }
        print_r(json_encode($return));
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getMappedUserListView() {
        $this->load->model('Mira_applicatio_model');
        $app_id = $this->session->userdata('app_id');
        $level = $this->session->userdata('level');
        $return['level'] = $this->Mira_applicatio_model->getApplicationHirecharchyLevel($app_id, $level);
        $this->session->set_userdata('page_name', 'mira_mapped_user_region_list');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getMappedUserList() {
        $create_region_id = $this->session->userdata('create_region_id');
        $app_id = $this->session->userdata('app_id');
        $return['mapped_list'] = $this->User_model->getMappedUserRegionListModel($app_id, $_POST['region_level'], $create_region_id);
        print_r(json_encode($return));
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getMappedUserListActivation() {

        $create_region_id = $this->session->userdata('create_region_id');
        $app_id = $this->session->userdata('app_id');
        $return['mapped_list'] = $this->User_model->getMappedUserRegionListUpdateModel($app_id, $create_region_id, $this->session->userdata('level'));
        $this->session->set_userdata('page_name', 'mira_mapped_region_user_list_for_activation');
        $this->load->view('mira_welcome_page', $return);
        //print_r($return);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function mappingRegionToUser() {
        $this->load->model('Mira_applicatio_model');
        $app_id = $this->session->userdata('app_id');
        $create_region_id = $this->session->userdata('create_region_id');
        $level = $this->session->userdata('level');
        if ($level != 1) {
            $return['right'] = $this->Region_model->getAdminUserRightModel($app_id);
        } else {
            $return['right'] = $this->Region_model->getAdminUserRightModel($app_id);
        }
        $return['level'] = $this->Mira_applicatio_model->getApplicationHirecharchyLevel($app_id, $level);
        $this->session->set_userdata('page_name', 'mira_mapping_region_user');
        $this->session->set_userdata('data_submit_status', TRUE);
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getMappedRegionList(){
        $create_region_id = $this->session->userdata('create_region_id');
        $app_id = $this->session->userdata('app_id');
        $return['mapped_list'] = $this->User_model->getMappedUserRegionListForUnmappedModel($app_id, $create_region_id);
        $this->session->set_userdata('page_name', 'mira_mapped_region_user_list_for_unmapped');
        $this->load->view('mira_welcome_page', $return);
        //print_r($return);
    }
    
    /**
     * 
     * 
     * 
     * 
     */
    public function updateUnmappedRegionUser(){
        if($this->User_model->updateUserRegionUnmappedModel($_POST['region_id'],$_POST['auto_id'],$this->session->userdata('app_id'))){
            print_r(json_encode(array('status'=>TRUE)));
        }  else {
            print_r(json_encode(array('status'=>FALSE)));
        }
        
//        $create_region_id = $this->session->userdata('create_region_id');
//        $app_id = $this->session->userdata('app_id');
//        $return['mapped_list'] = $this->User_model->getMappedUserRegionListUpdateModel($app_id, $create_region_id, $this->session->userdata('level'));
//        $this->session->set_userdata('page_name', 'mira_mapped_region_user_list_for_unmapped');
//        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getUserRegionUnmapped() {
        $app_id = $this->session->userdata('app_id');
        $create_region_id = $this->session->userdata('create_region_id');
        $return['region'] = $this->Region_model->getRegionListModel(array('app_id' => $app_id, 'region_level' => $_POST['region_level'], 'mapping_status' => 0));
        $return['user'] = $this->User_model->getUserListModel(array('app_id' => $app_id, 'user_level' => $_POST['region_level'], 'super_user_mapped_region_code' => $create_region_id, 'user_mapping_status' => 0, 'delete_status' => 0));
        print_r(json_encode($return));
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function mappingRegionUserSubmit() {
        $this->session->set_userdata('page_name', 'mira_success_page');
        if ($this->session->userdata('data_submit_status')) {
            $this->session->set_userdata('data_submit_status', FALSE);
            $app_id = $this->session->userdata('app_id');
            $data_right = NULL;
            for ($i = 0; $i < $_POST['count_number']; $i++) {
                if (isset($_POST['right' . $i])) {
                    $data_right[] = array('app_id' => $app_id, 'region_code' => $_POST['region_id'], 'right_id' => $_POST['right' . $i]);
                }
            }
            $data_login = array(
                'user_login_id' => $_POST['login_id' . $_POST['user_auto_id']],
                'user_password' => $_POST['password' . $_POST['user_auto_id']],
                'create_region_id' => $_POST['region_id'],
                'user_level' => $_POST['region_level'],
                'app_id' => $app_id
            );
            $history_data=array(
                'user_id' => $_POST['user_auto_id'],
                'mapping_date' => date('Y-m-d'),
                'region_code' => $_POST['region_id'],
                'user_level' => $_POST['region_level'],
                'app_id' => $app_id,
                'current_status' =>0
            );
            $return['status'] = TRUE;
            if ($this->Region_model->mappingRegionUser($_POST['region_id'], array('mapping_status' => 1, 'mapping_date' => date('Y-m-d')), $_POST['user_auto_id'], array('user_mapping_status' => 1, 'mapping_date' => date('Y-m-d')), $data_right, $data_login,$history_data)) {
                $return['message'] = "User and region mapped successfully.";
            } else {

                $return['message'] = "User and region mapped fail.";
            }
        } else {
            $return['status'] = TRUE;
            $return['message'] = "Please do not refresh.";
        }
        $this->session->set_userdata('data_submit_status', FALSE);
        $this->session->set_userdata('alert_message_info', 'add_mapping_region_user');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function deleteUser() {
        $data = array('delete_status' => 1);
        $return['delete_status'] = TRUE;
        if ($this->User_model->updateUserModel($_POST['auto_id'], $data)) {
            $return['message'] = 'User delete successfully.';
        } else {
            $return['message'] = 'User delete Fail.';
        }
        print_r(json_encode($return));
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function updateUser() {
        $data = array('user_name' => $_POST['user_name'], 'email' => $_POST['email']);
        $return['update_status'] = TRUE;
        if ($this->User_model->updateUserModel($_POST['auto_id'], $data)) {
            $return['message'] = 'User Update Successfully.';
        } else {
            $return['message'] = 'User Update Fail.';
        }
        print_r(json_encode($return));
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getUserList() {
        $return['user_details'] = $this->User_model->getUserListModel(array('app_id' => $this->session->userdata('app_id'), 'delete_status' => 0, 'super_user_mapped_region_code' => $this->session->userdata('create_region_id')));
        $this->session->set_userdata('page_name', 'mira_edit_delete_list_users');
        $this->load->view('mira_welcome_page', $return);
        //print_r($return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function updateRegion() {
        $return['region_update'] = $this->Region_model->updateRegionModel($_POST['region_id'], array('region_name' => $_POST['region_name']));
        if ($return['region_update']) {
            $return['message'] = 'Update Success.';
        } else {
            $return['message'] = 'Update Fail.';
        }
        print_r(json_encode($return));
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function regionUpdateEditDeleteView() {

        $this->load->model('Mira_applicatio_model');
        $return['level'] = $this->Mira_applicatio_model->getApplicationHirecharchyLevel($this->session->userdata('app_id'), $this->session->userdata('level'));
        $this->session->set_userdata('page_name', 'mira_region_edit_delete_view');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getRegionUpdateEditDelete() {

        $rteurn['region'] = $this->Region_model->getRegionUpdateEditDeleteModel($this->session->userdata('app_id'), $_POST['region_level']);
        print_r(json_encode($rteurn));
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function createNewRegion() {
        $this->session->set_userdata('page_name', 'mira_success_page');
        if ($this->session->userdata('data_submit_status')) {
            if (isset($_POST['level_name' . ($_POST['create_level'] - 1)])) {
                $data = array(
                    'region_name' => $_POST['region_name'],
                    'region_level' => $_POST['create_level'],
                    'region_code_super' => $_POST['level_name' . ($_POST['create_level'] - 1)],
                    'app_id' => $this->session->userdata('app_id')
                );
            } else {
                $data = array(
                    'region_name' => $_POST['region_name'],
                    'region_level' => $_POST['create_level'],
                    'region_code_super' => $this->session->userdata('create_region_id'),
                    'app_id' => $this->session->userdata('app_id')
                );
            }
            //print_r($data);
            $return['status'] = $this->Region_model->createNewRegionModel($data);
            $return['message'] = "Region Create successfully.";
        } else {
            $return['status'] = TRUE;
            $return['message'] = "Please do not refresh.";
        }
        $this->session->set_userdata('data_submit_status', FALSE);
        $this->session->set_userdata('alert_message_info', 'add_region_message');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     */
    public function getAreaBySuperArea() {
        $app_id = $this->session->userdata('app_id');
        $return['level_data'] = $this->Region_model->getRegionDetail(array('app_id' => $app_id, 'region_code_super' => $_POST['super_id']));
        print_r(json_encode($return));
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getRegionLevel() {
        $this->load->model('Mira_applicatio_model');
        $app_id = $this->session->userdata('app_id');
        $user_level=$this->session->userdata('level');
        $super_region_code=$this->session->userdata('create_region_id');
        $return['level'] = $this->Mira_applicatio_model->getRegionLevelModel($app_id, $_POST['level_id'],$user_level);
        if ($return['level']) {
            $levle_array = NULL;
            foreach ($return['level'] as $value) {
                $levle_array[] = $value['hierar_level_id'];
            }
            $return['level_data'] = $this->Region_model->getRegionDetail(array('app_id' => $app_id, 'region_code_super' =>$super_region_code , 'region_level' => min($levle_array)));
        }
        print_r(json_encode($return));
    }

    /**
     * 
     * 
     * 
     */
    public function newUserCreateForm() {
        $this->session->set_userdata('data_submit_status', TRUE);
        $app_id = $this->session->userdata('app_id');
        $level_id = $this->session->userdata('level');
        $this->load->model('Mira_applicatio_model');
        $this->load->model('Add_area_model');
        $return['country'] = $this->Add_area_model->getCountryModel(1);
        if (isset($level_id) and ! $level_id) {
            $return['user_levels'][] = array('hierar_level_id' => 1, 'hierar_level_name' => "Admin");
            ;
        } else {
            $return['user_levels'] = $this->Mira_applicatio_model->getApplicationHirecharchyLevel($app_id, $level_id);
        }
        $this->session->set_userdata('page_name', 'mira_registration_form_page');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function newRegionCreateForm() {
        $this->session->set_userdata('data_submit_status', TRUE);
        $this->load->model('Mira_applicatio_model');
        $return['user_levels'] = $this->Mira_applicatio_model->getApplicationHirecharchyLevel($this->session->userdata('app_id'), $this->session->userdata('level'));
        $this->session->set_userdata('page_name', 'mira_create_new_region');
        $this->load->view('mira_welcome_page', $return);
    }

}
