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

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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Inventory',
    'workspaceCrumbCurrent' => $count['number'],
    'workspaceTitle'        => esc($count['number']),
    'workspaceDesc'         => 'Physical count at ' . esc($count['warehouse_name'] ?? '') . ' on ' . esc($count['count_date']) . '.',
]) ?>

<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-clipboard-check"></i>
                <h2 class="enterprise-settings-card__title">Count 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($count['number']) ?></dd>
                <dt class="col-sm-4 text-muted">Date</dt>
                <dd class="col-sm-8"><?= esc($count['count_date']) ?></dd>
                <dt class="col-sm-4 text-muted">Warehouse</dt>
                <dd class="col-sm-8"><?= esc($count['warehouse_name'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($count['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Notes</dt>
                <dd class="col-sm-8"><?= esc($count['notes'] ?: '—') ?></dd>
                <?php if ($count['status'] === 'completed' && ! empty($count['completed_at'])): ?>
                    <dt class="col-sm-4 text-muted">Completed</dt>
                    <dd class="col-sm-8"><?= esc($count['completed_at']) ?></dd>
                <?php endif; ?>
            </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">System</th>
                                <th class="text-end">Counted</th>
                                <th class="text-end">Variance</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['system_qty'], 3) ?></td>
                                    <td class="text-end"><?= $line['counted_qty'] !== null && $line['counted_qty'] !== '' ? number_format((float) $line['counted_qty'], 3) : '—' ?></td>
                                    <td class="text-end fw-semibold"><?= $line['variance'] !== null ? number_format((float) $line['variance'], 3) : '—' ?></td>
                                    <td class="text-muted small"><?= esc($line['notes'] ?: '—') ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (count($lines) === 0): ?>
                                <tr><td colspan="6" class="text-center text-muted py-4">No lines on this count.</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 (in_array($count['status'], ['draft', 'in_progress'], true)): ?>
                    <a href="<?= base_url('inventory/stock-counts/' . $count['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-counts/' . $count['id'] . '/complete') ?>" method="post" onsubmit="return confirm('Complete this count and apply stock adjustments?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-primary btn-sm w-100">
                            <i class="fa-solid fa-check me-1"></i> Complete Count
                        </button>
                    </form>
                    <form action="<?= base_url('inventory/stock-counts/' . $count['id'] . '/cancel') ?>" method="post" onsubmit="return confirm('Cancel this stock count?');">
                        <?= 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-counts/' . $count['id'] . '/delete') ?>" method="post" onsubmit="return confirm('Delete this stock count?');">
                        <?= 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-counts') ?>" class="btn btn-outline-secondary btn-sm">Back to List</a>
            </div>
        </article>
    </div>
</div>

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