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

$pdo = getDB();
$status = $_GET['status'] ?? '';
$where = '';
$params = [];
if ($status) { $where = "WHERE pi.status = ?"; $params[] = $status; }

$imei_list = $pdo->prepare("SELECT pi.*, p.product_name as product_name, p.model, si.invoice_no, si.invoice_date as sale_date FROM product_imei pi JOIN products p ON pi.product_id = p.id LEFT JOIN sales_invoice_items sii ON pi.sales_invoice_item_id = sii.id LEFT JOIN sales_invoices si ON sii.sales_invoice_id = si.id $where ORDER BY pi.id DESC LIMIT 500");
$imei_list->execute($params);
$imei_list = $imei_list->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-microchip me-2"></i>IMEI History Report</h4>
        <div class="card">
            <div class="card-body">
                <form method="GET" class="row g-3 mb-3">
                    <div class="col-md-4">
                        <select name="status" class="form-select">
                            <option value="">All Status</option>
                            <option value="Available" <?= $status == 'Available' ? 'selected' : '' ?>>Available</option>
                            <option value="Sold" <?= $status == 'Sold' ? 'selected' : '' ?>>Sold</option>
                            <option value="Returned" <?= $status == 'Returned' ? 'selected' : '' ?>>Returned</option>
                            <option value="Damaged" <?= $status == 'Damaged' ? 'selected' : '' ?>>Damaged</option>
                            <option value="Warranty Claim" <?= $status == 'Warranty Claim' ? 'selected' : '' ?>>Warranty Claim</option>
                        </select>
                    </div>
                    <div class="col-md-4"><button type="submit" class="btn btn-primary"><i class="fas fa-filter me-1"></i>Filter</button></div>
                </form>
                <div class="table-responsive">
                    <table id="imeiReportTable" class="table table-hover datatable">
                        <thead><tr><th>IMEI</th><th>Product</th><th>Model</th><th>Invoice</th><th>Cost Price</th><th>Selling Price</th><th>Status</th><th>Sale Date</th></tr></thead>
                        <tbody>
                            <?php foreach ($imei_list as $i): ?>
                            <tr>
                                <td><code><?= htmlspecialchars($i['imei_number']) ?></code></td>
                                <td><?= htmlspecialchars($i['product_name']) ?></td>
                                <td><?= htmlspecialchars($i['model'] ?? '-') ?></td>
                                <td><?= htmlspecialchars($i['invoice_no'] ?? '-') ?></td>
                                <td><?= formatCurrency($i['cost_price']) ?></td>
                                <td><?= formatCurrency($i['selling_price']) ?></td>
                                <td><?= getStatusBadge($i['status']) ?></td>
                                <td><?= $i['sale_date'] ? formatDate($i['sale_date']) : '-' ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
