@extends('layouts.user')

@section('title', __('salary.salary_history'))
@section('page_title', __('salary.salary_history'))
@section('breadcrumb', __('salary.hr_payroll'))

@section('content')
<div class="w-full px-1 sm:px-2 lg:px-3 space-y-6">

    <div class="mb-4 flex items-center justify-between">
        <div>
            <h1 class="text-lg font-bold text-slate-800">{{ __('salary.salary_history') }}</h1>
            <p class="mt-1 text-xs text-slate-500">
                {{ $employee->name }} ({{ $employee->employee_code }})
            </p>
        </div>

        <div class="flex items-center gap-2">
            @if($employee->currentSalaryStructure)
                <a href="{{ route('user.hr.salary.edit', $employee->id) }}"
                   class="border border-amber-200 bg-amber-50 px-3 py-1.5 text-xs font-semibold text-amber-700 hover:bg-amber-100">
                    {{ __('salary.change_rate') }}
                </a>
            @else
                <a href="{{ route('user.hr.salary.create', $employee->id) }}"
                   class="border border-blue-200 bg-blue-50 px-3 py-1.5 text-xs font-semibold text-blue-700 hover:bg-blue-100">
                    {{ __('salary.setup_salary') }}
                </a>
            @endif

            <a href="{{ route('user.hr.salary.index') }}"
               class="border border-slate-200 px-3 py-1.5 text-xs font-semibold text-slate-700 hover:bg-slate-50">
                {{ __('salary.back') }}
            </a>
        </div>
    </div>

    {{-- Summary --}}
    <div class="mb-4 grid grid-cols-1 gap-3 md:grid-cols-3">
        <div class="border border-slate-200 bg-white p-3 shadow-sm">
            <p class="text-[11px] font-semibold uppercase tracking-wide text-slate-500">{{ __('salary.employee_type') }}</p>
            <p class="mt-1 text-sm font-bold text-slate-800">
                {{ $employee->employee_type === 'monthly' ? __('salary.monthly_salary') : __('salary.daily_roz') }}
            </p>
        </div>

        <div class="border border-slate-200 bg-white p-3 shadow-sm">
            <p class="text-[11px] font-semibold uppercase tracking-wide text-slate-500">{{ __('salary.current_salary_rate') }}</p>
            <p class="mt-1 text-sm font-bold text-slate-800">
                @if($employee->currentSalaryStructure)
                    @if($employee->employee_type === 'monthly')
                        ৳ {{ number_format($employee->currentSalaryStructure->monthly_salary, 2) }}/{{ __('salary.month') }}
                    @else
                        ৳ {{ number_format($employee->currentSalaryStructure->per_roz_rate, 2) }}/{{ __('salary.roz') }}
                    @endif
                @else
                    {{ __('salary.not_set') }}
                @endif
            </p>
        </div>

        <div class="border border-slate-200 bg-white p-3 shadow-sm">
            <p class="text-[11px] font-semibold uppercase tracking-wide text-slate-500">{{ __('salary.current_effective_from') }}</p>
            <p class="mt-1 text-sm font-bold text-slate-800">
                {{ optional($employee->currentSalaryStructure?->effective_from)->format('d M Y') ?? '-' }}
            </p>
        </div>
    </div>

    {{-- Table --}}
    <div class="overflow-hidden border border-slate-200 bg-white shadow-sm">
        <div class="overflow-x-auto">
            <table class="min-w-full text-xs">
                <thead class="bg-slate-50">
                    <tr class="text-left text-slate-600">
                        <th class="px-4 py-3 font-semibold">{{ __('salary.effective_from') }}</th>
                        <th class="px-4 py-3 font-semibold">{{ __('salary.effective_to') }}</th>
                        <th class="px-4 py-3 font-semibold">{{ __('salary.salary_type') }}</th>

                        @if($employee->employee_type === 'monthly')
                            <th class="px-4 py-3 font-semibold">{{ __('salary.monthly_salary') }}</th>
                            <th class="px-4 py-3 font-semibold">{{ __('salary.ot_rate') }}</th>
                        @else
                            <th class="px-4 py-3 font-semibold">{{ __('salary.per_roz_rate') }}</th>
                        @endif

                        <th class="px-4 py-3 font-semibold">{{ __('salary.change_note') }}</th>
                    </tr>
                </thead>

                <tbody class="divide-y divide-slate-100">
                    @forelse($employee->salaryHistories as $history)
                        <tr class="hover:bg-slate-50/70">
                            <td class="px-4 py-3">
                                {{ optional($history->effective_from)->format('d M Y') }}
                            </td>

                            <td class="px-4 py-3">
                                {{ optional($history->effective_to)->format('d M Y') ?? __('salary.current') }}
                            </td>

                            <td class="px-4 py-3">
                                {{ $history->salary_type === 'monthly' ? __('salary.monthly') : __('salary.daily_roz') }}
                            </td>

                            @if($employee->employee_type === 'monthly')
                                <td class="px-4 py-3">
                                    ৳ {{ number_format($history->monthly_salary, 2) }}
                                </td>

                                <td class="px-4 py-3">
                                    ৳ {{ number_format($history->overtime_rate, 2) }}
                                </td>
                            @else
                                <td class="px-4 py-3">
                                    ৳ {{ number_format($history->per_roz_rate, 2) }}
                                </td>
                            @endif

                            <td class="px-4 py-3">
                                {{ $history->change_note ?: '-' }}
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="{{ $employee->employee_type === 'monthly' ? 6 : 5 }}"
                                class="px-4 py-10 text-center text-xs text-slate-500">
                                {{ __('salary.no_salary_history_found') }}
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

</div>
@endsection