<?php
/**
 * @var string $startDate
 * @var string $endDate
 * @var array $movements
 * @var array $products
 * @var int|null $productId
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-exchange-alt me-2 text-gold"></i>Stock Movement Report</h5>
</div>

<div class="glass-card p-3 mb-4">
    <form class="row g-3 align-items-end" method="GET" action="<?= url('reports/stock-movement') ?>">
        <div class="col-md-3">
            <label class="form-label">Start Date</label>
            <input type="date" name="start_date" class="form-control" value="<?= e($startDate) ?>">
        </div>
        <div class="col-md-3">
            <label class="form-label">End Date</label>
            <input type="date" name="end_date" class="form-control" value="<?= e($endDate) ?>">
        </div>
        <div class="col-md-2">
            <label class="form-label">Product</label>
            <select name="product_id" class="form-select">
                <option value="">All</option>
                <?php foreach ($products as $p): ?>
                    <option value="<?= $p->id ?>" <?= $productId == $p->id ? 'selected' : '' ?>><?= e($p->name) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-4">
            <button type="submit" class="btn btn-gold"><i class="fas fa-search me-1"></i>Generate</button>
            <a href="<?= url('reports/export/stock_movement?start_date=' . $startDate . '&end_date=' . $endDate . '&product_id=' . ($productId ?? '')) ?>" class="btn btn-outline-gold ms-2">
                <i class="fas fa-download me-1"></i>CSV
            </a>
        </div>
    </form>
</div>

<?php
$totalIn = array_sum(array_map(fn($m) => $m->type === 'in' ? $m->quantity : 0, $movements));
$totalOut = array_sum(array_map(fn($m) => $m->type === 'out' ? $m->quantity : 0, $movements));
?>
<div class="row g-4 mb-4">
    <div class="col-md-3">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(25,135,84,0.15);border-color:rgba(25,135,84,0.3);color:#198754;"><i class="fas fa-arrow-down"></i></div>
            <div class="stats-info">
                <span class="stats-label">Stock In</span>
                <span class="stats-value text-success">+<?= $totalIn ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(220,53,69,0.15);border-color:rgba(220,53,69,0.3);color:#dc3545;"><i class="fas fa-arrow-up"></i></div>
            <div class="stats-info">
                <span class="stats-label">Stock Out</span>
                <span class="stats-value text-danger">-<?= $totalOut ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(13,202,240,0.15);border-color:rgba(13,202,240,0.3);color:#0dcaf0;"><i class="fas fa-exchange-alt"></i></div>
            <div class="stats-info">
                <span class="stats-label">Total Movements</span>
                <span class="stats-value"><?= count($movements) ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(200,164,78,0.15);border-color:rgba(200,164,78,0.3);color:var(--gold-primary);"><i class="fas fa-boxes"></i></div>
            <div class="stats-info">
                <span class="stats-label">Net Change</span>
                <span class="stats-value"><?= ($totalIn - $totalOut) >= 0 ? '+' : '' ?><?= $totalIn - $totalOut ?></span>
            </div>
        </div>
    </div>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">Movement Details</h6>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table table-dark table-hover mb-0">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Product</th>
                    <th class="text-center">Type</th>
                    <th class="text-center">Quantity</th>
                    <th>Reference</th>
                    <th>User</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($movements)): ?>
                    <tr><td colspan="6" class="text-center text-muted py-4">No stock movements found</td></tr>
                <?php else: ?>
                    <?php foreach ($movements as $m): ?>
                        <tr>
                            <td><?= formatDateTime($m->created_at ?? '') ?></td>
                            <td><?= e($m->product_name ?? '-') ?></td>
                            <td class="text-center">
                                <?php if ($m->type === 'in'): ?>
                                    <span class="badge bg-success">IN</span>
                                <?php else: ?>
                                    <span class="badge bg-danger">OUT</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-center"><?= $m->quantity ?></td>
                            <td><?= e($m->reference ?? '-') ?></td>
                            <td><?= e($m->user_name ?? '-') ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
