<a href="<?= Router::url('delivery') ?>" style="font-size:13px;color:var(--color-text-muted);"><i class="fa-solid fa-arrow-left"></i> Back to Delivery Board</a>

<div style="display:flex;justify-content:space-between;align-items:center;margin:14px 0 16px;flex-wrap:wrap;gap:10px;">
    <div style="color:var(--color-text-muted);font-size:13.5px;"><i class="fa-solid fa-location-dot"></i> Areas covered for online delivery orders</div>
    <?php if (Auth::can('delivery', 'edit')): ?>
    <button type="button" class="btn btn-primary" onclick="rmsOpenModal('rmsAddZoneModal')"><i class="fa-solid fa-plus"></i> Add Delivery Zone</button>
    <?php endif; ?>
</div>

<div class="panel" style="padding:0;overflow-x:auto;">
<table class="data-table">
<thead><tr><th>Zone Name</th><th>Radius</th><th>Delivery Fee</th><th>Orders</th><th>Status</th><th></th></tr></thead>
<tbody>
<?php foreach ($zones as $z): ?>
<tr>
    <td><strong><?= e($z['zone_name']) ?></strong></td>
    <td><?= number_format((float) $z['radius_km'], 1) ?> km</td>
    <td><?= $z['is_free_delivery'] ? '<span class="badge badge-green"><i class="fa-solid fa-gift"></i> Free Delivery</span>' : formatMoney((float) $z['delivery_fee']) ?></td>
    <td><span class="badge badge-blue"><?= (int) $z['order_count'] ?></span></td>
    <td><?= $z['is_active'] ? '<span class="badge badge-green">Active</span>' : '<span class="badge badge-gray">Inactive</span>' ?></td>
    <td style="white-space:nowrap;">
        <?php if (Auth::can('delivery', 'edit')): ?>
        <button type="button" class="icon-btn icon-btn-gray" onclick="rmsOpenModal('rmsEditZoneModal<?= $z['id'] ?>')" title="Edit"><i class="fa-solid fa-pen"></i></button>
        <form method="POST" action="<?= Router::url('delivery/zones/toggle') ?>" style="display:inline;">
            <?= $csrf ?>
            <input type="hidden" name="id" value="<?= $z['id'] ?>">
            <button type="submit" class="icon-btn <?= $z['is_active'] ? 'icon-btn-red' : 'icon-btn-blue' ?>" title="<?= $z['is_active'] ? 'Deactivate' : 'Activate' ?>"><i class="fa-solid <?= $z['is_active'] ? 'fa-ban' : 'fa-check' ?>"></i></button>
        </form>
        <?php endif; ?>
        <?php if (Auth::can('delivery', 'delete')): ?>
        <form method="POST" action="<?= Router::url('delivery/zones/delete') ?>" style="display:inline;" onsubmit="return confirm('Delete this zone? If it has order history, it will be deactivated instead.');">
            <?= $csrf ?>
            <input type="hidden" name="id" value="<?= $z['id'] ?>">
            <button type="submit" class="icon-btn icon-btn-red"><i class="fa-solid fa-trash"></i></button>
        </form>
        <?php endif; ?>
    </td>
</tr>
<?php endforeach; ?>
<?php if (empty($zones)): ?>
<tr><td colspan="6" style="text-align:center;color:var(--color-text-muted);padding:30px;">No delivery zones yet.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>

<!-- Add Zone Modal -->
<?php if (Auth::can('delivery', 'edit')): ?>
<div class="rms-modal-overlay" id="rmsAddZoneModal">
    <div class="rms-modal" style="max-width:380px;">
        <div class="rms-modal-head">
            <h3><i class="fa-solid fa-location-dot" style="color:var(--color-teal);margin-right:6px;"></i>Add Delivery Zone</h3>
            <button type="button" class="rms-modal-close" onclick="rmsCloseModal('rmsAddZoneModal')"><i class="fa-solid fa-xmark"></i></button>
        </div>
        <form method="POST" action="<?= Router::url('delivery/zones/store') ?>">
            <?= $csrf ?>
            <div class="form-group"><label>Zone Name</label><input type="text" name="zone_name" class="form-control" placeholder="e.g. Anuradhapura Town" required></div>
            <div class="form-group"><label>Coverage Radius (km)</label><input type="number" step="0.1" name="radius_km" class="form-control" value="5"></div>
            <div class="form-group">
                <label style="display:flex;align-items:center;gap:8px;">
                    <input type="checkbox" name="is_free_delivery" value="1" checked onchange="document.getElementById('rmsAddFeeGroup').style.display = this.checked ? 'none' : 'block';">
                    Free Delivery in this zone
                </label>
            </div>
            <div class="form-group" id="rmsAddFeeGroup" style="display:none;">
                <label>Delivery Fee (Rs.)</label>
                <input type="number" step="0.01" name="delivery_fee" class="form-control" value="0">
            </div>
            <button type="submit" class="btn btn-primary" style="width:100%;"><i class="fa-solid fa-plus"></i> Add Zone</button>
        </form>
    </div>
</div>
<?php endif; ?>

<!-- Edit Zone Modals -->
<?php foreach ($zones as $z): if (!Auth::can('delivery', 'edit')) continue; ?>
<div class="rms-modal-overlay" id="rmsEditZoneModal<?= $z['id'] ?>">
    <div class="rms-modal" style="max-width:380px;">
        <div class="rms-modal-head">
            <h3><i class="fa-solid fa-pen" style="color:var(--color-teal);margin-right:6px;"></i>Edit — <?= e($z['zone_name']) ?></h3>
            <button type="button" class="rms-modal-close" onclick="rmsCloseModal('rmsEditZoneModal<?= $z['id'] ?>')"><i class="fa-solid fa-xmark"></i></button>
        </div>
        <form method="POST" action="<?= Router::url('delivery/zones/update') ?>">
            <?= $csrf ?>
            <input type="hidden" name="id" value="<?= $z['id'] ?>">
            <div class="form-group"><label>Zone Name</label><input type="text" name="zone_name" class="form-control" value="<?= e($z['zone_name']) ?>" required></div>
            <div class="form-group"><label>Coverage Radius (km)</label><input type="number" step="0.1" name="radius_km" class="form-control" value="<?= e((string) $z['radius_km']) ?>"></div>
            <div class="form-group">
                <label style="display:flex;align-items:center;gap:8px;">
                    <input type="checkbox" name="is_free_delivery" value="1" <?= $z['is_free_delivery'] ? 'checked' : '' ?> onchange="document.getElementById('rmsEditFeeGroup<?= $z['id'] ?>').style.display = this.checked ? 'none' : 'block';">
                    Free Delivery in this zone
                </label>
            </div>
            <div class="form-group" id="rmsEditFeeGroup<?= $z['id'] ?>" style="<?= $z['is_free_delivery'] ? 'display:none;' : '' ?>">
                <label>Delivery Fee (Rs.)</label>
                <input type="number" step="0.01" name="delivery_fee" class="form-control" value="<?= e((string) $z['delivery_fee']) ?>">
            </div>
            <button type="submit" class="btn btn-primary" style="width:100%;"><i class="fa-solid fa-floppy-disk"></i> Save Changes</button>
        </form>
    </div>
</div>
<?php endforeach; ?>
