<?php
defined(page_security_key) or exit;

$page = 'history_manage';
//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
$history = [
    'historyID' => '',
    'equipmentid' => $_GET['equipmentID'] ?? '',
    'type' => '',
    'description' => '',
    'created' => $date,
    'createdby' => $_SESSION['username']
];

//defaults
$rowID = $_GET['rowID'] ?? '';

if ($rowID !=''){
    $url = 'index.php?page=history&equipmentID='.$_GET['equipmentID'];
} else {
    $url = 'index.php?page=equipment&equipmentID='.$_GET['equipmentID'];
}

if (isset($_GET['rowID'])) {
    // ID param exists, edit an existing product
    //CALL TO API
    $api_url = '/v1/history/historyid='.$rowID;
    $responses = ioServer($api_url,'');
    //Decode Payload
    if (!empty($responses)){$responses = decode_payload($responses);}else{$responses = null;}
    
    $history = json_decode(json_encode($responses[0]), true);
  
    if ($update_allowed === 1){
        if (isset($_POST['submit'])) {
            //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=history&equipmentID='.$_GET['equipmentID'].'&success_msg=2');
            exit;
            }
        }
    }
    
    if ($delete_allowed === 1){
        if (isset($_POST['delete'])) {
            //GET ALL POST DATA
            $data = json_encode($_POST , JSON_UNESCAPED_UNICODE);
            //Secure data
            $payload = generate_payload($data);
            //API call
            $responses = ioServer('/v1/history', $payload);
            // Redirect and delete product
            if ($responses === 'NOK'){

            } else {
            header('Location: index.php?page=history&equipmentID='.$_GET['equipmentID'].'&success_msg=3');
            exit;
            }
        }
    }
   
} else {
    // Create a new product
    if (isset($_POST['submit']) && $create_allowed === 1) {
        //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=history$equipmentID='.$_GET['equipmentID'].'&success_msg=1');
        exit;
        }
    }
}

template_header('history', 'history', 'manage');

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

if ($delete_allowed === 1){
    $view .= '<input type="submit" name="delete" value="Delete" class="btn red mar-right-2" onclick="return confirm(\'Are you sure you want to delete this history?\')">';
}
if ($update_allowed === 1){
    $view .= '<input type="submit" name="submit" value="Save" class="btn">';
}
    
$view .= '</div>';

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

$view .= '<div class="content-block tab-content active">
            <div class="form responsive-width-100">
                <label for="">'.$history_label2 .'</label>
                <select id="status" name="type">
                    <option value="'.$type1.'" '.($history['type']==$type1 ?' selected':'').'>'.$type1 .'</option>
                    <option value="'.$type2.'" '.($history['type']==$type2 ?' selected':'').'>'.$type2 .'</option>
                    <option value="'.$type3.'" '.($history['type']==$type3 ?' selected':'').'>'.$type3 .'</option>
                    <option value="'.$type4.'" '.($history['type']==$type4 ?' selected':'').'>'.$type4 .'</option>
                    <option value="'.$type5.'" '.($history['type']==$type5 ?' selected':'').'>'.$type5 .'</option>
                    <option value="'.$type6.'" '.($history['type']==$type6 ?' selected':'').'>'.$type6 .'</option>
                    <option value="'.$type7.'" '.($history['type']==$type7 ?' selected':'').'>'.$type7 .'</option>
                    <option value="'.$type8.'" '.($history['type']==$type8 ?' selected':'').'>'.$type8 .'</option>
                    <option value="'.$type9.'" '.($history['type']==$type9 ?' selected':'').'>'.$type9 .'</option>
                    <option value="'.$type10.'" '.($history['type']==$type10 ?' selected':'').'>'.$type10 .'</option>
                    <option value="'.$type11.'" '.($history['type']==$type11 ?' selected':'').'>'.$type11 .'</option>
                    <option value="'.$type12.'" '.($history['type']==$type12 ?' selected':'').'>'.$type12 .'</option>
                    <option value="'.$type13.'" '.($history['type']==$type13 ?' selected':'').'>'.$type13 .'</option>
                    <option value="'.$type14.'" '.($history['type']==$type14 ?' selected':'').'>'.$type14 .'</option>
                    <option value="'.$type15.'" '.($history['type']==$type15 ?' selected':'').'>'.$type15 .'</option>
                    <option value="'.$type16.'" '.($history['type']==$type16 ?' selected':'').'>'.$type16 .'</option>
                    <option value="'.$type17.'" '.($history['type']==$type17 ?' selected':'').'>'.$type17 .'</option>
                 </select>
            </div>
            <div class="form responsive-width-100">
                <label for="">'.$equipment_label10.'</label>
                <input id="name" type="text" name="equipmentid" placeholder="'.$equipment_label10.'" value="'.$history['equipmentID'].'" required>
            </div>
            <div class="form responsive-width-100">
                <label for="">'.$history_label5.'</label>
                <textarea id="'.$history_label5.'" name="description" placeholder="'.$history_label5.'">'.$history['description'].'</textarea>
           </div>
        </div>';

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

$view .= '</form>';


//Output
echo $view;
template_footer();