<?php
    $data = array();
    $data['title'] = "Yardım Sayfası Ekle | ".$config->get('site_title');
    $data['links'] = [];
    $data['styles'] = [];
    $data['header']['scripts'] = [];
    $data['version'] = $config->get('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'] = [];
    $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' => "/admin",
        'index' => 1
    );
    $data['breadcrumbs'][] = array(
        'text' => "Yardım Sayfaları",
        'href' => "/admin/faq/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Yeni",
        'href' => "#",
        'index' => 3
    );

    $response = array();
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $question           = $_POST['question'];
        $status             = $_POST['status'];
        $category           = $_POST['category'];
        $answer             = $_POST['answer'];

        $stmt = $db->query("INSERT INTO {$db->prefix}faq (status, question, `answer`, category) VALUES (:status, :question, :answer, :category)",array(':question'=>$question, ':status'=>$status, ':answer'=>$answer, ':category'=>$category));
        if($stmt->error){
            $utils->errorLog("error", "Cannot add page. 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 yardım sayfası kaydedilemedi."; 
            header("Refresh: 1;");
            exit();
        }else{
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Yardım sayfası başarıyla kaydedildi."; 
            header("Location: /admin/faq/edit?id=".$inserted_id);
            exit();
        }
	}

    $categories        = $faq->getCategories();
?>
<?php include DIR_VIEWS."parts/header.php"; ?>
        <main class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header">
                                <h5 class="card-title">Yarım Detayları</h5>
                            </div>
                            <div class="card-body">
                                <form action="" method="post" enctype="multipart/form-data">
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="question" class="form-label font-weight-bold">Soru</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="question" id="question" placeholder="Soru" value="" required>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="status" class="form-label font-weight-bold">Durum</label>
                                        </div>
                                        <div class="col-md-4">
                                            <select name="status" id="status" class="form-control form-select">
                                                <option value="1">Aktif</option>
                                                <option value="0">Pasif</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="category" class="form-label font-weight-bold">Kategori</label>
                                        </div>
                                        <div class="col-md-4">
                                            <select name="category" id="category" class="form-control form-select">
                                                <?php foreach ($categories as $c): ?>
                                                    <option value="<?php echo $c['id']; ?>"><?php echo $c['title']; ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-12">
                                            <label for="answer" class="form-label font-weight-bold">Cevap</label>
                                        </div>
                                        <div class="col-md-12">
                                            <textarea class="form-control editor" name="answer" id="answer" rows="10"></textarea>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <a href="/admin/faq/list" class="btn btn-secondary me-2">Geri</a>
                                            <input type="submit" name="update" class="btn btn-primary" value="Kaydet">
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
<?php include DIR_VIEWS."parts/footer.php"; ?>