<?php

/**
 * @var Mobicms\Render\Template\Template $this
 * @var list<Johncms\Modules\Contacts\Application\DTO\ContactMessageListItemDTO> $items
 * @var string $pagination
 * @var string $current_status
 * @var string $base_url
 * @var int $new_count
 * @var string|null $success_message
 */

$this->layout('system::layout/default');

$filters = [
    ['value' => '', 'title' => __('All')],
    ['value' => 'new', 'title' => __('New')],
    ['value' => 'processed', 'title' => __('Processed')],
];
?>

<?php if (! empty($success_message)): ?>
    <div class="alert alert-success shadow border-0"><?= $this->e($success_message) ?></div>
<?php endif ?>

<div class="mb-3">
    <?php foreach ($filters as $filter): ?>
        <a href="<?= $this->e($base_url . ($filter['value'] !== '' ? '?status=' . $filter['value'] : '')) ?>"
           class="btn btn-sm <?= $current_status === $filter['value'] ? 'btn-primary' : 'btn-outline-primary' ?>">
            <?= $this->e($filter['title']) ?>
            <?php if ($filter['value'] === 'new' && $new_count > 0): ?>
                <span class="badge text-bg-danger"><?= $new_count ?></span>
            <?php endif ?>
        </a>
    <?php endforeach ?>
</div>

<?php if (empty($items)): ?>
    <?= $this->fetch('system::app/alert', ['alert_type' => 'alert-info', 'alert' => __('The list is empty')]) ?>
<?php else: ?>
    <div class="table-responsive">
        <table class="table table-sm align-middle">
            <thead>
            <tr>
                <th><?= __('Date') ?></th>
                <th><?= __('Sender') ?></th>
                <th><?= __('Message') ?></th>
                <th><?= __('Status') ?></th>
                <th></th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($items as $item): ?>
                <tr>
                    <td class="text-nowrap"><?= $this->e($item->createdAt) ?></td>
                    <td>
                        <?= $this->e($item->name) ?>
                        <div class="text-muted small"><?= $this->e($item->email) ?></div>
                    </td>
                    <td>
                        <a href="<?= $this->e($base_url . '/' . $item->id) ?>"><?= $this->e($item->preview) ?></a>
                    </td>
                    <td>
                        <?php if ($item->status === Johncms\Modules\Contacts\Domain\Enums\ContactMessageStatus::New): ?>
                            <span class="badge text-bg-danger"><?= __('New') ?></span>
                        <?php else: ?>
                            <span class="badge text-bg-success"><?= __('Processed') ?></span>
                        <?php endif ?>
                    </td>
                    <td class="text-nowrap">
                        <a href="<?= $this->e($base_url . '/' . $item->id . '/delete') ?>" class="text-danger"><?= __('Delete') ?></a>
                    </td>
                </tr>
            <?php endforeach ?>
            </tbody>
        </table>
    </div>
    <?= $pagination ?>
<?php endif ?>
