<?php
$t = 'Просмотр тикета';
require_once '../../wcore/core.php';
require_once '../../wcore/head.php';
iank();
$id = !isset($_GET['id']) || empty($_GET['id'])?0:intval($_GET['id']);
$sql = mysqli_query($mysqli,"SELECT * FROM `tickets_list` WHERE `tid` = '$id'");
if (mysqli_num_rows($sql) == 0){
    echo msg_err('err',$lang['ticket_read_err_1']);
    require_once '../../wcore/foot.php';
    exit();
}
$data = mysqli_fetch_object($sql);
$depo = mysqli_fetch_object(mysqli_query($mysqli,"SELECT * FROM `tickets_cat` WHERE `id` = '$data->department'"));
if ($data->uid == $ank->id){
    $ace = 1;
    $acr = 1;
} elseif ($ank->prv >= 2){
    $ace = 1;
    $acr = 2;
} else {
    $ace = 0;
}

if ($ace == 0){
    echo msg_err('err',$lang['ticket_read_err_1']);
    require_once '../../wcore/foot.php';
    exit();
}
if (isset($_POST['ok']) && $data->status != 0) {
    $msg = antixs($_POST['msg']);
    $count_msg_s = mb_strlen($_POST['msg']);
    if ($count_msg_s <= 3) {
        echo msg_err('err', $lang['ticket_read_err_2']);
        require_once '../../wcore/foot.php';
        exit();
    }
    $ankz = mysqli_fetch_assoc(mysqli_query($mysqli,"SELECT `email`,`login` FROM `users` WHERE `id` = '".$data->uid."'"));
    $read_ti = $ank->prv >= 2 ? 0 : 1;
    mysqli_query($mysqli, "INSERT INTO `tickets_mess`(`tid`, `uid`, `mess`, `time`, `read_mess`) VALUES ($id,$ank->id,'$msg','" . time() . "',$read_ti)");
    if ($read_ti == 0){
        $mailsend->SendEmail($ankz['email'], '[Тикет №'.$id.'] '.$data->name, 'Здравствуйте '.$ankz['login'].'!<br> На ваш тикет <b>'.$data->name.'</b> поступил ответ от оператора '.$ank->login.'. <br>Это автоматическое сообщение и на него отвечать не требуется.');
    }
    //var_dump($ankz,$data->name);
    mysqli_query($mysqli, "UPDATE `tickets_list` SET `status`='$acr' WHERE `tid` = '$id'");
    header('Location: ?id=' . $id);
    exit;
} elseif (isset($_GET['clope'])){
    mysqli_query($mysqli,"UPDATE `tickets_list` SET `status`='".($data->status == 0?2:0)."' WHERE `tid` = '$id'");
    header('Location: ?id='.$id);
    exit;
} elseif (isset($_GET['del'])) {
    iank(2);
    mysqli_query($mysqli,"DELETE FROM `tickets_mess` WHERE `tcid` = '".intval($_GET['del'])."'");
    header('Location: ?id='.$id);
    exit;
} else {
    $k_post = _mc('tickets_mess',"WHERE `tid` = '$id'");
    $k_page = k_page($k_post, 10);
    $page = page($k_page);
    $start = 10 * $page - 10;
    if ($data->uid == $ank->id){
        mysqli_query($mysqli,"UPDATE `tickets_mess` SET `read_mess`=1 WHERE `tid` = '$id'");
    }
    $_tmp_tickets = mysqli_query($mysqli, "SELECT * FROM `tickets_mess` WHERE `tid` = '$id' ORDER BY `tcid` DESC LIMIT $start, 10") or die("Ошибка запроса: " . mysqli_error($mysqli));
    while ($ns = mysqli_fetch_assoc($_tmp_tickets)) {
        $array[] = array(
            'id' => $ns['tcid'],
            'user' => acc($ns['uid']),
            'msg' => output($ns['mess']),
            'time' => ptime($ns['time']));
    }
}
if (!isset($array)){$array = FALSE;}
echo $twig->render('pages_tickets_ticket.tpl', [
    'listmsg'   =>  $array,
    'info'      =>  $data,
    'depo'      =>  $depo,
    'lang'      =>  $lang,
    'crfs'      =>  _csrf(),
    'user'      =>  $ank->prv,
    'status'    =>  $lang['ticket_status_'.$data->status],
    'adm_depo'  =>  acc($depo->adm_us),
    'time'      =>  ptime($data->time),

    ]);
if ($k_page > 1){str('?id='.$id.'&',$k_page,$page);}
require_once '../../wcore/foot.php';
?>