@extends('layouts.admin')

@include("backend.orders.scripts")

@push("js")
<script>
    $(function () {
        $('.order-set-status-to').click(function (e) {
            e.preventDefault();
            var after = function(){
                location.href = "{{route("admin.orders.show",$order)}}";
            };
            changeStatus($(this),after)
        })
    })
</script>
@endpush


@section('content')
    <div class="card shadow mb-4">
        <div class="card-header py-3 d-flex">
            <h6 class="m-0 font-weight-bold text-success">{{__('Order')}} ({{ $order->ref_id }})</h6>
            <div class="ml-auto">

                <div class="dropdown mb-4">
                    <button class="btn btn-light dropdown-toggle" type="button" id="dropdownMenuButton"
                            data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        {!! $order->status->badge_dot ?? $status->name ?? "--" !!}
                    </button>

                    @if(!in_array($order->status_code,$rejectedStatuses))
                        <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                            @forelse($nextStatuses as $status)
                                <a class="dropdown-item order-set-status-to" href="javascript:void(0);"
                                   data-status-id="{{$status->id}}"
                                   data-order-id="{{$order->id}}"
                                   data-bagde-dot="{{$status->dot ?? "--"}}"
                                   data-confirm-message="{{__("Set ':orderId' to :status",["status" => $status->name,'orderId' => $order->ref_id])}}">
                                    {!! $status->badge_dot ?? $status->name ?? "--" !!}
                                </a>
                            @empty
                            @endforelse
                        </div>
                    @endif

                </div>
            </div>
        </div>
        <div class="d-flexs row card-body">
            <div class="col-lg-8">
                <div class="table-responsive">
                    <table class="table">
                        <tbody>
                        <tr>
                            <th>{{__('Order Id')}}</th>
                            <td>{{ $order->ref_id }}</td>
                            <th>{{__("Customer")}}</th>
                            <td><a href="{{ route('admin.users.show', $order->customer_id) }}">{{ $order->customer->full_name ?? "--" }}</a></td>
                        </tr>
                        <tr>
                            <th>{{__("Address")}}</th>
                            <td>
                                @if(filled($order->userAddress))
                                    <a href="{{ route('admin.user_addresses.show', $order->user_address_id ) }}">
                                        {{ $order->userAddress->title ?? "--" }}
                                    </a>
                                @else
                                    ---
                                @endif
                            </td>
                            <th>{{__("Shipping Company")}}</th>
                            @if($order->shippingCompany)
                                <td>{{ $order->shippingCompany->name ?? "--" }} ({{$order->shippingCompany->code ?? "--"}})</td>
                            @endif

                        </tr>
                        <tr>
                            <th>{{__("Created At")}}</th>
                            <td>{{ format_date($order->created_at,__("d-m-Y à H:i:s")) }}</td>
                            <th>{{__("Status")}}</th>
                            <td>
                                {!! $order->status->badge_label ?? $order->statusWithBadge() ?? "--" !!}
{{--                                @if(!blank($order->status))--}}
{{--                                    <br>--}}
{{--                                    <small class="progress" style="height: 10px !important;">--}}
{{--                                        <small class="progress-bar" role="progressbar"--}}
{{--                                               style="width: 100%; {{  "background-color:".$order->status->color}}"--}}
{{--                                               aria-valuenow="100" aria-valuemin="0" aria-valuemax="100"></small>--}}
{{--                                    </small>--}}
{{--                                @endif--}}

                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="col-lg-4">
                <div class="table-responsive">
                    <table class="table">
                        <tbody>
                        <tr>
                            <th>{{__("Subtotal")}}</th>
                            <td>{{ format_amount($order->subtotal,$order->currency()) }}</td>
                        </tr>
                        <tr>
                            <th>{{__("Discount code")}}</th>
                            <td>{{ $order->discount_code }}</td>
                        </tr>
                        <tr>
                            <th>{{__("Discount")}}</th>
                            <td>{{ format_amount($order->discount,$order->currency()) }}</td>
                        </tr>
                        <tr>
                            <th>{{_("Shipping")}}</th>
                            <td>{{ format_amount($order->shipping, $order->currency()) }}</td>
                        </tr>
                        <tr>
                            <th>{{__("Tax")}}</th>
                            <td>{{ format_amount($order->tax,$order->currency()) }}</td>
                        </tr>
                        <tr>
                            <th>{{__("Amount")}}</th>
                            <td>{{ format_amount($order->total,$order->currency()) }}</td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="col-lg-12">
                <h2 class="h5 text-uppercase">{{__('Products')}}</h2>
                <div class="table-responsive mb-4">
                    <table class="table table-bordered table-striped">
                        <thead class="bg-light">
                            <tr>
                                <th>{{__("Product")}}</th>
                                <th class="right">{{__('Price')}}</th>
                                <th class="right">{{__('Quantity')}}</th>
                                <th class="right">{{__("Amount")}}</th>
                            </tr>
                        </thead>
                        <tbody>
                        @forelse($order->items as $item)
                            <tr>
                                <td>
                                    <a href="{{ route('admin.products.show', $item->product->id) }}">
                                        {{ $item->product->name }}
                                    </a>
                                </td>
                                <td class="right">{{ format_amount($item->price) }}</td>
                                <td class="right">{{ format_amount($item->quantity) }}</td>
                                <td class="right">{{ format_amount($item->amount,$order->currency()) }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="4">{{__("No products found")}}</td>
                            </tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="col-lg-12">
                <h2 class="h5 text-uppercase">{{__('Payments')}}</h2>
                <div class="table-responsive mb-4">
                    <table class="table table-bordered table-striped">
                        <thead class="bg-light">
                            <tr>
                                <th><strong>{{__("Method")}}</strong></th>
                                <th><strong>{{__("Reference")}}</strong></th>
                                <th class=right ><strong>{{__("Amount Paid")}}</strong></th>
                                <th class="right" ><strong>{{__("Amount Used")}}</strong></th>
                                <th><strong>{{__("Paid At")}}</strong></th>
                            </tr>
                        </thead>
                        <tbody>
                        @forelse($order->paymentUsages as $usage)
                            <tr class="{{ ($usage->rejected) ? "table-danger" : "" }}">
                                <td>{{ $usage->payment->method->name }}</td>
                                <td>{{ $usage->payment->provider_reference }}</td>
                                <td>{{ format_amount($usage->payment->paid_amount,$order->currency()) }}</td>
                                <td>{{ format_amount($usage->amount_used,$order->currency()) }}</td>
                                <td>{{ format_date($usage->payment->created_at) }}</td>
                            </tr>
                            @empty
                                <tr>
                                    <td colspan="{{!in_array($order->status_code,$rejectedStatuses) ? "3" : "5"}}">{{__("No payments in this order.")}}</td>
                                    @if(!in_array($order->status_code,$rejectedStatuses))
                                        <td colspan="2">
                                            <a href="javascript:void(0);" class="text-danger order-set-status-to text-underline"
                                               data-status-id="{{$paidStatus->id}}"
                                               data-order-id="{{$order->id}}"
                                               data-bagde-dot="{{$paidStatus->dot}}"
                                               data-confirm-message="{{__("Set ':orderId' to :status",["status" => $paidStatus->name,'orderId' => $order->ref_id])}}">
                                                <strong>{{__("Mark this order as Paid")}}</strong>
                                            </a>
                                        </td>
                                    @endif


                                </tr>
                        @endforelse
                    </table>
                </div>
            </div>


            @if($order->statusChanges)
                <div class="no-print col-lg-12">
                    <h2 class="h5 text-uppercase">{{__('Process')}}</h2>
                    <div class="table-responsive mb-4">
                        <table class="table">
                            <thead class="bg-light">
                            <tr>
                                <th class="border-0">{{__("Status")}}</th>
                                <th class="border-0">{{__("At")}}</th>
                                <th class="border-0">{{__("By")}}</th>
                            </tr>
                            </thead>

                            <tbody>
                                @foreach($order->statusChanges as $change)
                                    <tr >
                                        <td>{!! $change->status->badge_dot ?? "" !!}</td>
                                        <td>{{ format_date($change->created_at, __("d-m-Y H:i:s")) }}</td>
                                        <td>{{ $change->createdBy->fullName ?? "--" }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>

                    </div>
                </div>
            @endif

        </div>
    </div>

@endsection
