@extends('layouts.app')

@section('title', 'Add Promotion')
@section('heading', 'Add Promotion')

@section('content')
    <x-card class="max-w-3xl">
        <form action="{{ route('promotions.store') }}" method="POST" class="space-y-6" novalidate data-promotion-form="create">
            @csrf
            <div class="grid gap-4 sm:grid-cols-2">
                <div>
                    <label class="block text-sm font-medium text-slate-700">Employee <span class="text-red-500">*</span></label>
                    <select id="promotion_user_id" name="user_id" class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 js-select2">
                        <option value="">Select</option>
                        @foreach($users as $u)
                            <option
                                value="{{ $u->id }}"
                                data-designation-id="{{ $u->designation_id }}"
                                data-department-id="{{ $u->department_id }}"
                                {{ old('user_id') == $u->id ? 'selected' : '' }}
                            >
                                {{ $u->name }}
                            </option>
                        @endforeach
                    </select>
                    @error('user_id')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                    <p data-js-error="user_id" class="mt-1 text-sm text-red-600 hidden"></p>
                </div>
                <div>
                    <label class="block text-sm font-medium text-slate-700">Promotion Date <span class="text-red-500">*</span></label>
                    <input type="date" name="promotion_date" value="{{ old('promotion_date', date('Y-m-d')) }}" class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2">
                    @error('promotion_date')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                    <p data-js-error="promotion_date" class="mt-1 text-sm text-red-600 hidden"></p>
                </div>
                <div>
                    <label class="block text-sm font-medium text-slate-700">From Designation</label>
                    <select id="promotion_from_designation_id" name="from_designation_id" class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 js-select2">
                        <option value="">—</option>
                        @foreach($designations as $d)<option value="{{ $d->id }}" {{ old('from_designation_id') == $d->id ? 'selected' : '' }}>{{ $d->name }}</option>@endforeach
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-medium text-slate-700">To Designation</label>
                    <select name="to_designation_id" class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 js-select2">
                        <option value="">—</option>
                        @foreach($designations as $d)<option value="{{ $d->id }}" {{ old('to_designation_id') == $d->id ? 'selected' : '' }}>{{ $d->name }}</option>@endforeach
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-medium text-slate-700">From Department</label>
                    <select id="promotion_from_department_id" name="from_department_id" class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 js-select2">
                        <option value="">—</option>
                        @foreach($departments as $d)<option value="{{ $d->id }}" {{ old('from_department_id') == $d->id ? 'selected' : '' }}>{{ $d->name }}</option>@endforeach
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-medium text-slate-700">To Department</label>
                    <select name="to_department_id" class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 js-select2">
                        <option value="">—</option>
                        @foreach($departments as $d)<option value="{{ $d->id }}" {{ old('to_department_id') == $d->id ? 'selected' : '' }}>{{ $d->name }}</option>@endforeach
                    </select>
                </div>
                <div class="sm:col-span-2">
                    <label class="block text-sm font-medium text-slate-700">Notes</label>
                    <textarea name="notes" rows="2" class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2">{{ old('notes') }}</textarea>
                    @error('notes')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                    <p data-js-error="notes" class="mt-1 text-sm text-red-600 hidden"></p>
                </div>
            </div>
            <div class="flex gap-2">
                <x-button type="submit" variant="primary">Save</x-button>
                <a href="{{ route('promotions.index') }}" class="inline-flex items-center rounded-lg bg-slate-100 px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-200">Cancel</a>
            </div>
        </form>
    </x-card>
@endsection

@push('scripts')
<script src="{{ asset('js/form-validation.js') }}"></script>
<script src="{{ asset('js/form-text-rules.js') }}"></script>
<script src="{{ asset('js/hr-lifecycle-form-validation.js') }}"></script>
<script>
    (function () {
        const employeeSelect = document.getElementById('promotion_user_id');
        const fromDesignationSelect = document.getElementById('promotion_from_designation_id');
        const fromDepartmentSelect = document.getElementById('promotion_from_department_id');

        if (!employeeSelect || !fromDesignationSelect || !fromDepartmentSelect) {
            return;
        }

        const applyEmployeeDefaults = (force) => {
            const selectedOption = employeeSelect.options[employeeSelect.selectedIndex];
            if (!selectedOption || !selectedOption.value) {
                return;
            }

            const designationId = selectedOption.dataset.designationId || '';
            const departmentId = selectedOption.dataset.departmentId || '';

            if (force || !fromDesignationSelect.value) {
                setSelectValue(fromDesignationSelect, designationId);
            }
            if (force || !fromDepartmentSelect.value) {
                setSelectValue(fromDepartmentSelect, departmentId);
            }
        };

        const setSelectValue = (select, value) => {
            const next = value || '';
            if (window.jQuery && window.jQuery(select).data('select2')) {
                window.jQuery(select).val(next).trigger('change.select2');
                return;
            }
            select.value = next;
        };

        employeeSelect.addEventListener('change', function () {
            applyEmployeeDefaults(true);
        });

        applyEmployeeDefaults(false);
    })();
</script>
@endpush
