@extends('backend.layouts.app')
@section('title', @$data['title'])

@section('content')
    {!! breadcrumb([
        'title' => @$data['title'],
        route('admin.dashboard') => _trans('common.Dashboard'),
        '#' => @$data['title'],
    ]) !!}

    @include('travel::travel-plans.nav', ['plan_id' => $data['plan_id'], 'is_approved' => true])

    <div class="card ot-card">
        <div class="card-body">
            <form 
                method="POST" 
                action="{{ route('travel.plans.expenses.update', ['plan_id' => $data['plan_id'], 'id' => $data['travelExpense']->id]) }}" 
                enctype="multipart/form-data" 
                autocomplete="off"
            >
                @csrf
                @method('PUT')

                <div class="col-md-12">
                    <div class="row form-group mb-5 text-start">
                        <label class="col-md-4 col-lg-3 col-xl-2 form-label pt-3 text-end"></label>
                        <div class="col-md-8 col-lg-9 col-xl-10 bg-light p-3">
                            <h3>{{ _trans('common.Expense Edit Form') }}</h3>
                        </div>
                    </div>
                    <small class="text-muted">{{ _trans('common.Basic Info') }}:</small>
                    <div class="row form-group mb-3 text-start">
                        <label for="date" class="col-md-4 col-lg-3 col-xl-2 form-label pt-3 text-end">{{ _trans('common.Date') }} <span class="text-danger">*</span></label>
                        <div class="col-md-8 col-lg-9 col-xl-10">
                            <input type="date" name="date" class="form-control ot-form-control ot-input" value="{{ old('date', $data['travelExpense']->date) }}" required>
                            @if ($errors->has('date'))
                                <span class="error text-danger">
                                    {{ $errors->first('date') }}
                                </span>
                            @endif
                        </div>
                    </div>
                    <div class="row form-group mb-3 text-start">
                        <label for="mode_of_transportation" class="col-md-4 col-lg-3 col-xl-2 form-label pt-3 text-end">{{ _trans('common.Mode Of Transportation') }} <span class="text-danger">*</span></label>
                        <div class="col-md-8 col-lg-9 col-xl-10">
                            <select name="mode_of_transportation" class="form-control ot-form-control ot-input select2" required>
                                <option value="" selected disabled>{{ _trans('common.Select') }}</option>
                                @foreach ($data['modeOfTransportations'] ?? [] as $modeOfTransportation)
                                    <option value="{{ $modeOfTransportation }}" {{ old('mode_of_transportation', $data['travelExpense']->mode_of_transportation) == $modeOfTransportation ? 'selected' : '' }}>{{ $modeOfTransportation }}</option>
                                @endforeach
                            </select>
                            @if ($errors->has('mode_of_transportation'))
                                <span class="error text-danger">
                                    {{ $errors->first('mode_of_transportation') }}
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="row  mt-5">
                        <small class="col-md-4 col-lg-3 col-xl-2 text-muted">{{ _trans('common.Expense Info') }}:</small>
                        <div class="col-md-8 col-lg-9 col-xl-10">
                            <a href="{{ route('hrm.deposit_category.expense') }}" class="text-primary text-sm" target="_blank">
                                {{ _trans('common.Add Travel Expense Category') }}
                                <i class="las la-external-link-alt"></i>
                            </a>
                        </div>
                    </div>

                    <div class="mb-5">
                        @foreach ($data['travelExpense']->travelExpenseLogs ?? [] as $key => $travelExpenseLog)
                            <input type="hidden" name="travel_expense_log_id[]" value="{{ $travelExpenseLog->id }}">
                            <input type="hidden" name="expense_category_id[]" value="{{ $travelExpenseLog->expense_category_id }}">
                            <div class="mb-5">
                                <div class="row form-group mb-3 text-start">
                                    <label for="amount" class="col-md-4 col-lg-3 col-xl-2 form-label pt-3 text-end">
                                        {{ $travelExpenseLog->expenseCategory->name . ' ' . _trans('common.Amount') }}
                                    </label>
                                    <div class="col-md-8 col-lg-9 col-xl-10">
                                        <input type="number" 
                                            name="amount[]" 
                                            class="form-control ot-form-control ot-input" 
                                            step="0.01" 
                                            min="0" 
                                            value="{{ old('amount.' . $key, $travelExpenseLog->amount) }}"
                                        >
                                        @error('amount.' . $key)
                                            <span class="error text-danger">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>

                                <div class="row form-group mb-3 text-start">
                                    <label for="file" class="col-md-4 col-lg-3 col-xl-2 form-label pt-3 text-end">
                                        {{ _trans('common.Bill/File') }}
                                    </label>
                                    <div class="col-md-8 col-lg-9 col-xl-10">
                                        <div class="ot_fileUploader left-side">
                                            <input class="form-control" 
                                                type="text" 
                                                name="file[]" 
                                                readonly
                                                placeholder="{{ _trans('common.Choose') }}"
                                            >
                                            <button class="primary-btn-small-input" type="button">
                                                <label class="btn btn-lg ot-btn-primary m-0" for="fileBrouse{{ $key }}">Browse</label>
                                                <input type="file" class="d-none form-control" name="file[]" id="fileBrouse{{ $key }}">
                                            </button>
                                        </div>
                                        @error('file.' . $key)
                                            <span class="error text-danger">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>

                                <div class="row form-group mb-3 text-start">
                                    <label for="remark" class="col-md-4 col-lg-3 col-xl-2 form-label pt-3 text-end">
                                        {{ _trans('common.Remark') }}
                                        @if (Str::lower($travelExpenseLog->expenseCategory->name) == 'other' || Str::lower($travelExpenseLog->expenseCategory->name) == 'others')
                                            <span class="text-danger">*</span>
                                        @endif
                                    </label>
                                    <div class="col-md-8 col-lg-9 col-xl-10">
                                        <input type="text" 
                                            name="remark[]" 
                                            class="form-control ot-form-control ot-input" 
                                            value="{{ old('remark.' . $key, $travelExpenseLog->remark) }}"
                                        >
                                        @error('remark.' . $key)
                                            <span class="error text-danger">{{ $message }}</span>
                                        @enderror

                                        @if (Str::lower($travelExpenseLog->expenseCategory->name) == 'other' || Str::lower($travelExpenseLog->expenseCategory->name) == 'others')
                                            <small class="text-muted">{{ _trans('common.What is your Other Expenses?') }}</small>
                                        @endif
                                    </div>
                                </div>
                            </div>
                        @endforeach

                        @foreach ($data['expenseCategories'] ?? [] as $key => $expenseCategory)

                            @php
                                $keySn = count($data['travelExpense']->travelExpenseLogs) + $key + 1;
                            @endphp

                            <input type="hidden" name="travel_expense_log_id[]" value="">
                            <input type="hidden" name="expense_category_id[]" value="{{ $expenseCategory->id }}">
                            <div class="mb-5">
                                <div class="row form-group mb-3 text-start">
                                    <label for="amount" class="col-md-4 col-lg-3 col-xl-2 form-label pt-3 text-end">
                                        {{ $expenseCategory->name . ' ' . _trans('common.Amount') }}
                                    </label>
                                    <div class="col-md-8 col-lg-9 col-xl-10">
                                        <input type="number" 
                                            name="amount[]" 
                                            class="form-control ot-form-control ot-input" 
                                            step="0.01" 
                                            min="0" 
                                            value="{{ old('amount.' . $keySn) }}">
                                        @error('amount.' . $keySn)
                                            <span class="error text-danger">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>

                                <div class="row form-group mb-3 text-start">
                                    <label for="file" class="col-md-4 col-lg-3 col-xl-2 form-label pt-3 text-end">
                                        {{ _trans('common.Bill/File') }}
                                    </label>
                                    <div class="col-md-8 col-lg-9 col-xl-10">
                                        <div class="ot_fileUploader left-side">
                                            <input class="form-control" 
                                                type="text" 
                                                name="file[]" 
                                                readonly 
                                                placeholder="{{ _trans('common.Choose') }}"
                                            >
                                            <button class="primary-btn-small-input" type="button">
                                                <label class="btn btn-lg ot-btn-primary m-0" for="fileBrouse{{ $keySn }}">Browse</label>
                                                <input type="file" class="d-none form-control" name="file[]" id="fileBrouse{{ $keySn }}">
                                            </button>
                                        </div>
                                        @error('file.' . $keySn)
                                            <span class="error text-danger">{{ $message }}</span>
                                        @enderror
                                    </div>
                                </div>

                                <div class="row form-group mb-3 text-start">
                                    <label for="remark" class="col-md-4 col-lg-3 col-xl-2 form-label pt-3 text-end">
                                        {{ _trans('common.Remark') }}
                                        @if (Str::lower($expenseCategory->name) == 'other' || Str::lower($expenseCategory->name) == 'others')
                                            <span class="text-danger">*</span>
                                        @endif
                                    </label>
                                    <div class="col-md-8 col-lg-9 col-xl-10">
                                        <input type="text" 
                                            name="remark[]" 
                                            class="form-control ot-form-control ot-input" 
                                            value="{{ old('remark.' . $keySn) }}"
                                        >
                                        @error('remark.' . $keySn)
                                            <span class="error text-danger">{{ $message }}</span>
                                        @enderror

                                        @if (Str::lower($expenseCategory->name) == 'other' || Str::lower($expenseCategory->name) == 'others')
                                            <small class="text-muted">{{ _trans('common.What is your Other Expenses?') }}</small>
                                        @endif
                                    </div>
                                </div>
                            </div>
                        @endforeach
                    </div>


                    <small class="text-muted mt-5">{{ _trans('common.Rating') }}:</small>
                    <div class="row form-group mb-3 text-start">
                        <label for="rating_of_travel_experience" class="col-md-4 col-lg-3 col-xl-2 form-label pt-3 text-end">{{ _trans('common.Rate Your Travel Experience') }}</label>
                        <div class="col-md-8 col-lg-9 col-xl-10">
                            <select name="rating_of_travel_experience" class="form-control select2">
                                <option value="" selected></option>
                                <option value="1" {{ old('rating_of_travel_experience', $data['travelExpense']->rating_of_travel_experience) == 1 ? 'selected' : '' }}>1</option>
                                <option value="2" {{ old('rating_of_travel_experience', $data['travelExpense']->rating_of_travel_experience) == 2 ? 'selected' : '' }}>2</option>
                                <option value="3" {{ old('rating_of_travel_experience', $data['travelExpense']->rating_of_travel_experience) == 3 ? 'selected' : '' }}>3</option>
                                <option value="4" {{ old('rating_of_travel_experience', $data['travelExpense']->rating_of_travel_experience) == 4 ? 'selected' : '' }}>4</option>
                                <option value="5" {{ old('rating_of_travel_experience', $data['travelExpense']->rating_of_travel_experience) == 5 ? 'selected' : '' }}>5</option>
                            </select>
                            @if ($errors->has('rating_of_travel_experience'))
                                <span class="error text-danger">
                                    {{ $errors->first('rating_of_travel_experience') }}
                                </span>
                            @endif
                        </div>
                    </div>
                    <div class="d-flex justify-content-end gap-2 mt-5">
                        <a href="{{ route('travel.plans.expenses.index', $data['plan_id']) }}" class="btn btn-secondary d-flex align-items-center gap-2">
                            <i class="las la-arrow-left fs-4"></i> 
                            {{ _trans('common.Back to list') }}
                        </a>
                        <button class="btn btn-gradian d-flex align-items-center gap-2"><i class="las la-check fs-4"></i> {{ _trans('common.Update') }}</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
@endsection