<?php $this->start('body');
    $p = $this->data['project'];
    $existing = $this->data['existing'] ?? [];
    $statusPill = [
        'PLANNED'   => ['#f59e0b', 'Planned'],
        'ACTIVE'    => ['#22c55e', 'Active'],
        'CLOSED'    => ['#94a3b8', '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-calendar-check me-1 text-muted"></i>
            PM Plans · <?= 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>
    <a href="<?= PROOT; ?>ProjectStarter/index/<?= (int)$p['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i> Back to checklist</a>
</div>

<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white"><strong>Create a PM plan</strong></div>
    <div class="card-body p-3" style="background:#F8FAFC;">
        <div class="row g-2">
            <div class="col-md-6">
                <label class="form-label" style="font-size:.78rem;">Title</label>
                <input id="pmp_title" class="form-control form-control-sm" placeholder="e.g. Vodacom sites · monthly generator PM">
            </div>
            <div class="col-md-3">
                <label class="form-label" style="font-size:.78rem;">Planned start</label>
                <input type="date" id="pmp_start" class="form-control form-control-sm" value="<?= date('Y-m-d'); ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label" style="font-size:.78rem;">Planned end</label>
                <input type="date" id="pmp_end" class="form-control form-control-sm" value="<?= date('Y-m-d', strtotime('+365 days')); ?>">
            </div>
            <div class="col-12">
                <label class="form-label" style="font-size:.78rem;">Description (optional)</label>
                <textarea id="pmp_desc" class="form-control form-control-sm" rows="2"></textarea>
            </div>
        </div>
    </div>
    <div class="card-footer bg-white text-end">
        <button type="button" id="btn_create" class="btn btn-sm btn-primary"><i class="fas fa-plus me-1"></i> Create plan</button>
    </div>
</div>

<?php if (!empty($existing)): ?>
<div class="card border-0 shadow-sm">
    <div class="card-header bg-white"><strong>Existing plans on this project</strong></div>
    <div class="card-body p-0">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>Title</th>
                    <th style="width:110px;">Start</th>
                    <th style="width:110px;">End</th>
                    <th style="width:100px;">Status</th>
                    <th style="width:100px;" class="text-end">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($existing as $pl):
                    $sp = $statusPill[$pl['status']] ?? ['#94a3b8', $pl['status']];
                ?>
                    <tr>
                        <td><strong><?= htmlspecialchars($pl['title']); ?></strong></td>
                        <td class="text-muted small"><?= htmlspecialchars($pl['planned_start'] ?? '—'); ?></td>
                        <td class="text-muted small"><?= htmlspecialchars($pl['planned_end'] ?? '—'); ?></td>
                        <td><span class="badge" style="background:<?= $sp[0]; ?>;color:#fff;"><?= htmlspecialchars($sp[1]); ?></span></td>
                        <td class="text-end">
                            <a href="<?= PROOT; ?>ServicePmPlans/view/<?= (int)$pl['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i> Open</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<?php $this->end(); ?>

<?php $this->start('section_js'); ?>
<script>
$('#btn_create').on('click', function () {
    var payload = {
        project_id: <?= (int)$p['c0d']; ?>,
        title:      $('#pmp_title').val().trim(),
        description:$('#pmp_desc').val(),
        planned_start: $('#pmp_start').val(),
        planned_end:   $('#pmp_end').val(),
    };
    if (!payload.title) { rsToast('Title required.', 'error'); return; }
    $.post('<?= PROOT; ?>ServicePmPlans/createAj', payload, 'json')
        .done(function (r) {
            if (r && r.ok) location.href = '<?= PROOT; ?>ServicePmPlans/view/' + r.id;
            else rsToast('Failed: ' + (r.msg || 'unknown'), 'error');
        });
});
</script>
<?php $this->end(); ?>
