<?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.php';

$page = 'equipments';
//Check if allowed
if (isAllowed($page,$_SESSION['profile'],$_SESSION['permission'],'R') === 0){
    header('location: index.php');
    exit;
}
//PAGE Security
$page_manage = 'equipment_manage';
$update_allowed = isAllowed($page_manage ,$_SESSION['profile'],$_SESSION['permission'],'U');
$delete_allowed = isAllowed($page_manage ,$_SESSION['profile'],$_SESSION['permission'],'D');
$create_allowed = isAllowed($page_manage ,$_SESSION['profile'],$_SESSION['permission'],'C');

//GET PARAMETERS
$pagination_page = isset($_GET['p']) ? $_GET['p'] : 1;
$status = isset($_GET['status']) ? '&status='.$_GET['status'] : '';
$sort = isset($_GET['sort']) ? '&sort='.$_GET['sort'] : '';
$search = isset($_GET['search']) ? '&search='.$_GET['search'] : '';
$software = isset($_GET['firmware']) ? '&firmware='.$_GET['firmware'] : '';
$servicedate = isset($_GET['servicedate']) ? '&servicedate='.$_GET['servicedate'] : '';
$warrantydate = isset($_GET['warrantydate']) ? '&warrantydate='.$_GET['warrantydate'] : '';
$partnerid = isset($_GET['partnerid']) ? '&partnerid='.$_GET['partnerid'] : '';

// Determine the URL
$url = 'index.php?page=equipments'.$status.$search.$software.$servicedate.$warrantydate.$partnerid.$sort;
//GET Details from URL
$GET_VALUES = urlGETdetails($_GET) ?? '';
//CALL TO API
$api_url = '/v1/equipments/'.$GET_VALUES;
$responses = ioServer($api_url,'');
//Decode Payload
if (!empty($responses)){$responses = decode_payload($responses);}else{$responses = null;}

//Return QueryTotal from API
$api_url = '/v1/equipments/'.$GET_VALUES.'&totals=';
$query_total = ioServer($api_url,'');
//Decode Payload
if (!empty($query_total)){$query_total = decode_payload($query_total);}else{$query_total = null;}

// Handle success messages
if (isset($_GET['success_msg'])) {
    if ($_GET['success_msg'] == 1) {
        $success_msg = $message_eq_1;
    }
    if ($_GET['success_msg'] == 2) {
        $success_msg = $message_eq_2;
    }
    if ($_GET['success_msg'] == 3) {
        $success_msg = $message_eq_3;
    }
}

if (isset($_GET['download'])){
    downloadToExcel('equipment',$responses);
}

template_header('Assets', 'assets','view');
$view = '
<div class="content-title">
    <div class="title">
        <i class="fa-solid fa-database"></i>
        <div class="txt">
            <h2>'.$assets_h2.' ('.$query_total.')</h2>
            <p>'.$assets_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">';

if ($create_allowed ===1){
    $view .= '<a href="index.php?page=equipment_manage" class="btn">'.$button_create_asset.'</a>';
}
$view .= '    
    <form action="" method="get">
        <input type="hidden" name="page" value="equipments">
        <div class="filters">
            <a href="#"><i class="fa-solid fa-filter"></i>'.$general_filters.'</a>
            <div class="list">
            <select name="status">
                <option value="" disabled selected>'.$equipment_label3.'</option>
                <option value="0"'.($status==0?' selected':'').'>'.$status0_text.'</option>
                <option value="1"'.($status==1?' selected':'').'>'.$status1_text.'</option>
                <option value="2"'.($status==2?' selected':'').'>'.$status2_text.'</option>
                <option value="3"'.($status==3?' selected':'').'>'.$status3_text.'</option>
                <option value="4"'.($status==4?' selected':'').'>'.$status4_text.'</option>
                <option value="5"'.($status==5?' selected':'').'>'.$status5_text.'</option>
                </select>
                <button type="submit">'.$button_apply.'</button>
            </div>
        </div>
        <div class="sort">
            <a href="#"><i class="fa-solid fa-sort"></i>'.$general_sort.'</a>
            <div class="list">
            <select name="sort">
                <option value="" disabled selected>'.$general_sort.'</option>
                <option value="1"'.($sort==1?' selected':'').'>'.$equipment_label2.' '.$general_sort_type_1.'</option>
                <option value="2"'.($sort==2?' selected':'').'>'.$equipment_label2.' '.$general_sort_type_2.'</option>
                <option value="3"'.($sort==3?' selected':'').'>'.$equipment_label3.' '.$general_sort_type_1.'</option>
                <option value="4"'.($sort==4?' selected':'').'>'.$equipment_label3.' '.$general_sort_type_2.'</option>
                <option value="5"'.($sort==5?' selected':'').'>'.$warranty_status.' '.$general_sort_type_1.'</option>
                <option value="6"'.($sort==6?' selected':'').'>'.$warranty_status.' '.$general_sort_type_2.'</option>
                <option value="7"'.($sort==7?' selected':'').'>'.$service_status.' '.$general_sort_type_1.'</option>
                <option value="8"'.($sort==8?' selected':'').'>'.$service_status.' '.$general_sort_type_2.'</option>
                
                </select>
                <button type="submit">'.$button_apply.'</button>
            </div>
        </div>';

//SHOW DOWNLOAD TO EXCELL OPTION ONLY TO ADMIN USERS
if ($_SESSION['permission'] == 3 || $_SESSION['permission'] == 4){
    $view .='<div class="filters">
                <a href="index.php?page=equipments&download="><i class="fa-solid fa-download"></i></a>        
            </div>';
}
$view .= '<div class="search">
            <label for="search">
                <input id="search" type="text" name="search" placeholder="'.$equiment_search.'" value="" class="responsive-width-100">
                <i class="fas fa-search"></i>
            </label>
        </div>
    </form>
</div>
';
$view .= '
<div class="content-block">
    <div class="table">
        <table class="sortable">
            <thead>
                <tr>
                    <th class="responsive-hidden">'.$equipment_label1.'</th>
                    <th>'.$equipment_label2.'</th>
                    <th>'.$equipment_label3.'</th>
                    <th>'.$equipment_label4.'</th>
                    <th class="responsive-hidden"></th>
                    <th class="responsive-hidden">'.$general_location.'</th>
                    <th class="responsive-hidden">'.$general_section.'</th>
                    <th>'.$view_asset_actions.'</th>
                </tr>
            </thead>
            <tbody>
            ';

            if (empty($responses)){

                $view .= '
                <tr>
                    <td colspan="8" style="text-align:center;">'.$message_no_assets.'</td>
                </tr>';
            }

foreach ($responses as $response){
//Translate status INT to STR
$status_text = 'status'.$response->status.'_text';
//GetPartnerDetails
$partner_data = json_decode($response->accounthierarchy);

//Default location
$location = getPartnerName($partner_data->location) ?? $not_specified;
//Check if productcode is 0 and status 
if ($response->productcode == 0 && $response->status == 0) {$location = $product_location_raw;}
if ($response->productcode == 0 && $response->status == 1) {$location = $product_location_SFG;}
if ($response->productcode == 0 && $response->status == 2) {$location = $product_location_FG;}

//Check for Section
if (isset($partner_data->section)){$section = getPartnerName($partner_data->section) ?? $not_specified;} else {$section = $not_specified;}

$picture = glob("./assets/images/products/".$response->productcode.".{jpg,jpeg,png,gif}", GLOB_BRACE);

$indicators = overviewIndicators($response->warranty_date,$response->service_date,$response->sw_version,$response->hw_version);

$view .= '<tr>
            <td class="responsive-hidden">'.$indicators.'</td>
            <td>'.$response->serialnumber.'</td>
            <td><span class="status id'.$response->status.'">'.$$status_text.'</span></td>
            <td>'.$response->productcode.'-'.$response->productname.'</td>';
$view .= '  <td class="responsive-hidden">';
    if (!empty($picture)){
        $view .='    
            <img style="border-radius: 4px;height: 40px;" src="'.$picture[0].'" alt="">
            '; 
        }
$view .= ' </td>
            <td class="responsive-hidden">'.$location.'</td>
            <td class="responsive-hidden">'.$section.'</td>
            <td><a href="index.php?page=equipment&equipmentID='.$response->equipmentID.'" 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_equipment) == 0 ? 1 : ceil($query_total / $page_rows_equipment);
$view .= '<span> '.$general_page.$pagination_page.$general_page_of.$totals.'</span>';
if ($pagination_page * $page_rows_equipment < $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();
?>