<?php

/**
 *
 * bareos-webui - Bareos Web-Frontend
 *
 * @link      https://github.com/bareos/bareos for the canonical source repository
 * @copyright Copyright (c) 2013-2017 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 = _('Clients');
$this->headTitle($title);

?>

<ul class="nav nav-tabs">
   <li class="active"><a href="<?php echo $this->url('client', array('action'=>'index')); ?>"><?php echo $this->translate('Show'); ?></a></li>
</ul>

<br />

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

<div class="row">

<div class="col-md-8">
<div class="panel panel-default">

<div class="panel-heading">
<h3 class="panel-title"><?php echo $this->translate('Clients'); ?></h3>
</div>

<div class="panel-body">

<table class="table table-no-bordered table-hover" id="clients">

<thead class="bg-primary">
   <th><?php echo $this->translate("Name"); ?></th>
   <th><?php echo $this->translate("Version"); ?></th>
   <th></th>
   <th><?php echo $this->translate("Status"); ?></th>
   <th><?php echo $this->translate("Actions"); ?></th>
   <th></th>
   <th></th>
</thead>

</table>

</div>
</div>
</div>
</div>

<?php
   echo $this->headScript()->prependFile($this->basePath() . '/js/custom-functions.js');
   echo $this->headLink()->prependStylesheet($this->basePath() . '/css/bootstrap-table.min.css');
   echo $this->headScript()->prependFile($this->basePath() . '/js/bootstrap-table-locale-all.min.js');
   echo $this->headScript()->prependFile($this->basePath() . '/js/bootstrap-table-cookie.min.js');
   echo $this->headScript()->prependFile($this->basePath() . '/js/bootstrap-table.min.js');
?>

<!-- modal-001 start -->
<div id="modal-001" class="modal fade modal-001" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
  <div class="modal-dialog modal-md">
    <div class="modal-content">
      <div class="modal-header">
         <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
         </button>
         <h4 class="modal-title" id="myModalLabel">Client status</h4>
      </div>
      <div class="modal-body">
         <p><?php echo str_replace(array("\n","\r"), "<br />", $this->result); ?></p>
      </div>
      <div class="modal-footer">
         <button type="button" class="btn btn-default" data-dismiss="modal"><?php echo $this->translate("Close"); ?></button>
      </div>
    </div>
  </div>
</div>
<!-- modal-001 end -->

<script>

   var clients = null;

   function dlc(url) {
      window.open(url, '_blank');
   }

   function getClients() {
      clients = $('#clients').bootstrapTable({
         locale: '<?php echo str_replace('_','-', $_SESSION['bareos']['locale']); ?>',
         cookie: <?php echo $_SESSION['bareos']['dt_statesave']; ?>,
         cookieIdTable: 'client_clients',
         url: '<?php echo $this->url('client', array('action' => 'getData'), null) . '?data=all'; ?>',
         method: 'get',
         dataType: 'json',
         pagination : true,
         sidePagination: 'client',
         pageList: [ <?php echo $_SESSION['bareos']['dt_lengthmenu']; ?> ],
         pageSize: <?php echo $_SESSION['bareos']['dt_pagelength']; ?>,
         search: true,
         showPaginationSwitch: true,
         showRefresh: true,
         sortName: 'name',
         sortOrder: 'asc',
         columns: [
            {
               field: 'name',
               sortable: true,
               formatter: function(value) {
                  return '<a href="<?php echo $this->basePath() . '/client/details/'; ?>'+value+'">'+value+'</a>';
               }
            },
            {
               field: 'uname',
               sortable: true,
               formatter: function(value) {
                  if(value.toLowerCase().search("suse") > -1) {
                     return '<img src="<?php echo $this->basePath('img/icons/os/suse.png'); ?>" id="icon-os" title="'+value+'" data-toggle="tooltip" data-placement="top">';
                  }
                  else if(value.toLowerCase().search("sle") > -1) {
                     return '<img src="<?php echo $this->basePath('img/icons/os/suse.png'); ?>" id="icon-os" title="'+value+'" data-toggle="tooltip" data-placement="top">';
                  }
                  else if(value.toLowerCase().search("debian") > -1) {
                     return '<img src="<?php echo $this->basePath('img/icons/os/debian.png'); ?>" id="icon-os" title="'+value+'" data-toggle="tooltip" data-placement="top">';
                  }
                  else if(value.toLowerCase().search("fedora") > -1) {
                     return '<img src="<?php echo $this->basePath('img/icons/os/fedora.png'); ?>" id="icon-os" title="'+value+'" data-toggle="tooltip" data-placement="top">';
                  }
                  else if(value.toLowerCase().search("centos") > -1) {
                     return '<img src="<?php echo $this->basePath('img/icons/os/centos.png'); ?>" id="icon-os" title="'+value+'" data-toggle="tooltip" data-placement="top">';
                  }
                  else if(value.toLowerCase().search("redhat") > -1) {
                     return '<img src="<?php echo $this->basePath('img/icons/os/redhat.png'); ?>" id="icon-os" title="'+value+'" data-toggle="tooltip" data-placement="top">';
                  }
                  else if(value.toLowerCase().search("ubuntu") > -1) {
                     return '<img src="<?php echo $this->basePath('img/icons/os/ubuntu.png'); ?>" id="icon-os" title="'+value+'" data-toggle="tooltip" data-placement="top">';
                  }
                  else if(value.toLowerCase().search("univention") > -1) {
                     return '<img src="<?php echo $this->basePath('img/icons/os/univention.png'); ?>" id="icon-os" title="'+value+'" data-toggle="tooltip" data-placement="top">';
                  }
                  else if(value.toLowerCase().search("windows") > -1) {
                     return '<img src="<?php echo $this->basePath('img/icons/os/windows.png'); ?>" id="icon-os" title="'+value+'" data-toggle="tooltip" data-placement="top">';
                  }
                  else if(value.toLowerCase().search("win32") > -1) {
                     return '<img src="<?php echo $this->basePath('img/icons/os/windows.png'); ?>" id="icon-os" title="'+value+'" data-toggle="tooltip" data-placement="top">';
                  }
                  else if(value.toLowerCase().search("win64") > -1) {
                     return '<img src="<?php echo $this->basePath('img/icons/os/windows.png'); ?>" id="icon-os" title="'+value+'" data-toggle="tooltip" data-placement="top">';
                  }
                  else if(value.toLowerCase().search("macos") > -1) {
                     return '<img src="<?php echo $this->basePath('img/icons/os/macos.png'); ?>" id="icon-os" title="'+value+'" data-toggle="tooltip" data-placement="top">';
                  }
                  else if(value.toLowerCase().search("solaris") > -1) {
                     return '<img src="<?php echo $this->basePath('img/icons/os/sunsolaris.png'); ?>" id="icon-os" title="'+value+'" data-toggle="tooltip" data-placement="top">';
                  }
                  else if(value.toLowerCase().search("freebsd") > -1) {
                     return '<img src="<?php echo $this->basePath('img/icons/os/freebsd.png'); ?>" id="icon-os" title="'+value+'" data-toggle="tooltip" data-placement="top">';
                  }
                  else {
                     return '';
                  }
               }
            },
            {
               field: 'hf-1',
               formatter: function(value, row, index) {
                  if(row.available_fd === "" || row.available_fd === null || row.available_fd === undefined) {
                     return '<span class="label label-default" id="label-fd-version" data-toggle="tooltip" data-placement="top" title="Unknown update status">'+row.installed_fd+'</span>';
                  }
                  if(row.update_fd === true) {
                     if(row.update_dird === true) {
                        return '<span class="label label-danger" id="label-fd-version" data-toggle="tooltip" data-placement="top" title="Version '+row.available_fd+' is available">'+row.installed_fd+'</span> <span class="text-danger" data-toggle="tooltip" data-placement="top">(Director upgrade '+row.available_dird+' required first)</span>';
                     }
                     else {
                        return '<span class="label label-danger" id="label-fd-version" data-toggle="tooltip" data-placement="top" title="Version '+row.available_fd+' is available">'+row.installed_fd+'</span>';
                     }
                  }
                  else {
                     return '<span class="label label-success" id="label-fd-version" data-toggle="tooltip" data-placement="top" title="Up to date">'+row.installed_fd+'</span>';
                  }
               }
            },
            {
               field: 'enabled',
               sortable: true,
               formatter: function(value) {
                  if(value) {
                     return '<span class="label label-success"><?php echo $this->translate("Enabled"); ?></span>';
                  }
                  else {
                     return '<span class="label label-danger"><?php echo $this->translate("Disabled"); ?></span>';
                  }
               }
            },
            {
               field: 'actions',
               formatter: function(value, row, index) {
                  var actions = null;
                  var rlink = '<a class="btn btn-default btn-xs" data-toggle="tooltip" data-placement="top" href="<?php echo $this->basePath() . '/restore/index?client='; ?>'+row.name+'" title="<?php echo $this->translate("Restore"); ?>" id="btn-1"><span class="glyphicon glyphicon-import"></span></a>&nbsp;<a class="btn btn-default btn-xs" data-toggle="tooltip" data-placement="top" href="<?php echo $this->basePath(); ?>/client/status?client='+row.name+'" title="<?php echo $this->translate("Status"); ?>" id="btn-1"><span class="glyphicon glyphicon-zoom-in"></span></a>';
                  var ulink = '<a href="'+row.url_package+'" target="_blank"><button type="button" class="btn btn-default btn-xs" id="btn-1" data-toggle="tooltip" data-placement="top" title="Download update"><span class="glyphicon glyphicon-download-alt"></span></button></a>';
                  if(row.enabled) {
                     actions = rlink + '&nbsp;' + '<a class="btn btn-default btn-xs" data-toggle="tooltip" data-placement="top" href="<?php echo $this->basePath(); ?>/client/index?action=disable&client='+row.name+'" title="<?php echo $this->translate("Disable"); ?>" id="btn-1"><span class="glyphicon glyphicon-remove"></span></a>';
                  }
                  else {
                     actions = rlink + '&nbsp;' + '<a class="btn btn-default btn-xs" data-toggle="tooltip" data-placement="top" href="<?php echo $this->basePath(); ?>/client/index?action=enable&client='+row.name+'" title="<?php echo $this->translate("Enable"); ?>" id="btn-1"><span class="glyphicon glyphicon-ok"></span></a>';
                  }
                  if(row.update_fd === true) {
                     return actions + '&nbsp;' + ulink;
                  }
                  else {
                     return actions;
                  }
               }
            },
            {
               field: 'hf-2',
               visible: false,
            },
            {
               field: 'hf-3',
               visible: false,
            }
         ]
      });
   }

   $(document).ready(function() {

      setDtTextDomain('<?php echo $this->basePath() . '/js/locale'; ?>');
      setDtLocale('<?php echo $_SESSION['bareos']['locale']; ?>');

      var result = '<?php echo str_replace(array("\n","\r"), "", $this->result); ?>';

      if(result.length > 0) {
         $("#modal-001").modal();
      }

      getClients();

   });

</script>

<?php endif; ?>
