<?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
$_SESSION['prev_origin_equipment'] = $_SERVER['REQUEST_URI'];
$page = 'equipment';
//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 ,$_SESSION['profile'],$_SESSION['permission'],'U');
$update_allowed_edit = 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');
$view_product =  isAllowed('product' ,$_SESSION['profile'],$_SESSION['permission'],'R');
$view_history =  isAllowed('history' ,$_SESSION['profile'],$_SESSION['permission'],'C');
$view_contracts =  isAllowed('contracts' ,$_SESSION['profile'],$_SESSION['permission'],'R');

//GET Details from URL
$GET_VALUES = urlGETdetails($_GET) ?? '';

//CALL TO API FOR General information
$api_url = '/v1/equipments/'.$GET_VALUES;
$responses = ioServer($api_url,'');
//Decode Payload
if (!empty($responses)){$responses = decode_payload($responses);}else{$responses = null;}
$responses = $responses[0];

//CALL TO API FOR RELATED
$api_url = '/v2/media/rowID='.$responses->product_media;
$media_responses = ioServer($api_url,'');

//Decode Payload
if (!empty($media_responses)){$media_responses = json_decode($media_responses,true);}else{$media_responses = null;}
$media_responses = $media_responses[0];

//CALL TO API FOR History
$api_url = '/v1/equipments/equipmentID='.$responses->equipmentID.'&type=ServiceReport&history=1';
$history = ioServer($api_url,'');

//Decode Payload
if (!empty($history)){$history = decode_payload($history);}else{$history = null;}

//CALL TO API FOR EQUIPMENT DATA
$api_url = '/v1/equipment_data/equipmentid='.$responses->equipmentID;
$equipment_data = ioServer($api_url,'');

//Decode Payload
if (!empty($equipment_data )){$equipment_data  = decode_payload($equipment_data );}else{$equipment_data  = null;}

//GET PRODUCTS_SOFTWARE
$api_url = '/v1/products_software/productrowid='.$responses->productrowid.'&status=1';
$products_software = ioServer($api_url,'');
//Decode Payload
if (!empty($products_software)){$products_software = decode_payload($products_software);}else{$products_software = null;}

//------------------------------
//Variables
//------------------------------
$status_text = 'status'.$responses->status.'_text';
$warrantydate = warrantyStatus($responses->warranty_date);
$service_date_due = serviceStatus($responses->service_date);
$firmware_status = availableFirmware($responses->sw_version, $responses->sw_version_latest);

//Calculate Healthindex based on last test
$total_score = assetHealthIndex($_SESSION['profile'],$_SESSION['permission'],$equipment_data,0);

//GetPartnerDetails
$partner_data = json_decode($responses->accounthierarchy);
$salesid = getPartnerName($partner_data->salesid) ?? $not_specified;
$soldto = getPartnerName($partner_data->soldto) ?? '-';
$shipto = getPartnerName($partner_data->shipto) ?? '-';
$location = getPartnerName($partner_data->location) ?? '-';
if (isset($partner_data->section)){$section = getPartnerName($partner_data->section) ?? '-';} else {$section = '-';}

//Check if productcode is 0 and status 
if ($responses->productcode == 0 && $responses->status == 0) {$location = $product_location_raw;}
if ($responses->productcode == 0 && $responses->status == 1) {$location = $product_location_SFG;}
if ($responses->productcode == 0 && $responses->status == 2) {$location = $product_location_FG;}

if ($update_allowed === 1){
    if (isset($_POST['description'])) {
        //GET ALL POST DATA
        $data = json_encode($_POST, JSON_UNESCAPED_UNICODE);
        
        //Secure data
        $payload = generate_payload($data);
        
        //API call
        $responses = ioServer('/v1/history', $payload);
        if ($responses === 'NOK'){

        } else {
        header('Location: index.php?page=equipment&equipmentID='.$_POST['equipmentid'].'&success_msg=2');
        exit;
        }
    }
}

// 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;
    }
}

template_header('Asset', 'asset', 'view');
$view = '
    <div class="content-title responsive-flex-wrap responsive-pad-bot-3">
        <h2 class="responsive-width-100">'.$view_asset_h2.' - '.$responses->equipmentID.'</h2>
        <a href="index.php?page='.$_SESSION['origin'].'&p='.$_SESSION['p'].$_SESSION['status'].$_SESSION['sort'].$_SESSION['search'].$_SESSION['firmware'].$_SESSION['servicedate'].$_SESSION['warrantydate'].$_SESSION['partnerid'].'" class="btn alt mar-right-2">'.$button_cancel.'</a>
';

//------------------------------------
//CHECK IF USER IS ALSO CREATOR OF RECORD THEN OVERRIDE UPDATE_ALLOWED_EDIT
//------------------------------------
$equipment_owner = (($responses->createdby == $_SESSION['username'])? 1 : 0);
//------------------------------------
//
//------------------------------------
if ($update_allowed_edit === 1 || $equipment_owner === 1){
    $view .= '<a href="index.php?page=equipment_manage&equipmentID='.$responses->equipmentID.'" class="btn">Edit</a>';
}

$view .= '</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-block-wrapper">';

$view .= '  <div class="content-block order-details">
                <div class="block-header">
                <i class="fa-solid fa-circle-info"></i></i>'.$view_asset_information.'
                </div>
                <div class="order-detail">
                    <h3>'.$equipment_label3.'</h3>
                    <p><span class="status id'.$responses->status.'">'.$$status_text.'</span></p>
                </div>
                 <div class="order-detail">
                    <h3>'.$view_asset_data_ranking.'</h3>
                    <p><span class="status '.(($total_score >= 85) ?'enabled' : (($total_score <= 50)? 'disabled' : 'id2')).'">'.$total_score.'</span></p>
                </div>
                <div class="order-detail">
                    <h3>'.$equipment_label2.'</h3>
                    <p>'.$responses->serialnumber.'</p>
                </div>
                <div class="order-detail">
                    <h3>'.$equipment_label10.'</h3>
                    <p>'.$responses->equipmentID.'</p>
                </div>
                <div class="order-detail">
                    <h3>'.$product_code.'</h3>
                    <p>'.(($view_product == 1)? '<a href="index.php?page=product&rowID='.$responses->productrowid.'" class="btn2">'.$responses->productcode.'</a>':$responses->productcode).'</p>
                </div>  
                <div class="order-detail">
                    <h3>'.$product_name.'</h3>
                    <p>'.(($view_product == 1)? '<a href="index.php?page=product&rowID='.$responses->productrowid.'" class="btn2">'.(${$responses->productname} ?? $responses->productname).'</a>':(${$responses->productname} ?? $responses->productname)).'</p>
                </div>';
                if (!empty($media_responses['full_path'])){
                    $view .='  
                    <div class="order-detail"> 
                        <img style="border-radius: 4px;height: 200px;margin: auto;" src="'.$media_responses['full_path'].'" alt="">
                    </div>    
                        '; 
                }
                
$view .='                
            </div>
';

$view .='<div class="content-block order-details">
            <div class="block-header">
                <i class="fa-solid fa-user fa-sm"></i>'.$view_asset_partners.'
            </div>
            <div class="order-detail">
                    <h3>'.$general_salesid.'</h3>
                    <p>'.$salesid.'</p>
            </div>
            <div class="order-detail">
                    <h3>'.$general_soldto.'</h3>
                    <p>'.$soldto.'</p>
            </div>
            <div class="order-detail">
                    <h3>'.$general_shipto.'</h3>
                    <p>'.$shipto.'</p>
            </div>
            <div class="order-detail">
                    <h3>'.$general_location.'</h3>
                    <p>'.$location.'</p>
            </div>
            <div class="order-detail">
                    <h3>'.$general_section.'</h3>
                    <p>'.$section.'</p>
            </div>
        </div>';

$view .= '</div>';
$view .= '<div class="content-block">
            <div class="block-header">
                <i class="fa-solid fa-bars fa-sm"></i>'.$view_asset_details.'
            </div>
             <div class="table order-table">
            <table>
                <tr>
                    <td style="width:25%;">'.$warranty_status.'</td>
                    <td>'.$warrantydate.'</td>
                </tr>
                <tr>
                    <td style="width:25%;">'.$service_status.'</td>
                    <td>'.$service_date_due.'</td>
                </tr>
                <tr>
                    <td style="width:25%;">'.$software_status.'</td>
                    <td>'.$firmware_status.'</td>
                </tr>
                <tr>
                    <td style="width:25%;">'.$equipment_label5.'</td>
                    <td>'.$responses->hw_version.'</td>
                </tr> 
                <tr>
                    <td style="width:25%;">'.$equipment_label6.'</td>
                    <td>'.$responses->sw_version.'</td>
                </tr>';
//SHOW ONLY SW_UPGRADE WHEN SET
if (isset($products_software) && $products_software !=''){
    foreach ($products_software as $products_soft){
        if ($products_soft->rowID == $responses->sw_version_upgrade){
             $view .= ' <tr>
                            <td style="width:25%;">'.($equipment_sw_version_upgrade ?? 'sw_version_upgrade').'</td>
                            <td>'.$products_soft->version.'</td>
                        </tr>';
        }
    }
}
$view .= '    </table>    
            </div>
        </div>
    ';

//SHOW LOCATION BASED ON GEOLOCATION
if (!empty($responses->geolocation) || $responses->geolocation != ''){

    $geolocation = json_decode($responses->geolocation,true);
    if ($geolocation[1] != ''){
        $view .= '<div class="content-block">
                    <div id="map" style="height:300px;z-index: 0;">
                    </div>
                    <script>
                    // initialize Leaflet
                    var map = L.map(\'map\').setView({lon: '.$geolocation[1].', lat: '.$geolocation[0].'}, 10);

                    //Add TSS Avatar to MAP
                    var TSSemp = L.icon({
                        iconUrl: \'./assets/images/EP.png\',
                        iconSize:     [50, 50], // size of the icon
                    });
                    L.marker(['.$geolocation[0].','.$geolocation[1].'], {icon: TSSemp}).addTo(map);

                    // add the OpenStreetMap tiles
                    L.tileLayer(\'https://tile.openstreetmap.org/{z}/{x}/{y}.png\', {
                        maxZoom: 19,
                        attribution: \'&copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>\'
                    }).addTo(map);

                    // show the scale bar on the lower left corner
                    L.control.scale({imperial: true, metric: true}).addTo(map);
                    </script>
        </div>
        ';
    }
}

//Get all related service events
if (isAllowed('servicereports',$_SESSION['profile'],$_SESSION['permission'],'R') === 1){
$service_events = serviceEvents($history,$page);

$view .= '<div class="content-block">
    <div class="block-header">
        <i class="fa-solid fa-bars fa-sm"></i>'.$view_asset_servicereport.'
    </div>
    <div class="table order-table">'.$service_events.'</div>
</div>
';
}

//Show equipment_data when available and allowed
if (isAllowed('equipment_data',$_SESSION['profile'],$_SESSION['permission'],'R') === 1 && !empty($equipment_data)){
$view .= '<div class="content-block">
        <div class="block-header">
            <i class="fa-solid fa-bars fa-sm"></i>'.($view_asset_data_text ?? '').'
        </div>
        <div class="table">
            <table class="sortable">
                <thead>
                    <tr>
                        <th>'.$view_asset_data_rowID.'</th>
                        <th>'.$view_asset_data_historyid.'</th>
                        <th>'.$view_asset_data_ranking.'</th>
                        <th>'.$general_actions.'</th>
                    </tr>
                </thead>
                <tbody>';
        foreach ($equipment_data as $data){

            $view .= '<tr>
                        <td>'.$data->rowID.'</td>
                        <td>'.(($view_history == 1)? '<a href="index.php?page=history&equipmentID='.$responses->equipmentID.'&historyid='.$data->historyid.'" class="btn2">'.$data->historyid.'</a>':$data->historyid).'</td>
                        <td><span class="status '.(($data->healthindex >= 85) ?'enabled' : (($data->healthindex <= 50)? 'disabled' : 'id2')).'">'.$data->healthindex.'</span></td>
                        <td><a href="index.php?page=equipment_data&equipmentID='.$responses->equipmentID.'&rowID='.$data->rowID.'" class="btn_link">'.$general_view.'</a></td>
                    </tr>';
        }
        $view .= '
                </tbody>
            </table>
        </div>
        ';
$view .= '
    </div>
';
}

if ($update_allowed === 1){
    $view .= '<div class="content-block">
                <div class="block-header">
                    <i class="fa-solid fa-bars fa-sm"></i>'.$view_asset_notes.'
                </div>
                <form action="" method="post">
                    <textarea id="description" name="description" placeholder="'.$view_asset_notes.'" style="width: 100%;height: 150px;" onchange="this.form.submit();"></textarea>  
                    <input type="hidden" name="equipmentid" value="'.$responses->equipmentID.'">
                    <input type="hidden" name="type" value="'.$type16.'">
                </form>
            </div>
    ';
}

$view .= '<div class="content-block">
    <div class="block-header">
        <i class="fa-solid fa-bars fa-sm"></i>'.$tab3.'
    </div>
    <div class="table order-table">
        <table>
            <tr>
                <td style="width:25%;">'.$general_created.'</td>
                <td>'.getRelativeTime($responses->created).'</td>
            </tr>
            <tr>
                <td style="width:25%;">'.$general_updated.'</td>
                <td>'.getRelativeTime($responses->updated).'</td>
            </tr>
        </table>
    </div>
</div>
';

//DISPLAY RELATED COMMUNICATION RECORDS
$soldto_id = explode("-",$partner_data->soldto) ?? '';  
$shipto_id = explode("-",$partner_data->shipto) ?? '';

$partner_users_id = ($shipto_id[0] != '')? $shipto_id[0] : (($soldto_id[0] != '')? $soldto_id[0] : 1);

$view_communication = '';
if ($partner_users_id != 1 && ($_SESSION['permission'] == 3 || $_SESSION['permission'] == 4)){
    $view_communication = ' <a href="index.php?page=communications&partnerid='.$partner_users_id.'" class="btn">'.$button_partner_assigned_communication.'</a>';
}

//DISPLAY RELATED USERS
$view_users ='';
if ($partner_users_id != 1 && ($_SESSION['permission'] == 3 || $_SESSION['permission'] == 4)){
    $view_users = ' <a href="index.php?page=users&partnerid='.$partner_users_id.'" class="btn">'.$button_partner_assigned_users.'</a>';
}

$view .= '<div class="content-block">
    <div class="block-header">
        <i class="fa-solid fa-bars fa-sm"></i>'.$view_asset_actions.'
    </div>
    <a href="index.php?page=history&equipmentID='.$responses->equipmentID.'" class="btn">'.$button_history.'</a>
    '.$view_communication.'
    '.$view_users.'
    ';

if ($update_allowed === 1){

    $view .='<a href="index.php?page=firmwaretool&equipmentID='.$responses->equipmentID.'" class="btn">'.$button_firmware.'</a>';
}
$view .='</div>';

//OUTPUT
echo $view;    

template_footer()

?>