<?php
$isEdit = ! empty($employee);
$emp    = $employee ?? [];
$oldOr  = static function (string $key, $default = '') use ($emp) {
    return old($key, $emp[$key] ?? $default);
};
?>

<?php if (session()->getFlashdata('errors')): ?>
    <div class="alert alert-danger py-2 small enterprise-alert">
        <ul class="mb-0 ps-3">
            <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
            <?php endforeach; ?>
        </ul>
    </div>
<?php endif; ?>

<article class="enterprise-settings-card mb-3">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-user"></i>
        <h2 class="enterprise-settings-card__title">Basic Information</h2>
    </div>
    <p class="enterprise-settings-card__intro">Core personnel identifiers and organizational assignment.</p>
    <div class="enterprise-settings-fields">
        <div class="enterprise-field">
            <label class="enterprise-field-label">Full Name <span class="text-danger">*</span></label>
            <input type="text" name="full_name" class="form-control" value="<?= esc($oldOr('full_name')) ?>" required>
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Employee ID</label>
            <?php if ($isEdit): ?>
                <div class="form-check mb-2">
                    <input class="form-check-input" type="checkbox" id="use_custom_id" name="use_custom_id" value="1"
                           <?= old('use_custom_id') ? 'checked' : '' ?> onchange="toggleCustomId()">
                    <label class="form-check-label small" for="use_custom_id">Edit employee ID</label>
                </div>
                <input type="text" name="employee_number" id="employee_number" class="form-control"
                       value="<?= esc($oldOr('employee_number')) ?>" readonly>
            <?php else: ?>
                <div class="form-check mb-2">
                    <input class="form-check-input" type="checkbox" id="use_custom_id" name="use_custom_id" value="1"
                           <?= old('use_custom_id') ? 'checked' : '' ?> onchange="toggleCustomId()">
                    <label class="form-check-label small" for="use_custom_id">Use custom employee ID</label>
                </div>
                <input type="text" name="employee_number" id="employee_number" class="form-control enterprise-field-readonly--mono"
                       value="<?= esc(old('employee_number', $nextEmployeeId ?? '')) ?>"
                       placeholder="<?= esc($nextEmployeeId ?? 'EMP-2026-0001') ?>" readonly>
                <div class="form-text small">Auto-generated: <strong><?= esc($nextEmployeeId ?? '') ?></strong></div>
            <?php endif; ?>
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Department</label>
            <select name="department_id" class="form-select">
                <option value="">— None —</option>
                <?php foreach ($departments as $d): ?>
                    <option value="<?= $d['id'] ?>" <?= (string) $oldOr('department_id') === (string) $d['id'] ? 'selected' : '' ?>><?= esc($d['name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Branch</label>
            <select name="branch_id" class="form-select">
                <option value="">— None —</option>
                <?php foreach ($branches as $b): ?>
                    <option value="<?= $b['id'] ?>" <?= (string) $oldOr('branch_id') === (string) $b['id'] ? 'selected' : '' ?>><?= esc($b['name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Job Title</label>
            <input type="text" name="job_title" class="form-control" value="<?= esc($oldOr('job_title')) ?>">
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Status</label>
            <select name="status" class="form-select">
                <?php foreach (['active', 'inactive', 'on_leave', 'terminated'] as $s): ?>
                    <option value="<?= $s ?>" <?= $oldOr('status', 'active') === $s ? 'selected' : '' ?>><?= esc(ucwords(str_replace('_', ' ', $s))) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Employment Type</label>
            <select name="employment_type" class="form-select">
                <?php foreach (['full_time', 'part_time', 'contract', 'intern'] as $t): ?>
                    <option value="<?= $t ?>" <?= $oldOr('employment_type', 'full_time') === $t ? 'selected' : '' ?>><?= esc(ucwords(str_replace('_', ' ', $t))) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Hire Date</label>
            <input type="date" name="hire_date" class="form-control" value="<?= esc($oldOr('hire_date')) ?>">
        </div>
    </div>
</article>

<article class="enterprise-settings-card mb-3">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-id-card"></i>
        <h2 class="enterprise-settings-card__title">Identity & Contact</h2>
    </div>
    <div class="enterprise-settings-fields">
        <div class="enterprise-field">
            <label class="enterprise-field-label">CPR Number</label>
            <input type="text" name="cpr_number" class="form-control" value="<?= esc($oldOr('cpr_number')) ?>" placeholder="e.g. 123456789">
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Nationality</label>
            <input type="text" name="nationality" class="form-control" value="<?= esc($oldOr('nationality')) ?>">
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Date of Birth</label>
            <input type="date" name="date_of_birth" class="form-control" value="<?= esc($oldOr('date_of_birth')) ?>">
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Gender</label>
            <select name="gender" class="form-select">
                <option value="">— Select —</option>
                <?php foreach (['male', 'female', 'other'] as $g): ?>
                    <option value="<?= $g ?>" <?= $oldOr('gender') === $g ? 'selected' : '' ?>><?= esc(ucfirst($g)) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Marital Status</label>
            <select name="marital_status" class="form-select">
                <option value="">— Select —</option>
                <?php foreach (['single', 'married', 'divorced', 'widowed'] as $m): ?>
                    <option value="<?= $m ?>" <?= $oldOr('marital_status') === $m ? 'selected' : '' ?>><?= esc(ucfirst($m)) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Email</label>
            <input type="email" name="email" class="form-control" value="<?= esc($oldOr('email')) ?>">
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Phone</label>
            <input type="text" name="phone" class="form-control" value="<?= esc($oldOr('phone')) ?>">
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Mobile</label>
            <input type="text" name="mobile" class="form-control" value="<?= esc($oldOr('mobile')) ?>">
        </div>
        <div class="enterprise-field enterprise-settings-fields--single" style="grid-column:1/-1;">
            <label class="enterprise-field-label">Address</label>
            <textarea name="address" class="form-control" rows="2"><?= esc($oldOr('address')) ?></textarea>
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">City</label>
            <input type="text" name="city" class="form-control" value="<?= esc($oldOr('city')) ?>">
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Country</label>
            <input type="text" name="country" class="form-control" value="<?= esc($oldOr('country', 'Bahrain')) ?>">
        </div>
    </div>
</article>

<article class="enterprise-settings-card mb-3">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-building-columns"></i>
        <h2 class="enterprise-settings-card__title">Emergency & Payroll</h2>
    </div>
    <div class="enterprise-settings-fields">
        <div class="enterprise-field">
            <label class="enterprise-field-label">Emergency Contact Name</label>
            <input type="text" name="emergency_contact_name" class="form-control" value="<?= esc($oldOr('emergency_contact_name')) ?>">
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Emergency Contact Phone</label>
            <input type="text" name="emergency_contact_phone" class="form-control" value="<?= esc($oldOr('emergency_contact_phone')) ?>">
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Bank Name</label>
            <input type="text" name="bank_name" class="form-control" value="<?= esc($oldOr('bank_name')) ?>">
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Bank Account (IBAN)</label>
            <input type="text" name="bank_account" class="form-control" value="<?= esc($oldOr('bank_account')) ?>">
        </div>
        <div class="enterprise-field enterprise-settings-fields--single" style="grid-column:1/-1;">
            <label class="enterprise-field-label">Notes</label>
            <textarea name="notes" class="form-control" rows="3"><?= esc($oldOr('notes')) ?></textarea>
        </div>
    </div>
</article>

<article class="enterprise-settings-card mb-3">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-file-image"></i>
        <h2 class="enterprise-settings-card__title">Documents & Photo</h2>
    </div>
    <div class="enterprise-settings-fields">
        <div class="enterprise-field">
            <label class="enterprise-field-label">Employee Photo</label>
            <input type="file" name="photo" class="form-control" accept="image/jpeg,image/png,image/webp">
            <?php if ($isEdit && ! empty($emp['photo_path'])): ?>
                <div class="form-text small">Current: <a href="<?= base_url($emp['photo_path']) ?>" target="_blank">View photo</a></div>
            <?php endif; ?>
        </div>
        <div class="enterprise-field">
            <label class="enterprise-field-label">CPR Document (scan)</label>
            <input type="file" name="cpr_document" class="form-control" accept="image/jpeg,image/png,image/webp,application/pdf">
            <?php if ($isEdit && ! empty($emp['cpr_document_path'])): ?>
                <div class="form-text small">Current: <a href="<?= base_url($emp['cpr_document_path']) ?>" target="_blank">View document</a></div>
            <?php endif; ?>
        </div>
    </div>
</article>

<script>
function toggleCustomId() {
    const cb = document.getElementById('use_custom_id');
    const input = document.getElementById('employee_number');
    const autoId = <?= json_encode($nextEmployeeId ?? ($emp['employee_number'] ?? '')) ?>;

    if (cb.checked) {
        input.removeAttribute('readonly');
        if (!input.value || input.value === autoId) {
            input.value = '';
        }
        input.focus();
    } else {
        input.setAttribute('readonly', 'readonly');
        input.value = autoId;
    }
}
document.addEventListener('DOMContentLoaded', function () {
    if (document.getElementById('use_custom_id')?.checked) {
        toggleCustomId();
    }
});
</script>
