<?php

ini_set('display_errors', 'On');
	ini_set('error_reporting', -1);

	$title = 'Кланы';

	require_once '../protected/sys.php';

	if(isset($_GET['invite'])) {

		$_GET['invite'] = abs(intval($_GET['invite']));

		$uinvite = $db->fetch("SELECT id, clan_id FROM users WHERE id = ?", array($_GET['invite']));

		if(!$uinvite) {

			$_SESSION['error'] = 'Пользователь не найден!';
			header('Location: /');
			exit;

		}

		if(!$user['clan_id'] || !$db->rows("SELECT id FROM clans WHERE admin_id = ?", array($user['id']))) {

			$_SESSION['error'] = 'Увас нет клана!';
			header('Location: /user?id='. $uinvite['id']);
			exit;

		}

		if($uinvite['clan_id']) {

			$_SESSION['error'] = 'Пользователь уже состоит в клане!';
			header('Location: /user?id='. $uinvite['id']);
			exit;

		}

		if($db->rows("SELECT id FROM clans_invite WHERE clan_id = ? AND user_id = ?", array($user['clan_id'], $uinvite['id']))) {

			$_SESSION['error'] = 'Вы уже отправили приглашение данному пользователю!';
			header('Location: /user?id='. $uinvite['id']);
			exit;

		}

		if($db->rows("SELECT id FROM users WHERE clan_id = ?", array($user['clan_id'])) > 9) {

			$_SESSION['error'] = 'Ваш клан уже набран!';
			header('Location: /user?id='. $uinvite['id']);
			exit;

		}

		$db->query("INSERT INTO clans_invite SET clan_id = ?, user_id = ?", array($user['clan_id'], $uinvite['id']));

			$_SESSION['info'] = 'Приглашение отправлено!';
			header('Location: /user?id='. $uinvite['id']);
			exit;

	}

	if(isset($_GET['confirm'])) {

		$_GET['confirm'] = abs(intval($_GET['confirm']));

		if(!$db->rows("SELECT id FROM clans_invite WHERE clan_id = ?", array($_GET['confirm']))) {

			$_SESSION['error'] = 'Приглашение не найдено!';
			header('Location: /');
			exit;

		}


		if($db->rows("SELECT id FROM users WHERE clan_id = ?", array($_GET['confirm'])) > 9) {

			$db->query("DELETE FROM clans_invite WHERE clan_id = ?", array($_GET['confirm']));

			$_SESSION['error'] = 'Кланлан уже набран!';
			header('Location: /');
			exit;

		}

		$db->query("DELETE FROM clans_invite WHERE user_id = ?", array($user['id']));
		$db->query("UPDATE users SET clan_id = ? WHERE id = ?", array($_GET['confirm'], $user['id']));

		$_SESSION['info'] = 'Вы теперь в клане!';
		header('Location: /clans/clan?id='. $_GET['confirm']);
		exit;


	}

	if(isset($_GET['decline'])) {

		$_GET['decline'] = abs(intval($_GET['decline']));

		if(!$db->rows("SELECT id FROM clans_invite WHERE clan_id = ?", array($_GET['decline']))) {

			$_SESSION['error'] = 'Приглашение не найдено!';
			header('Location: /');
			exit;

		}

		$db->query("DELETE FROM clans_invite WHERE clan_id = ?", array($_GET['decline']));

		$_SESSION['info'] = 'Приглашение отклонено!';
		header('Location: /');
		exit;

	}

	if(isset($_GET['delete'])) {

		$_GET['delete'] = abs(intval($_GET['delete']));

		if(!$db->rows("SELECT id FROM users WHERE id = ? AND id != ? AND clan_id = ?", array($_GET['delete'], $user['id'], $user['clan_id']))) {

			$_SESSION['error'] = 'Нет такого пользователя!';
			header('Location: /clans/clan?id='. $user['clan_id']);
			exit;

		}

		if(!$user['clan_id'] || !$db->rows("SELECT id FROM clans WHERE id = ?", array($user['clan_id']))) {

			$_SESSION['error'] = 'Вы не являетесь основателем клана!';
			header('Location: /clans/clan?id='. $user['clan_id']);
			exit;

		}

		$db->query("UPDATE users SET clan_id = ? WHERE id = ?", array(0, $_GET['delete']));

		$_SESSION['info'] = 'Пользователь исключен с клана!';
		header('Location: /clans/clan?id='. $user['clan_id']);
		exit;

	}


?>