<?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 Registration_model
 *
 * @author mzq163
 */
class Registration_model extends CI_Model {

    //put your code here
    var $db_sbcc;
    var $status;
    var $message;
    var $response_data = array();

    public function __construct() {
        parent::__construct();
        $this->db_sbcc = $this->load->database('db_sbcc', true);
        $this->status = false;
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function userRegistrationNewFormSubmit_model($post_data = null) {

        if ($post_data != null) {
            if ($this->checkLoginIdAvilability($post_data['loginId'])) {
                $this->db_sbcc->insert('MasterUser', $post_data);
                if ($this->db_sbcc->affected_rows() > 0) {
                    $tempUserDetail = $this->getRegisterUserDetailByLoginId($post_data['loginId']);
                    $tempFunctionMaster = $this->getFunctionMaster();
                    $tempMappingUserIdFunctionId = array();
                    $k = 0;
                    foreach ($tempFunctionMaster as $key => $value) {
                        $tempMappingUserIdFunctionId[$k]['userId'] = $tempUserDetail[0]['userId'];
                        $tempMappingUserIdFunctionId[$k]['functionId'] = $value['functionId'];
                        $k++;
                    }
                    $this->db_sbcc->insert_batch('MappingUserFunction', $tempMappingUserIdFunctionId);
                    $this->status = TRUE;
                    $this->response_data['loginId'] = $post_data['loginId'];
                    $this->response_data['password'] = base64_decode($post_data['password']);
                    $this->message = 'Successfully submit your registration.';
                } else {
                    $this->status = false;
                    $this->message = 'Fail your registration.';
                }
            } else {
                $this->response_data['password'] = base64_decode($post_data['password']);
                $this->response_data['loginId'] = $post_data['loginId'];
                $this->status = false;
                $this->message = 'This loginid ' . $post_data['loginId'] . ' already register. Please try another loginid';
            }
        } else {
            $this->status = false;
            $this->message = 'Successfully submit your registration.';
        }
        return $this->getResponseAfterSubmitData($this->status, $this->message, $this->response_data);
    }

    /**
     * 
     * 
     * 
     * 
     */
    private function getResponseAfterSubmitData($_status, $_message, $_responsedata) {
        return array('status' => $_status, 'message' => $_message, 'response_data' => $_responsedata);
    }

    /**
     * 
     * 
     * 
     */
    private function checkLoginIdAvilability($loginId = null) {
        if ($loginId != null) {
            $query = $this->getData($this->db_sbcc->select('*')->from('MasterUser')->where('loginId', $loginId)->get());
            if ($query) {
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }

    /**
     * 
     * 
     * 
     */
    private function getRegisterUserDetailByLoginId($loginId = null) {
        if ($loginId != null) {
            return $this->getData($this->db_sbcc->select('*')->from('MasterUser')->where('loginId', $loginId)->get());
        } else {
            return false;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    private function getFunctionMaster() {
        return $this->getData($this->db_sbcc->select('*')->from('MasterFunction')->where('assignBy', 0)->get());
    }

    /**
     * 
     * 
     * 
     * 
     */
    private function getData($query = null) {
        if ($query != null and $query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }

}
