<?php $this->start('body');
    $p       = $this->data['project'];
    $phases  = $this->data['phases']  ?? [];
    $summary = $this->data['summary'] ?? [];
    $statusPill = [
        'PLANNED'   => ['#f59e0b', 'Planned'],
        'ACTIVE'    => ['#22c55e', 'Active'],
        'CLOSED'    => ['#7A8BA1', 'Closed'],
        'CANCELLED' => ['#dc2626', 'Cancelled'],
    ];
?>
<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-layer-group me-1 text-muted"></i>
            Phase Planning · <?= htmlspecialchars($p['project_no']); ?> · <?= htmlspecialchars($p['project_name']); ?>
        </h5>
        <div class="text-muted" style="font-size:.72rem;">
            <?= htmlspecialchars($p['project_customer']); ?> ·
            <span style="text-transform:capitalize;"><?= htmlspecialchars($p['project_type']); ?></span>
        </div>
    </div>
    <div>
        <a href="<?= PROOT; ?>WorksBoq/view/<?= (int)$p['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-file-invoice me-1"></i> BOQ</a>
        <a href="<?= PROOT; ?>ProjectStarter/index/<?= (int)$p['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i> Checklist</a>
    </div>
</div>

<!-- Add phase form -->
<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white"><strong>Add phase</strong>
        <span class="text-muted small ms-2">Break BOQ execution into containers (e.g. Mobilisation · Earthworks · Base · Handover)</span>
    </div>
    <div class="card-body p-3" style="background:#F8FAFC;">
        <div class="row g-2 align-items-end">
            <div class="col-md-2">
                <label class="form-label" style="font-size:.78rem;">Code</label>
                <input id="ph_code" class="form-control form-control-sm" placeholder="PH-1">
            </div>
            <div class="col-md-4">
                <label class="form-label" style="font-size:.78rem;">Title</label>
                <input id="ph_title" class="form-control form-control-sm" placeholder="e.g. Mobilisation + site prep">
            </div>
            <div class="col-md-2">
                <label class="form-label" style="font-size:.78rem;">Start</label>
                <input type="date" id="ph_start" class="form-control form-control-sm" value="<?= date('Y-m-d'); ?>">
            </div>
            <div class="col-md-2">
                <label class="form-label" style="font-size:.78rem;">End</label>
                <input type="date" id="ph_end" class="form-control form-control-sm" value="<?= date('Y-m-d', strtotime('+60 days')); ?>">
            </div>
            <div class="col-md-2 text-end">
                <button type="button" class="btn btn-sm btn-primary" onclick="phCreate()"><i class="fas fa-plus me-1"></i> Add phase</button>
            </div>
            <div class="col-12">
                <label class="form-label" style="font-size:.78rem;">Description (optional)</label>
                <textarea id="ph_desc" class="form-control form-control-sm" rows="1"></textarea>
            </div>
        </div>
    </div>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white"><strong>Phases on this project · <?= count($phases); ?></strong></div>
    <div class="card-body p-0">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th style="width:50px;">#</th>
                    <th style="width:80px;">Code</th>
                    <th>Title</th>
                    <th style="width:110px;">Start</th>
                    <th style="width:110px;">End</th>
                    <th style="width:100px;">Status</th>
                    <th style="width:130px;">BOQ · Acts · CRs</th>
                    <th style="width:80px;" class="text-end">Open</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($phases as $ph):
                    $sp = $statusPill[$ph['status']] ?? ['#94a3b8', $ph['status']];
                    $s = $summary[(int)$ph['c0d']] ?? ['boq_items'=>0,'activities'=>0,'crs'=>0];
                ?>
                    <tr>
                        <td class="text-muted"><?= (int)$ph['phase_no']; ?></td>
                        <td><code><?= htmlspecialchars($ph['code'] ?: '—'); ?></code></td>
                        <td>
                            <strong><?= htmlspecialchars($ph['title']); ?></strong>
                            <?php if (!empty($ph['description'])): ?><div class="text-muted small"><?= htmlspecialchars(mb_strimwidth($ph['description'], 0, 120, '…')); ?></div><?php endif; ?>
                        </td>
                        <td class="text-muted small"><?= htmlspecialchars($ph['planned_start'] ?? '—'); ?></td>
                        <td class="text-muted small"><?= htmlspecialchars($ph['planned_end'] ?? '—'); ?></td>
                        <td><span class="badge" style="background:<?= $sp[0]; ?>;color:#fff;"><?= htmlspecialchars($sp[1]); ?></span></td>
                        <td class="small">
                            <?= $s['boq_items']; ?> BOQ · <?= $s['activities']; ?> acts · <strong><?= $s['crs']; ?> CR<?= $s['crs'] === 1 ? '' : 's' ?></strong>
                        </td>
                        <td class="text-end"><a href="<?= PROOT; ?>WorksPhases/view/<?= (int)$ph['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($phases)): ?>
                    <tr><td colspan="8" class="text-center text-muted py-4">No phases yet — add the first one above.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $this->end(); ?>

<?php $this->start('section_js'); ?>
<script>
function phCreate() {
    var payload = {
        project_id:    <?= (int)$p['c0d']; ?>,
        code:          $('#ph_code').val().trim(),
        title:         $('#ph_title').val().trim(),
        description:   $('#ph_desc').val(),
        planned_start: $('#ph_start').val(),
        planned_end:   $('#ph_end').val(),
    };
    if (!payload.title) { rsToast('Title required.', 'error'); return; }
    $.post('<?= PROOT; ?>WorksPhases/createAj', payload, 'json')
        .done(function (r) {
            if (r && r.ok) location.href = '<?= PROOT; ?>WorksPhases/view/' + r.id;
            else rsToast('Failed: ' + (r.msg || 'unknown'), 'error');
        });
}
</script>
<?php $this->end(); ?>
