<?php
$t = 'Добавление тикета';
require_once '../../wcore/core.php';
require_once '../../wcore/head.php';
iank(3);
if (isset($_POST['ok'])){
    if (!isset($_POST['who'])){
        echo msg_err('err','Так ты не выбрал кому отправим тикет');
        require_once '../../wcore/foot.php';
        exit();
    }
    if ($_POST['who'] == 0){
        $uid = intval($_POST['user_id']);
        $profile = mysqli_query($mysqli,"SELECT * FROM `users` WHERE `id` = ".$uid);
        if (mysqli_num_rows($profile) != 0) {
            $user = mysqli_fetch_object($profile);
        } else {
            echo msg_err('err',$lang['emptylogin']);
            require_once '../../wcore/foot.php';
            exit();
        }
    } elseif ($_POST['who'] == 1){
        $username = antixs($_POST["user_login"]);
        $profile = mysqli_query($mysqli,"SELECT * FROM `users` WHERE `login` = '$username'");
        if (mysqli_num_rows($profile) != 0) {
            $user = mysqli_fetch_object($profile);
            $uid = $user->id;
        } else {
            echo msg_err('err',$lang['emptylogin']);
            require_once '../../wcore/foot.php';
            exit();
        }
    
    } else {
        echo msg_err('err',"Извините, но мне нельзя!");
        require_once '../../wcore/foot.php';
        exit();
    }
    
    if (empty($_POST['name']) || !isset($_POST['name'])){
        echo msg_err('err',$lang['ticket_new_err_1']);
        require_once '../../wcore/foot.php';
        exit();
    }
    if (empty($_POST['cat']) || !isset($_POST['cat'])){
        echo msg_err('err',$lang['ticket_new_err_2']);
        require_once '../../wcore/foot.php';
        exit();
    }
    if (empty($_POST['msg']) || !isset($_POST['msg'])){
        echo msg_err('err',$lang['ticket_new_err_3']);
        require_once '../../wcore/foot.php';
        exit();
    }
    $name   =   antixs($_POST['name']);
    $cat    =   intval($_POST['cat']);
    $msg    =   antixs($_POST['msg']);
    $count_msg_s = mb_strlen($_POST['msg']);
    $sql = mysqli_query($mysqli,"SELECT * FROM `tickets_cat` WHERE `id` = '$cat' LIMIT 1");
    if (mysqli_num_rows($sql) == 0){
        echo msg_err('err',$lang['ticket_new_err_7']);
        require_once '../../wcore/foot.php';
        exit();
    }
    mysqli_query($mysqli,"INSERT INTO `tickets_list`(`name`, `uid`, `time`, `department`, `status`) VALUES ('$name',$uid,'".time()."',$cat,2)");
    $ticket_created_id = mysqli_insert_id($mysqli);
    $mailsend->SendEmail($ank->email, 'У вас новый тикет №'.$ticket_created_id.'', 'Здравствуйте!<br>У вас появился новый тикет номер '.$ticket_created_id.' с названием <b>'.$name.'</b>.<br>Это автоматическое сообщение и на него отвечать не требуется.');
    $author_sms_id = $ank->id;
    mysqli_query($mysqli,"INSERT INTO `tickets_mess`(`tid`, `uid`, `mess`, `time`, `read_mess`) VALUES ($ticket_created_id,$author_sms_id,'$msg','".time()."',1)");
    echo msg_err('suc',$lang['adm_ticket_mgr_t7']);
    require_once '../../wcore/foot.php';
    exit();
}

    $sql_query = mysqli_query($mysqli,"SELECT * FROM `tickets_cat`") or die("Ошибка запроса: ".mysqli_error($mysqli));;
    while ($cat = mysqli_fetch_assoc($sql_query)){
        $data[] = $cat;
    }
$data = !isset($data)?NULL:$data;
echo $twig->render('admin_add_ticket.tpl', [
    'lang'  =>  $lang,
    'data'  =>  $data,

]);
require_once '../../wcore/foot.php';
?>