<?php
session_start();
include ('private/mysql.php');
include ('tmp/head.php');

switch($sd){
default:

if($_SESSION['cl_ok'] == yes){
    echo "<font color=lightgreen>Вы успешно создали клан <b>$_SESSION[cl_name]</b>, посетите <a href='croom.php'>комнату клана</a></font><br><br>";
    $_SESSION['cl_ok'] = null;
    $_SESSION['cl_name'] = null;
}
if($user['clan'] == ''){
echo "<a style='color:lightgreen' href='clans.php?sd=create'>Создать клан</a><br>";
}
echo "<a href='clans.php?sd=all_clan'>Список кланов</a><br>";

break;

case 'create':

if($user['clan'] !== ''){
    header('Location: clans.php');
    exit;
}

if(empty($_POST['submit'])){
    
    if($_SESSION['cl_gold'] == yes){
        $last = 1000 - $user['gold'];
        echo "<font color=red><b>Для создания клана вам не хватает <span class='gold money'><span></span>$last</span></font></b><br><br>";
        $_SESSION['cl_gold'] = null;
    }
    if($_SESSION['cl_not'] == yes){
        echo "<font color=red><b>Клан с таким названием уже существует!</font></b><br><br>";
        $_SESSION['cl_not'] = null;
    }
    
    echo "Стоимость создания клана - <span class='gold money'><span></span>1000</span><br><br>";
    
    echo "<form action='clans.php?sd=create' method='post'>
    Название клана:<br>
    <input type='text' name='name' class='enter'><br>
    <input type='submit' name='submit' value='Создать' class='enter'></form>";
    
}else{
    
    $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
    
    if($name == ''){
        header('Location: clans.php');
        exit;
    }
    
    if($user['clan'] !== ''){
        header('Location: clans.php');
        exit;
    }
    
    if($user['gold'] < 1000){
        $_SESSION['cl_gold'] = yes;
        header('Location: clans.php?sd=create');
        exit;
    }
    
    $q = mysql_num_rows(mysql_query("SELECT * FROM `clan` WHERE `name`='$name'"));
    if($q >= 1){
        $_SESSION['cl_not'] = yes;
        header('Location: clans.php?sd=create');
        exit;
    }
    
    mysql_query("UPDATE `users` SET `gold`=`gold`-'1000',`clan`='$uid' WHERE `id`='$uid'");
    
    mysql_query("INSERT INTO `clan` SET `name`='$name',`lider`='$uid'");
    
    $_SESSION['cl_ok'] = yes;
    $_SESSION['cl_name'] = $name;
    header('Location: clans.php');
    exit;
}


break;



case 'all_clan':

$q = mysql_query("SELECT * FROM `clan`");
$e = mysql_num_rows($q);

if($e == 0){
    echo "Ещё нет кланов.<br>Вы можете <a href='clans.php?sd=create'>создать</a> клан.";
    include ('tmp/foot.php');
    exit;
}

While($w = mysql_fetch_assoc($q)){
    
    $i++;
    
    $lider = mysql_fetch_assoc(mysql_query("SELECT `login` FROM `users` WHERE `id`='$w[lider]'"));
    
    echo "$i. <a href='clans.php?sd=clan_info&id=$w[id]'>$w[name]</a> (Лидер: $lider[login], Репутация: $w[reputation])<hr>";
    
}


break;


}
include ('tmp/foot.php');
?>