<?php
require_once __DIR__ . '/../config/auth.php';
require_once __DIR__ . '/../includes/functions.php';
requirePermission('report_view');

$pdo = getDB();
$type = $_GET['type'] ?? '';
$where = "WHERE p.status = 1";
$params = [];
if ($type === 'low') { $where .= " AND p.current_stock <= p.low_stock_qty"; }
if ($type === 'phone') { $where .= " AND p.product_type = 'Phone'"; }
if ($type === 'accessory') { $where .= " AND p.product_type = 'Accessory'"; }
if ($type === 'spare') { $where .= " AND p.product_type = 'Spare Part'"; }

$products = $pdo->query("SELECT p.*, c.category_name as category_name, s.company_name as supplier_name FROM products p LEFT JOIN categories c ON p.category_id = c.id LEFT JOIN suppliers s ON p.supplier_id = s.id $where ORDER BY p.current_stock ASC")->fetchAll();

include __DIR__ . '/../includes/header.php';
include __DIR__ . '/../includes/sidebar.php';
?>
<div class="content-wrapper">
    <div class="container-fluid">
        <h4 class="page-title"><i class="fas fa-boxes me-2"></i>Stock Report</h4>
        <div class="card">
            <div class="card-body">
                <div class="mb-3">
                    <a href="?type=all" class="btn btn-sm <?= $type == '' ? 'btn-primary' : 'btn-outline-primary' ?>">All</a>
                    <a href="?type=low" class="btn btn-sm <?= $type == 'low' ? 'btn-danger' : 'btn-outline-danger' ?>">Low Stock</a>
                    <a href="?type=phone" class="btn btn-sm <?= $type == 'phone' ? 'btn-primary' : 'btn-outline-primary' ?>">Phones</a>
                    <a href="?type=accessory" class="btn btn-sm <?= $type == 'accessory' ? 'btn-success' : 'btn-outline-success' ?>">Accessories</a>
                    <a href="?type=spare" class="btn btn-sm <?= $type == 'spare' ? 'btn-warning' : 'btn-outline-warning' ?>">Spare Parts</a>
                    <button class="btn btn-sm btn-success" onclick="window.print()"><i class="fas fa-print me-1"></i>Print</button>
                </div>
                <div class="table-responsive">
                    <table id="stockReportTable" class="table table-hover datatable">
                        <thead><tr><th>Product</th><th>Type</th><th>Category</th><th>Supplier</th><th>Cost Price</th><th>Selling Price</th><th>Stock</th><th>Alert</th><th>Status</th></tr></thead>
                        <tbody>
                            <?php foreach ($products as $p): ?>
                            <tr class="<?= $p['current_stock'] <= $p['low_stock_qty'] ? 'table-danger' : '' ?>">
                                <td><?= htmlspecialchars($p['product_name']) ?></td>
                                <td><span class="badge bg-<?= $p['product_type'] == 'Phone' ? 'primary' : ($p['product_type'] == 'Accessory' ? 'success' : 'warning') ?>"><?= $p['product_type'] ?></span></td>
                                <td><?= htmlspecialchars($p['category_name'] ?? '-') ?></td>
                                <td><?= htmlspecialchars($p['supplier_name'] ?? '-') ?></td>
                                <td><?= formatCurrency($p['cost_price']) ?></td>
                                <td><?= formatCurrency($p['selling_price']) ?></td>
                                <td><strong><?= $p['current_stock'] ?></strong></td>
                                <td><?= $p['low_stock_qty'] ?></td>
                                <td><?= $p['status'] ? 'Active' : 'Inactive' ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
