<?php
defined(page_security_key) or exit;

if (debug && debug_id == $_SESSION['id']){
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);  
}

include_once './assets/functions.php';
include_once './settings/settings_redirector.php';

//SET ORIGIN FOR NAVIGATION
$prev_page = $_SESSION['prev_origin'] ?? '';
$page = $_SESSION['origin'] = 'categories';

//Check if allowed
if (isAllowed($page,$_SESSION['profile'],$_SESSION['permission'],'R') === 0){
    header('location: index.php');
    exit;
}

//GET PARAMETERS
$pagination_page = isset($_GET['p']) ? $_GET['p'] : 1;
$search = isset($_GET['search']) ? '&search='.$_GET['search'] : '';

// Determine the URL
$url = 'index.php?page=categories'.$search;
//GET Details from URL
$GET_VALUES = urlGETdetails($_GET) ?? '';
//CALL TO API
$api_url = '/v2/categories/'.$GET_VALUES;
$categories = ioServer($api_url,'');
//Decode Payload
if (!empty($categories)){$categories = json_decode($categories,true);}else{$categories = null;}

//Return QueryTotal from API
$api_url = '/v2/categories/totals=';
$query_total = ioServer($api_url,'');

//Decode Payload
if (!empty($query_total)){$query_total = json_decode($query_total,true);}else{$query_total = null;}

// Handle success messages
if (isset($_GET['success_msg'])) {
    if ($_GET['success_msg'] == 1) {
        $success_msg = $message_categories_1 ?? 'Created';
    }
    if ($_GET['success_msg'] == 2) {
        $success_msg = $message_categories_2 ?? 'Updated';
    }
    if ($_GET['success_msg'] == 3) {
        $success_msg = $message_categories_3 ?? 'Deleted' ;
    }
}

function admin_populate_categories($categories, $parent_id = 0, $n = 0) {
    $html = '';
    foreach ($categories as $category) {
        if ($parent_id == $category['parent_id']) {
            $html .= '
            <tr>
                <td><span style="padding-right:8px;color:#bbbec0;border-left:1px solid #bbbec0;padding-bottom:2px;">-' . str_repeat('----', $n) . '</span>' . $category['name'] . '</td>
                <td>'.(($category['filter'] == 1) ? 'V' : '').'</td>
                <td><a href="index.php?page=category&rowID=' . $category['rowID'] . '" class="btn_link">Edit</a> (ID =' . $category['rowID'] . ') </td>
            </tr>           
            ';
            $html .= admin_populate_categories($categories, $category['rowID'], $n+1);
        }
    }
    return $html;
}

template_header('Categories', 'categories','view');
$view = '
<div class="content-title">
    <div class="title">
        <i class="fa-solid fa-box-open"></i>
        <div class="txt">
            <h2>'.($categories_h2 ?? 'Categories').' ('.$query_total.')</h2>
            <p>'.($categories_p ?? '').'</p>
        </div>
    </div>
</div>';

if (isset($success_msg)){
$view .= ' <div class="msg success">
                <i class="fas fa-check-circle"></i>
                <p>'.$success_msg.'</p>
                <i class="fas fa-times"></i>
            </div>';
}

$view .= '
<div class="content-header responsive-flex-column pad-top-5">
    <a href="index.php?page=category" class="btn">Create Category</a>
</div>
<div class="content-block">
    <div class="table">
        <table>
            <thead>
                <tr>
                    <td>'.($categories_name ?? 'Name').'</td>
                    <td>'.($categories_filter ?? 'Filter').'</td>
                    <td>'.$general_actions.'</td>
                </tr>
            </thead>
            <tbody>';
                if (empty($categories)){
                $view .= '<tr>
                            <td colspan="8" style="text-align:center;">'.($message_no_categories ?? 'There are no categories').'</td>
                         </tr>';
                }
                else {
                    $view .= admin_populate_categories($categories);
                }
             
$view .= '                
            </tbody>
        </table>
    </div>
</div>
';

$view.='<div class="pagination">';
if ($pagination_page > 1) {
    $page = $pagination_page-1;
    $view .= '<a href="'.$url.'&p=1">'.$general_first.'</a>';
    $view .= '<a href="'.$url.'&p='.$page.'">'.$general_prev.'</a>';
}
$totals = ceil($query_total / $page_rows_categories) == 0 ? 1 : ceil($query_total / $page_rows_categories);
$view .= '<span> '.$general_page.$pagination_page.$general_page_of.$totals.'</span>';
if ($pagination_page * $page_rows_categories < $query_total){
    $page = $pagination_page+1;
    $view .= '<a href="'.$url.'&p='.$page.'">'.$general_next.'</a>';
    $view .= '<a href="'.$url.'&p='.$totals.'">'.$general_last.'</a>';

}
$view .= '</div>';
//OUTPUT
echo $view;

template_footer();
?>