<?php
    $data = array();
    $data['title'] = "Menü Banner 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' => "Ürün Rozetleri",
        'href' => "/admin/product-badges/list",
        'index' => 3
    );
    $data['breadcrumbs'][] = array(
        'text' => "Yeni",
        'href' => "#",
        'index' => 4
    );

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

        if(!file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])){
            $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("INSERT INTO {$db->prefix}menu_banners (status, title, `link`, image, parent) VALUES (:status, :title, :link, :image, :parent)", array(':title'=>$title, ':status'=>$status, ':link'=>$link, ':image'=>$image, ':parent'=>$parent));
        if($stmt->error){
            $utils->errorLog("error", "Cannot update menu 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 kaydedilemedi."; 
            header("Refresh: 0;");
            exit();
        }else{
            $inserted_id = $db->getLastId();
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Banner başarıyla kaydedildi."; 
            header("Location: /admin/menu-banners/edit?id=".$inserted_id);
            exit();
        }
	}

    $parents = $menu->getMenuItems(array('status'=>1, 'position'=>'main'));
?>
<?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">Menü 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="col-form-label fw-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">
                                        </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="parent" class="col-form-label fw-bold">Konum</label>
                                        </div>
                                        <div class="col-md-4">
                                            <select name="parent" id="parent" class="form-control form-select">
                                                <option value="0">Seçiniz</option>
                                                <?php foreach($parents as $parent): ?>
                                                    <?php if($parent['parent']==0): ?>
                                                        <option value="<?php echo $parent['id']; ?>"><?php echo $parent['title']; ?></option>
                                                    <?php endif; ?>
                                                <?php endforeach; ?>
                                            </select>
                                        </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/menu-banners/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"; ?>