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

$pdo = getDB();
$from = $_GET['from'] ?? date('Y-m-01');
$to = $_GET['to'] ?? date('Y-m-d');

$stmt = $pdo->prepare("
    SELECT rj.*, c.name as customer_name, u.name as technician_name
    FROM repair_jobs rj
    LEFT JOIN customers c ON rj.customer_id = c.id
    LEFT JOIN users u ON rj.assigned_technician_id = u.id
    WHERE DATE(rj.created_at) BETWEEN ? AND ?
    ORDER BY rj.id DESC
");
$stmt->execute([$from, $to]);
$repairs = $stmt->fetchAll();

$stmt = $pdo->prepare("SELECT COUNT(*) as total, COALESCE(SUM(final_amount),0) as final_amount, COALESCE(SUM(paid_amount),0) as paid FROM repair_jobs WHERE DATE(created_at) BETWEEN ? AND ?");
$stmt->execute([$from, $to]);
$summary = $stmt->fetch();

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-tools me-2"></i>Repair Report</h4>
        <div class="card">
            <div class="card-body">
                <form method="GET" class="row g-3 mb-3">
                    <div class="col-md-4"><input type="date" name="from" class="form-control" value="<?= $from ?>"></div>
                    <div class="col-md-4"><input type="date" name="to" class="form-control" value="<?= $to ?>"></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="row mb-3">
                    <div class="col-md-4"><div class="stat-card bg-primary-gradient"><div class="stat-value"><?= $summary['total'] ?></div><div class="stat-label">Total Jobs</div></div></div>
                    <div class="col-md-4"><div class="stat-card bg-success-gradient"><div class="stat-value"><?= formatCurrency($summary['final_amount']) ?></div><div class="stat-label">Total Value</div></div></div>
                    <div class="col-md-4"><div class="stat-card bg-info-gradient"><div class="stat-value"><?= formatCurrency($summary['paid']) ?></div><div class="stat-label">Total Collected</div></div></div>
                </div>
                <div class="table-responsive">
                    <table id="repairReportTable" class="table table-hover datatable">
                        <thead><tr><th>Job #</th><th>Customer</th><th>Device</th><th>Technician</th><th>Parts Cost</th><th>Labour</th><th>Total</th><th>Paid</th><th>Status</th></tr></thead>
                        <tbody>
                            <?php foreach ($repairs as $rj): ?>
                            <tr>
                                <td><?= htmlspecialchars($rj['job_no']) ?></td>
                                <td><?= htmlspecialchars($rj['customer_name'] ?? '-') ?></td>
                                <td><?= htmlspecialchars($rj['device_brand'] ?? '') ?> <?= htmlspecialchars($rj['device_model'] ?? '') ?></td>
                                <td><?= htmlspecialchars($rj['technician_name'] ?? '-') ?></td>
                                <td><?= formatCurrency($rj['parts_total']) ?></td>
                                <td><?= formatCurrency($rj['labour_charge']) ?></td>
                                <td><?= formatCurrency($rj['final_amount']) ?></td>
                                <td><?= formatCurrency($rj['paid_amount']) ?></td>
                                <td><?= getStatusBadge($rj['repair_status']) ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
