@extends('layouts.app')

@section('title', 'إضافة هيكل راتب جديد')

@section('content')
<div class="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 py-8 px-4 md:px-8">
    <div class="max-w-4xl mx-auto">
        <div class="mb-8 flex items-center justify-between">
            <div>
                <h1 class="text-3xl font-bold text-gray-900 mb-2">إضافة هيكل راتب جديد</h1>
                <p class="text-gray-600">قم بتحديد نسب توزيع الراتب والبدلات</p>
            </div>
            <a href="{{ route('salary-structures.index') }}" class="inline-flex items-center px-4 py-2 bg-white border border-gray-300 rounded-lg text-gray-700 font-medium hover:bg-gray-50 transition-all duration-200 shadow-sm">
                <i class="fas fa-arrow-right ml-2"></i>
                <span>العودة للقائمة</span>
            </a>
        </div>

        @if($errors->any())
        <div class="bg-red-50 border-l-4 border-red-500 p-4 mb-6 rounded-r-lg shadow-sm">
            <div class="flex">
                <div class="flex-shrink-0">
                    <i class="fas fa-exclamation-circle text-red-500 text-xl"></i>
                </div>
                <div class="mr-3">
                    <h3 class="text-red-800 font-bold mb-1">يوجد بعض الأخطاء في البيانات:</h3>
                    <ul class="list-disc list-inside text-red-700 text-sm">
                        @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            </div>
        </div>
        @endif

        <div class="bg-white rounded-2xl shadow-xl overflow-hidden border border-gray-100">
            <form action="{{ route('salary-structures.store') }}" method="POST" class="p-8">
                @csrf

                <div class="grid grid-cols-1 md:grid-cols-2 gap-8 mb-8">
                    <div class="col-span-1 md:col-span-2">
                        <label for="name" class="block text-sm font-semibold text-gray-700 mb-2">اسم الهيكل <span class="text-red-500">*</span></label>
                        <input type="text" name="name" id="name" value="{{ old('name') }}" required
                            class="w-full px-4 py-3 rounded-xl border border-gray-200 focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200 @error('name') border-red-500 @enderror"
                            placeholder="مثال: الهيكل الموحد للموظفين الإداريين">
                    </div>

                    <div class="col-span-1 md:col-span-2">
                        <label for="description" class="block text-sm font-semibold text-gray-700 mb-2">الوصف</label>
                        <textarea name="description" id="description" rows="3"
                            class="w-full px-4 py-3 rounded-xl border border-gray-200 focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200">{{ old('description') }}</textarea>
                    </div>

                    <div class="bg-blue-50/50 p-6 rounded-2xl border border-blue-100 col-span-1 md:col-span-2">
                        <h3 class="text-blue-900 font-bold mb-4 flex items-center">
                            <i class="fas fa-percent ml-2"></i>
                            توزيع النسب (يجب أن يكون المجموع 100%)
                        </h3>
                        
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div>
                                <label for="basic_salary_percentage" class="block text-sm font-medium text-gray-700 mb-2">الراتب الأساسي (%) <span class="text-red-500">*</span></label>
                                <div class="relative">
                                    <input type="number" step="0.01" min="0" max="100" name="basic_salary_percentage" id="basic_salary_percentage" value="{{ old('basic_salary_percentage', 0) }}" required
                                        class="w-full pl-10 pr-4 py-3 rounded-xl border border-gray-200 focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200">
                                    <div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none text-gray-400">
                                        %
                                    </div>
                                </div>
                            </div>

                            <div>
                                <label for="housing_allowance_percentage" class="block text-sm font-medium text-gray-700 mb-2">بدل السكن (%) <span class="text-red-500">*</span></label>
                                <div class="relative">
                                    <input type="number" step="0.01" min="0" max="100" name="housing_allowance_percentage" id="housing_allowance_percentage" value="{{ old('housing_allowance_percentage', 0) }}" required
                                        class="w-full pl-10 pr-4 py-3 rounded-xl border border-gray-200 focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200">
                                    <div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none text-gray-400">
                                        %
                                    </div>
                                </div>
                            </div>

                            <div>
                                <label for="transportation_allowance_percentage" class="block text-sm font-medium text-gray-700 mb-2">بدل المواصلات (%) <span class="text-red-500">*</span></label>
                                <div class="relative">
                                    <input type="number" step="0.01" min="0" max="100" name="transportation_allowance_percentage" id="transportation_allowance_percentage" value="{{ old('transportation_allowance_percentage', 0) }}" required
                                        class="w-full pl-10 pr-4 py-3 rounded-xl border border-gray-200 focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200">
                                    <div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none text-gray-400">
                                        %
                                    </div>
                                </div>
                            </div>

                            <div>
                                <label for="other_allowances_percentage" class="block text-sm font-medium text-gray-700 mb-2">بدلات أخرى (%) <span class="text-red-500">*</span></label>
                                <div class="relative">
                                    <input type="number" step="0.01" min="0" max="100" name="other_allowances_percentage" id="other_allowances_percentage" value="{{ old('other_allowances_percentage', 0) }}" required
                                        class="w-full pl-10 pr-4 py-3 rounded-xl border border-gray-200 focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200">
                                    <div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none text-gray-400">
                                        %
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="mt-4 pt-4 border-t border-blue-100 flex justify-between items-center font-bold text-blue-900">
                            <span>الإجمالي:</span>
                            <span id="total-percentage">0%</span>
                        </div>
                        @error('percentage_sum')
                        <div class="mt-2 text-sm text-red-600 font-bold">
                            {{ $message }}
                        </div>
                        @enderror
                    </div>

                    <div class="col-span-1 md:col-span-2">
                        <div class="flex items-center">
                            <input type="checkbox" name="is_default" id="is_default" value="1" {{ old('is_default') ? 'checked' : '' }}
                                class="w-5 h-5 text-blue-600 border-gray-300 rounded focus:ring-blue-500">
                            <label for="is_default" class="mr-3 block text-sm font-semibold text-gray-700">
                                تعيين كنموذج افتراضي
                                <span class="text-xs font-normal text-gray-500 block">سيتم استخدامه تلقائياً عند إضافة عقود موظفين جدد</span>
                            </label>
                        </div>
                    </div>
                </div>

                <div class="flex items-center justify-end pt-6 border-t border-gray-100 gap-4">
                    <button type="reset" class="px-6 py-3 border border-gray-300 rounded-xl text-gray-700 font-semibold hover:bg-gray-50 transition-all duration-200">
                        إعادة تعيين
                    </button>
                    <button type="submit" class="px-10 py-3 bg-gradient-to-r from-blue-600 to-blue-700 text-white font-bold rounded-xl hover:from-blue-700 hover:to-blue-800 transition-all duration-200 shadow-lg hover:shadow-xl transform hover:scale-[1.02]">
                        <i class="fas fa-save ml-2"></i>
                        حفظ هيكل الراتب
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

@push('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const inputs = [
            'basic_salary_percentage',
            'housing_allowance_percentage',
            'transportation_allowance_percentage',
            'other_allowances_percentage'
        ];
        
        const totalDisplay = document.getElementById('total-percentage');
        
        function calculateTotal() {
            let total = 0;
            inputs.forEach(id => {
                const val = parseFloat(document.getElementById(id).value) || 0;
                total += val;
            });
            
            totalDisplay.textContent = total.toFixed(2) + '%';
            
            if (Math.abs(total - 100) < 0.01) {
                totalDisplay.classList.remove('text-red-600');
                totalDisplay.classList.add('text-green-600');
            } else {
                totalDisplay.classList.remove('text-green-600');
                totalDisplay.classList.add('text-red-600');
            }
        }
        
        inputs.forEach(id => {
            document.getElementById(id).addEventListener('input', calculateTotal);
        });
        
        calculateTotal();
    });
</script>
@endpush
@endsection
