<?= $this->extend('admin/layout/main') ?>
<?= $this->section('content') ?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h3><?= lang('App.products') ?></h3>
    <a href="/admin/products/create" class="btn btn-dark btn-sm"><?= lang('App.add_new') ?></a>
</div>
<div class="card p-3">
<table class="table">
    <thead><tr>
        <th><?= lang('App.name') ?></th><th>سعر الشراء</th><th>سعر البيع</th><th>الهامش</th><th><?= lang('App.status') ?></th><th><?= lang('App.actions') ?></th>
    </tr></thead>
    <tbody>
    <?php foreach ($products as $p):
        $margin = (float) $p['price'] - (float) $p['cost_price'];
        $marginPct = $p['price'] > 0 ? round($margin / $p['price'] * 100, 1) : 0;
    ?>
        <tr>
            <td><?= esc($p['name']) ?></td>
            <td><?= number_format($p['cost_price'], 2) ?> <?= esc($p['currency']) ?></td>
            <td><?= number_format($p['price'], 2) ?> <?= esc($p['currency']) ?></td>
            <td class="<?= $margin >= 0 ? 'text-success' : 'text-danger' ?>">
                <?= number_format($margin, 2) ?> (<?= $marginPct ?>%)
            </td>
            <td><?= $p['is_active'] ? '✅' : '❌' ?></td>
            <td>
                <a href="/admin/products/<?= $p['id'] ?>/edit" class="btn btn-sm btn-outline-primary"><?= lang('App.edit') ?></a>
                <form method="post" action="/admin/products/<?= $p['id'] ?>/delete" class="d-inline" onsubmit="return confirm('?')">
                    <?= csrf_field() ?><button class="btn btn-sm btn-outline-danger"><?= lang('App.delete') ?></button>
                </form>
            </td>
        </tr>
    <?php endforeach; ?>
    <?php if (empty($products)): ?><tr><td colspan="6" class="text-center text-muted"><?= lang('App.no_data') ?></td></tr><?php endif; ?>
    </tbody>
</table>
</div>
<?= $this->endSection() ?>
