<?
require_once '../core/system.php';
echo only_reg();
if($user['clan_id'] == 0) {
    header("Location: /clan/");
    exit();
}

$error = NULL;
if(isset($_POST['msg'])) {
    $msg = mysql_real_escape_string($_POST['msg']);
    if((mb_strlen($msg, 'UTF-8') < 3) || (mb_strlen($msg, 'UTF-8') > 400)) $error =  'Длина сообщения должна быть в пределах 3 - 400 символов';
    if(!isset($error)) {
        mysql_query("INSERT INTO `clan_chat` SET `clan_id` = '$user[clan_id]', `id_user` = '$user[id]', `msg` = '$msg', `time` = '".time()."'");
        header('Location: /clan/chat.php');
        exit();
    }
}

if(isset($_GET['del']) && $user['clan_prava'] == 4) {
    $id = (int) $_GET['del'];
    mysql_query("DELETE FROM `clan_chat` WHERE `id` = '$id'");
    header("Location: /clan/chat.php");
    exit();
}

$header = 'Чат клана';
require_once '../core/head.php';

$k_post = mysql_result(mysql_query("SELECT COUNT(*)  FROM `clan_chat` WHERE `clan_id` = '".$user['clan_id']."'") ,0);
$k_page = k_page($k_post,$set['p_str']);
$page = page($k_page);
$start = $set['p_str']*$page-$set['p_str'];

if($error) {
    echo '<div class="player" style="color: red">
    '.$error.'
    </div>';
}

echo '<div class="player menuList">
<li><a href="?"><img src="/images/icon/arrow.png">Обновить</a></li>
</div>
<div class="mini-line"></div>';

if(isset($_GET['id'])) {
    $ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = ".(int) $_GET['id']." LIMIT 1"));
    echo '<form class="player" method="post" action="">
    <input class="text large" value="'.htmlspecialchars($ank[nick]).', " type="text" name="msg" /><br />
    <span class="btn"><span class="end"><input class="label" type="submit" value="Отправить">Отправить</span></span>
    </form>';
} else {
    echo '<form class="player" method="post" action="">
    <input class="text large" type="text" name="msg" /><br />
    <span class="btn"><span class="end"><input class="label" type="submit" value="Отправить">Отправить</span></span>
    </form>';
}

$q = mysql_query("SELECT * FROM `clan_chat`  WHERE `clan_id` = '".$user['clan_id']."' ORDER BY `id` DESC LIMIT $start, $set[p_str]");
echo '<div class="dot-line"></div><div class="player" style = "color: #d3e2ed;">';
while($post = mysql_fetch_assoc($q)) {
    $ank = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = $post[id_user] LIMIT 1"));
    if($ank[prava] >= 5 ){
        $color = 'Admin';
    }
    if($ank[prava] == 7 ){
        $color = 'quality-4';
    }
    if($ank[id] == 2638 ){
        $color = 'red';
    }
    if($ank[id] == 2408 ){
        $color = 'yellow';
    }
    if($ank[prava] == 4 ){
        $color = 'Moder';
    }
    if($ank[prava] == 0 ){
        $color = '';
    }
    
    if ($ank['storona'] == '0') {
        echo '<img src="/images/icon/0.png" height="11" alt="a"/>';
        }
        
    if ($ank['storona'] == '1') {
        echo '<img src="/images/icon/1.png" height="11" alt="d"/>';
        }
        
echo online($ank[id]);
    
$nick = ($ank['id'] == 2403) ? '<span style="color: red">'.$ank['nick'].'</span>' : $ank['nick'];
echo '<a href="/user/?id='.$ank['id'].'"><b><span style = "color: #becdd8;">'.$nick.'</span></b></a>';
    
echo '<span class="yellow"> ['.vremja($post['time']).']</span>
<a href="?id='.$ank['id'].'">(отв.)</a>';
    if($user['clan_prava'] == 4) echo '<a href="?del='.$post['id'].'">(удал.)</a>';
    echo ': <span class="'.$color.'">'.smiles($post['msg']).'</span><br>';
}

echo '</div>';
if ($k_page>1){
    echo "<div class='dot-line'></div>";
    str('?',$k_page,$page);
}

include '../core/foot.php';