<?php
/*
* @ServerMon v1.1
* @Developed by Maksa988
* @www.Server-Mon.ru
* @Copyright (C) 2014
*/
 if(!defined('SMON'))
	die('Hacking attempt!');
	
class users extends Controller {
	public function __construct() {
		global $db;
		
		$this->header();
		$this->model('users');
		//$this->model('servers');
		$this->model('vote');
		
		if(empty($_SESSION['user_id'])):
			return $this->view(array('login/index'), $this->data); //Загружаем шаблон
		endif;
		
		$user = $this->usersModel->getUser($_SESSION['user_id']);
		$this->data['user'] = $user;
		
		if($user['user_group'] != $this->setting('admin_group')):
			$this->data['error'] = 'Вы не имеете доступа!';
			return $this->view(array('login/index'), $this->data); //Загружаем шаблон
		endif;
		
		$this->data['activeSection'] = 'modules';
		$this->data['menu'] = array(array('url'=> 'users/add', 'icon' => 'plus', 'name'=> 'Добавить пользователя')); 
		
		if($this->GET() >= '1'){
			$user = $this->usersModel->getUser($this->GET());
			
			if($user){
				$this->data['user'] = $user;
				$this->data['auth'] = $this->usersModel->getAuth("WHERE `al_user` = '".$user['user_login']."'");
				$this->data['vote'] = $this->voteModel->getVote("WHERE `vote_ip` = '".$user['user_ip_login']."'");
				$this->data['invoices'] = $this->usersModel->getInvoices($user['user_id']);
				//$this->data['servers'] = $this->serversModel->getServers("WHERE `user_id` = '".$this->GET()."'");
				if($this->POST('edit')){
					$login = $this->POST('login');
					$email = $this->POST('email');
					$group = $this->POST('group');
					$balance = $this->POST('balance');
					
					if($email){
						if($this->usersModel->editUser("`user_email` = '{$email}', `user_group` = '{$group}', `user_balance` = '{$balance}'", $this->GET())){
							$this->data['success'] = 'Пользователь отредактирован.';
						} else {
							$this->data['error'] = 'При изменении данных произошла ошибка. Повторите попытку.';
						}
					} else {
						$this->data['error'] = 'Все поля обязательных к заполнению';
					}
				}
				
				return $this->view(array('common/header', 'users/edit', 'common/footer'), $this->data); //Загружаем шаблон
			} else {
				return $this->view(array('common/header', 'users/404user', 'common/footer'), $this->data); //Загружаем шаблон
			}
		} else {
			return $this->view(array('common/header', 'users/404user', 'common/footer'), $this->data); //Загружаем шаблон
		}
	}
}