<?php
    $data = array();
    $data['title'] = "Blog Yazısı 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' => "Blog Yazıları",
        'href' => "/admin/blog/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Yeni",
        'href' => "#",
        'index' => 3
    );

    $response = array();
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $title              = $_POST['title'];
        $author             = $_POST['author'];
        $status             = $_POST['status'];
        $short_description  = $_POST['short_description'];
        $description        = $_POST['description'];
        $category           = $_POST['category'];

        $title_seo          = $utils->generateUrl($title, "blog/");
        $meta_title	        = $title;
        $meta_description   = $short_description;
		
		if(!isset($_FILES['image']) || !file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])){
            $image               = "default.jpg";
        } else {
            $image_name          = $utils->generateUrl($title);
            $image_parts         = pathinfo($_FILES['image']["name"]);
            $image               = $image_name."-".uniqid().".".$image_parts['extension'];
            $utils->resizeCropImage(500, 400, $_FILES['image']['tmp_name'], "../public/assets/images/blog/list/".$image);
            $utils->resizeCropImage(200, 120, $_FILES['image']['tmp_name'], "../public/assets/images/blog/thumb/".$image);
			move_uploaded_file($_FILES['image']['tmp_name'], "../public/assets/images/blog/".$image);
        }

        $stmt = $db->query("INSERT INTO {$db->prefix}blog_posts (`status`, `author`, `description`, `image`, `title`, `short_description`, `category`, `date_added`) VALUES (:status, :author, :description, :image, :title, :short_description, :category, NOW())",array(':title'=>$title, ':status'=>$status, ':author'=>$author, ':image'=>$image, ':category'=>$category, ':description'=>$description, ':short_description'=>$short_description));
        if($stmt->error){
            $utils->errorLog("error", "Cannot add blog post. 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 blog yazısı kaydedilemedi."; 
            header("Refresh: 1;");
            exit();
        }else{
            $inserted_id = $db->getLastId();
            $utils->addUrl($title_seo, "blog", $inserted_id, "", $meta_title, $meta_description);

            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Blog yazısı başarıyla kaydedildi."; 
            header("Location: /admin/blog/edit?id=".$inserted_id);
            exit();
        }
	}

    $categories          = $blog->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">Blog 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="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="" 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">
                                                <option value="0">Seçiniz</option>
                                                <?php foreach ($categories as $t): ?>
                                                    <option value="<?php echo $t['id']; ?>"><?php echo $t['title']; ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="author" class="form-label font-weight-bold">Yazar</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="author" id="author" placeholder="Yazar" value="">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="image" class="form-label font-weight-bold">Görsel</label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="file" class="form-control" name="image" id="image">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-12">
                                            <label for="short_description" class="form-label font-weight-bold">Kısa Açıklama</label>
                                        </div>
                                        <div class="col-md-12">
                                            <textarea class="form-control editor" name="short_description" id="short_description" rows="10"></textarea>
                                        </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"></textarea>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <a href="/admin/blog/list" class="btn btn-secondary me-2">Geri</a>
                                            <input type="submit" name="update" class="btn btn-primary" value="Güncelle">
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
<?php include DIR_VIEWS."parts/footer.php"; ?>