<?php
$translate = $this->plugin('translate');
// Prepare the property queries.
$properties = isset($query['property']) ? $query['property'] : [];
$properties = array_filter($properties, function ($value) {
    return isset($value['text']) ? '' !== trim($value['text']) : true;
});
if (!$properties) {
    $properties[] = [];
}

if (isset($query['search'])) {
    unset($properties[0]['joiner']);
    array_unshift($properties, [
        'property' => '',
        'type' => 'in',
        'text' => $query['search']
    ]);
}

$queryOption = function($value, array $search, $key, $text) {
    $selected = null;
    if (isset($search[$key]) && $value === $search[$key]) {
        $selected = ' selected';
    }
    return sprintf('<option value="%s"%s>%s</option>', $value, $selected, $text);
};
$queryText = function(array $search, $index) {
    $text = isset($search['text']) ? $search['text'] : null;
    return sprintf('<input type="text" class="query-text" name="%s" value="%s" aria-label="%s">',
        $this->escapeHtml("property[$index][text]"),
        $this->escapeHtml($text),
        $this->escapeHtml($this->translate('Query text')));
}
?>

<div id="property-queries" class="field removable multi-value" role="group" aria-labelledby="by-value-label">
    <fieldset class="fieldset">
        <legend id="by-value-label"><?php echo $translate('Search by value'); ?></legend>
        <div class="inputs">
            <?php
            $index = 0;
            foreach ($properties as $property):
            $stem = "property[$index]";
            ?>
            <div class="value input-group">
                <select class="joiner" name="<?php echo $this->escapeHtml($stem . '[joiner]'); ?>">
                    <?php echo $queryOption('and', $property, 'joiner', $translate('AND')); ?>
                    <?php echo $queryOption('or', $property, 'joiner', $translate('OR')); ?>
                </select>
                <?php echo $this->propertySelect([
                    'name' => $stem . '[property]',
                    'attributes' => [
                        'class' => 'query-property',
                        'value' => isset($property['property']) ? $property['property'] : null,
                        'aria-label' => $translate('Property'),
                    ],
                    'options' => [
                        'empty_option' => '[Any Property]', // @translate
                        'apply_templates' => $this->status()->isSiteRequest() ? $this->siteSetting('search_apply_templates') : false,
                    ]
                ]); ?>
                <select class="query-type" name="<?php echo $this->escapeHtml($stem . '[type]'); ?>" aria-label="<?php echo $translate('Query type'); ?>">
                    <?php echo $queryOption('eq', $property, 'type', $translate('is exactly')); ?>
                    <?php echo $queryOption('neq', $property, 'type', $translate('is not exactly')); ?>
                    <?php echo $queryOption('in', $property, 'type', $translate('contains')); ?>
                    <?php echo $queryOption('nin', $property, 'type', $translate('does not contain')); ?>
                    <?php echo $queryOption('res', $property, 'type', $translate('is resource with ID')); ?>
                    <?php echo $queryOption('nres', $property, 'type', $translate('is not resource with ID')); ?>
                    <?php echo $queryOption('ex', $property, 'type', $translate('has any value')); ?>
                    <?php echo $queryOption('nex', $property, 'type', $translate('has no values')); ?>
                </select>
                    <?php echo $queryText($property, $index); ?>
                <div class="input-group-button remove-value">
                    <button type="button" class="o-icon-delete button" aria-label="<?php echo $translate('Remove value'); ?>" title="<?php echo $translate('Remove value'); ?>"></button>
                </div>
            </div>
            <?php
            $index++;
            endforeach;    
            ?>
        </div>
        <a href="#" class="add-value button"><?php echo $translate('Add new value'); ?></a>
    </fieldset>
</div>
