<?php
/**
 * The browse mobile view file of flow module of ZDOO.
 *
 * @copyright   Copyright 2009-2020 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
 * @license     ZPL (http://zpl.pub/page/zplv12.html)
 * @author      Gang Liu <liugang@easycorp.ltd>
 * @package     flow
 * @version     $Id$
 * @link        http://www.zdoo.com
 */

if($extView = $this->getExtViewFile(__FILE__)){include $extView; return helper::cd();}

include "../../common/view/m.header.html.php";

$canCreate = commonModel::hasPriv($flow->module, 'create');
$canView   = commonModel::hasPriv($flow->module, 'view');
?>
<div class='heading'>
  <div class='title'>
    <a id='sortTrigger' class='text-right sort-trigger' data-display data-target='#sortPanel' data-backdrop='true'><i class='icon icon-sort'></i>&nbsp;<span class='sort-name'><?php echo $lang->sort;?></span></a>
  </div>
  <?php if($canCreate):?>
  <nav class='nav'>
    <a class='btn primary' data-display='modal' data-placement='bottom' data-remote='<?php echo $this->createLink($flow->module, 'create');?>'>
      <i class='icon icon-plus'> </i> &nbsp;&nbsp;<?php echo $lang->create;?>
    </a>
  </nav>
  <?php endif;?>
</div>

<section id='page' class='section list-with-pager'>
  <?php $refreshUrl = $this->createLink($flow->module, 'browse', "mode=$mode&label=$label&orderBy=$orderBy&recTotal={$pager->recTotal}&recPerPage={$pager->recPerPage}&pageID={$pager->pageID}");?>
  <div class='box' data-page='<?php echo $pager->pageID;?>' data-refresh-url='<?php echo $refreshUrl;?>'>
    <table class='table bordered'>
      <thead>
        <?php $i = 0;?>
        <?php foreach($fields as $field):?>
        <?php if($i > 4) continue;?>
        <?php if(!$field->mobileShow || $field->field == 'actions') continue;?>
        <th class="text-<?php echo $field->position;?>" style="width:<?php echo $field->width ? $field->width . 'px' : '';?>">
          <?php echo $field->name;?>
        </th>
        <?php $i++;?>
        <?php endforeach;?>
      </thead>
      <tbody>
        <?php foreach($dataList as $data):?>
        <?php $viewUrl = $canView ? $this->createLink($flow->module, 'view', "id={$data->id}") : '';?>
        <tr class='text-center' data-url='<?php echo $viewUrl;?>' data-id='<?php echo $data->id;?>'>
          <?php $i = 0;?>
          <?php foreach($fields as $field):?>
          <?php if($i > 4) continue;?>
          <?php if(!$field->mobileShow || $field->field == 'actions') continue;?>
          <td class="text-<?php echo $field->position;?> align-middle">
          <?php 
          if(is_array($data->{$field->field}))
          {
              foreach($data->{$field->field} as $value) echo zget($field->options, $value) . ' ';
          }
          else
          {
              if($field->field == 'id')
              {
                  echo $viewUrl ? html::a($viewUrl, $data->id) : $data->id;
              }
              else
              {
                  echo zget($field->options, $data->{$field->field});
              }
          }
          ?>
          </td>
          <?php $i++;?>
          <?php endforeach;?>
        </tr>
        <?php endforeach;?>
      </tbody>
    </table>
  </div>

  <nav class='nav justify pager'>
    <?php $pager->show($align = 'justify');?>
  </nav>
</section>

<div class='list sort-panel hidden affix enter-from-bottom layer' id='sortPanel'>
  <?php
  $vars = "mode=$mode&label=$label&orderBy=%s&recTotal={$pager->recTotal}&recPerPage={$pager->recPerPage}&pageID={$pager->pageID}";
  foreach($fields as $field)
  {
      if($field->field == 'deleted' or $field->field == 'desc') continue;
      commonModel::printOrderLink($field->field, $orderBy, $vars, '<i class="icon icon-sort-indicator"></i>' . $field->name);
  }
  ?>
</div>
<script>
$(function()
{
    var module = '<?php echo $flow->module;?>';
    var label  = '<?php echo $label;?>';

    if(module)
    {
        var selector = '';

        if(config.requestType == 'GET')
        {
            selector = '[href*="' + config.moduleVar + '=' + module + '&' + config.methodVar + '=' + 'browse&"]';
        }
        else
        {
            selector = '[href*="' + module + '-browse-"]';
        }

        if(selector) $('#appnav > a').removeClass('active').filter(selector).addClass('active');
    }

    if(label)
    {
        var selector = '';

        if(config.requestType == 'GET')
        {
            selector = '[href*="&mode=browse&label=' + label + '"]';
        }
        else
        {
            selector = '[href*="-browse-' + label + '"]';
        }

        if(selector) $('#menu > a').removeClass('active').filter(selector).addClass('active');
    }

    $(document).on('click', '.btn-add-child', function()
    {  
        var child = $(this).parents('.children').data('child');
        $(this).before($('.template-' + child).html());
    });

    $(document).on('click', '.child-deleter', function()
    {  
        if($(this).parents('.children').find('.row').length > 1)
        {
            $(this).closest('.row').remove();
        }
        else
        {
            $(this).closest('.row').find('input,select,textarea').val('');
        }
    });
});
</script>
<?php include "../../common/view/m.footer.html.php";?>
