<?php $this->start('body');
    $p = $this->data['project'];
    $existing = $this->data['existing'] ?? [];
    $statusPill = [
        'DRAFT'     => ['#f59e0b', 'Draft'],
        'SUBMITTED' => ['#0ea5e9', 'Submitted'],
        'APPROVED'  => ['#22c55e', 'Approved · awaiting issue'],
        'ISSUED'    => ['#7A9E7E', 'Issued'],
        'RETIRING'  => ['#a78bfa', 'Retiring'],
        'RETIRED'   => ['#7A8BA1', 'Retired'],
        'CLOSED'    => ['#94a3b8', 'Closed'],
        'DECLINED'  => ['#dc2626', 'Declined'],
        'CANCELLED' => ['#94a3b8', '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-hand-holding-usd me-1 text-muted"></i>
            New Requisition · <?= 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>Draft a new requisition</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 <span class="text-danger">*</span></label>
                <input id="sr_title" class="form-control form-control-sm" placeholder="e.g. Weekly float · site 03">
            </div>
            <div class="col-md-3">
                <label class="form-label" style="font-size:.78rem;">Currency</label>
                <select id="sr_currency" class="form-select form-select-sm">
                    <option value="TZS" selected>TZS</option>
                    <option value="USD">USD</option>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label" style="font-size:.78rem;">Context ref</label>
                <input id="sr_ctx" class="form-control form-control-sm" placeholder="Callout / visit ref">
            </div>
            <div class="col-12">
                <label class="form-label" style="font-size:.78rem;">Purpose</label>
                <textarea id="sr_purpose" class="form-control form-control-sm" rows="2" placeholder="What this float covers…"></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</button>
    </div>
</div>

<?php if (!empty($existing)): ?>
<div class="card border-0 shadow-sm">
    <div class="card-header bg-white"><strong>Existing requisitions on this project · <?= count($existing); ?></strong></div>
    <div class="card-body p-0">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr><th>Ref</th><th>Title</th><th class="text-end">Amount</th><th>Status</th><th style="width:80px;" class="text-end">Open</th></tr>
            </thead>
            <tbody>
                <?php foreach ($existing as $r):
                    $sp = $statusPill[$r['status']] ?? ['#94a3b8', $r['status']];
                ?>
                    <tr>
                        <td><code><?= htmlspecialchars($r['req_no']); ?></code></td>
                        <td><?= htmlspecialchars($r['title']); ?></td>
                        <td class="text-end"><?= number_format((float)$r['total_amount']); ?></td>
                        <td><span class="badge" style="background:<?= $sp[0]; ?>;color:#fff;"><?= htmlspecialchars($sp[1]); ?></span></td>
                        <td class="text-end"><a href="<?= PROOT; ?>ServiceRequisitions/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></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:       $('#sr_title').val().trim(),
        currency:    $('#sr_currency').val(),
        context_ref: $('#sr_ctx').val(),
        purpose:     $('#sr_purpose').val(),
    };
    if (!payload.title) { rsToast('Title required.', 'error'); return; }
    $.post('<?= PROOT; ?>ServiceRequisitions/createAj', payload, 'json')
        .done(function (r) {
            if (r && r.ok) location.href = '<?= PROOT; ?>ServiceRequisitions/view/' + r.id;
            else rsToast('Failed: ' + (r.msg || 'unknown'), 'error');
        });
});
</script>
<?php $this->end(); ?>
