<?php
require_once __DIR__ . '/core/auth.php';
$user = require_login();
$pdo = pdo();
$reportId = (int) ($_GET['id'] ?? 0);
if ($reportId < 1) {
    redirect('reports.php');
}
require_report_access($pdo, $user, $reportId);
$bundle = fetch_report_bundle($pdo, $reportId);
$report = $bundle['report'];
if (!$report) {
    redirect('reports.php');
}
$progressMap = [];
foreach ($bundle['progress'] as $progress) {
    $progressMap[(int) $progress['form_no']] = $progress['saved_at'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Report #<?= (int) $report['id'] ?> - <?= e(app_name()) ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/app.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-lg bg-white border-bottom sticky-top">
    <div class="container-fluid px-3">
        <a class="navbar-brand fw-bold" href="dashboard.php"><?= e(app_name()) ?></a>
        <div class="d-flex gap-2">
            <a href="report_wizard.php?id=<?= (int) $report['id'] ?>" class="btn btn-primary btn-sm">Edit</a>
            <a href="reports.php" class="btn btn-outline-secondary btn-sm">Back</a>
        </div>
    </div>
</nav>
<div class="container py-4">
    <div class="card shadow-sm border-0 mb-4">
        <div class="card-body">
            <div class="d-flex justify-content-between flex-wrap gap-2 align-items-start">
                <div>
                    <h1 class="h3 mb-1">Report #<?= (int) $report['id'] ?> - <?= e($report['agent_name']) ?></h1>
                    <div class="text-muted">Visit type: <?= e($report['visited_type']) ?> | Status: <?= e(ucfirst($report['status'])) ?></div>
                </div>
                <div class="text-end small text-muted">
                    <div>Started: <?= e($report['started_at'] ?: '-') ?></div>
                    <div>Ended: <?= e($report['ended_at'] ?: '-') ?></div>
                    <div>GPS Start: <?= e(($report['start_lat'] ?: '-') . ', ' . ($report['start_lng'] ?: '-')) ?></div>
                    <div>GPS End: <?= e(($report['end_lat'] ?: '-') . ', ' . ($report['end_lng'] ?: '-')) ?></div>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <?php for ($i = 1; $i <= 8; $i++): ?>
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body p-3">
                    <div class="fw-semibold">Form <?= sprintf('%02d', $i) ?></div>
                    <div class="small text-muted"><?= isset($progressMap[$i]) ? 'Saved at ' . e($progressMap[$i]) : 'Pending' ?></div>
                </div>
            </div>
        </div>
        <?php endfor; ?>
    </div>

    <div class="card shadow-sm border-0 mb-3">
        <div class="card-header bg-white"><h2 class="h5 mb-0">Form 01 - Visit Details</h2></div>
        <div class="card-body row g-3">
            <div class="col-md-3"><strong>Date of Visit</strong><div><?= e($report['date_of_visit']) ?></div></div>
            <div class="col-md-3"><strong>DB Name</strong><div><?= e($report['agent_name']) ?></div></div>
            <div class="col-md-3"><strong>Accompanied By</strong><div><?= e($report['accompanied_by']) ?></div></div>
            <div class="col-md-3"><strong>Visit Type</strong><div><?= e($report['visited_type']) ?></div></div>
        </div>
    </div>

    <div class="card shadow-sm border-0 mb-3">
        <div class="card-header bg-white"><h2 class="h5 mb-0">Form 02 - DB Profile</h2></div>
        <div class="card-body row g-3">
            <?php $profile = $bundle['db_profile']; ?>
            <div class="col-md-4"><strong>Owner / Contact</strong><div><?= e($profile['owner_contact'] ?? '-') ?></div></div>
            <div class="col-md-4"><strong>Coverage Area</strong><div><?= e($profile['coverage_area'] ?? '-') ?></div></div>
            <div class="col-md-4"><strong>Route Strength</strong><div><?= e($profile['route_strength'] ?? '-') ?></div></div>
            <div class="col-md-4"><strong>Vehicle Availability</strong><div><?= e($profile['vehicle_availability'] ?? '-') ?></div></div>
            <div class="col-md-4"><strong>Log Book Update</strong><div><?= e($profile['log_book_updated'] ?? '-') ?></div></div>
            <div class="col-md-4"><strong>Route Plan</strong><div><?= e($profile['route_plan'] ?? '-') ?></div></div>
            <div class="col-md-3"><strong>No. Credit Bills</strong><div><?= e($profile['no_credit_bills'] ?? '0') ?></div></div>
            <div class="col-md-3"><strong>Credit Bill Total</strong><div><?= e($profile['credit_bill_total'] ?? '0') ?></div></div>
            <div class="col-md-3"><strong>No. Cheques</strong><div><?= e($profile['no_cheques'] ?? '0') ?></div></div>
            <div class="col-md-3"><strong>Cheque Total</strong><div><?= e($profile['cheque_total'] ?? '0') ?></div></div>
            <div class="col-md-3"><strong>Cash + Bank Total</strong><div><?= e($profile['cash_bank_total'] ?? '0') ?></div></div>
            <div class="col-md-3"><strong>Progress Sheet</strong><div><?= e($profile['progress_sheet_updated'] ?? '-') ?></div></div>
            <div class="col-md-3"><strong>SKU Sales Update</strong><div><?= e($profile['sku_sales_update'] ?? '-') ?></div></div>
            <div class="col-md-3"><strong>Store Size</strong><div><?= e(($profile['store_length'] ?? '-') . ' x ' . ($profile['store_width'] ?? '-')) ?></div></div>
            <div class="col-md-6"><strong>Store Condition</strong><div><?= e($profile['store_condition'] ?? '-') ?></div></div>
            <div class="col-md-6"><strong>Market Return Condition</strong><div><?= e($profile['market_return_condition'] ?? '-') ?></div></div>
            <div class="col-md-6"><strong>Comments</strong><div><?= e($profile['store_comments'] ?? '-') ?></div></div>
            <div class="col-md-6"><strong>Route Map</strong><div>
                <?php if (!empty($profile['territory_route_map_file'])): ?>
                    <a href="<?= e($profile['territory_route_map_file']) ?>" target="_blank">Open image</a>
                <?php else: ?>-
                <?php endif; ?>
            </div></div>
        </div>
    </div>

    <div class="card shadow-sm border-0 mb-3">
        <div class="card-header bg-white"><h2 class="h5 mb-0">Form 03 - Sales Snapshot</h2></div>
        <div class="table-responsive">
            <table class="table table-sm mb-0">
                <thead class="table-light"><tr><th>Category</th><th>Target</th><th>Achievement</th><th>Variance</th><th>Status</th></tr></thead>
                <tbody>
                <?php foreach ($bundle['sales_snapshot'] as $row): ?>
                    <tr>
                        <td><?= e($row['category_name']) ?></td>
                        <td><?= e($row['mtd_target']) ?></td>
                        <td><?= e($row['mtd_achievement']) ?></td>
                        <td><?= e($row['variance']) ?></td>
                        <td><?= e($row['status_remarks']) ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="card shadow-sm border-0 mb-3">
        <div class="card-header bg-white"><h2 class="h5 mb-0">Form 04 - Stock Status</h2></div>
        <div class="table-responsive">
            <table class="table table-sm mb-0">
                <thead class="table-light"><tr><th>Product</th><th>Stock Level</th><th>System Stock</th><th>Variance</th><th>Comment</th></tr></thead>
                <tbody>
                <?php foreach ($bundle['stock_status'] as $row): ?>
                    <tr>
                        <td><?= e($row['product_name']) ?></td>
                        <td><?= e($row['stock_level']) ?></td>
                        <td><?= e($row['system_stock']) ?></td>
                        <td><?= e($row['variance']) ?></td>
                        <td><?= e($row['variance_comment']) ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="card shadow-sm border-0 mb-3">
        <div class="card-header bg-white"><h2 class="h5 mb-0">Form 05 - Issues Identified</h2></div>
        <div class="table-responsive">
            <table class="table table-sm mb-0">
                <thead class="table-light"><tr><th>Issue Type</th><th>Description</th><th>Severity</th></tr></thead>
                <tbody>
                <?php foreach ($bundle['issues'] as $row): ?>
                    <tr>
                        <td><?= e($row['issue_type']) ?></td>
                        <td><?= e($row['description']) ?></td>
                        <td><?= e($row['severity']) ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="row g-3 mb-3">
        <div class="col-12 col-lg-6">
            <div class="card shadow-sm border-0 h-100">
                <div class="card-header bg-white"><h2 class="h5 mb-0">Form 06A - Action Agreed with DB</h2></div>
                <div class="table-responsive"><table class="table table-sm mb-0"><thead class="table-light"><tr><th>Action</th><th>Comments</th></tr></thead><tbody>
                <?php foreach ($bundle['actions_db'] as $row): ?>
                    <tr><td><?= e($row['action_name']) ?></td><td><?= e($row['comments']) ?></td></tr>
                <?php endforeach; ?>
                </tbody></table></div>
            </div>
        </div>
        <div class="col-12 col-lg-6">
            <div class="card shadow-sm border-0 h-100">
                <div class="card-header bg-white"><h2 class="h5 mb-0">Form 06B - Action Agreed with Staff</h2></div>
                <div class="table-responsive"><table class="table table-sm mb-0"><thead class="table-light"><tr><th>Staff</th><th>Comments</th></tr></thead><tbody>
                <?php foreach ($bundle['actions_staff'] as $row): ?>
                    <tr><td><?= e($row['staff_role']) ?></td><td><?= e($row['comments']) ?></td></tr>
                <?php endforeach; ?>
                </tbody></table></div>
            </div>
        </div>
    </div>

    <div class="card shadow-sm border-0 mb-3">
        <div class="card-header bg-white"><h2 class="h5 mb-0">Form 07 - Follow Up</h2></div>
        <div class="table-responsive">
            <table class="table table-sm mb-0">
                <thead class="table-light"><tr><th>Action</th><th>Responsible</th><th>Deadline</th></tr></thead>
                <tbody>
                <?php foreach ($bundle['follow_ups'] as $row): ?>
                    <tr>
                        <td><?= e($row['action_name']) ?></td>
                        <td><?= e($row['responsible_person']) ?></td>
                        <td><?= e($row['deadline_period']) ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="card shadow-sm border-0 mb-3">
        <div class="card-header bg-white"><h2 class="h5 mb-0">Form 08 - General Remark & Signatures</h2></div>
        <div class="card-body row g-3">
            <?php $general = $bundle['general']; ?>
            <div class="col-12"><strong>Remark</strong><div><?= nl2br(e($general['remark'] ?? '-')) ?></div></div>
            <div class="col-md-4">
                <strong>Prepared By</strong><br>
                <?php if (!empty($general['prepared_by_signature'])): ?><img src="<?= e($general['prepared_by_signature']) ?>" class="img-fluid signature-view mt-2"><?php else: ?>-<?php endif; ?>
            </div>
            <div class="col-md-4">
                <strong>Distributor</strong><br>
                <?php if (!empty($general['distributor_signature'])): ?><img src="<?= e($general['distributor_signature']) ?>" class="img-fluid signature-view mt-2"><?php else: ?>-<?php endif; ?>
            </div>
            <div class="col-md-4">
                <strong>Employee</strong><br>
                <?php if (!empty($general['employee_signature'])): ?><img src="<?= e($general['employee_signature']) ?>" class="img-fluid signature-view mt-2"><?php else: ?>-<?php endif; ?>
            </div>
        </div>
    </div>
</div>
</body>
</html>
