<?php
/**
 * The operate 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();}
?>
<div class='heading divider'>
  <div class='title'><i class='icon-plus muted'></i> <strong><?php echo $title;?></strong></div>
  <nav class='nav'><a data-dismiss='display'><i class='icon-remove muted'></i></a></nav>
</div>

<form class='content box' data-form-refresh='#page' method='post' id='operateForm' action='<?php echo $this->createLink($flow->module, $action->action, "id={$data->id}");?>'>
  <?php if($action->position == 'menu'):?>
  <div class='control'>
    <label for='dataID'><?php echo $flow->name;?></label>
    <div class='select'><?php echo html::select('dataID', $dataPairs);?></div>
  </div>
  <?php endif;?>

  <?php $hasChildFields = false;?>
  <?php foreach($fields as $field):?>
  <?php if(!$field->show) continue;?>
  <?php $readonly = $field->readonly;?>
  <?php $value    = isset($data->{$field->field}) ? $data->{$field->field} : $field->defaultValue;?>

  <?php /* Print files. */ ?>
  <?php if($field->field == 'file'):?>
  <div class='control'>
    <?php if($readonly)  echo $this->fetch('file', 'printFiles', array('files' => $data->files, 'fieldset' => 'false'));?>
    <?php if(!$readonly) echo $this->fetch('file', 'buildForm', 'fileCount=1');?>
  </div>

  <?php /* Print sub tables. */ ?>
  <?php elseif(isset($childFields[$field->field])):?>
  <?php $hasChildFields = true;?>
  <div class='children' data-child='<?php echo $field->field;?>' class='has-padding gray'>
    <div class='control'><label><?php echo $field->name;?></label></div>
    <?php $datas = isset($childDatas[$field->field]) ? $childDatas[$field->field] : array('');?>
    <?php foreach($datas as $childData):?>
    <div class='row flex-nowrap'>
      <?php foreach($childFields[$field->field] as $childField):?>
      <?php $childValue = isset($childData->{$childField->field}) ? $childData->{$childField->field} : $childField->defaultValue;?>
      <?php if(!$childField->show) continue;?>
      <?php if($childField->field == 'file') continue;?>
      <div class='cell'>
        <div class='control'>
           <?php
           if($readonly or $childField->readonly)
           {
               if(is_array($childValue))
               {
                   foreach($childValue as $v) echo zget($childField->options, $v) . ' ';
               }
               else
               {
                   echo zget($childField->options, $childValue);
               }
               echo html::hidden("children[$field->field][$childField->field][]", $childValue);
           }
           else
           {
               echo $this->flow->buildControl($childField, $childValue, $field->field, $field->field);
           }
           ?>
        </div>
      </div>
      <?php endforeach;?>
      <?php if(!$readonly):?>
      <div class='cell flex-none'>
        <a class='btn child-deleter'><i class='icon-trash text-danger'></i></a>
      </div>
      <?php endif;?>
      <?php echo html::hidden("children[{$field->field}][id][]", $childData->id);?>
    </div>
    <?php endforeach;?>


    <?php /* Add a empty row of sub table. */ ?>
    <?php if(!$readonly):?>
    <div class='row flex-nowrap'>
      <?php foreach($childFields[$field->field] as $childField):?>
      <?php if(!$childField->show) continue;?>
      <?php if($childField->field == 'file') continue;?>
      <div class='cell'>
        <div class='control'>
          <?php echo $this->flow->buildControl($childField, '', $field->field, $field->field, $emptyValue = true);?>
        </div>
      </div>
      <?php endforeach;?>
      <div class='cell flex-none'>
        <a class='btn child-deleter'><i class='icon-trash text-danger'></i></a>
      </div>
      <?php echo html::hidden("children[{$field->field}][id][]");?>
    </div>
    <a class="btn text-primary btn-add-child"><i class="icon-plus"></i>&nbsp; <?php echo $lang->add . ' ' . $field->name;?></a>
    <?php endif;?>
  </div>

  <?php /* Print other fields. */ ?>
  <?php else:?>
  <div class='control'>
    <label for='<?php echo $field->field;?>'><?php echo $field->name;?></label>
    <?php
    if($readonly)
    {
        if(is_array($value))
        {
            foreach($value as $v) echo zget($field->options, $v) . ' ';
        }
        else
        {
            echo zget($field->options, $value);
        }

        echo html::hidden($field->field, $value);
    }
    else
    {
        echo $this->flow->buildControl($field, $value);
    }
    ?>
  </div>
  <?php endif;?>
  <?php endforeach;?>
</form>

<?php /* The table below is used to generate dom when click plus button. */ ?>
<?php if($hasChildFields):?>
<?php foreach($childFields as $childModule => $fields):?>
<div class='template template-<?php echo $childModule;?>'>
  <div class='row flex-nowrap'>
    <?php foreach($fields as $childField):?>
    <?php if(!$childField->show) continue;?>
    <?php if($childField->field == 'file') continue;?>
    <div class='cell'>
      <div class='control'>
        <?php echo $this->flow->buildControl($childField, '', $childModule, $childModule, $emptyValue = true);?>
      </div>
    </div>
    <?php endforeach;?>
    <div class='cell flex-none'>
      <a class='btn child-deleter'><i class='icon-trash text-danger'></i></a>
    </div>
    <?php echo html::hidden("children[{$childModule}][id][]");?>
  </div>
</div>
<?php endforeach;?>
<?php endif;?>

<div class='footer has-padding'>
  <button type='button' data-submit='#operateForm' class='btn primary'><?php echo $lang->save;?></button>
</div>

<script>
$(function()
{
    $form = $('#operateForm').modalForm().listenScroll({container: 'parent'});
})
</script>
