<?php
defined(page_security_key) or exit;

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

// Default input product values
$equipment_data = [
    'rowID' => '',
    'equipmentid' => '',
    'historyid' => '',
    'created' => '',
    'createdby' => $_SESSION['username'],
    'measurement' => ''
];

$equipmentID = $_GET['equipmentID']??'';

if (isset($_GET['rowID'])) {
    // ID param exists, edit an existing product
    //CALL TO API
    $api_url = '/v1/equipment_data/rowID='.$_GET['rowID'];
    $responses = ioServer($api_url,'');

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

    $equipment_data = json_decode(json_encode($responses[0]), true);
}


template_header('Equipment_data', 'equipment_data', 'manage');

$view ='
<form action="" method="post">
    <div class="content-title responsive-flex-wrap responsive-pad-bot-3">
        <h2 class="responsive-width-100">'.$view_asset_data_text.'</h2>
        <a href="index.php?page=equipment&equipmentID='.$equipmentID.'" class="btn alt mar-right-2">'.$button_cancel.'</a>
';   
$view .= '</div>';

$view .= '<div class="tabs">
            <a href="#" class="active">'.$tab1.'</a>
            <a href="#">'.$tab3.'</a>
         </div>
    ';

$view .= '<div class="content-block tab-content active">
            <div class="form responsive-width-100">
                <label for="">'.$view_asset_data_rowID.'</label>
                <p>'.$equipment_data['rowID'].'</p>          
                <label for="">'.$view_asset_data_historyid.'</label>
                <p>'.$equipment_data['historyid'].'</p>
                <label for="">'.$view_asset_data_ranking.'</label>
                <p>'.$equipment_data['healthindex'].'</p> 
                ';
                
                if (isset($_GET['rowID']) && $_GET['rowID'] !='' && !empty($equipment_data['measurement'])){  
                    $measurements = json_decode($equipment_data['measurement'],true);
        
                    $view .= '
                        <label for="">'.$view_asset_data.'</label>
                        <div class="table">
                        <table>
                            <thead>
                                <tr>
                                    <td>Test</td>
                                    <td>Measurement</td>
                                    <td>Deviaton</td>
                                </tr>
                            </thead>
                            <tbody>
                    ';
                    foreach ($measurements as $name => $measurement){
                        
                        $view .='
                            <tr>
                                <td>'.$measurement['measurement'].'</td>
                                <td>'.$measurement['value'].'</td>
                                <td>'.$measurement['deviation'].'</td>
                            </tr>
                        ';
                    }
                    $view .= '</tbody>
                    </table>
                    </div>';

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

$view .= '<div class="content-block tab-content">
        <div class="form responsive-width-100">
            <label for="productcode">'.$general_created.'</label>
            <input id="name" type="text" name="" placeholder="'.$general_created.'" value="'.$equipment_data['created'].'" readonly>
            <label for="productcode">'.$general_createdby.'</label>
            <input id="name" type="text" name="" placeholder="'.$general_createdby.'" value="'.$equipment_data['createdby'].'" readonly>
        </div>
    </div>';
$view .= '</form>';

//Output
echo $view;
template_footer()
?>