<?php
declare(strict_types=1);
require_once 'config/database.php';
require_once 'app/helpers/auth.php';
requireLogin();

require_once 'app/helpers/rbac.php';
if (!isAdmin() && !can('workshop.manage') && !can('maintenance.view')) {
    echo "<div class='alert alert-danger m-4'>Access denied.</div>";
    exit;
}

$page_js = 'workshop_report.js';
$userName = $_SESSION['user_name'] ?? 'User';

include 'views/layouts/header.php';
include 'views/layouts/sidebar.php';
?>

<main class="main-content">
<?php include 'views/layouts/topbar.php'; ?>

<div class="container-fluid p-4">
    <div class="card shadow-sm border-0 workshop-hero mb-4">
        <div class="card-body p-4 p-lg-5">
            <span class="section-chip workshop-chip mb-3">Workshop Report</span>
            <h2 class="mb-2">Workshop Performance Report</h2>
            <p class="mb-0 workshop-hero-copy">Overview of workshop activity, quote pipeline, and performance metrics.</p>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-12 col-md-6 col-xl-3">
            <div class="card kpi-card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="kpi-label">Total Workshops</div>
                    <div class="kpi-value" id="kpiTotalWorkshops">0</div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-6 col-xl-3">
            <div class="card kpi-card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="kpi-label">Active Workshops</div>
                    <div class="kpi-value text-success" id="kpiActiveWorkshops">0</div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-6 col-xl-3">
            <div class="card kpi-card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="kpi-label">Total RFQs</div>
                    <div class="kpi-value" id="kpiTotalRfqs">0</div>
                </div>
            </div>
        </div>
        <div class="col-12 col-md-6 col-xl-3">
            <div class="card kpi-card shadow-sm border-0 h-100">
                <div class="card-body">
                    <div class="kpi-label">Total Quote Value</div>
                    <div class="kpi-value text-primary" id="kpiTotalQuoteValue">&#8358;0.00</div>
                </div>
            </div>
        </div>
    </div>

    <div class="card shadow-sm border-0 mb-4">
        <div class="card-header bg-white border-0 d-flex flex-column flex-lg-row justify-content-between align-items-lg-center gap-3">
            <h5 class="mb-0">Workshop Register</h5>
            <div class="d-flex flex-wrap gap-2">
                <div class="input-group" style="min-width:240px">
                    <span class="input-group-text"><i class="bi bi-search"></i></span>
                    <input type="text" class="form-control form-control-sm" id="filterSearch" placeholder="Search workshop, contact...">
                </div>
                <select id="filterStatus" class="form-select form-select-sm w-auto">
                    <option value="">All Statuses</option>
                    <option value="active">Active</option>
                    <option value="inactive">Inactive</option>
                </select>
                <button class="btn btn-sm btn-outline-secondary" id="btnRefresh">Refresh</button>
                <button class="btn btn-sm btn-outline-success" id="btnExport">Export CSV</button>
            </div>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table align-middle table-hover mb-0" id="workshopReportTable">
                    <thead class="table-light">
                        <tr>
                            <th>Workshop</th>
                            <th>Contact</th>
                            <th>Phone</th>
                            <th>Email</th>
                            <th>Status</th>
                            <th>RFQs</th>
                            <th>Submitted</th>
                            <th>Selected</th>
                            <th>Total Quote Value</th>
                            <th>Latest Quote</th>
                        </tr>
                    </thead>
                    <tbody id="workshopReportBody">
                        <tr><td colspan="10" class="text-center text-muted py-4">Loading report data...</td></tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</main>

<?php include 'views/layouts/footer.php'; ?>
