@extends('dashboard.layouts.app')
@section('title', trans('admin.orders.title') . ' - ')

@include('dashboard.orders.components.index.speedbar')

@section('content')
    <div class="row" id="table-head">
        <div class="col-md-12 mb-1">
            {{--  <a href="{{ route('dashboard.orders.archive') }}" class="btn btn-icon btn-secondary float-right mr-1">
                <i class="feather icon-box"></i> @lang('admin.orders.Order archive')
            </a>  --}}

            {{--  <a href="{{ route('dashboard.orders.export') }}" class="btn btn-icon btn-success float-right mr-1">
                <i class="feather icon-box"></i> @lang('admin.orders.Export')
            </a>  --}}
        </div>
    </div>
    <div class="row" id="table-head">

        {{--  <div class="col-md-4 mb-1 ">
            <form action="{{ route('dashboard.orders.search') }}" method="get">
                <div class="input-group ">
                    <input type="text" class="form-control" name="id" placeholder="ID заказа"
                        aria-describedby="button-addon2">
                    <div class="input-group-append" id="button-addon2">
                        <button class="btn btn-primary waves-effect waves-light" type="button">@lang('admin.orders.Search')</button>
                    </div>
                </div>
            </form>
        </div>  --}}

        @include('dashboard.orders.components.index.filter')

        <div class="col-12">

            <div class="card">

                <div class="card-content">
                    <form method="post" action="{{ route('dashboard.orders.mass_archived') }}">
                        @csrf
                        <div class="table">
                            <table class="table mb-0">
                                <thead class="thead-dark">
                                    <tr>
                                        <th scope="col" width="50" class="text-right">
                                            <div class="form-group text-right">
                                                <fieldset class="checkbox">
                                                    <div class="vs-checkbox-con vs-checkbox-primary">
                                                        <input type="checkbox" name="order_id[]" class="change-check"
                                                            id="select-all">
                                                        <span class="vs-checkbox">
                                                            <span class="vs-checkbox--check">
                                                                <i class="vs-icon feather icon-check"></i>
                                                            </span>
                                                        </span>
                                                    </div>
                                                </fieldset>
                                            </div>
                                        </th>

                                        <th scope="col" width="50">ID</th>
                                        <th scope="col">@lang('admin.orders.user')</th>
                                        <th scope="col">@lang('admin.orders.delivery_type')</th>
                                        <th scope="col">@lang('admin.orders.payment_system')</th>
                                        <th scope="col">@lang('admin.orders.status')</th>
                                        <th scope="col">@lang('admin.orders.date')</th>
                                        <th scope="col" class="text-right">@lang('admin.actions')</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @if (count($orders) == 0)
                                        <tr>
                                            <td class="text-center" colspan="7">
                                                @lang('admin.no_data')
                                            </td>
                                        </tr>
                                    @endif

                                    @foreach ($orders as $order)
                                        <tr>
                                            <td class="text-right">
                                                <div class="form-group">
                                                    <fieldset class="checkbox">
                                                        <div class="vs-checkbox-con vs-checkbox-primary">
                                                            <input type="checkbox" value="{{ $order->id }}"
                                                                name="order_id[]" class="change-check"
                                                                id="checkbox-{{ $loop->iteration }}">
                                                            <span class="vs-checkbox">
                                                                <span class="vs-checkbox--check">
                                                                    <i class="vs-icon feather icon-check"></i>
                                                                </span>
                                                            </span>
                                                        </div>
                                                    </fieldset>
                                                </div>
                                            </td>

                                            <td>{{ $order->id }}</td>
                                            <td>+{{ $order->user?->getPhone() }}</td>

                                            <td>
                                                @lang('admin.orders.type_delivery.' . $order->delivery_type)
                                            </td>

                                            <td>
                                                @php
                                                    $payment_statuses = [
                                                        'waiting' => 'warning',
                                                        'cancelled' => 'danger',
                                                        'payed' => 'success',
                                                        'cash' => 'primary',
                                                        'review' => 'secondary',
                                                    ];
                                                    $class = $payment_statuses[$order->payment_status] ?? 'secondary';
                                                @endphp

                                                <span class="badge rounded-pill bg-{{ $class }}">
                                                    @lang('admin.orders.statuses.' . $order->payment_status)
                                                </span>:
                                                @lang('admin.orders.payment_type.' . $order->payment_type)
                                            </td>

                                            <td>
                                                <div class="btn-group dropleft mr-1 mb-1">
                                                    @php
                                                        $statusClasses = [
                                                            'completed' => 'btn-success',
                                                            'cancelled' => 'btn-danger',
                                                            'processing' => 'btn-primary',
                                                            'waiting_buyer' => 'btn-warning',
                                                        ];

                                                        $buttonClass =
                                                            $statusClasses[$order->status] ?? 'btn-secondary';
                                                    @endphp

                                                    <button type="button"
                                                        class="btn {{ $buttonClass }} dropdown-toggle waves-effect waves-light"
                                                        data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                                        @lang('admin.orders.statuses.' . $order->status)
                                                    </button>

                                                    <div class="dropdown-menu" x-placement="left-start"
                                                        style="position: absolute; transform: translate3d(-147px, 0px, 0px); top: 0px; left: 0px; will-change: transform;">
                                                        @foreach (App\Models\Order::statuses() as $status)
                                                            <a class="dropdown-item"
                                                                onclick="return confirm('Вы действительно хотите изменить статус')"
                                                                href="{{ route('dashboard.orders.change-status', ['order' => $order, 'status' => $status]) }}">
                                                                @lang('admin.statuses.' . $status)
                                                            </a>
                                                        @endforeach
                                                    </div>
                                                </div>
                                            </td>

                                            <td>
                                                {{ date('H:i - d.m.Y', strtotime($order->created_at)) }}
                                            </td>

                                            <td class="text-right">
                                                @if (!empty(auth()->user()->role->permissions['orders']['print']))
                                                    <a href="{{ route('dashboard.invoice_print', $order->id) }}"
                                                        target="_blank" class="btn btn-success btn-icon btn-sm">
                                                        <i class="fa fa-print"></i>
                                                    </a>
                                                @endif

                                                @can('view', 'orders')
                                                    <a href="{{ route('dashboard.orders.view', $order->id) }}"
                                                        class="btn btn-primary btn-icon btn-sm">
                                                        <i class="fa fa-eye"></i>
                                                    </a>
                                                @endcan
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>

                        <div class="card-footer" id="show-action">
                            <button type="submit" name="action" value="archived" class="btn btn-secondary">
                                <i class="feather icon-box"></i> @lang('admin.orders.archive')
                            </button>
                        </div>
                    </form>
                </div>
            </div>

            {{ $orders->links('pagination::bootstrap-4') }}

            {{--  {{ $orders->appends(request()->input())->links() }}  --}}
        </div>
    </div>
@endsection

@include('dashboard.orders.components.index.css')

@include('dashboard.orders.components.index.js')
