<?php $this->layout('main'); ?>

<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>Contractor Dashboard</h2>
        <span class="text-muted"><?= date('l, F j, Y') ?></span>
    </div>

    <!-- Stats -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card bg-primary text-white h-100">
                <div class="card-body">
                    <h5 class="card-title">Assigned Projects</h5>
                    <h3 class="card-text"><?= $assigned_projects ?? 0 ?></h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-success text-white h-100">
                <div class="card-body">
                    <h5 class="card-title">Reports Submitted</h5>
                    <h3 class="card-text"><?= $reports_submitted ?? 0 ?></h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-warning text-dark h-100">
                <div class="card-body">
                    <h5 class="card-title">Pending Invoices</h5>
                    <h3 class="card-text"><?= $pending_invoices ?? 0 ?></h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-danger text-white h-100">
                <div class="card-body">
                    <h5 class="card-title">Reports Due Today</h5>
                    <h3 class="card-text"><?= $reports_due_today ?? 0 ?></h3>
                </div>
            </div>
        </div>
    </div>

    <!-- Pending Invitations -->
    <div class="mb-4">
        <h4>Pending Project Invitations</h4>
        <div class="row">
            <?php if (empty($pending_invitations)): ?>
                <div class="col"><p class="text-muted">No pending invitations.</p></div>
            <?php else: ?>
                <?php foreach ($pending_invitations as $invite): ?>
                    <div class="col-md-4 mb-3">
                        <div class="card">
                            <div class="card-body">
                                <h5 class="card-title"><?= htmlspecialchars($invite['project_name']) ?></h5>
                                <p class="card-text">Invited by: <?= htmlspecialchars($invite['org_name']) ?></p>
                                <a href="<?= APP_URL ?>/projects/<?= $invite['project_id'] ?>/contractor-response" class="btn btn-primary btn-sm">Review & Respond</a>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>

    <div class="row">
        <!-- Active Projects -->
        <div class="col-md-6 mb-4">
            <div class="card h-100">
                <div class="card-header">Active Projects</div>
                <div class="card-body">
                    <?php foreach ($active_projects ?? [] as $project): ?>
                        <div class="mb-3">
                            <div class="d-flex justify-content-between">
                                <span><?= htmlspecialchars($project['name']) ?></span>
                                <span><?= $project['progress'] ?>%</span>
                            </div>
                            <div class="progress">
                                <div class="progress-bar" role="progressbar" style="width: <?= $project['progress'] ?>%" aria-valuenow="<?= $project['progress'] ?>" aria-valuemin="0" aria-valuemax="100"></div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>

        <div class="col-md-6 mb-4">
            <div class="card h-100 mb-3">
                <div class="card-header">Recent Reports</div>
                <ul class="list-group list-group-flush">
                    <?php foreach ($recent_reports ?? [] as $report): ?>
                        <li class="list-group-item">
                            <?= htmlspecialchars($report['project_name']) ?> - <?= htmlspecialchars($report['report_date']) ?>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>

            <div class="card h-100">
                <div class="card-header">Invoice Tracker</div>
                <ul class="list-group list-group-flush">
                    <?php foreach ($invoice_tracker ?? [] as $invoice): ?>
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            Inv #<?= htmlspecialchars($invoice['id']) ?>
                            <span class="badge bg-<?= $invoice['status_color'] ?> rounded-pill"><?= htmlspecialchars($invoice['status']) ?></span>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
    </div>
</div>

