<?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);  
}

$page = 'firmwaretool';
//Check if allowed
if (isAllowed($page,$_SESSION['profile'],$_SESSION['permission'],'R') === 0){
    header('location: index.php');
    exit;
}
$bearertoken = createCommunicationToken($_SESSION['userkey']);


template_header('Firmwaretool', 'firmwaretool','view');

$view = '
<div class="content-title">
    <div class="title">
        <i class="fa-solid fa-box-open"></i>
        <div class="txt">
            <h2>'.$firmwaretool_h2 .'</h2>
            <p>'.$firmwaretool_p.'</p>
        </div>
    </div>
</div>';

if (isset($_GET['equipmentID'])){$returnpage = 'equipment&equipmentID='.$_GET['equipmentID']; } else {$returnpage = 'dashboard';}


//SHOW BACK BUTTON ONLY FOR PORTAL USERS
if (isAllowed('dashboard',$_SESSION['profile'],$_SESSION['permission'],'R') != 0){
  $view .= '
      <div class="content-header responsive-flex-column pad-top-5">
          <a href="index.php?page='.$returnpage.'" class="btn">'.$button_cancel.'</a>
      </div>
  ';
}

$view .= '<div class="content-block">
    <div class="block-header">
        <i class="fa-solid fa-bars fa-sm"></i>
    </div>
    <p id="servicetoken" value="" hidden>'.$bearertoken.'</p>
    <div id="connectdevice" style="display:flex;">
      <div><button class="btn"  style="margin-right:10px;" id="connectButton" onClick="connectDevice()">🔌 Connect</button></div>
      <div id="readStatus" style="width: 75%;background-color: #f1f1f1;">
        <div id="readBar"></div>
      </div>
    </div>
    <div id="Device_output" style="display:none;margin-top: 10px;">
      <div id="serialResults" style="font-family: monospace;white-space: pre;padding: 10px;background-color:#f1f1f1;"></div>
      <input type="checkbox" id="action_firmware" onclick="checkFormUpdateFirmware()"> '.$firmware_update_confirm.'
      
        <div class="" id="updateAvailabe" style="margin-top: 10px;">
            <button class="btn" id="updateFirmware" style="display:none;"
              emergencyPlug-update
              board="emergencyPlug"
              port-filters= "[{usbVendorId: 1027, usbProductId: 24597}\];"
              disabled>Update Firmware
              <span class="upload-progress"></span>
            </button>
        </div>
      </div>
    <div>
    <p>
    <h4>'.$firmwaretool_step.'</h4>
    <ol type="1">
      <li>'.$firmwaretool_step_1.'</li>
      <li>'.$firmwaretool_step_2.'</li>
      <li>'.$firmwaretool_step_3.'</li>
      <li>'.$firmwaretool_step_4.'</li>
      <li>'.$firmwaretool_step_5.'</li>
      <li>'.$firmwaretool_step_6.'</li>
      <li>'.$firmwaretool_step_7.'</li>
      <li>'.$firmwaretool_step_8.'</li>
    </ol>
    </p>
    
</div>
';
           
 $view .= ' 
    </div>
</div>
';

$view .= '</div>';
//OUTPUT
echo $view;


echo '
<script src="assets/scripts.js?'.script_version.'"></script>
<script src="assets/upload.js?'.script_version.'"></script>
<script>
		var link = "'.$baseurl.'";
    var port, textEncoder, writableStreamClosed, writer, historyIndex = -1;
    const lineHistory = [];
</script>';

template_footer();
?>