<?php

if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

class User extends MX_Controller {
	const TOKENPW = 'violet';
	public function __construct() {
		parent::__construct ();
		$this->load->helper ( 'cookie' );
		$this->load->helper('language');
		$this->lang->load('messages', 'message');
	}
	
	public function index() {
                
		$admin_info = $this->session->userdata ( 'adminInfo' );
		if ($admin_info) {
			$data ['content'] = $this->get_users ();
			$this->load->view ( 'user/index', $data );
		} else {
			$this->load->view ( 'login' );
		}
	}
	
	public function get_users() {
		$this->load->helper ( 'pagging' );
		$this->load->model ( 'user_model' );
		$provinces = lang('_PROVINCES_');
		error_log(var_export($provinces, TRUE), 3, '/srv/www/sbg/log/provinces.log');
		
		$data ['current_page'] = $this->uri->segment ( 4, 1 );
		$data ['itemsoptions'] = array (
				10,
				25,
				50,
				100 
		);
		$data ['perpage'] = 10;
		$data ['statusoptions'] = array (
				0 => "Tất cả",
				1 => "Đã đăng ký VIP",
				2 => "Dùng thử 7 ngày",
				3=> "Dùng thử hạn chế",
				4=> "Hết hạn"
		);
		$data ['status'] = 0;
		if ($this->input->post ( 'status' )) {
			$data ['status'] = ( int ) $this->input->post ( 'status' );
		}
		$data['provinces'] = $provinces;
		$data['province_code'] = '';
		if ($this->input->post ( 'province-code' )) {
			$data ['province_code'] = $this->input->post ( 'province-code' );
		}
		$data ['keyword'] = "";
		$data ['sorting_order'] = "sorting_desc";
		$data ['sorting_field'] = "created_time";
		if ($this->input->post ( 'sorting_order' )) {
			if ($this->input->post ( 'sorting_order' ) != "sorting") {
				$data ['sorting_order'] = $this->input->post ( 'sorting_order' );
				$data ['sorting_field'] = $this->input->post ( 'sorting_field' );
			}
		}
		if ($this->input->post ( 'items' )) {
			$data ['perpage'] = $this->input->post ( 'items' );
		}
		if ($this->input->post ( 'keyword' )) {
			$data ['keyword'] = $this->input->post ( 'keyword' );
		}
		$data ['daterange_start'] = "";
		$data ['daterange_end'] = "";
		if ($this->input->post ( 'daterange' )) {
			$daterange = explode ( " - ", $this->input->post ( 'daterange' ) );
			$daterange [0] = str_replace ( '/', '-', $daterange [0] );
			$daterange [1] = str_replace ( '/', '-', $daterange [1] );
			$data ['daterange_start'] = date ( 'Y-m-d H:i:s', strtotime ( $daterange [0] ) );
			$data ['daterange_end'] = date ( 'Y-m-d H:is:s', strtotime ( $daterange [1] . ' + 1 day' ) );
		}
		$data ['start'] = ($data ['current_page'] - 1) * $data ['perpage'];
		$data ['total'] = $this->user_model->get_total_users ( $data );
		$us = $this->user_model->get_list_users ( $data );
		$users = array ();
		
		foreach ( $us as $u ) {
			$u ['province'] = isset($provinces[$u ['province']]) ? $provinces[$u ['province']] : $u ['province'];
			$userinfo = $this->get_userinfo ( $u ['us_id'] );
			if ($userinfo) {
				$u ['username'] = $userinfo ['username'];
				$u['fullname'] = $userinfo['fullname'];
			}
			array_push ( $users, $u );
		}
		
		$data ['users'] = $users;
		$data ['paging_url'] = base_url () . "admin/nguoi_dung/trang/";
		$data ['num_links'] = 2;
		$data ['paging'] = pagging ( $data );
		
		if ($this->input->is_ajax_request ()) {
			return $this->load->view ( 'user/listview', $data );
		}
		return $this->load->view ( 'user/listview', $data, true );
	}
	
	public function edit_user() {

		$admin_info = $this->session->userdata ( 'adminInfo' );
		if ($admin_info) {
			$input = $this->input->post ();
			$result = array ();
			$result ['success'] = 0;
			$us_id = ( int ) $this->uri->segment ( 4, 0 );
			if ($us_id == 0) {
				$result ['success'] = 0;
			} else {
				$this->load->model ( 'user_model' );
				$input ['updated_time'] = date ( "Y-m-d H:i:s" );
				$this->user_model->update ( $us_id, $input );
				$result ['success'] = 1;
			}
			echo json_encode ( $result );
		} else {
			$this->load->view ( 'login' );
		}
	}
	
	public function delete_user() {
		$admin_info = $this->session->userdata ( 'adminInfo' );
		if ($admin_info) {
			$result = array ();
			$result ['success'] = 0;
			$us_id = ( int ) $this->uri->segment ( 4, 0 );
			if ($us_id == 0) {
				$result ['success'] = 0;
			} else {
				$this->load->model ( 'user_model' );
				$this->user_model->delete ( $us_id );
				$result ['success'] = 1;
			}
			echo json_encode ( $result );
		} else {
			$this->load->view ( 'login' );
		}
	}
	
	public function get_user_by_id() {
		$admin_info = $this->session->userdata ( 'adminInfo' );
		if ($admin_info) {
			$result = array ();
			$result ['success'] = 0;
			$us_id = ( int ) $this->uri->segment ( 4, 0 );
			if ($us_id == 0) {
				$result ['success'] = 0;
			} else {
				$this->load->model ( 'user_model' );
				$data = $this->user_model->search_by_id ( $us_id );
				$result ['item'] = $data;
				$result ['success'] = 1;
			}
			echo json_encode ( $result );
		} else {
			$this->load->view ( 'login' );
		}
	}
	
	private function get_userinfo($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 );
		// var_dump($data);
		$result = array ();
		parse_str ( $data, $result );
		return $result;
		/*
		 * if (strpos($data, '&')){ $arr_users = explode("&", $data); $str_username = $arr_users[1]; $arr_username = explode("=", $str_username); return $arr_username[1]; }else { return ""; }
		 */
	}
}