<?php
$translate = $this->plugin('translate');
$escape = $this->plugin('escapeHtml');

$form->prepare();
$this->htmlElement('body')->appendAttribute('class', 'resource-templates review');
echo $this->pageTitle($import['o:label'], 1, $translate('Resource templates'), $translate('Review'));
?>

<style>
#properties .data-types .tablesaw-cell-content { display: block; }
</style>

<?php echo $this->form()->openTag($form); ?>
<div id="page-actions">
    <button><?php echo $translate('Complete import'); ?></button>
</div>
<?php echo $this->formCollection($form, false); ?>

<?php if (isset($import['o:resource_class'])): ?>
<?php
$class = $import['o:resource_class'];
$hasVocab = isset($class['vocabulary_prefix']);
$hasClass = isset($class['o:id']);
if (!$hasVocab) {
    $errorText = sprintf('Unknown class "%s" and unknown vocabulary "%s": %s%s', $class['label'], $class['vocabulary_label'], $class['vocabulary_namespace_uri'], $class['local_name']);
} elseif (!$hasClass) {
    $errorText = sprintf('Unknown class "%s" in vocabulary "%s": %s:%s', $class['label'], $class['vocabulary_label'], $class['vocabulary_prefix'], $class['local_name']);
}
?>
<div class="class-row" style="background-color:#<?php echo $hasClass ? 'cdffcd' : 'ffe6e6'; ?>;">
    <span class="class-label"><?php echo $translate('Class'); ?></span>
    <span class="class-value"><?php echo $this->escapeHtml($class['label']); ?></span>
</div>
<?php if (!$hasVocab || !$hasClass): ?>
<div style="background-color:#ffe6e6; color:red;"><?php echo $this->escapeHtml($errorText); ?></div>
<?php endif; ?>
<?php endif; ?>

<?php foreach (['o:title_property' => $translate('Resource title'), 'o:description_property' => $translate('Resource description')] as $property => $label): ?>
    <?php if (isset($import[$property])): ?>
        <?php
        $prop = $import[$property];
        $hasVocab = isset($prop['vocabulary_prefix']);
        $hasProp = isset($prop['o:id']);
        if (!$hasVocab):
            $errorText = sprintf('Unknown property "%s" and unknown vocabulary "%s": %s%s', $prop['label'], $prop['vocabulary_label'], $prop['vocabulary_namespace_uri'], $prop['local_name']);
        elseif (!$hasProp):
            $errorText = sprintf('Unknown property "%s" in vocabulary "%s": %s:%s', $prop['label'], $prop['vocabulary_label'], $prop['vocabulary_prefix'], $prop['local_name']);
        else:
            $errorText = null;
        endif;
        ?>
<div class="prop-row" style="background-color:#<?php echo $hasProp ? 'cdffcd' : 'ffe6e6'; ?>;">
    <span class="class-label"><?php echo $escape($label); ?></span>
    <span class="prop-value"><?php echo $escape($prop['label']); ?></span>
</div>
        <?php if ($errorText): ?>
<div style="background-color:#ffe6e6; color:red;"><?php echo $escape($errorText); ?></div>
        <?php endif; ?>
    <?php endif; ?>
<?php endforeach; ?>
<table id="properties" class="tablesaw" data-tablesaw-mode="stack">
<thead>
    <tr>
        <th><?php echo $translate('Original label'); ?></th>
        <th><?php echo $translate('Data types'); ?></th>
        <th><?php echo $translate('Alternate label'); ?></th>
        <th><?php echo $translate('Alternate comment'); ?></th>
        <th><?php echo $translate('Required?'); ?></th>
        <th><?php echo $translate('Private?'); ?></th>
        <th><?php echo $translate('Default language'); ?></th>
    </tr>
</thead>
<tbody>
<?php foreach ($import['o:resource_template_property'] as $key => $property): ?>
<?php
$hasVocab = isset($property['vocabulary_prefix']);
$hasProperty = isset($property['o:property']['o:id']);
if (!$hasVocab) {
    $errorText = sprintf('Unknown property "%s" and unknown vocabulary "%s": %s%s', $property['label'], $property['vocabulary_label'], $property['vocabulary_namespace_uri'], $property['local_name']);
} elseif (!$hasProperty) {
    $errorText = sprintf('Unknown property "%s" in vocabulary "%s": %s:%s', $property['label'], $property['vocabulary_label'], $property['vocabulary_prefix'], $property['local_name']);
}
// Prepare the data types (display the labels, then the select if needed).
if ($hasProperty) {
    if (empty($property['data_types'])):
        $dataTypes = $translate('Default');
    else:
        $dataTypes = '<ul>';
        foreach ($property['data_types'] as $dataType):
            $dataTypes .= '<li>' . (isset($dataType['label']) ? $translate($dataType['label']) : $dataType['name']) . '</li>';
        endforeach;
        $dataTypes .= '</ul>';
        if (count($property['data_types']) !== count($property['o:data_type'])):
            $dataTypes .= $this->dataType()->getSelect(
                sprintf('data_types[%s]', $key),
                isset($property['o:data_type']) ? $property['o:data_type'] : null,
                ['style' => 'width:100%;', 'class' => 'chosen-select', 'multiple' => 'multiple', 'data-placeholder' => $translate('Select data types…')]
            );
        endif;
    endif;
} else {
    $dataTypes = $property['data_type_label'];
}
?>
    <tr style="background-color:#<?php echo $hasProperty ? 'cdffcd;' : 'ffe6e6'; ?>">
        <td><?php echo $escape($property['label']); ?></td>
        <td class="data-types"><?php echo $dataTypes; ?></td>
        <td><?php echo $escape($property['o:alternate_label']); ?></td>
        <td><?php echo $escape($property['o:alternate_comment']); ?></td>
        <td><?php echo $property['o:is_required'] ?  $translate('Yes') : $translate('No'); ?></td>
        <td><?php echo $property['o:is_private'] ? $translate('Yes') : $translate('No'); ?></td>
        <td><?php echo $escape($property['o:default_lang'] ?? ''); ?></td>
    </tr>
    <?php if (!$hasProperty): ?>
    <tr>
        <td colspan="5" style="background-color:#ffe6e6; color:red;"><?php echo $escape($errorText); ?></td>
    </tr>
    <?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>

<?php echo $this->form()->closeTag(); ?>
