<?php
/**
 * The detail 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";

$sessionName = $flow->module . 'List';
$browseLink  = $this->session->$sessionName ? $this->session->$sessionName : $this->createLink($flow->module, 'browse');
?>

<div id='page' class='list-with-actions'>
  <div class='section no-margin'>
    <div class='heading gray'>
      <div class='title'><i class='icon-calendar'> </i><?php echo $flow->name;?> #<?php echo $data->id;?></div>
      <nav class='nav'>
        <a href='<?php echo $browseLink;?>' class='btn primary'><?php echo $lang->goback;?></a>
      </nav>
    </div>
    <div class='box'>
      <table class='table bordered table-detail'>
        <?php $children = array();?>
        <?php foreach($fields as $field):?>
        <?php
            if(!$field->show) continue;
            if($field->field == 'file') continue;
        
            if(isset($childFields[$field->field]))
            {
                $children[$field->field] = $field->name;
                continue;
            }
        ?>
        <tr>
          <td class='w-90px'><?php echo $field->name;?></td>
          <td>
            <?php 
            if(!empty($data->{$field->field}))
            {
                if(is_array($data->{$field->field}))
                {
                    foreach($data->{$field->field} as $value) echo zget($field->options, $value) . ' ';
                }
                else
                {
                    echo zget($field->options, $data->{$field->field});
                }
            }
            ?>
          </td>
        </tr>
        <?php endforeach;?>
        <?php if(!empty($data->files)):?>
        <tr>
          <td><?php echo $lang->file->common;?></td>
          <td><?php echo $this->fetch('file', 'printFiles', array('files' => $data->files, 'fieldset' => 'false'))?></td>
        </tr>
        <?php endif;?>
      </table>
    </div>

    <?php foreach($children as $child => $childName):?>
    <?php if(empty($childDatas[$child])) continue;?>
    <div class='heading gray'>
      <div class='title'><i class='icon-calendar'> </i><?php echo $childName;?></div>
    </div>
    <div class='box'>
      <table class='table bordered'>
        <thead>
          <tr class='text-center'>
            <?php foreach($childFields[$child] as $childField):?>
            <?php if(!$childField->mobileShow or $childField->field == 'file') continue;?>
            <th class='text-center'><?php echo $childField->name;?></th>
            <?php endforeach;?>
          </tr>
        </thead>
        <tbody>
          <?php foreach($childDatas[$child] as $childData):?>
          <tr class='text-center'>
            <?php foreach($childFields[$child] as $childField):?>
            <?php if(!$childField->mobileShow or $childField->field == 'file') continue;?>
            <td>
              <?php 
              if(strpos(',date,datetime,', ",$childField->control,") !== false)
              {
                  echo formatTime($childData->{$childField->field});
              }
              else
              {
                  echo zget($childField->options, $childData->{$childField->field});
              }
              ?>
            </td>
            <?php endforeach;?>
          </tr>
          <?php endforeach;?>
        </tbody>
      </table>
    </div>
    <?php endforeach;?>
  </div>

  <div class='section' id='history'>
    <?php echo $this->fetch('action', 'history', "objectType={$flow->module}&objectID={$data->id}");?>
  </div>

  <nav class='nav affix dock-bottom nav-auto footer-actions'>
    <?php echo $this->flow->buildOperateMenu($flow, $data, $type = 'view');?>
  </nav>
</div>
<script>
$(function()
{
    var module = '<?php echo $flow->module;?>';

    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');
    }

    $(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";?>
