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

    

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

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function assignVaccinePlannerForm() {
        $return['application'] = $this->Mira_applicatio_model->assignVaccinePlannerFormModel();
        $return['planner'] = $this->Mira_applicatio_model->getVaccinePlannerModel();
        $this->session->set_userdata('page_name', 'mira_appliaction_assign_vaccine_form');
        $this->session->set_userdata('data_submit_status', TRUE);
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function assignVaccinePlannerFormSubmit() {
        $this->session->set_userdata('data_submit_status', FALSE);
        $response = $this->Mira_applicatio_model->assignVaccinePlannerFormSubmitModel($this->input->post('plannerId'),  $this->input->post('applicationId'));
        if ($response) {

            $this->session->set_userdata('page_name', 'mira_success_page');
            $return['status'] = TRUE;
            $return['message'] = "Application assign vaccine planner success.";
            $this->session->set_userdata('alert_message_info', 'update_application_right_message');
            $this->load->view('mira_welcome_page', $return);
        } else {

            $this->session->set_userdata('page_name', 'mira_success_page');
            $return['status'] = FALSE;
            $return['message'] = "Application assign vaccine planner fail.";
            $this->session->set_userdata('alert_message_info', 'update_application_right_message');
            $this->load->view('mira_welcome_page', $return);
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function createApplicationForm() {

        $this->session->set_userdata('page_name', 'mira_appliaction_create_form');
        $this->session->set_userdata('data_submit_status', TRUE);
        $this->load->view('mira_welcome_page');
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function updateApplicationRight() {
        if (isset($_POST['app_id'])) {
            $right_view = NULL;
            for ($i = 0; $i < $_POST['count_number']; $i++) {
                if (isset($_POST['right' . $i])) {
                    $right_view[] = $_POST['right' . $i];
                }
            }
            if ($this->session->userdata('data_submit_status')) {
                $this->session->set_userdata('data_submit_status', FALSE);
                $this->session->set_userdata('page_name', 'mira_success_page');
                $return['status'] = $this->Mira_applicatio_model->updateMiraApplicationRightModel($_POST['app_id'], $right_view);
                $return['message'] = "Application Right Update Successfully.";
                $this->session->set_userdata('alert_message_info', 'update_application_right_message');
            } else {
                $this->session->set_userdata('data_submit_status', FALSE);
                $this->session->set_userdata('page_name', 'mira_success_page');
                $return['status'] = TRUE;
                $return['message'] = "Please don't refresh browser.";
                $this->session->set_userdata('alert_message_info', 'update_application_right_message');
            }
            $this->load->view('mira_welcome_page', $return);
        } else {
            redirect('get_app_right');
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getAppAndAppRight() {
        if (isset($_POST['app_id'])) {
            print_r(json_encode(array('status' => TRUE, 'application' => $this->Mira_applicatio_model->getAppDetailModel($_POST['app_id']), 'app_right' => $this->Mira_applicatio_model->getAppRightModel(array('app_id' => $_POST['app_id'], 'mapp_right_status' => 1)))));
        } else {
            print_r(json_encode(array('status' => FALSE)));
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getActivatedApplicationRight() {
        $this->load->model('Super_admin_user_rights');
        $return['activated_app_list'] = $this->Mira_applicatio_model->getActivatedApplicationRightModel();
        $return['right'] = $this->Super_admin_user_rights->getSuperAdminRignt();
        $this->session->set_userdata('page_name', 'mira_app_right_update_add');
        $this->session->set_userdata('data_submit_status', TRUE);
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function updateCurrentStatusApp() {
        $app_id_update = explode(':', $_POST['app_id']);
        if ($app_id_update[1]) {
            print_r(json_encode(array('app_id' => $app_id_update[0] . ":0", 'current' => 0, 'status' => $this->Mira_applicatio_model->mappedAppActivateModel($app_id_update[0]), 'activate_date' => date('d-m-Y'), 'expire_date' => date('d-m-Y'))));
        } else {
            print_r(json_encode(array('app_id' => $app_id_update[0] . ":1", 'current' => 1, 'status' => $this->Mira_applicatio_model->mappedAppDeactivateModel($app_id_update[0]))));
        }
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getMappedAppAdminList() {
        $return['mapped_app_list'] = $this->Mira_applicatio_model->getMappedAppAdminModel();
        $this->session->set_userdata('page_name', 'mira_mapped_app_admin_view');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getCurrentAppSatatus() {
        $return['mapped_app_list'] = $this->Mira_applicatio_model->getMappedAppAdminModel();
        $this->session->set_userdata('page_name', 'mira_current_app_status_update');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function applicationView() {
        $return['application'] = $this->Mira_applicatio_model->applicationViewModel();
        $this->session->set_userdata('page_name', 'mira_application_view_edit');
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function applicationDelete() {
        if ($this->Mira_applicatio_model->applicationDeleteModel($_POST['app_id_delete'])) {
            print_r(json_encode(array('app_delete_status' => TRUE, 'message' => 'Application delete Successfully')));
        } else {
            print_r(json_encode(array('app_delete_status' => FALSE, 'message' => 'Application is not delete')));
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function createApplication() {
        $hirarechy = array();
        $prefix_id = NULL;
        $applicatio_name_split = explode(' ', $_POST['app_name']);
        for ($i = 0; $i < ($_POST['hierarchy_number']); $i++) {
            $temp_array = array('hierar_level_id' => ($i + 2), 'hierar_level_name' => $_POST[($i + 1)]);
            array_push($hirarechy, $temp_array);
        }
        if ($this->session->userdata('data_submit_status')) {
            $this->load->library('File_upload_library');
            $this->session->set_userdata('data_submit_status', FALSE);

            if (isset($_FILES['miraAppLogo']) and $_FILES['miraAppLogo']['tmp_name'] != NULL) {
                $upload_file = new File_upload_library();
                $file_upload_status = $upload_file->appLogoUploadLibrary($applicatio_name_split[0], 'miraAppLogo');
                $this->session->set_userdata('page_name', 'mira_success_page');
                if ($file_upload_status['upload_status']) {
                    $application = array('app_logo_path' => $file_upload_status['resource_path'], 'app_name' => $_POST['app_name'], 'app_total_level' => ($_POST['hierarchy_number'] + 1));
                    $return['status'] = $this->Mira_applicatio_model->createApplicationModel($application, $hirarechy);
                    $return['message'] = "Application successfully created.";
                    $this->session->set_userdata('alert_message_info', 'add_application_message');
                } else {
                    $return['status'] = FALSE;
                    $return['message'] = "application creatsion is fail";
                    $this->session->set_userdata('alert_message_info', 'add_application_message');
                }
            } else {
                $this->session->set_userdata('page_name', 'mira_success_page');
                $application = array('app_logo_path' => NULL, 'app_name' => $_POST['app_name'], 'app_total_level' => ($_POST['hierarchy_number'] + 1));
                $return['status'] = $this->Mira_applicatio_model->createApplicationModel($application, $hirarechy);
                $return['message'] = "Application successfully created.";
                $this->session->set_userdata('alert_message_info', 'add_application_message');
            }
        } else {
            $return['status'] = TRUE;
            $return['message'] = "Please do not refresh.";
            $this->session->set_userdata('alert_message_info', 'add_application_message');
            $this->session->set_userdata('data_submit_status', FALSE);
        }
        $this->load->view('mira_welcome_page', $return);
    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
    public function getData($_query = NULL) {
        if ($_query != NULL and $_query->num_rows() > 0) {
            return $_query->result_array();
        } else {
            return FALSE;
        }
    }

}
