<?php 
    $data = array();
    $data['title'] = "Anket Ekle | ".$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'] = [];
    $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' => "Anketler",
        'href' => "/admin/survey/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Anket Ekle",
        'href' => "#",
        'index' => 3
    );

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $title                         = $_POST['title'];
        $status                        = $_POST['status'];
        $login_required                = $_POST['login_required'];
        $guid                          = md5(uniqid(rand(), true));
        $completion_confirmation       = $_POST['completion_confirmation'];
        $description                   = $_POST['description'];
        $seo_url                       = "anket/".$guid;

        $stmt = $db->query("INSERT INTO {$db->prefix}survey (`status`, `title`, `description`, `completion_confirmation`, `guid`, `login_required`, `date_added`) VALUES (:status, :title, :description, :completion_confirmation, :guid, :login_required, NOW())", array(':title'=>$title, ':status'=>$status, ':guid'=>$guid, ':login_required'=>$login_required, ':description'=>$description, ':completion_confirmation'=>$completion_confirmation));
        if($stmt->error){
            $utils->errorLog("error", "Cannot add survey. Error: ".$stmt->error_info['message'], $session->data['user_id'], $_SERVER['REQUEST_URI']);
            $data["warning_message"] = "Bir hata oluştu ve anket kaydedilemedi.";
        }else{
            $inserted_id = $db->getLastId();
            $utils->addUrl($seo_url, "survey", $inserted_id, "", $title, strip_tags($description));
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Anket başarıyla kaydedildi."; 
            header("Location: /admin/survey/edit?id={$guid}");
            exit();
        }
	}
?>
<?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">Anket Bilgileri</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="title" class="form-label font-weight-bold">Başlık</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="title" id="title" placeholder="Başlık" value="<?php echo isset($_POST["title"]) ? htmlspecialchars($_POST["title"], ENT_QUOTES, 'UTF-8') : ''; ?>" 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>
                                                <option value="2" selected>Taslak</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="login_required" class="form-label font-weight-bold">Üye Girişi Zorunlu</label>
                                        </div>
                                        <div class="col-md-4">
                                            <select name="login_required" id="login_required" class="form-control form-select">
                                                <option value="0">Hayır</option>
                                                <option value="1">Evet</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-12">
                                            <label for="description" class="form-label font-weight-bold">Açıklama</label>
                                        </div>
                                        <div class="col-md-12">
                                            <textarea class="form-control editor" name="description" id="description" rows="10"><?php echo isset($_POST["description"]) ? htmlspecialchars($_POST["description"], ENT_QUOTES, 'UTF-8') : ''; ?></textarea>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-12">
                                            <label for="completion_confirmation" class="form-label font-weight-bold">Onay Mesajı</label>
                                        </div>
                                        <div class="col-md-12">
                                            <textarea class="form-control editor" name="completion_confirmation" id="completion_confirmation" rows="10"><?php echo isset($_POST["completion_confirmation"]) ? htmlspecialchars($_POST["completion_confirmation"], ENT_QUOTES, 'UTF-8') : ''; ?></textarea>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <a href="/admin/survey/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"; ?>