@extends('layouts.app')
@section('title','Add Employee')
@section('page-title','Add Employee')

@section('content')
<div class="page-header">
    <div class="d-flex align-items-center gap-2 flex-wrap">
        <a href="{{ route('employees.index') }}" class="btn btn-sm btn-outline-secondary"><i class="bi bi-arrow-left"></i></a>
        <div>
            <h1>New Employee</h1>
            <nav aria-label="breadcrumb"><ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="{{ route('employees.index') }}">Employees</a></li>
                <li class="breadcrumb-item active">New</li>
            </ol></nav>
        </div>
    </div>
</div>

<form method="POST" action="{{ route('employees.store') }}" enctype="multipart/form-data">
@csrf
<div class="row g-3">

    {{-- ── Personal Information ── --}}
    <div class="col-12">
        <div class="card">
            <div class="card-header"><i class="bi bi-person me-2" style="color:var(--ul-primary);"></i>Personal Information</div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-12 col-sm-4">
                        <label class="form-label">Last Name *</label>
                        <input type="text" name="last_name" class="form-control" value="{{ old('last_name') }}" required>
                    </div>
                    <div class="col-12 col-sm-4">
                        <label class="form-label">First Name *</label>
                        <input type="text" name="first_name" class="form-control" value="{{ old('first_name') }}" required>
                    </div>
                    <div class="col-12 col-sm-4">
                        <label class="form-label">Middle Name</label>
                        <input type="text" name="middle_name" class="form-control" value="{{ old('middle_name') }}">
                    </div>
                    <div class="col-12">
                        <label class="form-label">Present Address</label>
                        <input type="text" name="present_address" class="form-control" value="{{ old('present_address') }}" placeholder="Street, Community, City, County">
                    </div>
                    <div class="col-12">
                        <label class="form-label">Permanent Address</label>
                        <input type="text" name="permanent_address" class="form-control" value="{{ old('permanent_address') }}" placeholder="If different from present address">
                    </div>
                    <div class="col-6 col-sm-3">
                        <label class="form-label">Date of Birth</label>
                        <input type="date" name="date_of_birth" class="form-control" value="{{ old('date_of_birth') }}">
                    </div>
                    <div class="col-6 col-sm-3">
                        <label class="form-label">Place of Birth</label>
                        <input type="text" name="place_of_birth" class="form-control" value="{{ old('place_of_birth') }}">
                    </div>
                    <div class="col-6 col-sm-3">
                        <label class="form-label">Gender</label>
                        <select name="gender" class="form-select">
                            <option value="">— Select —</option>
                            <option value="Male"   {{ old('gender')==='Male'?'selected':'' }}>Male</option>
                            <option value="Female" {{ old('gender')==='Female'?'selected':'' }}>Female</option>
                        </select>
                    </div>
                    <div class="col-6 col-sm-3">
                        <label class="form-label">Marital Status</label>
                        <select name="marital_status" class="form-select">
                            <option value="">— Select —</option>
                            @foreach(['Single','Married','Divorced','Widowed'] as $ms)
                            <option value="{{ $ms }}" {{ old('marital_status')===$ms?'selected':'' }}>{{ $ms }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-6 col-sm-3">
                        <label class="form-label">Citizenship</label>
                        <input type="text" name="citizenship" class="form-control" value="{{ old('citizenship') }}">
                    </div>
                    <div class="col-6 col-sm-3">
                        <label class="form-label">National ID No.</label>
                        <input type="text" name="national_id" class="form-control" value="{{ old('national_id') }}">
                    </div>
                    <div class="col-6 col-sm-3">
                        <label class="form-label">Passport No.</label>
                        <input type="text" name="passport_number" class="form-control" value="{{ old('passport_number') }}">
                    </div>
                    <div class="col-6 col-sm-3">
                        <label class="form-label">Physical Limitation</label>
                        <input type="text" name="physical_limitation" class="form-control" value="{{ old('physical_limitation') }}" placeholder="None if none">
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{-- ── Employment Details ── --}}
    <div class="col-12">
        <div class="card">
            <div class="card-header"><i class="bi bi-briefcase me-2" style="color:var(--ul-primary);"></i>Employment Details</div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-12 col-sm-6">
                        <label class="form-label">Position / Title *</label>
                        <input type="text" name="position" class="form-control" value="{{ old('position') }}" required placeholder="e.g. Lecturer I, Finance Officer">
                    </div>
                    <div class="col-12 col-sm-6">
                        <label class="form-label">Department *</label>
                        <select name="department_id" class="form-select" required>
                            <option value="">— Select Department —</option>
                            @foreach($departments as $d)
                            <option value="{{ $d->id }}" {{ old('department_id')==$d->id?'selected':'' }}>{{ $d->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-6 col-sm-3">
                        <label class="form-label">Date of Employment *</label>
                        <input type="date" name="date_of_employment" class="form-control" value="{{ old('date_of_employment', today()->format('Y-m-d')) }}" required>
                    </div>
                    <div class="col-6 col-sm-3">
                        <label class="form-label">Employment Type *</label>
                        <select name="employment_type" class="form-select" required>
                            <option value="full_time"  {{ old('employment_type','full_time')==='full_time'?'selected':'' }}>Full Time</option>
                            <option value="part_time"  {{ old('employment_type')==='part_time'?'selected':'' }}>Part Time</option>
                            <option value="contract"   {{ old('employment_type')==='contract'?'selected':'' }}>Contract</option>
                            <option value="temporary"  {{ old('employment_type')==='temporary'?'selected':'' }}>Temporary</option>
                        </select>
                    </div>
                    <div class="col-6 col-sm-3">
                        <label class="form-label">Salary (USD)</label>
                        <input type="number" name="salary_usd" class="form-control" value="{{ old('salary_usd') }}" step="0.01" min="0" placeholder="0.00">
                    </div>
                    <div class="col-6 col-sm-3">
                        <label class="form-label">Salary (LRD)</label>
                        <input type="number" name="salary_lrd" class="form-control" value="{{ old('salary_lrd') }}" step="0.01" min="0" placeholder="0.00">
                    </div>
                    <div class="col-6 col-sm-4">
                        <label class="form-label">Role *</label>
                        <select name="role" class="form-select" required>
                            <option value="employee"  {{ old('role','employee')==='employee'?'selected':'' }}>Employee</option>
                            <option value="dept_head" {{ old('role')==='dept_head'?'selected':'' }}>Department Head</option>
                            <option value="hr_admin"  {{ old('role')==='hr_admin'?'selected':'' }}>HR Administrator</option>
                        </select>
                    </div>
                    <div class="col-6 col-sm-4">
                        <label class="form-label">Email Address *</label>
                        <input type="email" name="email" class="form-control" value="{{ old('email') }}" required placeholder="name@company.com">
                    </div>
                    <div class="col-6 col-sm-4">
                        <label class="form-label">Phone</label>
                        <input type="text" name="phone" class="form-control" value="{{ old('phone') }}" placeholder="+231 880 000 000">
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{-- ── Bank Details ── --}}
    <div class="col-12 col-md-6">
        <div class="card h-100">
            <div class="card-header"><i class="bi bi-bank me-2" style="color:var(--ul-primary);"></i>Bank Details</div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-12">
                        <label class="form-label">Bank Name</label>
                        <input type="text" name="bank_name" class="form-control" value="{{ old('bank_name') }}">
                    </div>
                    <div class="col-12">
                        <label class="form-label">Account Number</label>
                        <input type="text" name="bank_account" class="form-control" value="{{ old('bank_account') }}">
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{-- ── Emergency Contact ── --}}
    <div class="col-12 col-md-6">
        <div class="card h-100">
            <div class="card-header"><i class="bi bi-telephone me-2" style="color:var(--ul-primary);"></i>Emergency Contact</div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-12">
                        <label class="form-label">Full Name</label>
                        <input type="text" name="emergency_contact_name" class="form-control" value="{{ old('emergency_contact_name') }}">
                    </div>
                    <div class="col-6">
                        <label class="form-label">Phone</label>
                        <input type="text" name="emergency_contact_phone" class="form-control" value="{{ old('emergency_contact_phone') }}">
                    </div>
                    <div class="col-6">
                        <label class="form-label">Relationship</label>
                        <input type="text" name="emergency_contact_relation" class="form-control" value="{{ old('emergency_contact_relation') }}" placeholder="e.g. Spouse, Parent">
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{-- ── Education ── --}}
    <div class="col-12">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span><i class="bi bi-mortarboard me-2" style="color:var(--ul-primary);"></i>Educational Background</span>
                <button type="button" class="btn btn-sm btn-outline-primary" onclick="addEducation()"><i class="bi bi-plus-lg me-1"></i>Add</button>
            </div>
            <div class="card-body" id="educationRows">
                <div class="edu-row border rounded-3 p-3 mb-2">
                    <div class="row g-2">
                        <div class="col-12 col-sm-4">
                            <label class="form-label">Level</label>
                            <select name="education[0][level]" class="form-select form-select-sm">
                                <option value="">— Select —</option>
                                @foreach(['high_school'=>'High School','undergraduate'=>'Undergraduate','graduate'=>'Graduate','post_graduate'=>'Post Graduate','professional'=>'Professional','other'=>'Other'] as $val=>$label)
                                <option value="{{ $val }}">{{ $label }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-12 col-sm-4">
                            <label class="form-label">Institution Name</label>
                            <input type="text" name="education[0][institution_name]" class="form-control form-control-sm">
                        </div>
                        <div class="col-6 col-sm-2">
                            <label class="form-label">From</label>
                            <input type="date" name="education[0][attendance_from]" class="form-control form-control-sm">
                        </div>
                        <div class="col-6 col-sm-2">
                            <label class="form-label">To</label>
                            <input type="date" name="education[0][attendance_to]" class="form-control form-control-sm">
                        </div>
                        <div class="col-6 col-sm-4">
                            <label class="form-label">Certificate / Degree</label>
                            <input type="text" name="education[0][certificate_degree]" class="form-control form-control-sm">
                        </div>
                        <div class="col-6 col-sm-4">
                            <label class="form-label">Major</label>
                            <input type="text" name="education[0][major]" class="form-control form-control-sm">
                        </div>
                        <div class="col-6 col-sm-2 d-flex align-items-end">
                            <label class="form-label d-flex align-items-center gap-2 mb-0">
                                <input type="checkbox" name="education[0][graduated]" value="1"> Graduated
                            </label>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{-- ── Previous Employment ── --}}
    <div class="col-12">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span><i class="bi bi-building me-2" style="color:var(--ul-primary);"></i>Previous Employment History</span>
                <button type="button" class="btn btn-sm btn-outline-primary" onclick="addEmployment()"><i class="bi bi-plus-lg me-1"></i>Add</button>
            </div>
            <div class="card-body" id="employmentRows">
                <div class="emp-row border rounded-3 p-3 mb-2">
                    <div class="row g-2">
                        <div class="col-12 col-sm-4">
                            <label class="form-label">Employer / Organisation</label>
                            <input type="text" name="employment_history[0][employer_name]" class="form-control form-control-sm">
                        </div>
                        <div class="col-12 col-sm-4">
                            <label class="form-label">Position Held</label>
                            <input type="text" name="employment_history[0][position]" class="form-control form-control-sm">
                        </div>
                        <div class="col-6 col-sm-2">
                            <label class="form-label">From</label>
                            <input type="date" name="employment_history[0][date_from]" class="form-control form-control-sm">
                        </div>
                        <div class="col-6 col-sm-2">
                            <label class="form-label">To</label>
                            <input type="date" name="employment_history[0][date_to]" class="form-control form-control-sm">
                        </div>
                        <div class="col-12 col-sm-6">
                            <label class="form-label">Reason for Leaving</label>
                            <input type="text" name="employment_history[0][reason_for_leaving]" class="form-control form-control-sm">
                        </div>
                        <div class="col-6 col-sm-3">
                            <label class="form-label">Highest Salary</label>
                            <input type="number" name="employment_history[0][highest_salary]" class="form-control form-control-sm" step="0.01">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{-- ── References ── --}}
    <div class="col-12">
        <div class="card">
            <div class="card-header"><i class="bi bi-people me-2" style="color:var(--ul-primary);"></i>Professional References</div>
            <div class="card-body">
                <div class="row g-3">
                    @for($r = 0; $r < 2; $r++)
                    <div class="col-12 col-md-6">
                        <div class="border rounded-3 p-3">
                            <div class="fw-semibold mb-2" style="font-size:.82rem;color:var(--ul-primary);">Reference {{ $r + 1 }}</div>
                            <div class="row g-2">
                                <div class="col-12"><label class="form-label">Full Name</label>
                                    <input type="text" name="references[{{ $r }}][name]" class="form-control form-control-sm"></div>
                                <div class="col-6"><label class="form-label">Title</label>
                                    <input type="text" name="references[{{ $r }}][title]" class="form-control form-control-sm"></div>
                                <div class="col-6"><label class="form-label">Phone</label>
                                    <input type="text" name="references[{{ $r }}][phone]" class="form-control form-control-sm"></div>
                                <div class="col-12"><label class="form-label">Email</label>
                                    <input type="email" name="references[{{ $r }}][email]" class="form-control form-control-sm"></div>
                                <div class="col-12"><label class="form-label">Address</label>
                                    <input type="text" name="references[{{ $r }}][address]" class="form-control form-control-sm"></div>
                            </div>
                        </div>
                    </div>
                    @endfor
                </div>
            </div>
        </div>
    </div>

    {{-- ── Special Skills / Notes ── --}}
    <div class="col-12">
        <div class="card">
            <div class="card-header"><i class="bi bi-star me-2" style="color:var(--ul-primary);"></i>Additional Information</div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-12 col-md-6">
                        <label class="form-label">Special Skills</label>
                        <textarea name="special_skills" class="form-control" rows="3" placeholder="List any special skills or competencies…">{{ old('special_skills') }}</textarea>
                    </div>
                    <div class="col-12 col-md-6">
                        <label class="form-label">Professional Memberships</label>
                        <textarea name="professional_memberships" class="form-control" rows="3" placeholder="Professional bodies, associations…">{{ old('professional_memberships') }}</textarea>
                    </div>
                    <div class="col-12 col-md-6">
                        <label class="form-label">Relatives at UL</label>
                        <textarea name="relatives_at_ul" class="form-control" rows="2" placeholder="Name, relationship, department…">{{ old('relatives_at_ul') }}</textarea>
                    </div>
                    <div class="col-12 col-md-6">
                        <label class="form-label">Notes</label>
                        <textarea name="notes" class="form-control" rows="2">{{ old('notes') }}</textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{-- ── Submit ── --}}
    <div class="col-12">
        <div class="card">
            <div class="card-body d-flex gap-2 align-items-center">
                <button type="submit" class="btn btn-primary px-4">
                    <i class="bi bi-person-plus me-2"></i>Create Employee
                </button>
                <a href="{{ route('employees.index') }}" class="btn btn-outline-secondary">Cancel</a>
                <span style="font-size:.78rem;color:var(--text-2);margin-left:auto;">
                    <i class="bi bi-info-circle me-1"></i>
                    A login account and employee number will be generated automatically.
                </span>
            </div>
        </div>
    </div>

</div>
</form>
@endsection

@push('scripts')
<script>
let eduCount = 1;
function addEducation() {
    const row = document.querySelector('.edu-row').cloneNode(true);
    row.querySelectorAll('input,select').forEach(el => {
        el.name = el.name.replace(/\[\d+\]/, `[${eduCount}]`);
        el.value = '';
        if (el.type === 'checkbox') el.checked = false;
    });
    document.getElementById('educationRows').appendChild(row);
    eduCount++;
}
let empCount = 1;
function addEmployment() {
    const row = document.querySelector('.emp-row').cloneNode(true);
    row.querySelectorAll('input').forEach(el => {
        el.name = el.name.replace(/\[\d+\]/, `[${empCount}]`);
        el.value = '';
    });
    document.getElementById('employmentRows').appendChild(row);
    empCount++;
}
</script>
@endpush
