<?php
    $data = array();
    $data['title'] = "Yeni Menü Item | ".$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'] = ['/admin/public/assets/js/pages/menu.js'];
    $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' => "Menü",
        'href' => "/admin/menu/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               = $_POST['link'];
        $sort_order         = $_POST['sort_order'];
        $position           = isset($_POST['position']) ? $_POST['position'] : "";
        $parent             = isset($_POST['parent']) ? $_POST['parent'] : 0;
        $icon               = $_POST['icon'];
        $col                = $_POST['col'];
        $class              = $_POST['class'];

        $stmt = $db->query("INSERT INTO {$db->prefix}menu (status, title, `link`, `sort_order`, position, parent, `icon`, col, class) VALUES (:status, :title, :link, :sort_order, :position, :parent, :icon, :col, :class)", array(':title'=>$title, ':status'=>$status, ':link'=>$link, ':sort_order'=>$sort_order, ':position'=>$position, ':parent'=>$parent, ':icon'=>$icon, ':col'=>$col, ':class'=>$class));
        if($stmt->error){
            $utils->errorLog("error", "Cannot add menu item. 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 menü kaydedilemedi."; 
            header("Refresh: 0;");
            exit();
        }else{
            $inserted_id = $db->getLastId();
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Menü başarıyla kaydedildi.";
            header("Location: /admin/menu/edit?id=".$inserted_id);
            exit();
        }
	}

    $menu_items         = $menu->getMenuItems();
?>
<?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ü 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">Başlık</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="title" id="title" placeholder="Başlık" required>
                                        </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" required>
                                        </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">Aktif</option>
                                                <option value="0">Pasif</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="position" class="form-label font-weight-bold">Konum</label>
                                        </div>
                                        <div class="col-md-4">
                                            <select name="position" id="position" class="form-control form-select" required>
                                                <option value="">Seçiniz</option>
                                                <option value="main">Ana Menü</option>
                                                <option value="footer">Site Alt Kısım</option>
                                                <option value="top-bar">Site Üst Kısım</option>
                                                <option value="sidebar">Kullanıcı Paneli</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="parent" class="form-label font-weight-bold">Ebeveyn</label>
                                        </div>
                                        <div class="col-md-4">
                                            <select name="parent" id="parent" class="form-control form-select">
                                                <option value="" data-position="">Seçiniz</option>
                                                <?php if(count($menu_items)>0): ?>
                                                    <?php foreach($menu_items as $row): ?>
                                                        <option value="<?php echo $row["id"]; ?>" data-position="<?php echo $row["position"]; ?>"><?php echo $row["title"]; ?></option>
                                                    <?php endforeach; ?>
                                                <?php endif; ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="sort_order" class="form-label font-weight-bold">Sıralama</label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="text" class="form-control" name="sort_order" id="sort_order" placeholder="Sıralama" value="">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="icon" class="form-label font-weight-bold">Simge</label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="text" class="form-control" name="icon" id="icon" placeholder="Simge" value="">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="col" class="form-label font-weight-bold">Sütun</label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="text" class="form-control" name="col" id="col" placeholder="Sütun" value="">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="class" class="form-label font-weight-bold">CSS Sınıfı</label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="text" class="form-control" name="class" id="class" placeholder="CSS Sınıfı" value="">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <a href="/admin/menu/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"; ?>