<?php
use \Illuminate\Support\Carbon;
use \A7kz\Platform\Models\UniModel;
use A7kz\Platform\Modules\Platform\Acl\Facades\Acl;
use \App\Modules\applications\Enum\ApplicationStatus;

// Заявки с утвержденным статусом
$approvedApplications = UniModel::model('pipi_applications', Acl::connection())
    ->where('user_id', auth()->user()->id)
    ->where('status', ApplicationStatus::approved)
    ->get();

// Заявки с ожидающим статусом
$pendingApplications = UniModel::model('pipi_applications', Acl::connection())
    ->where('user_id', auth()->user()->id)
    ->where('status', ApplicationStatus::approved)
    ->get();
?>

<div class="row d-flex flex-md-row flex-sm-column justify-content-center align-items-md-stretch align-items-sm-center" style="height: calc(100vh - 197px)">
    <div class="col-8 col-md-3 my-5 my-md-0 h-100">
        @include('platform.user_profile::user',['name'=>$user->name, 'about'=>$user->about])
    </div>
    <div class="card col-12 col-md-8 ms-md-3 ms-lg-5 applications-history">
        <div class="card-body">
            <ul class="nav nav-tabs" id="applicationsTab" role="tablist">
                <li class="nav-item me-4" role="presentation">
                    <a class="nav-link active" id="applications-tab" data-bs-toggle="tab" href="#applications" role="tab" aria-controls="applications" aria-selected="true">
                        @lang('История заказов')
                    </a>
                </li>
                <li class="nav-item" role="presentation">
                    <a class="nav-link" id="reserves-tab" data-bs-toggle="tab" href="#reserves" role="tab" aria-controls="reserves" aria-selected="false">
                        @lang('Бронирования')
                    </a>
                </li>
            </ul>
            <div class="tab-content" id="applicationsTabContent">
                <div class="tab-pane fade show active" id="applications" role="tabpanel" aria-labelledby="applications-tab">
                    <div class="card-body">
                        <div class="table-container">
                            <table class="table">
                                <thead>
                                <tr>
                                    <th><b>@lang('Дата начала')</b></th>
                                    <th><b>@lang('Дата окончания')</b></th>
                                    <th><b>@lang('Дней')</b></th>
                                    <th><b>@lang('Машина')</b></th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($approvedApplications as $applications_item)
                                    <tr>
                                        <td>
                                            {{ $applications_item->started_at ? Carbon::parse($applications_item->started_at)->format('d.m.Y') : '' }}
                                        </td>
                                        <td>
                                            {{ $applications_item->ended_at ? Carbon::parse($applications_item->ended_at)->format('d.m.Y') : '' }}
                                        </td>
                                        <td>
                                            {{ $applications_item->rent_day }}
                                        </td>
                                        <td>
                                            @php
                                                $car = \A7kz\Platform\Models\UniModel::model('pipi_auto')->where('id', $applications_item->car_id)->first();
                                            @endphp
                                            {{  $car->name }}
                                        </td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <!-- Бронирования -->
                <div class="tab-pane fade" id="reserves" role="tabpanel" aria-labelledby="reserves-tab">
                    <div class="card-body">
                        <div class="table-container">
                        <table class="table">
                            <thead>
                            <tr>
                                <th><b>@lang('Дата начала')</b></th>
                                <th><b>@lang('Дата окончания')</b></th>
                                <th><b>@lang('Дней')</b></th>
                                <th><b>@lang('Машина')</b></th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($pendingApplications as $applications_item)
                                <tr>
                                    <td>
                                        {{ $applications_item->started_at ? Carbon::parse($applications_item->started_at)->format('d.m.Y') : '' }}
                                    </td>
                                    <td>
                                        {{ $applications_item->ended_at ? Carbon::parse($applications_item->ended_at)->format('d.m.Y') : '' }}
                                    </td>
                                    <td>
                                        {{ $applications_item->rent_day }}
                                    </td>
                                    <td>
                                        @php
                                            $car = \A7kz\Platform\Models\UniModel::model('pipi_auto')->where('id', $applications_item->car_id)->first();
                                        @endphp
                                        {{  $car->name }}
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    var myTab = new bootstrap.Tab(document.getElementById('applications-tab'))
    myTab.show()
</script>
