<?php 
    $data = array();
    $data['title'] = "Ürün Kategorileri | ".$config->get('site_title');
    $data['links'] = [];
    $data['styles'] = ['/admin/public/assets/css/jstable.css'];
    $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/jstable.min.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' => "Ürün Kategorileri",
        'href' => "/admin/category/list",
        'index' => 2
    );

    $categories         = $product->getCategories();
?>
<?php include DIR_VIEWS."parts/header.php"; ?>
        <main class="content">
            <div class="container-fluid">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title">Ürün Kategorileri</h5>
                        <a href="/admin/category/add" class="btn btn-primary btn-sm"><i class="bi bi-plus-circle me-1"></i> Yeni</a>
                    </div>
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table table-striped table-hover table-bordered mb-0" id="category-table">
                                <thead>
                                    <tr>
                                        <th style="width:60px;">ID</th>
                                        <th style="width:100px;">Durum</th>
                                        <th class="sorting">Adı</th>
                                        <th>Üst Kategori</th>
                                        <th>Taksit Sayısı</th>
                                        <th>Taksit Sayısı (Ticari)</th>
                                        <th style="width:150px;"></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                        if(count($categories)>0):
                                            foreach($categories as $row): ?>
                                                <tr>
                                                    <td class="text-center"><?php echo $row['id']; ?></td>
                                                    <td style="width: 100px;" class="text-center"><?php echo $row['status']; ?></td>
                                                    <td><?php echo $row['title']; ?></td>
                                                    <td></td>
                                                    <td><?php echo $row['installment_count']; ?></td>
                                                    <td><?php echo $row['installment_count_business']; ?></td>
                                                    <td style="width: 60px;vertical-align: middle;" class="text-center">
                                                        <a class="btn btn-sm btn-primary me-1" href="/admin/category/edit?id=<?php echo $row['id']; ?>"><i class="bi bi-pencil-square"></i></a>
                                                        <a class="btn btn-sm btn-info me-1 text-white" href="/<?php echo $row['url']; ?>" target="_blank"><i class="bi bi-send"></i></a>
                                                        <button type="button" class="btn btn-sm btn-danger" data-delete data-href="/admin/category/delete?id=<?php echo $row['id']; ?>"><i class="bi bi-trash"></i></button>
                                                    </td>
                                                </tr>
                                                <?php if (!empty($row['sub_items'])): ?>
                                                    <?php foreach ($row['sub_items'] as $s1): ?>
                                                        <tr>
                                                            <td class="text-center"><?php echo $s1['id']; ?></td>
                                                            <td style="width: 100px;" class="text-center"><?php echo $s1['status']; ?></td>
                                                            <td><?php echo $s1['title']; ?></td>
                                                            <td><?php echo $row['title']; ?></td>
                                                            <td><?php echo $s1['installment_count']; ?></td>
                                                            <td><?php echo $s1['installment_count_business']; ?></td>
                                                            <td style="width: 60px;vertical-align: middle;" class="text-center">
                                                                <a class="btn btn-sm btn-primary me-1" href="/admin/category/edit?id=<?php echo $s1['id']; ?>"><i class="bi bi-pencil-square"></i></a>
                                                                <a class="btn btn-sm btn-info me-1 text-white" href="/<?php echo $s1['url']; ?>" target="_blank"><i class="bi bi-send"></i></a>
                                                                <button class="btn btn-sm btn-danger" data-delete data-href="/admin/category/delete?id=<?php echo $s1['id']; ?>"><i class="bi bi-trash"></i></button>
                                                            </td>
                                                        </tr>
                                                        <?php if (!empty($s1['sub_items'])): ?>
                                                            <?php foreach ($s1['sub_items'] as $s2): ?>
                                                                <tr>
                                                                    <td class="text-center"><?php echo $s2['id']; ?></td>
                                                                    <td style="width: 100px;" class="text-center"><?php echo $s2['status']; ?></td>
                                                                    <td><?php echo $s2['title']; ?></td>
                                                                    <td><?php echo $row['title']; ?> > <?php echo $s1['title']; ?></td>
                                                                    <td><?php echo $s2['installment_count']; ?></td>
                                                                    <td><?php echo $s2['installment_count_business']; ?></td>
                                                                    <td style="width: 60px;vertical-align: middle;" class="text-center">
                                                                        <a class="btn btn-sm btn-primary me-1" href="/admin/category/edit?id=<?php echo $s2['id']; ?>"><i class="bi bi-pencil-square"></i></a>
                                                                        <a class="btn btn-sm btn-info me-1 text-white" href="/<?php echo $s2['url']; ?>" target="_blank"><i class="bi bi-send"></i></a>
                                                                        <button class="btn btn-sm btn-danger" data-delete data-href="/admin/category/delete?id=<?php echo $s2['id']; ?>"><i class="bi bi-trash"></i></button>
                                                                    </td>
                                                                </tr>
                                                            <?php endforeach; ?>
                                                        <?php endif; ?>
                                                    <?php endforeach; ?>
                                                <?php endif; ?>
                                        <?php endforeach; ?>
                                    <?php else: 
                                            echo "<tr class=\"text-center\"><td colspan=\"7\">Hiçbir kayıt bulunamadı!</td></tr>";
                                        endif;
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </main>
<?php include DIR_VIEWS."parts/footer.php"; ?>
<script>
    var table = new JSTable("#category-table", {
        serverSide : false,
        perPage : 15,
        labels: {
            placeholder: "Ara...",
            perPage: "sayfa başına kayıt",
            noRows: "Hiçbir kayıt bulunamadı!",
            info: "{rows} kayıttan {start} - {end} arası görüntüleniyor",
            loading: "Yükleniyor...",
            infoFiltered: "{rows} kayıttan {start} - {end} arası görüntüleniyor ({rowsTotal} kayıt arasından filtrelendi)"
        },
        layout: {
            top: "{search}",
            bottom: "{info}{pager}"
        }
    });
    
    document.addEventListener('click', function (e) {
        let deleteButton = e.target.closest('[data-delete]');
        if (deleteButton) {
            if (deleteButton.hasAttribute("data-href")) {
                var href = deleteButton.getAttribute("data-href");
                var url = window.location.href;
                e.preventDefault();
                CustomAlert({
                    title: "Kategori Sil",
                    message: "Bu kategoriyi silmek istediğinizden emin misiniz?",
                    type: "question",
                    close: true,
                    showCancelButton: true,
                    showCloseButton: true,
                    showConfirmButton: true,
                    cancelButtonText: 'Vazgeç',
                    confirmButtonText: 'Sil',
                    static: false
                }).then((result) => {
                    if (result.value === true) {
                        window.location.href = href + "&redirect=" + url;
                    }
                });
            }
        }
    });
    
</script>