<?php
$canAssignItemsSites = [];
$cannotAssignItemsSites = [];

if ($item) {
    // This is an existing item.

    $sites = $item->sites();
    foreach ($sites as $site) {
        if ($site->userIsAllowed('can-assign-items')) {
            $canAssignItemsSites[] = ['id' => $site->id()];
        } else {
            $cannotAssignItemsSites[$site->id()] = $site;
        }
    }
} else {
    // This is a new item.

    // Get all sites that allow item assignment in site settings.
    $sites = $this->api()->search('sites', ['assign_new_items' => true])->getContent();
    foreach ($sites as $site) {
        if ($site->userIsAllowed('can-assign-items')) {
            $canAssignItemsSites[] = ['id' => $site->id()];
        } else {
            $cannotAssignItemsSites[$site->id()] = $site;
        }
    }

    // Get all sites that the user set as default in user settings.
    $defaultItemSites = $this->userSetting('default_item_sites');
    $sites = is_array($defaultItemSites)
        ? $this->api()->search('sites', ['id' => $defaultItemSites])->getContent()
        : [];
    foreach ($sites as $site) {
        if ($site->userIsAllowed('can-assign-items')) {
            $canAssignItemsSites[] = ['id' => $site->id()];
        } else {
            $cannotAssignItemsSites[$site->id()] = $site;
        }
    }
}

$siteTemplate = '
<tr class="resource-row">
    <td class="data-value" data-row-key="child-search"></td>
    <td class="data-value" data-row-key="resource-email"></td>
    <td>
        <ul class="actions">
            <li>' . $this->hyperlink('', '#', ['class' => 'o-icon-delete', 'title' => $this->translate('Unassign from site')]) . '</li>
        </ul>
        <input type="hidden" name="o:site[]" class="resource-id">
    </td>
</tr>';
?>
<table id="item-sites" data-existing-rows="<?php echo $this->escapeHtml(json_encode($canAssignItemsSites)); ?>" data-row-template="<?php echo $this->escapeHtml($siteTemplate); ?>" data-tablesaw-mode="stack" class="selector-table tablesaw tablesaw-stack <?php echo ($item && (count($sites) > 0)) ? '' : ''; ?>">
    <thead>
    <tr>
        <th><?php echo $this->translate('Title'); ?></th>
        <th><?php echo $this->translate('Owner'); ?></th>
        <th></th>
    </tr>
    </thead>
    <tbody class="resource-rows">
        <?php foreach ($cannotAssignItemsSites as $site): ?>
        <tr class="resource-row">
            <td class="data-value" data-row-key="child-search"><?php echo $this->escapeHtml($site->title()); ?></td>
            <td class="data-value" data-row-key="resource-email"><?php echo $this->escapeHtml($site->owner()->email()); ?></td>
            <td></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<div class="no-resources">
    <p><?php echo $this->translate('This item is part of no sites. Add it to a site using the interface to the right.'); ?></p>
</div>

<button id="site-selector-button" class="mobile-only"><?php echo $this->translate('Assign to site'); ?></button>

<?php echo $this->siteSelector(); ?>

<script>
  Omeka.initializeSelector('#item-sites', '#site-selector');
</script>
