<?php
    if(!isset($_GET["id"]) || empty($_GET["id"])){
        header("Location: /admin/slider/list");
        exit();
    }
    $slider_details = $slider->getSlider($_GET["id"]);
    if(empty($slider_details)){
        header("Location: /admin/slider/list");
        exit();
    }

    $data = array();
    $data['title'] = "Slider Güncelle | ".$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' => "Güncelle",
        '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                = $slider_details["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, 50);
        }
    
        if(!file_exists($_FILES['image_mobile']['tmp_name']) || !is_uploaded_file($_FILES['image_mobile']['tmp_name'])){
            $image_mobile         = $slider_details["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, 50);*/
        }
        
        $stmt = $db->query("UPDATE {$db->prefix}slider SET status=:status, alt=:alt_text, `link`=:link, `sort_order`=:order, image=:image, image_mobile=:image_mobile, thumbnail=:thumbnail WHERE id=:id", array(':id'=>$_GET['id'], ':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 güncellenemedi."; 
            header("Refresh: 0;");
            exit();
        }else{
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Slider başarıyla güncellendi."; 
            header("Refresh: 0;");
            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" value="<?php echo $slider_details["alt"]; ?>">
                                        </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" value="<?php echo $slider_details["link"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="image" class="col-form-label fw-bold">Görsel <a href="/public/assets/images/slider/<?php echo $slider_details["image"]; ?>" class="glightbox text-reset"><i class="bi bi-image"></i></a></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-2">
                                            <label for="image_mobile" class="col-form-label fw-bold">Mobil Görsel <a href="/public/assets/images/slider/mobile/<?php echo $slider_details["image_mobile"]; ?>" class="glightbox text-reset"><i class="bi bi-image"></i></a></label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="file" class="form-control" name="image_mobile" id="image_mobile">
                                        </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" value="<?php echo $slider_details["thumbnail"]; ?>" >
                                        </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="<?php echo $slider_details["sort_order"]; ?>" 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" <?php echo $slider_details["status"] == "1" ? 'selected' : ''; ?>>Aktif</option>
                                                <option value="0" <?php echo $slider_details["status"] == "0" ? 'selected' : ''; ?>>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"; ?>