<?php
require_once __DIR__ . '/../includes/auth.php';
requireLogin();
requirePermission('replacements.manage');

require_once __DIR__ . '/../config/database.php';

/*
|--------------------------------------------------------------------------
| FETCH REPLACEMENT HISTORY
|--------------------------------------------------------------------------
*/
$stmt = $pdo->query("
    SELECT rr.*,
           old.meter_number AS old_meter_number,
           new.meter_number AS new_meter_number,
           req.email AS requested_by_email,
           appr.email AS approved_by_email
    FROM replacement_requests rr
    JOIN meters old ON rr.old_meter_id = old.id
    LEFT JOIN meters new ON rr.new_meter_id = new.id
    LEFT JOIN users req ON rr.requested_by_user_id = req.id
    LEFT JOIN users appr ON rr.approved_by_user_id = appr.id
    ORDER BY rr.created_at DESC
");

$replacements = $stmt->fetchAll(PDO::FETCH_ASSOC);

/* ===== LOAD MASTER LAYOUT ===== */
require_once __DIR__ . '/../includes/header.php';
require_once __DIR__ . '/../includes/sidebar.php';
?>

<div class="main-content">

    <div class="page-header">
        <h2>Replacement History</h2>
        <p class="page-subtitle">
            Complete lifecycle of all meter replacement requests.
        </p>
    </div>

    <div class="table-wrap">
        <div class="table-responsive">
            <table>
                <thead>
                    <tr>
                        <th>Old Meter</th>
                        <th>New Meter</th>
                        <th>Reason</th>
                        <th>Status</th>
                        <th>Requested By</th>
                        <th>Approved By</th>
                        <th>Approved At</th>
                        <th>Created At</th>
                    </tr>
                </thead>
                <tbody>

                <?php if (empty($replacements)): ?>
                    <tr>
                        <td colspan="8">No replacement records found.</td>
                    </tr>
                <?php endif; ?>

                <?php foreach ($replacements as $r): ?>
                    <tr>
                        <td><?= htmlspecialchars($r['old_meter_number']) ?></td>
                        <td><?= htmlspecialchars($r['new_meter_number'] ?? '-') ?></td>
                        <td><?= htmlspecialchars($r['reason']) ?></td>

                        <td>
                            <span class="status <?= htmlspecialchars($r['status']) ?>">
                                <?= htmlspecialchars($r['status']) ?>
                            </span>
                        </td>

                        <td><?= htmlspecialchars($r['requested_by_email'] ?? '-') ?></td>
                        <td><?= htmlspecialchars($r['approved_by_email'] ?? '-') ?></td>
                        <td><?= htmlspecialchars($r['approved_at'] ?? '-') ?></td>
                        <td><?= htmlspecialchars($r['created_at']) ?></td>
                    </tr>
                <?php endforeach; ?>

                </tbody>
            </table>
        </div>
    </div>

</div>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>
