<?php 
    $data = array();
    $data['title'] = "Ürün Koleksiyonu 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' => "Ürün Koleksiyonları",
        'href' => "/admin/collections/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Yeni",
        'href' => "#",
        'index' => 3
    );

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $title              = $_POST['title'];
        $description        = $_POST['description'];
        $status             = $_POST['status'];
        $products_          = $_POST['products'];
        
        $seo_url	        = $utils->generateUrl($title);
        $meta_title	        = $title;
        $meta_description   = $description;

        
        if(!file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])){
            $image                = "";
        } else {
            $image_parts          = pathinfo($_FILES["image"]["name"]);
            $image                = $seo_url."-".time().".".$image_parts['extension'];
            move_uploaded_file($_FILES['image']['tmp_name'], "../public/assets/images/banners/".$image);
        }

        $stmt = $db->query("INSERT INTO {$db->prefix}collections (`status`, `title`, `description`, `image`) VALUES (:status, :title, :description, :image)",array(':title'=>$title, ':status'=>$status, ':description'=>$description, ':image'=>$image));
        if($stmt->error){
            $utils->errorLog("error", "Cannot add collection. 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 koleksiyon kaydedilemedi."; 
            header("Refresh: 0;");
            exit();
        }else{
            $inserted_id = $db->getLastId();
            $utils->addUrl($seo_url, "collection", $inserted_id, "", $meta_title, $meta_description);

            if(!empty($products_)){
                foreach($products_ as $c) {
                    $db->query("INSERT INTO {$db->prefix}collection_products (product_id, collection_id) VALUES (:product_id, :collection_id)", array(':collection_id'=>$inserted_id, ':product_id'=>$c));
                }
            }

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

    $products               = $product->getProducts();
?>
<?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">Yeni Ürün Koleksiyonu</h5>
                            </div>
                            <div class="card-body">
                                <form action="" method="post" enctype="multipart/form-data">
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="title" class="form-label font-weight-bold">Başlık</label>
                                        </div>
                                        <div class="col-md-6">
                                            <input type="text" class="form-control" name="title" id="title" placeholder="adı" value="">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="description" class="form-label font-weight-bold">Açıklama</label>
                                        </div>
                                        <div class="col-md-9">
                                            <input type="text" class="form-control" name="description" id="description" placeholder="" value="">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="products" class="col-form-label fw-bold">Ürünler</label>
                                        </div>
                                        <div class="col-md-9">
                                            <select name="products[]" id="products" class="form-control form-select js-choice" multiple>
                                                <option value="">Seçiniz</option>
                                                <?php foreach ($products as $c): ?>
                                                    <option value="<?php echo $c['id']; ?>" class="fw-bold"><?php echo htmlspecialchars($c['title'], ENT_QUOTES); ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="image" class="col-form-label fw-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-3">
                                            <label for="status" class="form-label font-weight-bold">Durum</label>
                                        </div>
                                        <div class="col-md-3">
                                            <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-12 text-end">
                                            <a href="/admin/collections/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"; ?>