<?php $isEdit = !empty($permit); $preselectedWo = $preselectWo ?? 0; ?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-shield-halved me-2 text-primary"></i><?= $isEdit ? 'Edit' : 'Request' ?> Permit to Work</h5>
    <a href="<?= url('/Permits') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<div class="rs-card p-3">
<form method="post" action="<?= url('/Permits/save') ?>">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <input type="hidden" name="permit_id" value="<?= (int) ($permit['permit_id'] ?? 0) ?>">

    <div class="row g-2">
        <div class="col-md-8"><label class="form-label">Title *</label>
            <input type="text" class="form-control form-control-sm" name="title" required value="<?= e($permit['title'] ?? '') ?>" placeholder="e.g. Weld replacement pipe run — line B">
        </div>
        <div class="col-md-4"><label class="form-label">Permit type *</label>
            <select name="permit_type_id" class="form-select form-select-sm" required>
                <option value="">— select —</option>
                <?php foreach ($types as $t): ?>
                    <option value="<?= (int) $t['permit_type_id'] ?>" <?= (int) ($permit['permit_type_id'] ?? 0) === (int) $t['permit_type_id'] ? 'selected' : '' ?>><?= e($t['code']) ?> · <?= e($t['name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>

        <div class="col-md-4"><label class="form-label">Work Order</label>
            <select name="work_order_id" class="form-select form-select-sm">
                <option value="">— none —</option>
                <?php foreach ($workOrders as $wo):
                    $sel = (int) ($permit['work_order_id'] ?? 0) === (int) $wo['work_order_id']
                        || (int) $preselectedWo === (int) $wo['work_order_id'];
                ?>
                    <option value="<?= (int) $wo['work_order_id'] ?>" <?= $sel ? 'selected' : '' ?>>WO #<?= (int) $wo['work_order_id'] ?> · <?= e($wo['title']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-4"><label class="form-label">Asset</label>
            <select name="asset_id" class="form-select form-select-sm">
                <option value="">— none —</option>
                <?php foreach ($assets as $a): ?><option value="<?= (int) $a['asset_id'] ?>" <?= (int) ($permit['asset_id'] ?? 0) === (int) $a['asset_id'] ? 'selected' : '' ?>><?= e($a['name']) ?></option><?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-4"><label class="form-label">Site</label>
            <select name="site_id" class="form-select form-select-sm">
                <option value="">— none —</option>
                <?php foreach ($sites as $s): ?><option value="<?= (int) $s['site_id'] ?>" <?= (int) ($permit['site_id'] ?? 0) === (int) $s['site_id'] ? 'selected' : '' ?>><?= e($s['name']) ?></option><?php endforeach; ?>
            </select>
        </div>

        <div class="col-md-6"><label class="form-label">Valid from</label>
            <input type="datetime-local" class="form-control form-control-sm" name="valid_from" value="<?= e(str_replace(' ', 'T', (string) ($permit['valid_from'] ?? ''))) ?>">
        </div>
        <div class="col-md-6"><label class="form-label">Valid to</label>
            <input type="datetime-local" class="form-control form-control-sm" name="valid_to" value="<?= e(str_replace(' ', 'T', (string) ($permit['valid_to'] ?? ''))) ?>">
        </div>

        <div class="col-12"><label class="form-label">Scope / Description</label>
            <textarea class="form-control form-control-sm" name="description" rows="2"><?= e($permit['description'] ?? '') ?></textarea>
        </div>
        <div class="col-md-6"><label class="form-label">Hazards identified</label>
            <textarea class="form-control form-control-sm" name="hazards" rows="3" placeholder="Fire, hot surfaces, sparks…"><?= e($permit['hazards'] ?? '') ?></textarea>
        </div>
        <div class="col-md-6"><label class="form-label">Controls in place</label>
            <textarea class="form-control form-control-sm" name="controls" rows="3" placeholder="Fire watch, extinguisher, blankets, area cordoned…"><?= e($permit['controls'] ?? '') ?></textarea>
        </div>
        <div class="col-md-8"><label class="form-label">PPE required</label>
            <input type="text" class="form-control form-control-sm" name="ppe" value="<?= e($permit['ppe'] ?? '') ?>" placeholder="Face shield, welding gloves, FR overalls">
        </div>
        <div class="col-md-4"><label class="form-label">Lockout tags</label>
            <input type="text" class="form-control form-control-sm" name="lockout_tags" value="<?= e($permit['lockout_tags'] ?? '') ?>" placeholder="TAG-045, TAG-046">
        </div>
    </div>

    <div class="mt-3">
        <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-paper-plane me-1"></i><?= $isEdit ? 'Save' : 'Submit for approval' ?></button>
    </div>
</form>
</div>

<?php if ($isEdit): ?>
    <form method="post" action="<?= url('/Permits/delete/' . (int) $permit['permit_id']) ?>"
          class="mt-3 rs-confirm-submit"
          data-confirm-title="Delete permit?"
          data-confirm-body="Soft-delete permit <strong><?= e($permit['title']) ?></strong>?"
          data-confirm-ok="Delete">
        <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
        <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fas fa-trash me-1"></i>Delete permit</button>
    </form>
<?php endif; ?>
