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

//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 History
$api_url = '/v1/equipments/equipmentID='.$_GET['equipmentID'].'&type=ServiceReport&history=';
$history = ioServer($api_url,'');

//Decode Payload
if (!empty($history)){$history = decode_payload($history);}else{$history = 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->hw_version);

//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.' - '.$_GET['equipmentID'].'</h2>
        <a href="index.php?page=equipments" 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='.$_GET['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>'.$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>'.$responses->productcode.'</p>
                </div>  
                <div class="order-detail">
                    <h3>'.$product_name.'</h3>
                    <p>'.$responses->productname.'</p>
                </div>';

                $picture = glob("./assets/images/products/".$responses->productcode.".{jpg,jpeg,png,gif}", GLOB_BRACE);
                if (!empty($picture)){
                    $view .='  
                    <div class="order-detail"> 
                        <img style="border-radius: 4px;height: 100px;" src="'.$picture[0].'" 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>
            </table>    
            </div>
        </div>
    ';

//Get all related service events

if (isAllowed('servicereports',$_SESSION['profile'],$_SESSION['permission'],'R') === 1){
$service_events = serviceEvents($history);

$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>
';
}
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="'.$_GET['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>'.$view_asset_actions.'
    </div>
    <a href="index.php?page=history&equipmentID='.$_GET['equipmentID'].'" class="btn">'.$button_history.'</a>';

if ($update_allowed === 1){

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



//OUTPUT
echo $view;    

template_footer()

?>