<?php $this->start('body');
    $p = $this->data['project'];
?>
<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-plus-circle me-1 text-muted"></i>
            New IPC · <?= htmlspecialchars($p['project_no']); ?>
        </h5>
        <div class="text-muted" style="font-size:.72rem;">
            <?= htmlspecialchars($p['project_customer']); ?> · <?= htmlspecialchars($p['project_name']); ?>
        </div>
    </div>
    <a href="<?= PROOT; ?>WorksIpcs/list/<?= (int)$p['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i> IPCs</a>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white"><strong>Certificate details</strong>
        <span class="text-muted small ms-2">Lines will auto-seed from the BOQ. Previous cumulative qty carries forward from prior APPROVED IPCs.</span>
    </div>
    <div class="card-body p-3" style="background:#F8FAFC;">
        <div class="row g-2">
            <div class="col-md-3">
                <label class="form-label" style="font-size:.78rem;">IPC date</label>
                <input type="date" id="ipc_date" 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;">Period from</label>
                <input type="date" id="ipc_from" class="form-control form-control-sm" value="<?= date('Y-m-01'); ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label" style="font-size:.78rem;">Period to</label>
                <input type="date" id="ipc_to" 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;">VAT %</label>
                <input type="number" step="0.01" min="0" id="ipc_vat" class="form-control form-control-sm" value="18.00">
            </div>
            <div class="col-12">
                <label class="form-label" style="font-size:.78rem;">Notes (optional)</label>
                <textarea id="ipc_notes" class="form-control form-control-sm" rows="2" placeholder="e.g. Interim certificate for August works · attach measurement sheets"></textarea>
            </div>
        </div>
    </div>
    <div class="card-footer bg-white text-end">
        <button type="button" id="btn_create" class="btn btn-sm btn-primary" style="background:#7A9E7E;border-color:#7A9E7E;">
            <i class="fas fa-plus me-1"></i> Draft IPC
        </button>
    </div>
</div>
<?php $this->end(); ?>

<?php $this->start('section_js'); ?>
<script>
$('#btn_create').on('click', function () {
    $.post('<?= PROOT; ?>WorksIpcs/createAj', {
        project_id:  <?= (int)$p['c0d']; ?>,
        ipc_date:    $('#ipc_date').val(),
        period_from: $('#ipc_from').val(),
        period_to:   $('#ipc_to').val(),
        vat_pct:     $('#ipc_vat').val(),
        notes:       $('#ipc_notes').val(),
    }, 'json').done(function (r) {
        if (r && r.ok) location.href = '<?= PROOT; ?>WorksIpcs/view/' + r.id;
        else            rsToast('Failed: ' + (r.msg || 'unknown'), 'error');
    });
});
</script>
<?php $this->end(); ?>
