<?php
/** @var \PortalPeople\DisabledPostcodes\Block\Adminhtml\Postcode\Index $block */
$rows = $block->getRows();
?>
<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= $block->escapeHtml(__('Disabled Delivery Postcodes')) ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__field">
            <a class="action-primary" href="<?= $block->escapeUrl($block->getNewUrl()) ?>">
                <span><?= $block->escapeHtml(__('Add New Rule')) ?></span>
            </a>
        </div>

        <div class="admin__field" style="margin-top: 16px;">
            <form action="<?= $block->escapeUrl($block->getImportUrl()) ?>" method="post" enctype="multipart/form-data">
                <?= $block->getBlockHtml('formkey') ?>
                <label for="import_file"><strong><?= $block->escapeHtml(__('Import CSV/XLSX')) ?></strong></label>
                <input type="file" name="import_file" id="import_file" accept=".csv,.xlsx" required="required"/>
                <label for="store_id" style="margin-left: 12px;"><?= $block->escapeHtml(__('Default Store ID')) ?></label>
                <input type="number" name="store_id" id="store_id" min="0" value="0" style="width: 80px;"/>
                <button type="submit" class="action-secondary" style="margin-left: 8px;">
                    <span><?= $block->escapeHtml(__('Import')) ?></span>
                </button>
            </form>
            <p class="note"><?= $block->escapeHtml(__('Headers: postcode, match_type (area/exact), description, store_id, is_active')) ?></p>
        </div>

        <div class="admin__table-wrapper" style="margin-top: 20px;">
            <table class="data-grid">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>Postcode</th>
                    <th>Type</th>
                    <th>Store ID</th>
                    <th>Active</th>
                    <th>Description</th>
                    <th>Updated</th>
                    <th>Actions</th>
                </tr>
                </thead>
                <tbody>
                <?php if (empty($rows)): ?>
                    <tr>
                        <td colspan="8"><?= $block->escapeHtml(__('No rules found.')) ?></td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($rows as $row): ?>
                        <tr>
                            <td><?= (int)$row['id'] ?></td>
                            <td><?= $block->escapeHtml($row['postcode']) ?></td>
                            <td><?= $block->escapeHtml(strtoupper((string)$row['match_type'])) ?></td>
                            <td><?= (int)$row['store_id'] ?></td>
                            <td><?= !empty($row['is_active']) ? $block->escapeHtml(__('Yes')) : $block->escapeHtml(__('No')) ?></td>
                            <td><?= $block->escapeHtml((string)$row['description']) ?></td>
                            <td><?= $block->escapeHtml((string)$row['updated_at']) ?></td>
                            <td>
                                <a href="<?= $block->escapeUrl($block->getEditUrl((int)$row['id'])) ?>"><?= $block->escapeHtml(__('Edit')) ?></a>
                                |
                                <a href="<?= $block->escapeUrl($block->getDeleteUrl((int)$row['id'])) ?>"
                                   onclick="return confirm('<?= $block->escapeHtmlAttr(__('Delete this rule?')) ?>');">
                                    <?= $block->escapeHtml(__('Delete')) ?>
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>
