<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?php
$statusPill = static function (string $status): string {
    $map = ['draft' => 'inactive', 'completed' => 'active', 'cancelled' => 'inactive'];
    $class = $map[$status] ?? 'inactive';
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc(ucfirst($status)) . '</span>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Inventory',
    'workspaceCrumbCurrent' => $transfer['number'],
    'workspaceTitle'        => esc($transfer['number']),
    'workspaceDesc'         => 'Stock transfer from ' . esc($transfer['from_warehouse_name'] ?? '') . ' to ' . esc($transfer['to_warehouse_name'] ?? '') . '.',
]) ?>

<div class="row g-4">
    <div class="col-lg-8">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-right-left"></i>
                <h2 class="enterprise-settings-card__title">Transfer Information</h2>
            </div>
            <dl class="row mb-0 small">
                <dt class="col-sm-4 text-muted">Number</dt>
                <dd class="col-sm-8 fw-semibold"><?= esc($transfer['number']) ?></dd>
                <dt class="col-sm-4 text-muted">Date</dt>
                <dd class="col-sm-8"><?= esc($transfer['transfer_date']) ?></dd>
                <dt class="col-sm-4 text-muted">From</dt>
                <dd class="col-sm-8"><?= esc($transfer['from_warehouse_name'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">To</dt>
                <dd class="col-sm-8"><?= esc($transfer['to_warehouse_name'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($transfer['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Notes</dt>
                <dd class="col-sm-8"><?= esc($transfer['notes'] ?: '—') ?></dd>
            </dl>
        </article>

        <div class="enterprise-data-panel mt-4">
            <div class="enterprise-data-panel__head">
                <h2 class="enterprise-data-panel__title">Lines</h2>
            </div>
            <div class="enterprise-data-panel__body">
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Product</th>
                                <th>SKU</th>
                                <th class="text-end">Quantity</th>
                                <th>Notes</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($lines as $line): ?>
                                <tr>
                                    <td><?= esc($line['product_name'] ?: '—') ?></td>
                                    <td><code><?= esc($line['sku'] ?: '—') ?></code></td>
                                    <td class="text-end"><?= number_format((float) $line['quantity'], 3) ?></td>
                                    <td class="text-muted small"><?= esc($line['notes'] ?: '—') ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (count($lines) === 0): ?>
                                <tr><td colspan="4" class="text-center text-muted py-4">No lines on this transfer.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-bolt"></i>
                <h2 class="enterprise-settings-card__title">Actions</h2>
            </div>
            <div class="d-grid gap-2">
                <?php if ($transfer['status'] === 'draft'): ?>
                    <a href="<?= base_url('inventory/stock-transfers/' . $transfer['id'] . '/edit') ?>" class="btn btn-outline-primary btn-sm">
                        <i class="fa-solid fa-pen me-1"></i> Edit
                    </a>
                    <form action="<?= base_url('inventory/stock-transfers/' . $transfer['id'] . '/complete') ?>" method="post" onsubmit="return confirm('Complete this transfer and update stock balances?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-primary btn-sm w-100">
                            <i class="fa-solid fa-check me-1"></i> Complete Transfer
                        </button>
                    </form>
                    <form action="<?= base_url('inventory/stock-transfers/' . $transfer['id'] . '/cancel') ?>" method="post" onsubmit="return confirm('Cancel this draft transfer?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-secondary btn-sm w-100">
                            <i class="fa-solid fa-ban me-1"></i> Cancel
                        </button>
                    </form>
                    <form action="<?= base_url('inventory/stock-transfers/' . $transfer['id'] . '/delete') ?>" method="post" onsubmit="return confirm('Delete this transfer?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-danger btn-sm w-100">
                            <i class="fa-solid fa-trash me-1"></i> Delete
                        </button>
                    </form>
                <?php endif; ?>
                <a href="<?= base_url('inventory/stock-transfers') ?>" class="btn btn-outline-secondary btn-sm">Back to List</a>
            </div>
        </article>
    </div>
</div>

<?= $this->endSection() ?>
