<?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'] = 'dealers';

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

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

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

//Return QueryTotal from API
$api_url = '/v2/dealers/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_dealers_1 ?? 'Created';
    }
    if ($_GET['success_msg'] == 2) {
        $success_msg = $message_dealers_2 ?? 'Updated';
    }
    if ($_GET['success_msg'] == 3) {
        $success_msg = $message_dealers_3 ?? 'Deleted' ;
    }
}

template_header('dealers', 'dealers','view');
$view = '
<div class="content-title">
    <div class="title">
        <i class="fa-solid fa-truck-fast"></i>
        <div class="txt">
            <h2>'.($dealers_h2 ?? 'dealers').' ('.$query_total.')</h2>
            <p>'.($dealers_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=dealer_manage" class="btn">'.($button_create_dealers ?? 'Create dealer').'</a>
</div>
<div class="content-block">
    <div class="table">
        <table>
            <thead>
                <tr>
                    <td>'.($dealers_id ?? '#').'</td>
                    <td>'.($dealers_status ?? 'status').'</td>
                    <td>'.($dealers_name ?? 'Name').'</td>
                    <td class="responsive-hidden"></td>
                    <td class="responsive-hidden">'.($dealers_created ?? 'Created').'</td>
                    <td>'.$general_actions.'</td>
                </tr>
            </thead>
            <tbody>';
                if (empty($dealers)){
                $view .= '<tr>
                            <td colspan="8" style="text-align:center;">'.($message_no_dealers ?? 'There are no dealers').'</td>
                         </tr>';
                }
                else {
                    foreach ($dealers as $dealer){
                    
                        //Translate status INT to STR
                        $dealer_status = 'general_status_'.$dealer['status'];
                        
                        $view .= '
                        <tr>
                            <td>'.$dealer['rowID'].'</td>
                            <td>'.(${$dealer_status} ?? $dealer_status).'</td>
                            <td>'.$dealer['name'].'</td>  
                            <td class="responsive-hidden">'.(($dealer['full_path'] !='')?'<img style="border-radius: 4px;height: 50px;" src="'.$dealer['full_path'].'" alt="">' : '').'</td>                      
                            <td class="responsive-hidden">'.getRelativeTime($dealer['created']).'</td>
                            <td><a href="index.php?page=dealer&id='.encodeUuid($dealer['rowID']).'" class="btn_link">'.$general_view.'</a></td>
                        </tr>';
                    }
                }
                
             
$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_dealers) == 0 ? 1 : ceil($query_total / $page_rows_dealers);
$view .= '<span> '.$general_page.$pagination_page.$general_page_of.$totals.'</span>';
if ($pagination_page * $page_rows_dealers < $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();
?>