<?php
$document = $document ?? [];
$types = $types ?? [];
$employees = $employees ?? [];
$isEdit = ! empty($document);
?>
<div class="enterprise-settings-fields enterprise-settings-fields--single">
    <?php if (! $isEdit): ?>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Employee <span class="text-danger">*</span></label>
        <select name="employee_id" class="form-select" required>
            <option value="">Select employee...</option>
            <?php foreach ($employees as $e): ?>
                <option value="<?= $e['id'] ?>" <?= old('employee_id') == $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">Document Type <span class="text-danger">*</span></label>
        <select name="document_type_id" class="form-select" required>
            <option value="">Select type...</option>
            <?php foreach ($types as $t): ?>
                <option value="<?= $t['id'] ?>" data-requires-expiry="<?= $t['requires_expiry'] ?>" data-requires-number="<?= $t['requires_number'] ?>"
                    <?= old('document_type_id') == $t['id'] ? 'selected' : '' ?>>
                    <?= esc($t['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <?php endif; ?>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Document Number</label>
        <input type="text" name="document_number" class="form-control" value="<?= esc(old('document_number', $document['document_number'] ?? '')) ?>" placeholder="Passport no., CPR, visa no., etc.">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Issue Date</label>
        <input type="date" name="issue_date" class="form-control" value="<?= esc(old('issue_date', $document['issue_date'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Expiry Date</label>
        <input type="date" name="expiry_date" class="form-control" value="<?= esc(old('expiry_date', $document['expiry_date'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Notes</label>
        <textarea name="notes" class="form-control" rows="2"><?= esc(old('notes', $document['notes'] ?? '')) ?></textarea>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label"><?= $isEdit ? 'New File (optional — creates new version)' : 'Document File' ?> <?= $isEdit ? '' : '<span class="text-danger">*</span>' ?></label>
        <input type="file" name="document_file" class="form-control" accept=".pdf,.jpg,.jpeg,.png,.webp,application/pdf,image/jpeg,image/png,image/webp" <?= $isEdit ? '' : 'required' ?>>
        <small class="text-muted">PDF, JPEG, PNG, or WEBP. Max 5MB.</small>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label"><?= $isEdit ? 'Version Notes' : 'Upload Notes' ?></label>
        <input type="text" name="change_notes" class="form-control" value="<?= esc(old('change_notes')) ?>" placeholder="e.g. Renewed passport, updated visa">
    </div>
</div>
