<?php
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        $code                  = $support->generateCode();
        $subject               = $_POST['subject'];
        $priority              = $_POST['priority'];
        $message               = $_POST['message'];
        $stmt = $db->query("INSERT INTO {$db->prefix}support_tickets (`status`, `code`, `priority`, `user_id`, `user_name`, `subject`, `message`, `date_added`, `date_updated`) VALUES (1, :code, :priority, :user_id, :user_name, :subject, :message, NOW(), NOW())", array(':code'=>$code, ':priority'=>$priority, ':user_id'=>$session->data['user_id'], ':user_name'=>$session->data['user_name'], ':subject'=>$subject, ':message'=>$message));
        if($stmt->error){
            $utils->errorLog("error", "Cannot add support ticket. Error: ".$stmt->error_info['message'], $session->data['user_id'], $_SERVER['REQUEST_URI']);
            $session->data['messageType'] = "error"; 
            $session->data['messageTitle'] = "Hata!"; 
            $session->data['message'] = "Bir hata oluştu ve destek talebi kaydedilemedi."; 
            header("Location: /panel/destek/yeni");
            exit();
        }else{
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Success!"; 
            $session->data['message'] = "Destek talebiniz alındı. En yakın zamanda sizinle iletişime geçeceğiz."; 

            $priority_text = $priority == 1 ? "<span style=\"color: #dc3545\">Acil</span>" : "<span>Normal</span>";
            $email_template = file_get_contents(DIR_COMMON.'/email/templates/new-support-ticket.html');
            $email_template = str_replace('%subject%', $subject, $email_template);
            $email_template = str_replace('%ticket_id%', $code, $email_template);
            $email_template = str_replace('%status%', "<span style=\"color: #198754\">Açık</span>", $email_template);
            $email_template = str_replace('%priority%', $priority_text, $email_template);
            $email_template = str_replace('%base_url%', $App->url, $email_template);
            
            $email->send(
                array(
                    'to'                 => array(array('email'=>$session->data['user_email'], 'name'=>$session->data['user_name'])),
                    'is_html'            => true,
                    'subject'            => "[Destek ID: {$code}] {$subject}",
                    'body'               => $email_template
                )
            );


            header("Location: /panel/destek");
            exit();
        }
    }

    $data = array();
    $data['title'] = "Yeni Destek Talebi | ".$App->title;
    $data['links'] = [];
    $data['styles'] = [];
    $data['header']['scripts'] = [];
    $data['version'] = $App->version;
    $data['csrf_token'] = $csrf_generator->create();
    $data['sidebar_menu'] = $menu->get(array('position'=>'sidebar'));
    $data['sidebar_active_menu'] = $menu->getActiveMenuItemId(trim($_SERVER['REQUEST_URI']));
    $data['footer']['scripts'] = [$App->url.'/public/panel/assets/js/tinymce/tinymce.min.js', $App->url.'/public/panel/assets/js/pages/support-ticket-add.js'];
    $menu_items = $menu->get(array('position'=>'footer'));
    foreach($menu_items as $item){
        if($item['col'] == "1"){
            $data['menu']['footer']["1"][] = $item;
        }
        if($item['col'] == "2"){
            $data['menu']['footer']["2"][] = $item;
        }
        if($item['col'] == "3"){
            $data['menu']['footer']["3"][] = $item;
        }
        if($item['col'] == "4"){
            $data['menu']['footer']["4"][] = $item;
        }
    }

    $data['breadcrumbs'] = array();

    $data['breadcrumbs'][] = array(
        'text' => "Pano",
        'href' => "/panel",
        'index' => 1
    );
    $data['breadcrumbs'][] = array(
        'text' => "Destek",
        'href' => "/panel/destek",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Yeni Destek Talebi",
        'href' => "/panel/destek/yeni",
        'index' => 3
    );
?>
<?php include DIR_PUBLIC."panel/parts/header.php"; ?>
        <main class="content">
            <div class="container">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">Yeni Destek Talebi</h5>
                    </div>
                    <div class="card-body">
                        <form action="" method="post">
                            <div class="row mb-3">
                                <div class="col-md-3">
                                    <label for="subject" class="col-form-label fw-bold">Konu</label>
                                </div>
                                <div class="col-md-9">
                                    <input type="text" name="subject" id="subject" class="form-control" required>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-md-3">
                                    <label for="priority" class="col-form-label fw-bold">Öncelik</label>
                                </div>
                                <div class="col-md-9">
                                    <select name="priority" id="priority" class="form-select" required>
                                        <option value="0">Normal</option>
                                        <option value="1">Acil</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-md-3">
                                    <label for="message" class="col-form-label fw-bold">Mesaj</label>
                                </div>
                                <div class="col-md-9">
                                    <textarea name="message" id="message" rows="5" class="form-control"></textarea>
                                </div>
                            </div>
                            <div class="d-flex align-items-center justify-content-center mt-5 mb-3">
                                <a href="/panel/destek" class="btn btn-secondary me-2">Geri</a>
                                <button type="submit" name="save" id="save" class="btn btn-primary">Gönder</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </main>
<?php include DIR_PUBLIC."panel/parts/footer.php"; ?>