<?php
require_once '../core/system.php';
require_once 'sys.php';
echo only_reg();

if(!isset($_GET['accept']) && !isset($_GET['decline'])) {
    header("Location: /");
    exit();
}

if(isset($_GET['decline'])) {
    $id = num($_GET['decline']);
    $row = mysql_fetch_assoc(mysql_query("SELECT * FROM `invite` WHERE `id` = '$id' AND `user_id` = '".$user['id']."'"));
    if(!empty($row['owner_id'])) {
        mysql_query("DELETE FROM `invite` WHERE `id` = '$id'");
        header("Location: /");
        $_SESSION['message'] = 'Приглашение отклонено';
        exit();
    }
}

if(isset($_GET['accept'])) {
    $id = num($_GET['accept']);
    $row = mysql_fetch_assoc(mysql_query("SELECT * FROM `invite` WHERE `id` = '$id' AND `user_id` = '".$user['id']."'"));
    $owner = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = '".$row['owner_id']."'"));
    $clan = mysql_fetch_assoc(mysql_query("SELECT * FROM `clans` WHERE `id` = '".$row['clan_id']."'"));
    $invited_user = mysql_fetch_assoc(mysql_query("Select * from `user` where `id` = '".$row['user_id']."' Limit 1"));
    if(!empty($row['owner_id'])) {
        mysql_query("update `user` set `clan_jornal` = `clan_jornal` +1  where `id`<> '$user[id]' and `clan_id`= '".$row['clan_id']."'");
        mysql_query("UPDATE `user` SET `clan_id` = '".$row['clan_id']."' WHERE `id` = '".$user['id']."'");
        mysql_query("DELETE FROM `invite` WHERE `user_id` = '".$user['id']."'");
        $text = '<img src="/images/icon/'.$owner['storona'].'.png" width="12" height="12"/> <a href="/user/'.$owner['id'].'/">'.$owner['nick'].'</a> принял игрока <img src="/images/icon/'.$invited_user['storona'].'.png" width="12" height="12"/> <a href="/user/'.$invited_user['id'].'/">'.$invited_user['nick'].'</a> в клан';
        mysql_query("INSERT INTO `jr_rekrut` SET `clan_id` = '".$row['clan_id']."', `text` = '$text'");
 header("Location: /clan/");
$_SESSION['message'] = 'Вы успешно вступили в клан!';
        exit();
    }
}

header("Location: /");
exit();