<?php $this->start('body');
    $f = $this->data['form'] ?? [];
    $cust = $this->data['customers'] ?? [];
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-plus-circle me-2 text-muted"></i>New Project</h5>
    <a href="<?= PROOT; ?>Projects" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i> Back</a>
</div>

<?= $this->raw('fb_msg'); ?>

<form method="post" action="<?= PROOT; ?>Projects/save" autocomplete="off">
    <div class="card border-0 shadow-sm mb-3">
        <div class="card-header bg-white"><strong>Project Basics</strong></div>
        <div class="card-body p-3">
            <div class="row g-3">
                <div class="col-md-6"><label class="form-label">Project Name *</label><input type="text" name="project_name" class="form-control" value="<?= htmlspecialchars($f['project_name'] ?? ''); ?>" required></div>
                <div class="col-md-3">
                    <label class="form-label">Project Type *</label>
                    <select name="project_type" class="form-select" required>
                        <option value="">Select…</option>
                        <?php foreach (['goods','services','works'] as $t): ?>
                            <option value="<?= $t; ?>" <?= (($f['project_type'] ?? '')===$t?'selected':''); ?>><?= ucfirst($t); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Currency</label>
                    <select name="currency" class="form-select">
                        <?php foreach (['TZS','USD','EUR','KES'] as $c): ?>
                            <option value="<?= $c; ?>" <?= (($f['currency'] ?? 'TZS')===$c?'selected':''); ?>><?= $c; ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-6">
                    <label class="form-label">Customer *</label>
                    <select name="project_customer_id" class="form-select" required>
                        <option value="">Select…</option>
                        <?php foreach ($cust as $c): ?>
                            <option value="<?= (int)$c['c0d']; ?>" <?= ((int)($f['project_customer_id'] ?? 0)===(int)$c['c0d']?'selected':''); ?>>
                                <?= htmlspecialchars(trim(($c['fname'] ?? '') . ' ' . ($c['lname'] ?? ''))); ?> · <?= htmlspecialchars($c['city']); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <div class="form-text">No customer yet? <a href="<?= PROOT; ?>Customers/add" target="_blank">Add one</a>.</div>
                </div>
                <div class="col-md-3"><label class="form-label">Contract Value</label><input type="number" step="0.01" name="contract_value" class="form-control" value="<?= htmlspecialchars($f['contract_value'] ?? '0'); ?>"></div>
                <div class="col-md-3"><label class="form-label">Default Margin %</label><input type="number" step="0.01" name="default_margin_pct" class="form-control" value="<?= htmlspecialchars($f['default_margin_pct'] ?? '25'); ?>"></div>

                <div class="col-md-3"><label class="form-label">Start Date</label><input type="date" name="project_start_date" class="form-control" value="<?= htmlspecialchars($f['project_start_date'] ?? ''); ?>"></div>
                <div class="col-md-3"><label class="form-label">End Date</label><input type="date" name="project_end_date" class="form-control" value="<?= htmlspecialchars($f['project_end_date'] ?? ''); ?>"></div>
                <div class="col-md-2"><label class="form-label">Region</label><input type="text" name="region" class="form-control" value="<?= htmlspecialchars($f['region'] ?? ''); ?>"></div>
                <div class="col-md-2"><label class="form-label">District</label><input type="text" name="district" class="form-control" value="<?= htmlspecialchars($f['district'] ?? ''); ?>"></div>
                <div class="col-md-2"><label class="form-label">Ward</label><input type="text" name="ward" class="form-control" value="<?= htmlspecialchars($f['ward'] ?? ''); ?>"></div>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm mb-3">
        <div class="card-header bg-white"><strong>Origin</strong> <span class="text-muted small ms-2">Every project must have a provable origin.</span></div>
        <div class="card-body p-3">
            <div class="row g-3">
                <div class="col-md-4">
                    <label class="form-label">Origin *</label>
                    <select name="origin" id="fldOrigin" class="form-select" required onchange="rsOriginChanged(this.value)">
                        <?php foreach ([
                            'DIRECT_AWARD'  => 'Direct Award',
                            'TENDER_BDM'    => 'BDM Tender (in-house)',
                            'TENDER_PUBLIC' => 'Public Tender (NeST)',
                            'MIGRATED'      => 'Migrated (legacy)',
                        ] as $k => $lbl): ?>
                            <option value="<?= $k; ?>" <?= (($f['origin'] ?? 'DIRECT_AWARD')===$k?'selected':''); ?>><?= $lbl; ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-8"><label class="form-label">Tender Reference (any)</label><input type="text" name="project_tender_reference" class="form-control" value="<?= htmlspecialchars($f['project_tender_reference'] ?? ''); ?>"></div>

                <div class="col-12" id="grpDirect" style="display:none;">
                    <label class="form-label">Direct Award Justification *</label>
                    <textarea name="direct_award_justification" class="form-control" rows="3" placeholder="Why this project was awarded without a formal tender."><?= htmlspecialchars($f['direct_award_justification'] ?? ''); ?></textarea>
                </div>
                <div class="col-md-6" id="grpBdm" style="display:none;">
                    <label class="form-label">Linked BDM Plan ID *</label>
                    <input type="number" name="linked_bdm_plan_id" class="form-control" value="<?= htmlspecialchars($f['linked_bdm_plan_id'] ?? ''); ?>">
                </div>
                <div class="col-md-6" id="grpNest" style="display:none;">
                    <label class="form-label">Linked NeST Tender ID *</label>
                    <input type="number" name="linked_nest_tender_id" class="form-control" value="<?= htmlspecialchars($f['linked_nest_tender_id'] ?? ''); ?>">
                </div>
                <div class="col-12" id="grpMigrated" style="display:none;">
                    <label class="form-label">Migration Note</label>
                    <textarea name="migrated_note" class="form-control" rows="2" placeholder="Why this legacy project bypasses the onboarding chain."><?= htmlspecialchars($f['migrated_note'] ?? ''); ?></textarea>
                    <div class="alert alert-warning mt-2 mb-0 py-2">Migrated projects are marked with a <code>⚠ Migrated</code> chip in every report.</div>
                </div>
            </div>
        </div>
        <div class="card-footer bg-white text-end">
            <a href="<?= PROOT; ?>Projects" class="btn btn-sm btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-sm btn-primary" style="background:<?= BRAND_COLOR; ?>;border-color:<?= BRAND_COLOR; ?>;">
                <i class="fas fa-save me-1"></i> Create Project
            </button>
        </div>
    </div>
</form>

<script>
function rsOriginChanged(v) {
    document.getElementById('grpDirect').style.display   = (v === 'DIRECT_AWARD')  ? '' : 'none';
    document.getElementById('grpBdm').style.display      = (v === 'TENDER_BDM')    ? '' : 'none';
    document.getElementById('grpNest').style.display     = (v === 'TENDER_PUBLIC') ? '' : 'none';
    document.getElementById('grpMigrated').style.display = (v === 'MIGRATED')      ? '' : 'none';
}
document.addEventListener('DOMContentLoaded', () => rsOriginChanged(document.getElementById('fldOrigin').value));
</script>

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