<?php require PATH . '/view/common/head.php'; ?>
<?php require PATH . '/view/common/header.php'; ?>
<?php require_once PATH.'/config/array.config.php'; ?>
<div class="container pb-5">
    <div class="row gx-5">
        <div class="col-lg-9">
            <?php if(isset($Listings)) { ?>
            <div class="row g-3">
                <?php 
                foreach ($Listings as $Listing) { 
                // Link check
                $ApiCheck = $this->db->from('posts')->where('api_type',$Listing['api_type'])->where('api_id',$Listing['id'])->first();
                if(empty($ApiCheck['id'])) {
                ?>
                    <form method="post" action="<?php echo APP.'/admin/ajax/addgame';?>" class="ajax-form col-lg-6" data-url="<?php echo $Listing['id'];?>">
                        <input type="hidden" name="data" value="<?php echo htmlspecialchars(json_encode($Listing, JSON_UNESCAPED_UNICODE));?>">
                        <?php 
                        $CategorySearch = array_search($Listing['category_name'], array_column($Data['category'], 'name'));
                        if(isset($CategorySearch)) {
                            $CategoryId     = $Data['category'][$CategorySearch]['category'];
                        } 
                        ?>
                        <input type="hidden" name="category" value="<?php echo isset($CategoryId) ? $CategoryId : null;?>">
                        <div class="card border-0 shadow-none h-100">
                            <div class="card-body">
                                <div class="row justify-content-center gx-lg-4">
                                    <div class="col-4">
                                        <img src="<?php echo $Listing['image'];?>" class="rounded-0 img-fluid">
                                    </div>
                                    <div class="col-8">
                                        <div class="fs-xs text-muted text-capitalize">
                                            <?php echo $Listing['category_name'];?>
                                        </div>
                                        <div class="fs-sm fw-bold h-1x">
                                            <?php echo $Listing['title'];?>
                                        </div>
                                        <div class="mt-2">
                                            <button type="submit" class="btn btn-soft-success btn-sm fs-xs fw-bold">Add game</button>
                                            <a href="<?php echo $Listing['url'];?>" target="_blank" class="btn btn-ghost btn-sm fs-xs fw-bold">View</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                <?php } ?>
                <?php } ?>
            </div>
            <?php if(isset($Paginations)) { ?>
            <ul class="pagination pagination-spaced mt-4 justify-content-center flex-wrap">
                <?php foreach ($Paginations as $Pagination) { ?>
                <li class="page-item <?php if(isset($_GET['page']) AND $_GET['page'] == $Pagination['url']) echo 'active';?>">
                    <a class="page-link  border-0" href="<?php echo APP.'/admin/tool/'.$Route->params->self.'?filter='.htmlspecialchars($_GET['filter']).'&page='.$Pagination['url'];?>">
                        <?php echo $Pagination['name'];?>
                    </a>
                </li>
                <?php } ?>
            </ul>
            <?php } ?>
            <?php } ?>
        </div>
        <div class="col-lg-3">
            <form method="post">
                <input type="hidden" name="_ACTION" value="filter">
                <input type="hidden" name="_TOKEN" value="<?php echo $Token?>">
                <div class="mb-3">
                    <label class="form-label"><?php echo $this->translate('Sortable');?></label>
                    <select name="sort" class="form-select">
                        <?php foreach ($Data['sort'] as $Key) { ?>
                        <option value="<?php echo $Key['url'];?>" <?php if(isset($Filter['category']) && $Key['url']==$Filter['category']) echo ' selected=""' ;?>>
                            <?php echo $Key['name'];?>
                        </option>
                        <?php } ?>
                    </select>
                </div>
                <div class="mb-3">
                    <label class="form-label"><?php echo $this->translate('Category');?></label>
                    <select name="category" class="form-select">
                        <?php foreach ($Data['category'] as $Key) { ?>
                        <option value="<?php echo $Key['url'];?>" <?php if(isset($Filter['category']) && $Key['url']==$Filter['category']) echo ' selected=""' ;?>>
                            <?php echo $Key['name'];?>
                        </option>
                        <?php } ?>
                    </select>
                </div> 
                <div class="d-grid">
                    <button type="submit" class="btn btn-primary"><?php echo $this->translate('Apply');?></button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php if(isset($Listings)) { ?>
<div class="position-fixed p-3 end-0 bottom-0 start-0 text-center">
    <button class="btn rounded-pill btn-primary fs-xs px-lg-4 py-3 d-inline-flex align-items-center btn-import">
        <svg width="14" height="14" stroke="currentColor" stroke-width="2" fill="none">
            <use xlink:href="https://localhost/game/public/assets/sprite/sprite.svg#upload" />
        </svg>
        <span class="ms-2">All import game</span>
    </button>
</div>
<?php } ?>
<?php require PATH . '/view/common/javascript.php'; ?>
<?php require PATH . '/view/common/footer.php'; ?>