<?php
    $data = array();
    $data['title'] = "Slider 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' => "Tasarım",
        'href' => "/admin/visual-settings",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Slider",
        'href' => "/admin/slider/list",
        'index' => 3
    );
    $data['breadcrumbs'][] = array(
        'text' => "Yeni",
        'href' => "#",
        'index' => 4
    );

    $response = array();
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $alt_text           = $_POST['alt_text'];
        $thumbnail          = $_POST['thumbnail'];
        $status             = $_POST['status'];
        $link               = isset($_POST['link']) && !empty($_POST['link']) ? $_POST['link'] : "#";
        $order              = $_POST['order'];

        if(!file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])){
            $image                = "";
        } else {
            /*$image_parts          = pathinfo($_FILES["image"]["name"]);
            $image                = "slider-".time().".".$image_parts['extension'];
            move_uploaded_file($_FILES['image']['tmp_name'], "../public/assets/images/slider/".$image);*/
            
            $image       = "slider-".time().".webp";
            $imgsize     = getimagesize($_FILES['image']['tmp_name']);
            $img_width   = $imgsize[0];
            $img_height  = $imgsize[1];

            $utils->createWebpImage($img_width, $img_height, $_FILES['image']['tmp_name'], "../public/assets/images/slider/".$image);
        }
    
        if(!file_exists($_FILES['image_mobile']['tmp_name']) || !is_uploaded_file($_FILES['image_mobile']['tmp_name'])){
            $image_mobile         = "";
        } else {
            /*$image_mobile_parts   = pathinfo($_FILES["image_mobile"]["name"]);
            $image_mobile         = "slider-".time().".".$image_mobile_parts['extension'];
            move_uploaded_file($_FILES['image_mobile']['tmp_name'], "../public/assets/images/slider/mobile/".$image_mobile);*/

            $image_mobile = "slider-".time().".webp";
            $imgsize     = getimagesize($_FILES['image_mobile']['tmp_name']);
            $img_width   = $imgsize[0];
            $img_height  = $imgsize[1];

            $utils->createWebpImage($img_width, $img_height, $_FILES['image_mobile']['tmp_name'], "../public/assets/images/slider/mobile/".$image_mobile);
        }

        $stmt = $db->query("INSERT INTO {$db->prefix}slider (status, alt, `link`, `sort_order`, image, image_mobile, thumbnail) VALUES (:status, :alt_text, :link, :order, :image, :image_mobile, :thumbnail)", array(':alt_text'=>$alt_text, ':status'=>$status, ':link'=>$link, ':order'=>$order, ':image'=>$image, ':image_mobile'=>$image_mobile, ':thumbnail'=>$thumbnail));
        if($stmt->error){
            $utils->errorLog("error", "Cannot update slider. 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 slider kaydedilemedi."; 
            header("Refresh: 0;");
            exit();
        }else{
            $inserted_id = $db->getLastId();
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Slider başarıyla kaydedildi."; 
            header("Location: /admin/slider/edit?id=".$inserted_id);
            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">Slider 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="alt_text" class="col-form-label fw-bold">Meta Yazısı</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="alt_text" id="alt_text" placeholder="Başlık">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="link" class="col-form-label fw-bold">Link</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="link" id="link" placeholder="Link">
                                        </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" required>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="image_mobile" class="col-form-label fw-bold">Mobil Görsel</label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="file" class="form-control" name="image_mobile" id="image_mobile" required>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="thumbnail" class="col-form-label fw-bold">Slider Başlık</label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="text" class="form-control" name="thumbnail" id="thumbnail" required>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="order" class="col-form-label fw-bold">Sıralama</label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="text" class="form-control" name="order" id="order" placeholder="Sıralama" value="1" required>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="status" class="col-form-label fw-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-12 text-end">
                                            <a href="/admin/slider/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"; ?>