<div x-data="{ showOrder: @entangle('showOrder') }">
    <div class="d-flex">
        <h2 class="h5 text-uppercase mb-4">Orders</h2>
    </div>

    <div class="my-4">
        <div class="table-responsive">
            <table class="table">
                <thead>
                <tr>
                    <th>{{__('Order Number')}}</th>
                    <th>Total</th>
                    <th>Status</th>
                    <th>Date</th>
                    <th class="col-2"></th>
                </tr>
                </thead>
                <tbody>
                @forelse($orders as $odr)
                    <tr wire:key="{{ $odr->id }}">
                        <td>{{ $odr->ref_id }}</td>
                        <td>{{ format_amount($odr->total,$odr->currency())}}</td>
                        <td>{!! $odr->statusWithBadge() !!}</td>
                        <td>{{ format_date($odr->created_at)}}</td>
                        <td class="text-right">
                            <button type="button" wire:click="displayOrder('{{ $odr->id }}')"
                                    x-on:click="showOrder = true" class="btn btn-link text-info">
                                <i class="fa fa-eye"></i>
                            </button>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="5">
                            <p class="text-center">{{__("No orders found.")}}</p>
                        </td>
                    </tr>
                @endforelse
                </tbody>
            </table>
        </div>
        @if($order)
            <div x-show="showOrder" x-on:click.away="showOrder = false" class="border rounded shadow p-4">
                <div class="table-responsive mb-4">
                    <table class="table">
                        <thead class="bg-light">
                        <tr>
                            <th class="border-0" scope="col"><strong class="text-small text-uppercase">{{__("Product")}}</strong></th>
                            <th class="border-0" scope="col"><strong class="text-small text-uppercase">{{__("Price")}}</strong></th>
                            <th class="border-0" scope="col"><strong class="text-small text-uppercase">{{__('Quantity')}}</strong></th>
                            <th class="border-0" scope="col"><strong class="text-small text-uppercase">{{__("Total")}}</strong></th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($order->products as $product)
                            <tr>
                                <td>{{ $product->name }}</td>
                                <td>{{ format_amount($product->price,$order->currency()) }}</td>
                                <td>{{ format_amount($product->pivot->quantity) }}</td>
                                <td>{{ format_amount($product->price * $product->pivot->quantity,$order->currency()) }}</td>
                            </tr>
                        @empty
                        @endforelse

                        <tr>
                            <td colspan="3" class="text-right"><strong>{{__("Subtotal")}}</strong></td>
                            <td>{{format_amount($order->subtotal,$order->currency()) }}</td>
                        </tr>
                        @if(!is_null($order->discount_code))
                            <tr>
                                <td colspan="3" class="text-right"><strong>Discount (<small>{{ $order->discount_code }}</small>)</strong></td>
                                <td>{{format_amount($order->discount,$order->currency()) }}</td>
                            </tr>
                        @endif
                        <tr>
                            <td colspan="3" class="text-right"><strong>{{__("Tax")}}</strong></td>
                            <td>{{format_amount($order->tax,$order->currency()) }}</td>
                        </tr>
                        <tr>
                            <td colspan="3" class="text-right"><strong>{{__("Shipping")}}</strong></td>
                            <td>{{format_amount($order->shipping,$order->currency()) }}</td>
                        </tr>
                        <tr>
                            <td colspan="3" class="text-right"><strong>{{__("Total")}}</strong></td>
                            <td>{{format_amount($order->total,$order->currency()) }}</td>
                        </tr>
                        </tbody>
                    </table>

                </div>

                <h2 class="h5 text-uppercase">{{__('Transactions')}}</h2>
                <div class="table-responsive mb-4">
                    <table class="table">
                        <thead class="bg-light">
                        <tr>
                            <th class="border-0" scope="col"><strong class="text-small text-uppercase">Transaction</strong></th>
                            <th class="border-0" scope="col"><strong class="text-small text-uppercase">Date</strong></th>
                       {{-- <th class="border-0" scope="col"><strong class="text-small text-uppercase">Days</strong></th> --}}
                            <th></th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($order->transactions as $transaction)
                            <tr>
                                <td>{{ $transaction->status() }}</td>
                                <td>{{ format_date($transaction->created_at) }}</td>
                           {{-- <td>{{ \Carbon\Carbon::now()->addDays(5)->diffInDays($transaction->created_at->format('Y-m-d')) }}</td>--}}
                                <td>
                                    @if ($loop->last && $transaction->transaction_status == \App\Models\OrderTransaction::FINISHED &&
                                        \Carbon\Carbon::now()->addDays(5)->diffInDays(format_date($transaction->created_at)) != 0)
                                        <button type="button" wire:click="requestReturnOrder('{{ $order->id }}')" class="btn btn-link text-right">
                                            {{__("Return Order")}}
                                        </button>
                                        <br>
                                        <small>{{__("you can return this order in :time days",['time' => 4 - $transaction->created_at->diffInDays()])}}</small>
                                    @endif
                                </td>
                            </tr>
                        @empty
                        @endforelse
                    </table>
                </div>
            </div>
        @endif()
    </div>
</div>
