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

    $data = array();
    $data['title'] = "Banner 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' => "Banner",
        'href' => "/admin/banner/list",
        'index' => 3
    );
    $data['breadcrumbs'][] = array(
        'text' => "Güncelle",
        'href' => "#",
        'index' => 4
    );

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

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

        $stmt = $db->query("UPDATE {$db->prefix}banners SET status=:status, title=:title, description=:description, `link`=:link, `order`=:order, image=:image, pos=:pos WHERE id=:id", array(':id'=>$_GET['id'], ':title'=>$title, ':description'=>$description, ':status'=>$status, ':link'=>$link, ':order'=>$order, ':image'=>$image, ':pos'=>$pos));
        if($stmt->error){
            $utils->errorLog("error", "Cannot update banner. 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 banner güncellenemedi."; 
            header("Refresh: 0;");
            exit();
        }else{
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Banner 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">Banner 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">Meta Yazısı / 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 htmlspecialchars($banner_details["title"], ENT_QUOTES); ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="description" class="form-label font-weight-bold">Açıklama</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="description" id="description" placeholder="Açıklama" value="<?php echo htmlspecialchars($banner_details["description"], ENT_QUOTES); ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="link" class="form-label font-weight-bold">Link</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="link" id="link" placeholder="Link" value="<?php echo $banner_details["link"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="image" class="form-label font-weight-bold">Görsel <a href="/public/assets/images/banners/<?php echo $banner_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="order" class="form-label font-weight-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 $banner_details["order"]; ?>" required>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="pos" class="form-label font-weight-bold">Konum</label>
                                        </div>
                                        <div class="col-md-4">
                                            <select name="pos" id="pos" class="form-control form-select">
                                                <option value="1" <?php echo $banner_details["pos"] == "1" ? 'selected' : ''; ?>>1</option>
                                                <option value="2" <?php echo $banner_details["pos"] == "2" ? 'selected' : ''; ?>>2</option>
                                                <option value="3" <?php echo $banner_details["pos"] == "3" ? 'selected' : ''; ?>>3</option>
                                            </select>
                                        </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" <?php echo $banner_details["status"] == "1" ? 'selected' : ''; ?>>Aktif</option>
                                                <option value="0" <?php echo $banner_details["status"] == "0" ? 'selected' : ''; ?>>Pasif</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <a href="/admin/banner/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"; ?>