<?php
require_once __DIR__ . '/includes/admin_bootstrap.php';

if ($_SERVER['REQUEST_METHOD']==='POST' && isset($_POST['update_stock'])) {
    foreach ($_POST['stock'] as $pid=>$qty) {
        $pdo->prepare("UPDATE products SET stock=? WHERE id=?")->execute([(int)$qty,(int)$pid]);
    }
    redirect(BASE_URL.'/admin/stock.php?saved=1');
}

$filter = $_GET['filter'] ?? 'all';
$where  = match($filter){ 'low'=>'WHERE stock>0 AND stock<=5', 'out'=>'WHERE stock=0', default=>'' };
$products = $pdo->query("SELECT p.*,c.name as cat_name FROM products p LEFT JOIN categories c ON p.category_id=c.id $where ORDER BY p.stock ASC, p.name ASC")->fetchAll();
$lowCount = $pdo->query("SELECT COUNT(*) FROM products WHERE stock>0 AND stock<=5")->fetchColumn();
$outCount = $pdo->query("SELECT COUNT(*) FROM products WHERE stock=0")->fetchColumn();
$total    = $pdo->query("SELECT COUNT(*) FROM products")->fetchColumn();


// ── HTML output starts ──
include __DIR__ . '/includes/admin_header.php';

?>
<h2>স্টক ম্যানেজার</h2>
<?php if(isset($_GET['saved'])): ?><div class="alert alert-success"><i class="fas fa-check-circle"></i> স্টক আপডেট সফল।</div><?php endif; ?>

<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(160px,1fr));gap:14px;margin-bottom:20px;">
    <div class="stat-card"><div class="stat-icon"><i class="fas fa-boxes"></i></div><div class="stat-info"><div class="stat-value"><?=$total?></div><div class="stat-label">মোট পণ্য</div></div></div>
    <div class="stat-card" style="border-left-color:var(--warning);"><div class="stat-icon" style="color:var(--warning);"><i class="fas fa-exclamation-triangle"></i></div><div class="stat-info"><div class="stat-value"><?=$lowCount?></div><div class="stat-label">কম স্টক (≤5)</div></div></div>
    <div class="stat-card" style="border-left-color:var(--danger);"><div class="stat-icon" style="color:var(--danger);"><i class="fas fa-times-circle"></i></div><div class="stat-info"><div class="stat-value"><?=$outCount?></div><div class="stat-label">স্টক শেষ</div></div></div>
</div>

<div class="card">
    <div class="card-header">
        <h3>পণ্যের স্টক</h3>
        <div style="display:flex;gap:8px;">
            <a href="?" class="topbar-btn <?=$filter==='all'?'btn-gold':'btn-gray'?>">সব</a>
            <a href="?filter=low" class="topbar-btn <?=$filter==='low'?'btn-gold':'btn-gray'?>">কম স্টক</a>
            <a href="?filter=out" class="topbar-btn <?=$filter==='out'?'btn-red':'btn-gray'?>">স্টক শেষ</a>
        </div>
    </div>
    <form method="POST">
        <div class="table-wrap">
            <table>
                <thead><tr><th>ছবি</th><th>পণ্য</th><th>ক্যাটেগরি</th><th>মূল্য</th><th>বর্তমান স্টক</th><th>নতুন স্টক</th></tr></thead>
                <tbody>
                <?php foreach($products as $p): ?>
                <tr>
                    <td><?php if($p['image']): ?><img src="<?=UPLOADS_URL.e($p['image'])?>" class="thumb"><?php else: ?><div class="thumb-placeholder"><i class="fas fa-image"></i></div><?php endif; ?></td>
                    <td><strong><?=e($p['name'])?></strong></td>
                    <td><?=e($p['cat_name']??'-')?></td>
                    <td>৳<?=number_format($p['price'],2)?></td>
                    <td><?php if($p['stock']===0||$p['stock']==='0'): ?><span class="badge badge-cancelled">স্টক নেই</span>
                        <?php elseif($p['stock']<=5): ?><span class="badge badge-pending"><?=$p['stock']?> (কম)</span>
                        <?php else: ?><span class="badge badge-active"><?=$p['stock']?></span><?php endif; ?></td>
                    <td><input type="number" class="form-control" name="stock[<?=$p['id']?>]" value="<?=$p['stock']?>" min="0" style="max-width:110px;"></td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <div style="padding:14px 20px;">
            <button type="submit" name="update_stock" class="topbar-btn btn-gold"><i class="fas fa-save"></i> সকল স্টক আপডেট করুন</button>
        </div>
    </form>
</div>
<?php include __DIR__ . '/includes/admin_footer.php'; ?>
