<?php

/**
 *
 * bareos-webui - Bareos Web-Frontend
 *
 * @link      https://github.com/bareos/bareos for the canonical source repository
 * @copyright Copyright (C) 2013-2026 Bareos GmbH & Co. KG (http://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 = 'Jobs';
$this->headTitle($title);

?>

<ul class="nav nav-tabs">
   <li><a href="<?php echo $this->url('job', array('action'=>'index')); ?>"><?php echo $this->translate('Show'); ?></a></li>
   <li><a href="<?php echo $this->url('job', array('action'=>'actions')); ?>"><?php echo $this->translate('Actions'); ?></a></li>
   <li class="active"><a href="<?php echo $this->url('job', array('action'=>'run')); ?>"><?php echo $this->translate('Run'); ?></a></li>
   <li><a href="<?php echo $this->url('job', array('action'=>'rerun')); ?>"><?php echo $this->translate('Rerun'); ?></a></li>
   <li><a href="<?php echo $this->url('job', array('action'=>'timeline')); ?>"><?php echo $this->translate('Timeline'); ?></a></li>
</ul>

<br />

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

<div class="container-fluid col-md-6">

<div class="panel panel-default">
   <div class="panel-heading">
      <h3 class="panel-title">Run jobs</h3>
   </div>
   <div class="panel-body">
   <div class="container-fluid">

<?php
   $form->prepare();
   $form->setAttribute('class','form-horizontal');
   echo $this->form()->openTag($form);
?>

<div class="row">

   <div class="form-group">
      <label class="control-label col-md-2"><?php echo $this->formLabel($form->get('job')); ?>*</label>
      <div class="control-input col-md-6">
         <?php echo $this->formSelect($form->get('job')); ?>
      </div>
   </div>

   <div class="form-group">
      <label class="control-label col-md-2"><?php echo $this->formLabel($form->get('client')); ?></label>
      <div class="control-input col-md-6">
         <?php echo $this->formSelect($form->get('client')); ?>
      </div>
   </div>

   <div class="form-group">
      <label class="control-label col-md-2"><?php echo $this->formLabel($form->get('fileset')); ?></label>
      <div class="control-input col-md-6">
         <?php echo $this->formSelect($form->get('fileset')); ?>
      </div>
   </div>

   <div class="form-group">
      <label class="control-label col-md-2"><?php echo $this->formLabel($form->get('storage')); ?></label>
      <div class="control-input col-md-6">
         <?php echo $this->formSelect($form->get('storage')); ?>
      </div>
   </div>

   <div class="form-group">
      <label class="control-label col-md-2"><?php echo $this->formLabel($form->get('pool')); ?></label>
      <div class="control-input col-md-6">
         <?php echo $this->formSelect($form->get('pool')); ?>
      </div>
   </div>

   <div class="form-group">
      <label class="control-label col-md-2"><?php echo $this->formLabel($form->get('level')); ?></label>
      <div class="control-input col-md-6">
         <?php echo $this->formSelect($form->get('level')); ?>
      </div>
   </div>

   <div class="form-group">
      <label class="control-label col-md-2"><?php echo $this->formLabel($form->get('type')); ?></label>
      <div class="control-input col-md-6">
         <?php echo $this->formInput($form->get('type')); ?>
      </div>
   </div>

   <div class="form-group">
      <label class="control-label col-md-2"><?php echo $this->formLabel($form->get('nextpool')); ?></label>
      <div class="control-input col-md-6">
         <?php echo $this->formSelect($form->get('nextpool')); ?>
      </div>
   </div>

   <div class="form-group">
      <label class="control-label col-md-2"><?php echo $this->formLabel($form->get('priority')); ?></label>
      <div class="control-input col-md-6">
         <?php echo $this->formInput($form->get('priority')); ?>
      </div>
   </div>

<!--
   <div class="form-group">
      <label class="control-label col-md-2"><?php // echo $this->formLabel($form->get('backupformat')); ?></label>
      <div class="control-input col-md-6">
         <?php // echo $this->formInput($form->get('backupformat')); ?>
      </div>
   </div>
-->

   <div class="form-group">
      <label class="control-label col-md-2"><?php echo $this->formLabel($form->get('when')); ?></label>
      <div class="col-md-6">
         <div class="input-group date" id="when-datepicker">
            <?php echo $this->formInput($form->get('when')); ?>
            <span class="input-group-addon">
               <span class="glyphicon glyphicon-calendar"></span>
            </span>
         </div>
      </div>
   </div>

   <div class="form-group">
      <div class="col-md-6 col-md-offset-2">
         <span class="text-muted"><i>(Input fields marked with * are required.)</i></span>
      </div>
   </div>

   <div class="form-group">
      <div class="col-md-6 col-md-offset-2">
         <?php echo $this->formSubmit($form->get('submit')->setAttribute('class','btn btn-primary')); ?>
      </div>
   </div>

</div>

</div>

<?php
   echo $this->formHidden($form->get('csrf'));
   echo $this->form()->closeTag($form);
?>

</div>
</div>

</div>
</div>

<?php endif; ?>

<script type="text/javascript">

   const pool_nextpool_mapping = <?php
      echo json_encode(
         $form->getPoolNextPoolMapping(),
         JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
      );
   ?>;

   function updateQueryParams(k, v) {
      var p = [];
      var params = [];

      p['jobname'] = '<?php echo $this->jobname; ?>';

      p[k] = v;

      for(key in p) {
          params.push(key + "=" + p[key]);
      }

      return params.join('&');
   }

   function hasNextPoolOption() {
      return $('#level').selectpicker('val') == "VirtualFull" ||
         $('#type').val() == "Copy" ||
         $('#type').val() == "Migrate"
   }

   $('#job').change(function(event) {
      window.location.href = window.location.pathname + '?' + updateQueryParams('jobname', this.value);
   });

   $('#pool').change(function(event) {
      if(hasNextPoolOption()) {
         let nextpool = pool_nextpool_mapping[$('#pool').selectpicker('val')];

         if(nextpool) {
            $('#nextpool').selectpicker('val', nextpool);
         }
      }
   });

   $('#level').change(function() {
      if(hasNextPoolOption()) {
         $("#nextpool").parents(".form-group").fadeIn();
         $('#pool').change();
      } else {
         $("#nextpool").parents(".form-group").hide();
         $('#nextpool').selectpicker('val', '');
      }
   });

   $(function() {
      $('#when-datepicker').datetimepicker({
         format: 'YYYY-MM-DD HH:mm:ss',
         sideBySide: true
      });

      // This will enable/disable the Next Pool select based on the Level value on load
      $('#level').change();
   });

</script>
