<?php 

if($this->user['user_group'] >= "4"){
	$items = $this->getMoneys();
} else {
	$items = $this->getMoneys("WHERE gd_users.user_id = '{$this->user['user_id']}'");
}

if($_POST['ajax'] == "okRequest"){
	$item_id = $_POST['item_id'];
	if($this->user['user_group'] >= 4){
		$this->editRequest("`money_status` = '2', `admin_id` = '{$this->user['user_id']}'", $item_id);
		exit("1");
	} else {
		exit("0");
	}
}

if($_POST['ajax'] == "aremoveRequest"){
	$item_id = $_POST['item_id'];
	if($this->user['user_group'] >= 4){
		$this->editRequest("`money_status` = '1', `admin_id` = '{$this->user['user_id']}'", $item_id);
		$item_info = $this->moneyInfo($item_id);
		$this->editUser("`user_balance` = user_balance + {$item_info['money_sum']}", $item_info['user_id']);
		exit("1");
	} else {
		exit("0");
	}
}

if($_POST['ajax'] == "removeRequest"){
	$item_id = $_POST['item_id'];
	$this->editRequest("`money_status` = '1'", $item_id);
	$item_info = $this->moneyInfo($item_id);
	$this->editUser("`user_balance` = user_balance + {$item_info['money_sum']}", $item_info['user_id']);
	exit("1");
}

if($_POST['ajax'] == "sendRequest"){
	$pay_system = $_POST['pay_system'];
	$sum[1] = (int)$_POST['money_sum'];
	if($this->user['user_rate'] == 1){
		$percent = 0.10;
	} elseif ($this->user['user_rate'] == 2) {
		$percent = 0.05;
	} elseif ($this->user['user_rate'] == 3) {
		$percent = 0;
	}
	$sum[2] = $sum[1]* $percent;
	$sum[3] = $sum[1]-$sum[2];
	if($this->user['user_balance'] >= $sum[1]){	
		if($sum[1] >= "50"){
			if($pay_system == "webmoney"){
				$public_key = md5(md5(md5(time().$sum)."maksa988"));
				$public_key = $public_key{1}.$public_key{2}.$public_key{3}.$public_key{4}.$public_key{5}.$public_key{6}.$public_key{7}.$public_key{8};
				if($this->addMoneyRequest($sum[3], $this->user['user_id'], $public_key, "1")){
					$this->editUser("`user_balance` = user_balance - {$sum[1]}", $this->user['user_id']);
					exit("1");
				} else {
					exit("Произошла ошибка, повторите попытку!");
				}
			} elseif($pay_system == "qiwi"){
				$public_key = md5(md5(md5(time().$sum)."maksa988"));
				$public_key = $public_key{1}.$public_key{2}.$public_key{3}.$public_key{4}.$public_key{5}.$public_key{6}.$public_key{7}.$public_key{8};
				if($this->addMoneyRequest($sum[3], $this->user['user_id'], $public_key, "2")){
					$this->editUser("`user_balance` = user_balance - {$sum[1]}", $this->user['user_id']);
					exit("1");
				} else {
					exit("Произошла ошибка, повторите попытку!");
				}
			} else {
				exit("Выберите платежную систему!");
			}
		} else {
			exit("Минимальная сумма вывода <b>50</b> рублей! <!-- ".$sum[1]." -->");
		}
	} else {
		exit("На вашем балансе недостаточно денег для вывода!");
	}
}
if($_POST['ajax'] == "changeRate"){
	$user_rate = $this->escape($_POST['user_rate']);
	if($user_rate >= $this->user['user_rate']){
		if($user_rate == "1"){
			if($this->editUser("`user_rate` = '{$user_rate}'", $this->user['user_id'])){
				echo 1;
				exit();
			} else {
				echo 0;
				exit();
			}
		} elseif($user_rate == "2"){
			if($this->user['user_balance'] >= "100"){
				if($this->editUser("`user_rate` = '{$user_rate}', `user_balance` = user_balance - 100, `user_rate_time` = NOW() + INTERVAL 1 MONTH", $this->user['user_id'])){
					echo 1;
					exit();
				} else {
					echo 0;
					exit();
				}
			} else {
				echo 0;
				exit();
			}
		} elseif($user_rate == "3"){
			if($this->user['user_balance'] >= "200"){
				if($this->editUser("`user_rate` = '{$user_rate}', `user_balance` = user_balance - 200, `user_rate_time` = NOW() + INTERVAL 1 MONTH", $this->user['user_id'])){
					echo 1;
					exit();
				} else {
					echo 0;
					exit();
				}
			} else {
				echo 0;
				exit();
			}
		} else {
			echo 0;
			exit();
		}
	} else {
		echo 0;
		exit();
	}
}

$this->view(array("theme/_common/_header", "theme/user/money", "theme/_common/_footer"), array("user" => $this->user, "shops" => $this->shops, "shop" => $shop, "items" => $items)); 