<?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) use ($translate) {
    $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($translate('Query text')));
};

$optionsQuery = [];
if ($this->status()->isSiteRequest()) {
    if ($this->siteSetting('vocabulary_scope') === 'sitewide') {
        $optionsQuery['site_id'] = $this->layout()->site->id();
    } elseif ($this->siteSetting('vocabulary_scope') === 'cross-site') {
        $optionsQuery['used'] = true;
    }
}
?>

<div id="property-queries" class="field removable multi-value" role="group" aria-labelledby="by-value-label">
    <div class="field-meta">
        <span id="by-value-label" class="label"><?php echo $translate('Search by value'); ?></span>
        <button type="button" class="add-value o-icon-add button" aria-label="<?php echo $translate('Add new value'); ?>" title="<?php echo $translate('Add new value'); ?>"></button>
    </div>
    <div class="inputs">
        <?php
        $index = 0;
        foreach ($properties as $property):
        $stem = "property[$index]";
        ?>
        <div class="value">
            <select class="joiner" name="<?php echo $this->escapeHtml($stem . '[joiner]'); ?>" aria-label="<?php echo $translate('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' => $property['property'] ?? null,
                    'aria-label' => 'Property', // @translate
                ],
                'options' => [
                    'empty_option' => '[Any Property]', // @translate
                    'apply_templates' => $this->status()->isSiteRequest() ? $this->siteSetting('search_apply_templates') : false,
                    'query' => $optionsQuery,
                ]
            ]); ?>
            <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('sw', $property, 'type', $translate('starts with')); ?>
                <?php echo $queryOption('nsw', $property, 'type', $translate('does not start with')); ?>
                <?php echo $queryOption('ew', $property, 'type', $translate('ends with')); ?>
                <?php echo $queryOption('new', $property, 'type', $translate('does not end with')); ?>
                <?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); ?>
            <button type="button" class="o-icon-delete remove-value button" aria-label="<?php echo $translate('Remove value'); ?>" title="<?php echo $translate('Remove value'); ?>"></button>
        </div>
        <?php
        $index++;
        endforeach;
        ?>
    </div>
</div>
