Menu="About:20"
Title="Downgrade OS"
Icon="icon-update"
Tag="upload"
---
<?php
/* Copyright 2005-2023, Lime Technology
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
/**
 * @note icon-update is rotated via CSS in myservers1.php
 */
require_once "$docroot/plugins/dynamix/include/ReplaceKey.php";
$replaceKey = new ReplaceKey();
$replaceKey->check();

require_once "$docroot/plugins/dynamix.my.servers/include/reboot-details.php";
$rebootDetails = new RebootDetails();
$rebootDetails->setPrevious();

$serverNameEscaped = htmlspecialchars(str_replace(' ', '_', strtolower($var['NAME'])));
?>

<script>
const nchan_diagnostics = new NchanSubscriber('/sub/diagnostics', { subscriber: 'websocket' });
const reportUrl = new URL('https://forums.unraid.net/bug-reports/');
let diagnosticsFile = '';

nchan_diagnostics.on('message', function(data) {
  if (data == '_DONE_') {
    nchan_diagnostics.stop();
    $('.sweet-alert').hide('fast').removeClass('nchan');
    swal.close();
    $('div.spinner').show('slow');
    location = diagnosticsFile;

    setTimeout(() => {
      cleanupDiagnostics();
      reportAfterDiagnosticsDownload();
    }, 2000);
  } else if (data) {
    let box = $('pre#swaltext');
    box.html(box.html() + '<br>' + data).scrollTop(box[0].scrollHeight);
  }
});

function downloadDiagnostics() {
  const tzoffset = (new Date()).getTimezoneOffset() * 60000; // offset in milliseconds
  const localISOTime = (new Date(Date.now() - tzoffset));
  const year = localISOTime.getFullYear();
  const month = String(localISOTime.getMonth() + 1).padStart(2, '0');
  const day = String(localISOTime.getDate()).padStart(2, '0');
  const hours = String(localISOTime.getHours()).padStart(2, '0');
  const minutes = String(localISOTime.getMinutes()).padStart(2, '0');
  const dateOutput = `${year}${month}${day}_${hours}${minutes}`;
  const zipName = '<?=$serverNameEscaped?>-diagnostics-' + dateOutput + '.zip';

  nchan_diagnostics.start();

  $.post(
    '/webGui/include/Download.php',
    {
      cmd: 'diag',
      file: zipName,
      anonymize: '',
    },
    function(zip) {
      if (!zip) {
        return nchan_diagnostics.stop();
      }

      diagnosticsFile = zip;
      swal({
        title: "_(Downloading)_...",
        text: "/boot/logs" + zip + "<hr><pre id='swaltext'></pre>",
        html: true,
        animation: 'none',
        showConfirmButton: false,
      });
      $('.sweet-alert').addClass('nchan');
      $('button.confirm').prop('disabled', true);
    },
  );
}

function reportAfterDiagnosticsDownload() {
  $('div.spinner').hide('fast');
  swal({
    title: "_(Open a bug report)_",
    text: "_(Create a bug report on our forums with a description of the issue along with your diagsnotics)_",
    html: true,
    type: 'warning',
    showCancelButton: true,
    confirmButtonText: "<?= _('Create Bug Report') ?>",
    cancelButtonText: "<?= _('Close') ?>",
  }, function(confirm) {
    if (!confirm) {
      return false;
    }
    window.open(reportUrl, '_blank');
  });
}

function cleanupDiagnostics() {
  if (document.hasFocus()) {
    return $.post('/webGui/include/Download.php', { cmd: 'delete', file: diagnosticsFile });
  }
  setTimeout(cleanupDiagnostics, 2000);
}

function startDowngrade() {
  $('div.spinner').show('slow');

  $.get(
    '/plugins/dynamix.plugin.manager/include/Downgrade.php',
    {
      version: '<?= $rebootDetails->previousVersion ?>',
    },
    function() {
      refresh();
    }
  );
}

function confirmDowngrade() {
  swal({
    title: "_(Confirm Downgrade)_",
    text: "<?= $rebootDetails->previousVersion ?><br>_(A reboot will be required)_",
    html: true,
    type: 'warning',
    showCancelButton: true,
    confirmButtonText: "<?= _('Confirm') ?>",
    cancelButtonText: "<?= _('Cancel') ?>",
  }, function(confirm) {
    if (!confirm) {
      return false;
    }
    startDowngrade();
  });
}
</script>

<unraid-i18n-host>
  <unraid-downgrade-os
    reboot-version="<?= $rebootDetails->rebootVersion ?>"
    restore-version="<?= $rebootDetails->previousVersion ?>"
    restore-release-date="<?= $rebootDetails->previousReleaseDate ?>"></unraid-downgrade-os>
</unraid-i18n-host>
