<?php

use Utopia\View;

$collection = $this->getParam('collection', null);
$collections = $this->getParam('collections', []);
$rules = $collection->getAttribute('rules', []);
$key = $this->getParam('key', null);
$type = $this->getParam('type', null);
$parent = $this->getParam('parent', true);
$namespace = $this->getParam('namespace', 'project-document');
$array = $this->getParam('array', false);

?>

<?php if($parent): ?>
<input name="documentId" type="hidden" data-ls-bind="{{router.params.id}}" />
<input name="collectionId" type="hidden" data-ls-bind="{{router.params.collection}}" />
<?php else: ?>
<?php  /*<div class="margin-bottom-small text-size-small" data-ls-if="({{<?php echo $this->escape($namespace); ?>.$id}})">
    <span data-ls-bind="Document #{{<?php echo $this->escape($namespace); ?>.$id}}"></span> &nbsp;
    <a data-ls-attrs="href=/console/database/document?id={{<?php echo $this->escape($namespace); ?>.$id}}&collection={{<?php echo $this->escape($namespace); ?>.$collection}}&project={{router.params.project}}" class="pull-end" target="_blank">Edit in a new window <i class="icon-link-ext"></i></a>
</div>
<div class="margin-bottom-small text-size-small" data-ls-if="(!{{<?php echo $this->escape($namespace); ?>.$id}})">
    Create a new child document
</div> */ ?>
<?php endif; ?>

<fieldset name="<?php echo $this->escape($key); ?>"<?php if(!$array): ?> data-cast-to="object"<?php endif; ?> class="<?php if($type === 'document'): ?> info<?php endif; ?>">
    <?php if(!$parent): ?>
    <input name="$id" type="hidden" data-ls-bind="{{<?php echo $this->escape($namespace); ?>.$id}}" />
    <input name="$collection" type="hidden" data-ls-bind="<?php echo $this->escape($collection->getId()); ?>" />
    <input name="$permissions" type="hidden" data-ls-bind="{{<?php echo $this->escape($namespace); ?>.$permissions}}" data-cast-to="json" />
    <?php endif; ?>

    <ul>
        <?php foreach($rules as $rule):
            $key = $rule['key'] ?? '';
            $label = $rule['label'] ?? '';
            $type = $rule['type'] ?? '';
            $array = $rule['array'] ?? false;
            $required = $rule['required'] ?? false;
            $list = $rule['list'] ?? false;
            $comp = new View(__DIR__.'/rules/'.$type.'.phtml');
            $loop = new View(__DIR__.'/rules/array.phtml');

            $comp
                ->setParam('key', $key)
                ->setParam('array', $array)
                ->setParam('required', $required)
                ->setParam('list', $list)
                ->setParam('namespace', $namespace.'.'.$key)
                ->setParam('collections', $collections)
            ;
                
            $loop
                ->setParam('type', $type)
                ->setParam('key', $key)
                ->setParam('array', $array)
                ->setParam('required', $required)
                ->setParam('list', $list)
                ->setParam('namespace', $namespace.'.'.$key)
                ->setParam('comp', $comp)
                ->setParam('collections', $collections)
            ;
            ?>
        <li>
            <label class="margin-bottom-no<?php if($type === 'document'): ?> margin-top-large<?php endif; ?>"<?php if($parent): ?> data-forms-nav-anchor="<?php echo $this->escape($key); ?>"<?php endif; ?>>
                <?php echo $this->escape($label); ?>

                <?php if($array): ?>
                    <span class="text-size-small text-fade">&nbsp;(Array)</span>
                <?php endif; ?>

                <?php if($required): ?>
                    <div class="text-size-xs text-danger text-fade">&nbsp;required</div>
                <?php endif; ?>
                
                <?php if(!$required): ?>
                    <div class="text-size-xs text-fade">&nbsp;optional</div>
                <?php endif; ?>
            </label>

            <div class="margin-top-small margin-bottom">
                <?php if(!$array): ?>
                    <?php echo $comp->render(); ?>
                <?php else: ?>
                    <?php echo $loop->render(); ?>
                <?php endif; ?>
            </div>
        </li>
        <?php endforeach; ?>
    </ul>
</fieldset>