<?php
$key = $this->getParam('key', '');
$required = $this->getParam('required', '');
$namespace = $this->getParam('namespace', '');
$collections = $this->getParam('collections', []);
$array = $this->getParam('array', false);
$list = $this->getParam('list', []);
$list = (is_array($list)) ? $list : [];
?>

<?php foreach($list  as $item):
    $collection = (isset($collections[$item])) ? $collections[$item] : null;
    
    if(empty($collection)) {
        continue;
    }

    $rules = $collection->getAttribute('rules', []);
    ?>


    <div data-ls-if="({{<?php echo $this->escape($namespace); ?>}})"
        data-service="database.getDocument"
        data-param-collection-id="<?php echo $this->escape($collection->getId()); ?>"
        data-param-document-id="{{<?php echo $this->escape($namespace); ?>}}"
        data-scope="sdk"
        data-event="load,document-selected-<?php echo $this->escape($collection->getId()); ?>"
        data-name="project-document-preview"
        data-success="default">
        
        <div class="box line margin-bottom-small padding-small fade-bottom">
            <ul>
                <?php foreach($rules as $i => $rule):
                    $collectionLabel = (isset($rule['label'])) ? $rule['label'] : '';
                    $collectionKey = (isset($rule['key'])) ? $rule['key'] : '';

                    if($i === 3) {break;}
                    ?>
                <li class="margin-bottom-small">
                    <p><b><?php echo $this->escape($collectionLabel); ?>: </b> <span data-ls-bind="{{project-document-preview.<?php echo $this->escape($collectionKey); ?>|limit}}" data-unsync="1"></span></p>
                </li>
                <?php endforeach; ?>
            </ul>
            <span class="label tag blue">preview</span>
        </div>
    </div>

    <input
        type="text" data-forms-document-preview="<?php echo $this->escape($collection->getId()); ?>"
        name="<?php echo $this->escape($key); ?>"
        data-ls-bind="{{<?php echo $this->escape($namespace); ?>}}"
        class="margin-bottom" disabled<?php if($required): ?> required<?php endif; ?>>

    <button class="reverse margin-end-small" type="button"
        data-forms-document="open-document-serach-<?php echo $this->escape($collection->getId()); ?>"
        data-search="<?php echo $this->escape($namespace); ?>"><i class="icon-search"></i> <?php echo $this->escape($collection->getAttribute('name')); ?></button>
<?php endforeach; ?>