@extends('layouts.app')
@section('title','Employee Compensation')
@section('content')
<x-ui.page-toolbar title="Employee Compensation" description="Configure the current salary components copied into new payroll periods." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Payroll','url'=>route('payroll.index')],['label'=>'Compensation Setup']]">
    <x-slot:actions><a class="mf-btn" href="{{ route('payroll.index') }}">Back to Payroll</a></x-slot:actions>
</x-ui.page-toolbar>

<div class="mf-card">
    <div class="mf-table-frame border-0"><table class="mf-table" data-standard-table>
        <thead><tr><th>Employee</th><th>Branch / Position</th><th>Basic Salary</th><th>Status</th><th>Effective From</th><th>Components</th></tr></thead>
        <tbody>
        @forelse($employees as $employee)
            @php($comp=$employee->compensation)
            <tr>
                <td><div class="font-medium">{{ $employee->name }}</div><div class="text-xs text-muted-foreground">{{ $employee->employee_number ?: 'No employee number' }}</div></td>
                <td>{{ $employee->primaryBranch?->name ?? '—' }}<div class="text-xs text-muted-foreground">{{ $employee->position ?: 'No position' }}</div></td>
                <td>{{ number_format((float)($comp?->basic_salary ?? 0),2) }}</td>
                <td><span class="mf-badge {{ ($comp?->status ?? 'inactive')==='active'?'mf-badge-success':'' }}">{{ ucfirst($comp?->status ?? 'not configured') }}</span></td>
                <td>{{ $comp?->effective_from?->format('d M Y') ?? '—' }}</td>
                <td class="min-w-[280px]">
                    <details>
                        <summary class="mf-btn cursor-pointer">Edit components</summary>
                        <form method="POST" action="{{ route('payroll.compensations.update',$employee) }}" class="mt-3" data-ui="form-shell">
                            @csrf @method('PUT')
                            <div class="grid gap-2 md:grid-cols-2">
                                @foreach(['basic_salary'=>'Basic salary','allowances'=>'Allowances','bonus'=>'Bonus','commission'=>'Commission','overtime'=>'Overtime','deductions'=>'Deductions','advances'=>'Advances','other_deductions'=>'Other deductions'] as $field=>$label)
                                    <label class="text-xs">{{ $label }}<input class="mf-input mt-1" type="number" step="0.01" min="0" name="{{ $field }}" value="{{ old($field,$comp?->{$field} ?? '0.00') }}" required></label>
                                @endforeach
                                <label class="text-xs">Effective from<input class="mf-input mt-1" type="date" name="effective_from" value="{{ old('effective_from',$comp?->effective_from?->format('Y-m-d')) }}"></label>
                                <label class="text-xs">Status<select class="mf-select mt-1" name="status"><option value="active" @selected(old('status',$comp?->status ?? 'active')==='active')>Active</option><option value="inactive" @selected(old('status',$comp?->status)==='inactive')>Inactive</option></select></label>
                            </div>
                            <label class="block text-xs mt-2">Notes<textarea class="mf-input mt-1" name="notes" rows="2">{{ old('notes',$comp?->notes) }}</textarea></label>
                            <div class="mt-2 text-right"><button class="mf-btn mf-btn-primary">Save compensation</button></div>
                        </form>
                    </details>
                </td>
            </tr>
        @empty<tr><td colspan="6" class="text-center text-muted-foreground">No employees are available in your assigned branches.</td></tr>@endforelse
        </tbody>
    </table></div>
</div>
@endsection
