@extends('layouts.user')

@section('title', __('salary.change_salary_rate'))
@section('page_title', __('salary.change_salary_rate'))
@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.change_salary_rate') }}</h1>
            <p class="mt-1 text-xs text-slate-500">
                {{ $employee->name }} ({{ $employee->employee_code }})
            </p>
        </div>
        <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 class="mb-4 grid grid-cols-1 gap-3 md:grid-cols-3">
        <div class="border border-slate-200 bg-white p-3">
            <p class="text-[11px] text-slate-500">{{ __('salary.current_type') }}</p>
            <p class="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">
            <p class="text-[11px] text-slate-500">{{ __('salary.current_salary_rate') }}</p>
            <p class="text-sm font-bold text-slate-800">
                @if($employee->employee_type === 'monthly')
                    ৳ {{ number_format($employee->currentSalaryStructure->monthly_salary, 2) }}
                @else
                    ৳ {{ number_format($employee->currentSalaryStructure->per_roz_rate, 2) }}
                @endif
            </p>
        </div>

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

    <form action="{{ route('user.hr.salary.update', $employee->id) }}" method="POST"
          class="border border-slate-200 bg-white p-4">
        @csrf
        @method('PUT')

        <div class="grid grid-cols-1 gap-4 md:grid-cols-2">

            @if($employee->employee_type === 'monthly')
                <div>
                    <label class="mb-1 block text-xs font-semibold text-slate-700">
                        {{ __('salary.new_monthly_salary_required') }}
                    </label>
                    <input type="number"
                           step="any"
                           name="monthly_salary"
                           value="{{ old('monthly_salary', $employee->currentSalaryStructure->monthly_salary) }}"
                           class="w-full border px-3 py-2 text-xs">
                </div>

                <div>
                    <label class="mb-1 block text-xs font-semibold text-slate-700">
                        {{ __('salary.overtime_rate') }}
                    </label>
                    <input type="number"
                           step="any"
                           name="overtime_rate"
                           value="{{ old('overtime_rate', $employee->currentSalaryStructure->overtime_rate) }}"
                           class="w-full border px-3 py-2 text-xs">
                </div>
            @else
                <div>
                    <label class="mb-1 block text-xs font-semibold text-slate-700">
                        {{ __('salary.new_per_roz_rate_required') }}
                    </label>
                    <input type="number"
                           step="any"
                           name="per_roz_rate"
                           value="{{ old('per_roz_rate', $employee->currentSalaryStructure->per_roz_rate) }}"
                           class="w-full border px-3 py-2 text-xs">
                </div>
            @endif

            <div>
                <label class="mb-1 block text-xs font-semibold text-slate-700">
                    {{ __('salary.effective_from_required') }}
                </label>
                <input type="date"
                       name="effective_from"
                       value="{{ old('effective_from', now()->format('Y-m-d')) }}"
                       class="w-full border px-3 py-2 text-xs">
            </div>

            <div class="md:col-span-2">
                <label class="mb-1 block text-xs font-semibold text-slate-700">{{ __('salary.internal_note') }}</label>
                <textarea name="note" rows="3"
                          class="w-full border px-3 py-2 text-xs">{{ old('note') }}</textarea>
            </div>

            <div class="md:col-span-2">
                <label class="mb-1 block text-xs font-semibold text-slate-700">{{ __('salary.change_note') }}</label>
                <textarea name="change_note" rows="3"
                          class="w-full border px-3 py-2 text-xs">{{ old('change_note') }}</textarea>
            </div>
        </div>

        <div class="mt-4 flex gap-2">
            <button type="submit"
                    class="bg-slate-900 px-4 py-2 text-xs font-semibold text-white">
                {{ __('salary.update_salary_rate') }}
            </button>

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

    <div class="mt-5 border border-slate-200 bg-white p-4">
        <h2 class="mb-3 text-sm font-bold text-slate-800">{{ __('salary.recent_salary_history') }}</h2>

        <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-3 py-2">{{ __('salary.from') }}</th>
                        <th class="px-3 py-2">{{ __('salary.to') }}</th>

                        @if($employee->employee_type === 'monthly')
                            <th class="px-3 py-2">{{ __('salary.salary') }}</th>
                            <th class="px-3 py-2">{{ __('salary.ot') }}</th>
                        @else
                            <th class="px-3 py-2">{{ __('salary.roz') }}</th>
                        @endif
                    </tr>
                </thead>

                <tbody class="divide-y">
                    @forelse($employee->salaryHistories->take(8) as $history)
                        <tr>
                            <td class="px-3 py-2">{{ optional($history->effective_from)->format('d M Y') }}</td>
                            <td class="px-3 py-2">{{ optional($history->effective_to)->format('d M Y') ?? __('salary.current') }}</td>

                            @if($employee->employee_type === 'monthly')
                                <td class="px-3 py-2">৳ {{ number_format($history->monthly_salary, 2) }}</td>
                                <td class="px-3 py-2">৳ {{ number_format($history->overtime_rate, 2) }}</td>
                            @else
                                <td class="px-3 py-2">৳ {{ number_format($history->per_roz_rate, 2) }}</td>
                            @endif
                        </tr>
                    @empty
                        <tr>
                            <td colspan="{{ $employee->employee_type === 'monthly' ? 4 : 3 }}"
                                class="px-3 py-4 text-center text-slate-500">
                                {{ __('salary.no_history_found') }}
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection