<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MX_Controller
{

	const TOKENPW = 'violet';

	public function __construct() {
		parent::__construct();		
	}
	
	public function login($return_type = 'xml')
	{
		$user_info = $this->session->userdata('userInfo');
		if ($user_info)
		{
			$user = $user_info['user'];
			$data['content'] = $this->get_data($user);
			if ($return_type == "xml"){
				$this->load->view('user', $data);
			}
		}else
		{
			redirect('/frontend/home');
		}
	}
	
	
	private function get_user($us_id)
	{
		$src = 'violet';
		$token = md5($us_id.self::TOKENPW);
		$this->load->model('user_model');
		$data = $this->user_model->get_user_info($src, $us_id, $token);
		return $data;
	}
	
	private function get_data($user)
	{
		$this->load->model('us_model');
		$arr_users = explode("&", $user);
		$str_id = $arr_users[0];
		$str_username = $arr_users[1];
		$str_fullname = $arr_users[2];
		$str_phone = $arr_users[5];
		$str_school = $arr_users[6];
		$str_province = $arr_users[6];
		$str_money = $arr_users[8];
		$str_level = $arr_users[9];
		$arr_id = explode("=", $str_id);
		$arr_username = explode("=", $str_username);
		$arr_fullname = explode("=", $str_fullname);
		$arr_phone = explode("=", $str_phone);
		$arr_school = explode("=", $str_school);
		$arr_province = explode("=", $str_province);
		$arr_money = explode("=", $str_money);
		$arr_level = explode("=", $str_level);
		$us = $this->us_model->search_by_id($arr_id[1]);
		return array("userId" => $arr_id[1], "userLevel" => 1, "userPhone" => $arr_phone[1], "userMoney" => $arr_money[1], "appUserName" => $arr_fullname[1], "appAddress" => $arr_school[1], "licType" => "SBG", "licCustomer" => $arr_username[1], "licCreate" => $us['created_time'], "licExpire" => $us['expire_date']);
	}
}