<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MX_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('cookie');
	}
	
	public function profile()
	{
		$user_info = $this->session->userdata('userInfo');
		if ($user_info)
		{
			$user = $user_info['user'];
			$data = $this->get_data($user);
			$this->load->view('frontend/user_profile', $data);
			
		}else
		{
			redirect('frontend/home');
		}
	}
	
	private function get_data($user)
	{
		$this->load->model('user_model');
		$arr_users = explode("&", $user);
		$str_id = $arr_users[0];
		$str_username = $arr_users[1];
		$str_fullname = $arr_users[2];
		$str_gender = $arr_users[3];
		$str_email = $arr_users[4];
		$str_phone = $arr_users[5];
		$str_school = $arr_users[6];
		$str_province = $arr_users[7];
		$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_gender = explode("=", $str_gender);
		$arr_email = explode("=", $str_email);
		$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->user_model->get_user_by_id($arr_id[1]);
		$data = array('us_id'=>$arr_id[1], 'username'=>$arr_username[1], 'full_name'=>$arr_fullname[1],
					'gender'=>$arr_gender[1], 'email'=>$arr_email[1], 'phone'=>$arr_phone[1], 'school'=>$arr_school[1],
					'acc_balanced'=>$us['acc_balanced'], 'expire_date'=>$us['expire_date'], 'date_diff'=>$this->dateDiff($us['expire_date'])
				);
		return $data;
	}
	
	private function dateDiff($expire_date)
	{
		$date1 = date("Y-m-d");
		$diff = abs(strtotime($expire_date) - strtotime($date1));
		$years = floor($diff / (365*60*60*24));
		$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
		$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
		if ($years>0){
			return $years." năm, ".$months." tháng, ".$days." ngày còn lại";
		}
		else{
			if ($months>0)
			{
				return  $months." tháng, ".$days." ngày còn lại";
			}
			else
			{
				return $days." ngày còn lại";
			}
		}
		
	}
}