@extends('layouts.admin')
@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">
                <form action="{{ route('admin.orders.update', $order) }}" method="post">
                    @csrf
                    @method('PUT')
                    <div class="form-row align-items-center">
                        <label class="sr-only" for="inlineFormInputGroupUsername">{{__("Order Status")}}</label>
                        <div class="input-group">
                            <div class="input-group-prepend">
                                <div class="input-group-text">{{__("Order status")}}</div>
                            </div>
                            <select class="form-control" name="order_status" style="outline-style: none;" onchange="this.form.submit()">
                                <option value=""> {{__("Choose Action")}} </option>
                                @foreach($StatusArray as $key => $value)
                                    <option value="{{ $key }}">{{ $value }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                </form>
            </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>
                                <a href="{{ route('admin.user_addresses.show', $order->user_address_id) }}">
                                    {{ $order->userAddress->title }}
                                </a>
                            </td>
                            <th>{{__("Shipping Company")}}</th>
                            <td>{{ $order->shippingCompany->name . '('. $order->shippingCompany->code .')' }}</td>
                        </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_dot ?? $order->statusWithBadge() !!}</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">{{__('Payments')}}</h2>
                <div class="table-responsive mb-4">
                    <table class="table table-bordered table-striped">
                        <thead >
                        <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="5">{{__("No payments in this order.")}}</td>
                                </tr>
                            @endforelse
                    </table>
                </div>
            </div>
        </div>
    </div>

    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-success">{{__("Transactions")}}</h6>
        </div>

        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>{{__("Transaction")}}</th>
                        <th>{{__("Transaction number")}}</th>
                        <th>{{__("Payment result")}}</th>
                        <th>{{__("Action date")}}</th>
                    </tr>
                </thead>
                <tbody>
                @forelse($order->transactions as $transaction)
                    <tr>
                        <td>{{ $transaction->transaction_status }}</td>
                        <td>{{ $transaction->transaction_number }}</td>
                        <td>{{ $transaction->payment_result }}</td>
                        <td>{{ format_date($transaction->created_at,__("d-m-Y à H:i:s")) }}</td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="4">{{__("No transactions found")}}</td>
                    </tr>
                @endforelse
                </tbody>
            </table>
        </div>
    </div>

    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-success">{{__("Details")}}</h6>
        </div>

        <div class="table-responsive">
            <table class="table">
                <thead>
                    <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,true) }}</td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="4">{{__("No products found")}}</td>
                    </tr>
                @endforelse
                </tbody>
            </table>
        </div>
    </div>
@endsection
