<?php require __DIR__ . '/settings_nav.php';
$money = static fn ($c) => $c === null ? '—' : number_format((int)$c / 100, 2, ',', ' ') . ' €'; ?>
<div class="mb-3">
    <h2 class="mb-1">💊 <?= h(t('inventory.title')) ?></h2>
    <div class="text-muted"><?= h(t('inventory.intro')) ?></div>
</div>
<?php if ($status): ?><div class="alert alert-success">✅ <?= h(t('inventory.saved')) ?></div>
<?php endif; ?><?php if ($error): ?><div class="alert alert-danger"><?= h($error) ?></div><?php endif; ?>
<?php if ($alerts['low'] || $alerts['expired']): ?><div class="alert alert-warning">
    <strong><?= h(t('inventory.alerts')) ?></strong> · <?= count($alerts['low']) ?> <?= h(t('inventory.low_stock')) ?> ·
    <?= count($alerts['expired']) ?> <?= h(t('inventory.expiring')) ?></div><?php endif; ?>
<div class="row g-3 mb-3">
    <div class="col-xl-4">
        <div class="card h-100">
            <div class="card-header">
                <h3 class="card-title"><?= h(t('inventory.add_product')) ?></h3>
            </div>
            <form method="post" action="/settings/inventory/product"><input type="hidden" name="csrf"
                    value="<?= h(Auth::csrf()) ?>">
                <div class="card-body row g-2">
                    <div class="col-12"><label class="form-label"><?= h(t('inventory.category')) ?></label><select
                            class="form-select" name="category" required>
                            <option value="medication"><?= h(t('inventory.medications')) ?></option>
                            <option value="dewormer"><?= h(t('inventory.dewormers')) ?></option>
                            <option value="vaccine"><?= h(t('inventory.vaccines')) ?></option>
                        </select></div>
                    <div class="col-12"><label class="form-label"><?= h(t('common.name')) ?></label><input
                            class="form-control" name="name" required></div>
                    <div class="col-6"><label class="form-label"><?= h(t('inventory.unit')) ?></label><input
                            class="form-control" name="unit" value="unité" required></div>
                    <div class="col-6"><label class="form-label"><?= h(t('inventory.minimum')) ?></label><input
                            class="form-control" type="number" step="0.01" min="0" name="minimum_quantity" value="0">
                    </div>
                </div>
                <div class="card-footer text-end"><button class="btn btn-primary"><?= h(t('common.add')) ?></button>
                </div>
            </form>
        </div>
    </div>
    <div class="col-xl-4">
        <div class="card h-100">
            <div class="card-header">
                <h3 class="card-title"><?= h(t('inventory.receive')) ?></h3>
            </div>
            <form method="post" action="/settings/inventory/receive"><input type="hidden" name="csrf"
                    value="<?= h(Auth::csrf()) ?>">
                <div class="card-body row g-2">
                    <div class="col-12"><label class="form-label"><?= h(t('inventory.product')) ?></label><select
                            class="form-select" name="product_id" required>
                            <option value="">—</option><?php foreach ($products as $p): ?><option
                                value="<?= (int)$p['id'] ?>"><?= h($p['name'] . ' · ' . $p['category']) ?></option>
                            <?php endforeach; ?>
                        </select></div>
                    <div class="col-6"><label class="form-label"><?= h(t('inventory.quantity')) ?></label><input
                            class="form-control" type="number" min="0.01" step="0.01" name="quantity" required></div>
                    <div class="col-6"><label class="form-label"><?= h(t('inventory.unit_cost')) ?></label><input
                            class="form-control" type="number" min="0" step="0.01" name="unit_cost"></div>
                    <div class="col-6"><label class="form-label"><?= h(t('animal.batch')) ?></label><input
                            class="form-control" name="batch_number"></div>
                    <div class="col-6"><label class="form-label"><?= h(t('inventory.expiry')) ?></label><input
                            class="form-control js-date" type="text" name="expires_on" placeholder="jj/mm/aaaa"
                            autocomplete="off"></div>
                    <div class="col-12"><label class="form-label"><?= h(t('inventory.supplier')) ?></label><select
                            class="form-select" name="supplier_contact_id">
                            <option value="">—</option><?php foreach ($suppliers as $s): ?><option
                                value="<?= (int)$s['id'] ?>"><?= h($s['name']) ?></option><?php endforeach; ?>
                        </select></div>
                </div>
                <div class="card-footer text-end"><button
                        class="btn btn-primary"><?= h(t('inventory.receive')) ?></button></div>
            </form>
        </div>
    </div>
    <div class="col-xl-4">
        <div class="card h-100">
            <div class="card-header">
                <h3 class="card-title"><?= h(t('inventory.remove_stock')) ?></h3>
            </div>
            <form method="post" action="/settings/inventory/move"><input type="hidden" name="csrf"
                    value="<?= h(Auth::csrf()) ?>">
                <div class="card-body row g-2">
                    <div class="col-12"><label class="form-label"><?= h(t('inventory.batch')) ?></label><select
                            class="form-select" name="batch_id" required>
                            <option value="">—</option><?php foreach ($batches as $b) {
                                if ((float)$b['quantity'] > 0): ?><option value="<?= (int)$b['id'] ?>">
                                <?= h($b['product_name'] . ' · ' . ($b['batch_number'] ?: t('inventory.no_batch')) . ' · ' . $b['quantity'] . ' ' . $b['unit']) ?>
                            </option><?php endif;
                            } ?>
                        </select></div>
                    <div class="col-6"><label class="form-label"><?= h(t('inventory.movement')) ?></label><select
                            class="form-select" name="movement_type">
                            <option value="administration"><?= h(t('inventory.administration')) ?></option>
                            <option value="loss"><?= h(t('inventory.loss')) ?></option>
                        </select></div>
                    <div class="col-6"><label class="form-label"><?= h(t('inventory.quantity')) ?></label><input
                            class="form-control" type="number" min="0.01" step="0.01" name="quantity" required></div>
                    <div class="col-12"><label class="form-label"><?= h(t('animal.animal')) ?></label><select
                            class="form-select" name="animal_id">
                            <option value="">—</option><?php foreach ($animals as $a): ?><option
                                value="<?= (int)$a['id'] ?>"><?= h($a['name']) ?></option><?php endforeach; ?>
                        </select></div>
                    <div class="col-12"><label class="form-label"><?= h(t('privacy.reason')) ?></label><input
                            class="form-control" name="reason"></div>
                </div>
                <div class="card-footer text-end"><button
                        class="btn btn-primary"><?= h(t('inventory.record')) ?></button></div>
            </form>
        </div>
    </div>
</div>
<div class="card mb-3">
    <div class="card-header">
        <h3 class="card-title"><?= h(t('inventory.stock')) ?></h3>
    </div>
    <div class="table-responsive">
        <table class="table table-vcenter card-table">
            <thead>
                <tr>
                    <th><?= h(t('inventory.category')) ?></th>
                    <th><?= h(t('inventory.product')) ?></th>
                    <th><?= h(t('inventory.stock')) ?></th>
                    <th><?= h(t('inventory.minimum')) ?></th>
                    <th><?= h(t('inventory.next_expiry')) ?></th>
                </tr>
            </thead>
            <tbody><?php foreach ($products as $p): $low = (float)$p['stock'] <= (float)$p['minimum_quantity']; ?><tr
                    class="<?= $low ? 'table-warning' : '' ?>">
                    <td><?= h(t('inventory.' . $p['category'])) ?></td>
                    <td><strong><?= h($p['name']) ?></strong></td>
                    <td><?= h($p['stock'] . ' ' . $p['unit']) ?></td>
                    <td><?= h($p['minimum_quantity'] . ' ' . $p['unit']) ?></td>
                    <td><?= h($p['next_expiry'] ? date('d/m/Y', strtotime($p['next_expiry'])) : '—') ?></td>
                </tr><?php endforeach; ?><?php if (!$products): ?><tr>
                    <td colspan="5" class="text-center text-muted py-4"><?= h(t('inventory.empty')) ?></td>
                </tr><?php endif; ?></tbody>
        </table>
    </div>
</div>
<div class="card">
    <div class="card-header">
        <h3 class="card-title"><?= h(t('inventory.history')) ?></h3>
    </div>
    <div class="table-responsive">
        <table class="table table-vcenter card-table">
            <thead>
                <tr>
                    <th><?= h(t('common.date')) ?></th>
                    <th><?= h(t('inventory.product')) ?></th>
                    <th><?= h(t('inventory.movement')) ?></th>
                    <th><?= h(t('inventory.quantity')) ?></th>
                    <th><?= h(t('animal.animal')) ?></th>
                    <th><?= h(t('audit.user')) ?></th>
                </tr>
            </thead>
            <tbody><?php foreach ($movements as $m): ?><tr>
                    <td><?= h(date('d/m/Y H:i', strtotime($m['occurred_at']))) ?></td>
                    <td><?= h($m['product_name']) ?><div class="small text-muted"><?= h($m['batch_number'] ?: '—') ?>
                        </div>
                    </td>
                    <td><?= h(t('inventory.' . $m['movement_type'])) ?></td>
                    <td class="<?= (float)$m['quantity_delta'] < 0 ? 'text-danger' : 'text-success' ?>">
                        <?= (float)$m['quantity_delta'] > 0 ? '+' : '' ?><?= h($m['quantity_delta'] . ' ' . $m['unit']) ?>
                    </td>
                    <td><?= h($m['animal_name'] ?: '—') ?></td>
                    <td><?= h($m['actor'] ?: '—') ?></td>
                </tr><?php endforeach; ?></tbody>
        </table>
    </div>
</div>
