<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! defined('_SBG_REGISTER_FOR')) 					define('_SBG_REGISTER_FOR', 				'DK');
if ( ! defined('_SBG_REGISTER_VIP_FOR')) 				define('_SBG_REGISTER_VIP_FOR', 				'VIP');
if ( ! defined('_SBG_CHARGING_FOR')) 					define('_SBG_CHARGING_FOR', 				'NAP');

class User extends MX_Controller {
	
	function __construct()
	{
		parent::__construct();
		$this->load->helper('cookie');
		$this->load->helper('language');
		$this->lang->load('messages', 'message');
		$this->load->helper(array('form', 'url'));
	}
	
	public function index(){
		$collaborator_info = $this->session->userdata('collaboratorInfo');
		if ($collaborator_info) {
			$data = array();
			$this->load->view('collaborator/user/index', $data);
		}else{
			$this->load->view(login);
		}
	}
	
	public function upload()
	{
		$collaborator_info = $this->session->userdata('collaboratorInfo');
        if ($collaborator_info) {
			$collaborator = $collaborator_info['login_name'];
			$config['upload_path'] = './upload/uploads/';
			$config['allowed_types'] = '*';
			$this->load->library('upload', $config);
			
			if ( $this->upload->do_upload() == false)
			{
				$error = array('error' => $this->upload->display_errors('', ''));
				$data['success'] = 0;
				$data['upload_error'] = $error;
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$file = $data['upload_data']['full_path'];
				$process = $this->processData($file, $collaborator);
				if ($process){
					$data['process'] = $process;
				}else{
					$data['process'] = null;				
				}
				$data['success'] = 1;
			}
			$result = $this->load->view('collaborator/user/result', $data, true);
			echo json_encode($result);

		}else{
			$this->load->view('login');
		}
	}
	
	protected function readExcelFile($file){
		$this->load->library("PHPExcel");
		$objPHPExcel = PHPExcel_IOFactory::load($file);
		$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
		
		foreach ($cell_collection as $cell):
			$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
			$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
			$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
			
			if ($row == 1){
				$header[$row][$column] = $data_value;
			} else {
				$arr_data[$row][$column] = $data_value;
			}
		endforeach;
		$data['header'] = $header;
		$data['values'] = $arr_data;
		return $data['values'];
	}
	
	protected function processData($file, $collaborator){
		$sentNumber = $collaborator;
		$this->load->model('services/services_model');
		$this->load->model('frontend/user_model');
		$this->load->model('collaborator_model');
		$data = $this->readExcelFile($file);

		$result = array();
		$user = array();
		if ($data){
			foreach ($data as $index => $value):
				$user['fullname'] = $value['B'];
				$user['province'] = $value['C'];
				$user['district'] = $value['D'];
				$arrPD = $this->collaborator_model->getProvinceAndDistict($user['province'], $user['district']);
				$user['province'] = $arrPD['province'];
				$user['district'] = $arrPD['district'];
				$user['school'] = $value['E'];
				$user['username'] = $this->services_model->formatPhoneNumber((string)$value['F'], 2);
				$user['service'] = $value['G'];
				$user['amount'] = $value['H'];
				$user['collboratorId'] = $value['I'];
				
				
				if ($user['username']){
	
					if (!preg_match('/^([0-9]{10,11}$)$/', $user['username'])) {
						$result[$index-1]['success'] = 0;
						$result[$index-1]['error'] = lang('_SBG_MO_PHONENUM_WRONG_FORMAT_MSG');
						$result[$index-1]['user'] = $user;
					}else{
						$result[$index-1] = $this->collaborator_model->processUser($user, $sentNumber);
					}
				}
				
			endforeach;
		}
		return $result;
	}
}