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

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

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function mappingApplicationAdmin() {
        $this->load->model('Mira_applicatio_model');
        $this->load->model('Super_admin_user_rights');
        $return_data['application'] = $this->Mira_applicatio_model->getApplicationByCondition(array('app_mapping_status' => 0));
        $return_data['user'] = $this->Admin_user_model->getAdminUserByCondition(array('admin_mapping_status' => 0));
        $return_data['right'] = $this->Super_admin_user_rights->getSuperAdminRignt();
        $this->session->set_userdata('page_name', 'mira_application_assign_to_admin');
        $this->session->set_userdata('data_submit_status', TRUE);
        $this->load->view('mira_welcome_page', $return_data);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function assignApplicationAdmin() {
        $this->load->model('Mira_applicatio_model');
        $this->load->model('Super_admin_user_rights');
        $this->load->model('Admin_user_login_model');

        $this->load->database();
        $admin_right = array();

        if (isset($_POST['user_id']) and isset($_POST['app_id'])) {
            $adin_login_data = array();
            for ($i = 0; $i < $_POST['count_number']; $i++) {
                if (isset($_POST['right' . $i])) {
                    $temp_array = array('app_id' => $_POST['app_id'], 'right_id' => $_POST['right' . $i]);
                    array_push($admin_right, $temp_array);
                }
            }
            if ($this->session->userdata('data_submit_status')) {
                $this->session->set_userdata('data_submit_status', FALSE);
                $this->db->trans_start();
                $this->Mira_applicatio_model->mappedAppAdminModel(array('admin_id' => $_POST['user_id'], 'app_id' => $_POST['app_id']));
                $admin = $this->Admin_user_model->updateAdminUser(array('admin_mapping_status' => 1), array('admin_id' => $_POST['user_id']));
                $app = $this->Mira_applicatio_model->updateMiraApplication(array('app_mapping_status' => 1), array('app_id' => $_POST['app_id']));
                $this->Super_admin_user_rights->mappinApplicationRight($admin_right);
                $adin_login_data['app_id'] = $app[0]['app_id'];
                $adin_login_data['create_region_id'] = $app[0]['app_id'];
                $adin_login_data['user_level'] = 1;
                $adin_login_data['user_password'] = $admin[0]['admin_password'];
                $adin_login_data['user_login_id'] = $admin[0]['admin_login_id'];
                $this->Admin_user_login_model->insertUserLoginDetail($adin_login_data);

                if ($this->db->trans_status() === FALSE) {
                    $this->db->trans_rollback();
                    $return['status'] = FALSE;
                    $return['message'] = "Mapping app not success.";
                } else {
                    $this->db->trans_commit();
                    $return['status'] = TRUE;
                    $return['message'] = "application assign is successfully";
                }
            } else {
                $return['status'] = TRUE;
                $return['message'] = "Please dont refresh page.";
                $this->session->set_userdata('data_submit_status', FALSE);
            }
            $this->session->set_userdata('alert_message_info', 'mapping_app_admin_message');
            $this->session->set_userdata('page_name', 'mira_success_page');
            $this->load->view('mira_welcome_page', $return);
        } else {
            redirect('mapping_app_admin');
        }
    }

    /**
     * 
     * 
     * 
     */
    public function checkUserId() {
        if($this->session->userdata('level') != 0){
          print_r(json_encode(array('check' => $this->Admin_user_model->checkUserIdModel($_POST['user_id']))));  
        }  else {
            print_r(json_encode(array('check' => $this->Admin_user_model->checkAdminUserIdModel($_POST['user_id']))));
        }
        
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function adminUserRegistrationView() {
        
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function adminUserRegistration() {
        if ($this->session->userdata('level') != 0) {
            $user_details = array(
                'user_name' => $_POST['full_name'],
                'email' => $_POST['email'],
                'gender' => $_POST['gender'],
                'date_of_birth' => date('Y-m-d', strtotime($_POST['dob'])),
                'user_login_id' => $_POST['login_id'],
                'user_password' => $_POST['password'],
                'mobile' => $_POST['mobile'],
                'country_code' => $_POST['country'],
                'state_code' => $_POST['state'],
                'district_code' => $_POST['district'],
                'pincode' => $_POST['pincode'],
                'address' => $_POST['complete_address'],
                'user_level' => $_POST['user_level'],
                'app_id' => $this->session->userdata('app_id'),
                'super_user_mapped_region_code' =>$this->session->userdata('create_region_id') 
            );
        } else {
            $user_details = array(
                'admin_name' => $_POST['full_name'],
                'email' => $_POST['email'],
                'gender' => $_POST['gender'],
                'date_of_birth' => date('Y-m-d', strtotime($_POST['dob'])),
                'admin_login_id' => $_POST['login_id'],
                'admin_password' => $_POST['password'],
                'mobile' => $_POST['mobile'],
                'country_code' => $_POST['country'],
                'state_code' => $_POST['state'],
                'district_code' => $_POST['district'],
                'pincode' => $_POST['pincode'],
                'address' => $_POST['complete_address']
            );
        }

        $this->session->set_userdata('page_name', 'mira_success_page');
        if ($this->session->userdata('data_submit_status')) {
            $return['status'] = $this->Admin_user_model->adminUserRegistrationModel($user_details);
            $return['message'] = "Admin Register Succesfully";
            $this->session->set_userdata('alert_message_info', 'add_admin_message');
            $this->session->set_userdata('data_submit_status', FALSE);
        } else {
            $return['status'] = TRUE;
            $return['message'] = "Admin Registration under process.";
            $this->session->set_userdata('alert_message_info', 'add_admin_message');
            $this->session->set_userdata('data_submit_status', FALSE);
        }
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function adminUserListView() {
        $return['admin_user'] = $this->Admin_user_model->adminUserListModel();
        $this->session->set_userdata('page_name', 'mira_admin_user_list_view');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getAdminUserView() {
        $return['user'] = $this->Admin_user_model->getAdminUserByAdminIdModel($_POST['admin_id']);
        print_r(json_encode($return));
    }

    /**
     * 
     * This controller update admin user details
     * retuen only true and false
     * 
     * 
     */
    public function updateAdmin() {
        //This block used for admin details update.
        $user_update_data = array(
            'mobile' => $_POST['phone'],
            'admin_login_id' => $_POST['login_id']
        );
        if ($this->Admin_user_model->updateAdminModel($_POST['admin_id'], $user_update_data)) {
            print_r(json_encode(array('admin_update_status' => TRUE, 'message' => 'Success', 'user' => $user_update_data)));
        } else {
            print_r(json_encode(array('admin_update_status' => FALSE, 'message' => 'Updation is fail..Plx retry.', 'user' => array('admin_id' => $_POST['admin_id'], 'phone' => $_POST['phone'], 'login_id' => $_POST['login_id']))));
        }
    }

    /**
     * 
     * 
     * 
     */
    public function deleteAdmin() {
        //This block used for admin user delete.
        if ($this->Admin_user_model->deleteAdminModel($_POST['admin_id_delete'])) {
            print_r(json_encode(array('admin_delete_status' => TRUE, 'message' => 'This admin successfully deleted.', 'admin_id' => $_POST['admin_id_delete'])));
        } else {
            print_r(json_encode(array('admin_delete_status' => FALSE, 'message' => 'Delete updation is fail.')));
        }
    }

}
