<?php

/**
*
* bareos-webui - Bareos Web-Frontend
*
* @link      https://github.com/bareos/bareos for the canonical source repository
* @copyright Copyright (c) 2013-2024 Bareos GmbH & Co. KG (https://www.bareos.org/)
* @license   GNU Affero General Public License (http://www.gnu.org/licenses/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

$title = 'Director';
$this->headTitle($title);

?>

<br />

<?php if($this->acl_alert) : echo $this->ACLAlert($this->invalid_commands); else : ?>

<pre class="bconsole" id="bconsole" style="font-size: 9pt; padding-left: 5px; overflow: scroll; width: 150vh; height: 75vh; background: #000; color: #fff;" onclick="focusScreen();">
   <p><?php echo $this->translate("bconsole (batch-mode), please handle with care."); ?><br /><?php echo $this->translate("All commands have to be a one liner, dialogs are not working."); ?><br /><?php echo $this->translate("Type help for a list of commands."); ?></p>
   <div class="prompt"><label>* </label><input type="text" class="cli" id="cli" value ="" size=120 style="display: inline; background: #000; color: #fff; border-color: #000; border-style: hidden; outline: none;"/></div>
</pre>

<br />

<input class="btn btn-primary" type="button" value="Clear" onclick="clearscr();"/>
<input class="btn btn-primary" type="button" value="Help" onclick="help();"/>

<script>

function focusScreen() {
   document.getElementById('cli').focus();
   $('.bconsole').scrollTop($('.bconsole')[0].scrollHeight - $('.bconsole')[0].clientHeight);
}

function clearscr() {
   $(".output").remove();
   document.getElementById('cli').focus();
}

function help() {
   send("help");
   document.getElementById('cli').focus();
}

function send(cmd) {

   $("<div class='output'>* "+cmd+"</div>").insertBefore(".prompt");

   if(cmd == "") {
      focusScreen();
   } else {
      $('.prompt').hide();
      var request = $.ajax({
         method: "POST",
         url: "<?php echo $this->url('api/console', array(), array()); ?>",
         data: {
            "command": cmd
         },
         dataType: "json",
      });
      request.done(function(msg) {
         $("<div class='output'>"+msg.replace(/(?:\r\n|\r|\n)/g, '<br />')+"</div>").insertBefore(".prompt");
         $('.prompt').show();
         document.getElementById('cli').value = "";
         focusScreen();
      });
   }

   
}

var cli = document.getElementById("cli");

cli.addEventListener("keydown", function (e) {
   if (e.keyCode === 13) {
      var cmd = document.getElementById('cli').value;
      send(cmd);
   }
});

$(document).ready(function() {
   focusScreen();
});

</script>

<?php endif; ?>
