<?php

	$paginate = 1;
	$load = array('user');
	$title = 'Чат клана';
	$to = NULL;
	$to_id = 0;

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

	$id = isset($_GET['id']) ? intval($_GET['id']) : NULL;

	if(empty($id) || !$clan = $db->fetch("SELECT * FROM clans WHERE id = ?", array($_GET['id']))) {

		$_SESSION['error'] = 'Такого клана не существует!';
		header('Location: /clans');
		exit;

	}

	if($user['clan_id'] != $clan['id']) {

		$_SESSION['error'] = 'Чат только для участников клана!';
		header('Location: /clans/clan?id='. $clan['id']);
		exit;

	}

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

		$uto = $db->fetch("SELECT id, login FROM users WHERE id = ?", array(abs(intval($_GET['to']))));

		if($uto) {

			$to = $uto['login'] .', ';
			$to_id = $uto['id'];
		}

	}

	if(isset($_POST['message'])) {

		$_POST['message'] = trim($_POST['message']);

		if(!empty($_POST['message']) && $_POST['message'] != trim($to)) {

			$db->query("INSERT INTO clans_chat SET clan_id = ?, user_id = ?, to_id = ?, message = ?, time = UNIX_TIMESTAMP()", array($clan['id'], $user['id'], $to_id, $_POST['message']));

			header('Location: /clans/chat?id='. $clan['id']);
			exit;

		}

	}

	$c = 10;
	$k_post = $db->rows("SELECT * FROM clans_chat WHERE clan_id = '". $clan['id'] ."'", array());
	$k_page = k_page($k_post, $c);
	$page = page($k_page);
	$start = $c * $page - $c;

	$db->offEmulate();

	$messages = $db->fetchAll("SELECT * FROM clans_chat WHERE clan_id = '". $clan['id'] ."' ORDER BY id DESC LIMIT ?,?", array($start, $c));
	$us = new user($db);

?>

<form action="/clans/chat?id=<?= $clan['id'] ?><?= !empty($to_id) ? '&amp;to='. $to_id : NULL ?>" method="post">
	<div class="menu">
		<center>
			<input type="text" name="message" class="input" style="width:90%;" value="<?= $to ?>" required> 
			<button class="button green">Отправить</button>
			<a class="button green" href="/clans/chat?id=<?= $clan['id'] ?>">Обновить</a>
		</center>
	</div>
</form>

</div>

<?php foreach($messages as $message): ?>

<?php

	$cuser = $db->fetch("SELECT id, login, sex, online FROM users WHERE id = '". $message['user_id'] ."'", array());

?>

<div class="menu">
	<a href ="/user?id=<?= $cuser['user_id'] ?>">
		<img src="/imgData/static/<?= $cuser['sex'] ?>.png">
		<?= $us->login($cuser['id']) ?> <?= $cuser['online'] > ($config['time'] - 600) ? '<img src="/imgData/static/online.png">' : NULL ?>
	</a>
	<?php if($cuser['id'] != $user['id']) echo ' (<a href="/clans/chat?id='. $clan['id'] .'&amp;to='. $cuser['id'] .'">отв</a>)'; ?><br>
	<?= htmlspecialchars($message['message']) ?>
</div>

<?php endforeach; ?>
</div>

<?php if($k_page > 1): ?>

<div class="menu">
	<div class="hr_arr mt2 mb2 mlr10">
		<div class="alf">
			<div class="art">
				<div class="acn">
				</div>
			</div>
		</div>
	</div>
<center>

<?php str('?id='. $clan['id'] .'&amp;', $k_page, $page) ?>

</center>

<div class="hr_arr mt2 mb2 mlr10">
		<div class="alf">
			<div class="art">
				<div class="acn">
				</div>
			</div>
		</div>
	</div>

</div>

<?php

	endif;
	require_once $config['root'] .'/protected/footermain.php';

?>