<?php
/**
 * The create 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='createForm' action='<?php echo $this->createLink($flow->module, 'create', 'step=save');?>'>
  <?php $hasChildFields = false;?>
  <?php $hasPrevField   = false;?>

  <?php foreach($fields as $field):?>
  <?php
  if(!$field->show) continue;
  $value = $field->defaultValue;
  if($field->field == $prevField)
  {
      $hasPrevField = true;
      $value        = $prevDataID;
  }
  ?>

  <?php /* Print files. */ ?>
  <?php if($field->field == 'file'):?>
  <div class='control'>
    <?php 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>
    <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>
  </div>

  <?php /* Print other fields. */ ?>
  <?php else:?>
  <div class='control'>
    <label for='<?php echo $field->field;?>'><?php echo $field->name;?></label>
    <?php
    if($field->readonly)
    {
        if(is_array($value))
        {
            foreach($value as $v) echo zget($field->options, $v) . ' ';
        }
        else
        {
            echo zget($field->options, $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'>
  <?php if($prevField && !$hasPrevField) echo html::hidden($prevField, is_array($prevDataID) ? helper::jsonEncode($prevDataID) : $prevDataID);?>
  <button type='button' data-submit='#createForm' class='btn primary'><?php echo $lang->save;?></button>
</div>

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