<?php 
// var_dump($this->getTicket($param['id']));
if($ticket = $this->getTicket($param['id'])){
	$messages = $this->getMessages($param['id']);
} else {
	$this->view(array("theme/_common/_header", "theme/errors/404", "theme/_common/_footer"), array("user" => $this->user, "shops" => $this->shops)); 
	exit();
}

if($_POST['ajax'] == 'getMessagess'){
	$id = $_POST['tid'];
	
	$messages = $this->getMessages($id);
	
	foreach($messages as $item):
		$item['ticket_message_text'] = preg_replace("#(https?|ftp)://\S+[^\s.,>)\];'\"!?]#",'<a href="\\0" target="_blank">\\0</a>', $item['ticket_message_text']);
		if($item['user_group'] == '0'):
		echo '<div class="clientticketreplyheader">
			<table width="100%" border="0" cellpadding="10" cellspacing="0">
				<tbody>
					<tr>
						<td><strong>'.$item['user_firstname'].' '.$item['user_lastname'].'</strong><br>Клиент</td>
						<td align="right">'. date("d.m.Y в H:i", strtotime($item['ticket_message_time'])) .'</td>
					</tr>
				</tbody>
			</table>
		</div>
		<div class="clientticketreply">'. nl2br($item['ticket_message_text']) .'<br></div>';
		endif;

		if($item['user_group'] >= '2'):
			echo '<div class="adminticketreplyheader">
				  <table width="100%" border="0" cellpadding="10" cellspacing="0">
						<tbody>
							<tr>
								  <td><strong>'.$item['user_firstname'].' '.$item['user_lastname'].'</strong><br>Сотрудник</td>
								  <td align="right">'. date("d.m.Y в H:i", strtotime($item['ticket_message_time'])) .'</td>
							</tr>
						</tbody>
				  </table>
			</div>
			<div class="adminticketreply">
				 '. nl2br($item['ticket_message_text']) .'<br>
				<br>
				(ПОДПИСЬ)';

				if($item['ticket_message_asses'] == 0):
				echo '<div style="float: right">
				как вы оцениваете ответ? <a style="cursor: pointer;" onclick="return assAnswer(\''.$item['ticket_message_id'].'\', \'2\', \''.$item['user_id'].'\')"><i class="fa fa-thumbs-down"></i> Плохой</a> / <a style="cursor: pointer;" onclick="return assAnswer(\''.$item['ticket_message_id'].'\', \'1\', \''.$item['user_id'].'\')"><i class="fa fa-thumbs-up"></i> Хороший</a>
				</div>';
				elseif($item['ticket_message_asses'] == 1):
				echo '<div style="float: right">
				вы оценили ответ как <a style="text-decoration: none;"><i class="fa fa-thumbs-up"></i> Хороший</a>
				</div>';
				elseif($item['ticket_message_asses'] == 2):
				echo '<div style="float: right">
				вы оценили ответ как <a style="text-decoration: none;"><i class="fa fa-thumbs-down"></i> Плохой</a>
				</div>';
				endif;
			echo '</div>';
		endif;
	endforeach; 
	
	return true;
	exit();
} elseif($_POST['ajax'] == 'assmessage'){
	$mid = $_POST['mid'];
	$ass = $_POST['ass'];
	$uid = $_POST['uid'];
	if($ass == 1){
		$this->editUser("`user_answers_good` = user_answers_good+1", $uid);
	} else {
		$this->editUser("`user_answers_bad` = user_answers_bad+1", $uid);
	}

	$this->editMessage("`ticket_message_asses` = '{$ass}'", $mid);
	echo $mid;
	return true;
	exit();
} elseif($_POST['ajax'] == 'closeticket'){
	$tid = $_POST['tid'];
	
	$this->editTicket("`ticket_status` = '0'", $tid);
	return true;
	exit();
} elseif($_POST['ajax'] == 'addmessage'){
	$tid = $_POST['tid'];
	$text = $_POST['text'];
	
	$this->addMessage($_SESSION['user_id'], $tid, $text);
	$this->editTicket("`ticket_status` = '3'", $tid);
	
	$tuser = $this->userInfo($ticket['user_id']);

	if($tuser['user_telegram_tickets']) {
		$code = $tuser['user_telegram_code'];
		$this->requestAPITelegram("sendMSG", "newanswer", $code, $tid);
	}
	echo 1;
	return true;
	exit();
}

if($this->user['user_group'] >= "2" or $this->user['user_id'] == $ticket['user_id']) {
	$messages = $this->getMessages($param['id']);
	// dd($messages);
	$this->view(array("theme/_common/_header", "theme/tickets/ticket", "theme/_common/_footer"), array("user" => $this->user, "shops" => $this->shops, "ticket" => $ticket, "messages" => $messages)); 
} else {
	$this->view(array("theme/_common/_header", "theme/errors/404", "theme/_common/_footer"), array("user" => $this->user, "shops" => $this->shops)); 
}