<x-app-layout>
    <x-slot name="header">{{ __('Edit Employee') }} — {{ $employee->name }}</x-slot>

    <div class="row g-4">
        {{-- Edit Form --}}
        <div class="col-12 col-xl-7">
            <div class="card shadow-sm border-0">
                <div class="card-header bg-white fw-semibold border-0 pt-3 pb-0">
                    <i class="bi bi-pencil me-1 text-primary"></i>{{ __('Update Details') }}
                </div>
                <div class="card-body">
                    <form action="{{ route('employees.update', $employee) }}" method="POST">
                        @csrf @method('PUT')
                        <div class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label fw-semibold">{{ __('Name') }} *</label>
                                <input type="text" name="name" class="form-control @error('name') is-invalid @enderror" value="{{ old('name', $employee->name) }}" required>
                                @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-semibold">{{ __('Designation') }}</label>
                                <input type="text" name="designation" class="form-control" value="{{ old('designation', $employee->designation) }}">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-semibold">{{ __('Phone') }}</label>
                                <input type="text" name="phone" class="form-control" value="{{ old('phone', $employee->phone) }}">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-semibold">{{ __('Email') }}</label>
                                <input type="email" name="email" class="form-control" value="{{ old('email', $employee->email) }}">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label fw-semibold">{{ __('Salary Type') }} *</label>
                                <select name="salary_type" class="form-select" required>
                                    @foreach(['monthly','weekly','daily'] as $type)
                                        <option value="{{ $type }}" {{ old('salary_type', $employee->salary_type) == $type ? 'selected' : '' }}>{{ ucfirst($type) }}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label fw-semibold">
                                    {{ __('Salary Amount') }} *
                                    @if($salaryHistories->count())
                                        <span class="text-muted small">(prev: {{ number_format($salaryHistories->first()->old_salary, 2) }})</span>
                                    @endif
                                </label>
                                <input type="number" step="0.01" name="salary_amount" class="form-control @error('salary_amount') is-invalid @enderror" value="{{ old('salary_amount', $employee->salary_amount) }}" required>
                                @error('salary_amount')<div class="invalid-feedback">{{ $message }}</div>@enderror
                            </div>
                            <div class="col-md-4">
                                <label class="form-label fw-semibold">{{ __('Advance Amount') }}</label>
                                <input type="number" step="0.01" name="advance_amount" class="form-control" value="{{ old('advance_amount', $employee->advance_amount) }}">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label fw-semibold">{{ __('Joining Date') }}</label>
                                <input type="date" name="joining_date" class="form-control" value="{{ old('joining_date', optional($employee->joining_date)->format('Y-m-d')) }}">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label fw-semibold">{{ __('Resign Date') }}</label>
                                <input type="date" name="resign_date" class="form-control" value="{{ old('resign_date', optional($employee->resign_date)->format('Y-m-d')) }}">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label fw-semibold">{{ __('Status') }} *</label>
                                <select name="status" class="form-select" required>
                                    <option value="active" {{ old('status', $employee->status) == 'active' ? 'selected' : '' }}>{{ __('Active') }}</option>
                                    <option value="inactive" {{ old('status', $employee->status) == 'inactive' ? 'selected' : '' }}>{{ __('Inactive') }}</option>
                                </select>
                            </div>
                            <div class="col-12">
                                <label class="form-label fw-semibold">{{ __('Notes') }}</label>
                                <textarea name="notes" class="form-control" rows="2">{{ old('notes', $employee->notes) }}</textarea>
                            </div>
                        </div>
                        <div class="mt-3 d-flex gap-2">
                            <button type="submit" class="btn btn-primary">{{ __('Update Employee') }}</button>
                            <a href="{{ route('employees.index') }}" class="btn btn-secondary">{{ __('Cancel') }}</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        {{-- Salary History --}}
        <div class="col-12 col-xl-5">
            <div class="card shadow-sm border-0 h-100">
                <div class="card-header bg-white fw-semibold border-0 pt-3 pb-0">
                    <i class="bi bi-clock-history me-1 text-secondary"></i>{{ __('Salary History') }}
                </div>
                <div class="table-responsive">
                    <table class="table table-sm align-middle mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>{{ __('Date') }}</th>
                                <th>{{ __('Type') }}</th>
                                <th class="text-end">{{ __('Old') }}</th>
                                <th class="text-end">{{ __('New') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($salaryHistories as $h)
                                <tr>
                                    <td class="small">{{ $h->effective_date->format('d M Y') }}</td>
                                    <td class="small">{{ ucfirst($h->salary_type) }}</td>
                                    <td class="text-end text-muted small">{{ number_format($h->old_salary, 2) }}</td>
                                    <td class="text-end fw-semibold text-success small">{{ number_format($h->new_salary, 2) }}</td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="4" class="text-center text-muted py-3 small">{{ __('No salary changes recorded.') }}</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>

                {{-- Current Salary Badge --}}
                <div class="card-footer bg-white border-0">
                    <div class="d-flex justify-content-between align-items-center">
                        <span class="text-muted small">{{ __('Current Salary') }}</span>
                        <span class="badge bg-primary fs-6">{{ number_format($employee->salary_amount, 2) }} / {{ ucfirst($employee->salary_type) }}</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
