<?php 
    $data = array();
    $data['title'] = "Menü | ".$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' => "Menü",
        'href' => "/admin/menu/list",
        'index' => 3
    );

    $search_keywords    = isset($_GET['q']) ? htmlspecialchars($_GET['q'], ENT_QUOTES, 'UTF-8') : "";
    $status             = isset($_GET["status"]) ? $_GET["status"] : "";
    $position           = isset($_GET["position"]) ? $_GET["position"] : "";
    $sort               = isset($_GET["sort"]) ? $_GET["sort"] : "id";
    $sort_order         = isset($_GET["order"]) ? $_GET["order"] : "DESC";

    $page               = !isset($_GET["page"]) || $_GET["page"] < 1 ? 1 : (int)$_GET["page"];
    $limit	            = isset($_GET["limit"]) ? (int)$_GET["limit"] : 25;
    $offset             = ($page-1)*$limit;
    $filter_data = array(
        'search_keywords'    => $search_keywords,
        'status'             => $status,
        'position'           => $position,
        'sort'               => $sort,
        'order'              => $sort_order,
        'offset'             => $offset,
        'limit'              => $limit
    );
    
    $menu_items         = $menu->getMenuItems($filter_data);
    $total_menu_items 	= $menu->getTotalMenuItems($filter_data);
    
    $pagination->total  = $total_menu_items;
    $pagination->page   = $page;
    $pagination->limit  = $limit;
    $pagination->url    = '#';
?>
<?php include DIR_VIEWS."parts/header.php"; ?>
        <main class="content">
            <div class="container-fluid">
                <div class="card">
                    <div class="card-header">
                        <form action="" method="GET" id="filter-form">
                            <div class="row">
                                <div class="col-md-4 text-center text-md-start">
                                    <label for="q" class="col-form-label">Anahtar Kelime</label>
                                    <input type="text" name="q" id="q" class="form-control" value="<?php echo $search_keywords; ?>">
                                </div>
                                <div class="col-md-2 text-center text-md-start">
                                    <label for="status" class="col-form-label">Durum</label>
                                    <select name="status" id="status" class="form-control form-select">
                                        <option value="" <?php echo $status == '' ? 'selected' : ''; ?>>Seçiniz</option>
                                        <option value="1" <?php echo $status == "1" ? 'selected' : ''; ?>>Aktif</option>
                                        <option value="0" <?php echo $status == "0" ? 'selected' : ''; ?>>Pasif</option>
                                    </select>
                                </div>
                                <div class="col-md-2 text-center text-md-start">
                                    <label for="position" class="col-form-label">Konum</label>
                                    <select name="position" id="position" class="form-control form-select">
                                        <option value="" <?php echo $position == '' ? 'selected' : ''; ?>>Seçiniz</option>
                                        <option value="main" <?php echo $position == "main" ? 'selected' : ''; ?>>Ana Menu</option>
                                        <option value="footer" <?php echo $position == "footer" ? 'selected' : ''; ?>>Site Alt Kısım</option>
                                        <option value="top-bar" <?php echo $position == "top-bar" ? 'selected' : ''; ?>>Site Üst Kısım</option>
                                        <option value="sidebar" <?php echo $position == "sidebar" ? 'selected' : ''; ?>>Kullanıcı Paneli</option>
                                    </select>
                                </div>
                                <div class="col-md-2 text-center text-md-start">
                                    <label for="" class="col-form-label">&nbsp;</label>
                                    <button type="submit" class="btn btn-primary w-100">Filtrele</button>
                                </div>
                                <div class="col-md-2 text-center text-md-start">
                                    <label for="" class="col-form-label d-block">&nbsp;</label>
                                    <a href="/admin/menu/add" class="btn btn-info text-white">Yeni</a>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-striped table-hover table-bordered table-sortable mb-0">
                            <thead>
                                <tr>
                                    <th style="width:50px;" class="sorting" data-column="id" data-order="">ID</th>
                                    <th class="sorting" data-column="status" data-order="">Durum</th>
                                    <th class="sorting" data-column="title" data-order="">Başlık</th>
                                    <th class="sorting" data-column="parent" data-order="">Ebeveyn</th>
                                    <th class="sorting" data-column="position" data-order="">Konum</th>
                                    <th class="sorting" data-column="sort_order" data-order="">Sıra</th>
                                    <th style="width:110px;"></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                    if($menu_items>0) {
                                        foreach($menu_items as $row) {
                                            echo "<tr>
                                                        <td class=\"text-center\">".$row['id']."</td>
                                                        <td>".$row['status']."</td>
                                                        <td>".$row['title']."</td>
                                                        <td>".$row['parent_text']."</td>
                                                        <td>".$row['position_text']."</td>
                                                        <td>".$row['sort_order']."</td>
                                                        <td style=\"width: 110px;vertical-align: middle;\" class=\"text-center\">
                                                            <a class=\"btn btn-sm btn-primary me-1\" href=\"/admin/menu/edit?id=".$row['id']."\"><i class=\"bi bi-pencil-square\"></i></a>
                                                            <button class=\"btn btn-sm btn-danger\" data-delete data-href=\"/admin/menu/delete?id=".$row['id']."\"><i class=\"bi bi-trash\"></i></button>
                                                        </td>
                                                    </tr>";
                                        }
                                    }else{
                                        echo "<tr class=\"text-center\"><td colspan=\"11\">Hiçbir kayıt bulunamadı!</td></tr>";
                                    }
                                ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="d-flex table-responsive align-items-center justify-content-center mt-3">
                        <div class="table-responsive">
                            <nav aria-label="Page navigation">
                                <ul class="pagination" id="pagination">
                                    <?php echo $pagination->render(); ?>
                                </ul>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>
        </main>
<?php include DIR_VIEWS."parts/footer.php"; ?>