<?php
$inspection = $inspection ?? [];
$employees  = $employees ?? [];
$companies  = $companies ?? [];
$workOrders = $workOrders ?? [];
$serviceVisits = $serviceVisits ?? [];
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Title <span class="text-danger">*</span></label>
        <input type="text" name="title" class="form-control" maxlength="200" required
               value="<?= esc(old('title', $inspection['title'] ?? '')) ?>" placeholder="e.g. Incoming material inspection">
    </div>
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Description</label>
        <textarea name="description" class="form-control" rows="3"><?= esc(old('description', $inspection['description'] ?? '')) ?></textarea>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Priority</label>
        <select name="priority" class="form-select">
            <?php foreach (['low', 'normal', 'high', 'urgent'] as $p): ?>
                <option value="<?= $p ?>" <?= old('priority', $inspection['priority'] ?? 'normal') === $p ? 'selected' : '' ?>>
                    <?= esc(ucfirst($p)) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Status</label>
        <select name="status" class="form-select">
            <?php foreach (['planned', 'in_progress'] as $s): ?>
                <option value="<?= $s ?>" <?= old('status', $inspection['status'] ?? 'planned') === $s ? 'selected' : '' ?>>
                    <?= esc(ucwords(str_replace('_', ' ', $s))) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Overall Result</label>
        <select name="overall_result" class="form-select">
            <?php foreach (['pending', 'pass', 'fail', 'na'] as $r): ?>
                <option value="<?= $r ?>" <?= old('overall_result', $inspection['overall_result'] ?? 'pending') === $r ? 'selected' : '' ?>>
                    <?= esc(strtoupper($r)) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Scheduled Date</label>
        <input type="date" name="scheduled_date" class="form-control"
               value="<?= esc(old('scheduled_date', $inspection['scheduled_date'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Location</label>
        <input type="text" name="location" class="form-control" maxlength="200"
               value="<?= esc(old('location', $inspection['location'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Score</label>
        <input type="number" name="score" class="form-control" step="0.01"
               value="<?= esc(old('score', $inspection['score'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Inspector</label>
        <select name="inspector_employee_id" class="form-select">
            <option value="">Unassigned</option>
            <?php foreach ($employees as $e): ?>
                <option value="<?= (int) $e['id'] ?>"
                    <?= (string) old('inspector_employee_id', $inspection['inspector_employee_id'] ?? '') === (string) $e['id'] ? 'selected' : '' ?>>
                    <?= esc($e['full_name']) ?> (<?= esc($e['employee_number']) ?>)
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Company</label>
        <select name="company_id" class="form-select">
            <option value="">None</option>
            <?php foreach ($companies as $c): ?>
                <option value="<?= (int) $c['id'] ?>"
                    <?= (string) old('company_id', $inspection['company_id'] ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                    <?= esc($c['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Work Order</label>
        <select name="work_order_id" class="form-select">
            <option value="">None</option>
            <?php foreach ($workOrders as $wo): ?>
                <option value="<?= (int) $wo['id'] ?>"
                    <?= (string) old('work_order_id', $inspection['work_order_id'] ?? '') === (string) $wo['id'] ? 'selected' : '' ?>>
                    <?= esc($wo['number']) ?> — <?= esc($wo['title']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Service Visit</label>
        <select name="service_visit_id" class="form-select">
            <option value="">None</option>
            <?php foreach ($serviceVisits as $sv): ?>
                <option value="<?= (int) $sv['id'] ?>"
                    <?= (string) old('service_visit_id', $inspection['service_visit_id'] ?? '') === (string) $sv['id'] ? 'selected' : '' ?>>
                    <?= esc($sv['number']) ?> — <?= esc($sv['title']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Notes</label>
        <textarea name="notes" class="form-control" rows="2"><?= esc(old('notes', $inspection['notes'] ?? '')) ?></textarea>
    </div>
</div>
