@extends('layouts.admin')

@section('page-title')
    {{ __('Leave Approver Setup') }}
@endsection

@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">{{ __('Dashboard') }}</a></li>
    <li class="breadcrumb-item">{{ __('Leave Management Setup') }}</li>
    <li class="breadcrumb-item">{{ __('Approver Setup') }}</li>
@endsection

@section('content')
    <div class="row">
        <div class="col-12">
            @include('layouts.hrm_setup')
        </div>
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5>{{ __('Leave & Petty Cash Expense Approver Configuration') }}</h5>
                    <small class="text-muted">{{ __('Set approvers per employee and/or department. Employee assignment takes priority over department assignment.') }}</small>
                </div>
                <div class="card-body">
                    @if (isset($assignmentTableReady) && !$assignmentTableReady)
                        <div class="alert alert-warning">{{ __('Leave approver table is missing. Please run migrations first.') }}</div>
                    @endif
                    @if (isset($pettyAssignmentTableReady) && !$pettyAssignmentTableReady)
                        <div class="alert alert-warning">{{ __('Petty cash expense approver table is missing. Please run migrations first.') }}</div>
                    @endif

                    {{ Form::open(['route' => 'leave.approver.setup.store', 'method' => 'post']) }}

                    <h6 class="mt-1">{{ __('Leave Department Approvers') }}</h6>
                    <div class="table-responsive mb-3">
                        <table class="table">
                            <thead>
                                <tr><th>{{ __('Department') }}</th><th>{{ __('Approver') }}</th></tr>
                            </thead>
                            <tbody>
                                @foreach ($departments as $department)
                                    <tr>
                                        <td>{{ $department->name }}</td>
                                        <td>{{ Form::select('department_approver[' . $department->id . ']', $approvers, $departmentAssignments[$department->id] ?? null, ['class' => 'form-control select', 'placeholder' => __('Select Approver')]) }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>

                    <h6>{{ __('Leave Employee Approvers') }}</h6>
                    <div class="table-responsive mb-4">
                        <table class="table">
                            <thead>
                                <tr><th>{{ __('Employee') }}</th><th>{{ __('Department') }}</th><th>{{ __('Approver') }}</th></tr>
                            </thead>
                            <tbody>
                                @foreach ($employees as $employee)
                                    <tr>
                                        <td>{{ $employee->name }}</td>
                                        <td>{{ !empty($employee->department) ? $employee->department->name : '-' }}</td>
                                        <td>{{ Form::select('employee_approver[' . $employee->id . ']', $approvers, $employeeAssignments[$employee->id] ?? null, ['class' => 'form-control select', 'placeholder' => __('Use Department Approver')]) }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>

                    <hr class="my-4">

                    <h6>{{ __('Petty Cash Expense Department Approvers') }}</h6>
                    <div class="table-responsive mb-3">
                        <table class="table">
                            <thead>
                                <tr><th>{{ __('Department') }}</th><th>{{ __('Approver') }}</th></tr>
                            </thead>
                            <tbody>
                                @foreach ($departments as $department)
                                    <tr>
                                        <td>{{ $department->name }}</td>
                                        <td>{{ Form::select('petty_department_approver[' . $department->id . ']', $approvers, $pettyDepartmentAssignments[$department->id] ?? null, ['class' => 'form-control select', 'placeholder' => __('Select Approver')]) }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>

                    <h6>{{ __('Petty Cash Expense Employee Approvers') }}</h6>
                    <div class="table-responsive mb-4">
                        <table class="table">
                            <thead>
                                <tr><th>{{ __('Employee') }}</th><th>{{ __('Department') }}</th><th>{{ __('Approver') }}</th></tr>
                            </thead>
                            <tbody>
                                @foreach ($employees as $employee)
                                    <tr>
                                        <td>{{ $employee->name }}</td>
                                        <td>{{ !empty($employee->department) ? $employee->department->name : '-' }}</td>
                                        <td>{{ Form::select('petty_employee_approver[' . $employee->id . ']', $approvers, $pettyEmployeeAssignments[$employee->id] ?? null, ['class' => 'form-control select', 'placeholder' => __('Use Department Approver')]) }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>

                    <div class="mt-3 text-end">
                        <input type="submit" value="{{ __('Save Configuration') }}" class="btn btn-primary" @if ((isset($assignmentTableReady) && !$assignmentTableReady) && (isset($pettyAssignmentTableReady) && !$pettyAssignmentTableReady)) disabled @endif>
                    </div>

                    {{ Form::close() }}
                </div>
            </div>
        </div>
    </div>
@endsection
