@extends('user.layouts.master')

@section('title','Order Details')

@section('main-content')
<div class="container-fluid">

    {{-- HEADER --}}
    <div class="card shadow-sm mb-4">
        <div class="card-body d-flex justify-content-between align-items-center">
            <div>
                <h5 class="mb-1">Order #{{ $order->order_number }}</h5>
                <small class="text-muted">
                    Placed on {{ $order->created_at->format('d M Y, h:i A') }}
                </small>
            </div>
            <div>


@if($order->shiprocket_invoice_url)
<a href="{{$order->shiprocket_invoice_url}}"
target="_blank"
class="btn btn-sm btn-success">
Download Invoice
</a>
@else
<span class="text-muted">
Invoice not generated yet
</span>
@endif


            </div>
        </div>
    </div>

    <div class="row">

        {{-- LEFT COLUMN --}}
        <div class="col-lg-8">

            {{-- ITEMS --}}
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-light">
                    <strong>Order Items</strong>
                </div>
                <div class="card-body">

                    @foreach($order->cart_info as $item)
                    <div class="d-flex justify-content-between border-bottom py-2">

                        <div>
                            @if($item->item_type === 'product' && $item->product)
                            <div class="font-weight-bold">
                                {{ $item->product->title }}
                            </div>
                            <small class="text-muted">
                                Product Item
                            </small>
                            @endif

                            @if($item->item_type === 'subscription' && $item->subscription)
                            <div class="font-weight-bold">
                                {{ $item->subscription->package_name }}
                            </div>
                            <small class="text-muted">
                                Subscription Package
                            </small>
                            @endif
                        </div>

                        <div class="text-right">
                            Qty: {{ $item->quantity }} <br>
                            ₹{{ number_format($item->price,2) }}
                        </div>

                    </div>
                    @endforeach

                </div>
            </div>

            {{-- SHIPPING --}}
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-light">
                    <strong>Shipping Information</strong>
                </div>
                <div class="card-body">
                    <p class="mb-1 font-weight-bold">
                        {{ $order->first_name }} {{ $order->last_name }}
                    </p>
                    <p class="mb-1">{{ $order->phone }}</p>
                    <p class="mb-1">{{ $order->email }}</p>
                    <p class="mb-0">
                        {{ $order->address1 }}, {{ $order->address2 }}<br>
                        {{ $order->country }} - {{ $order->post_code }}
                    </p>
                </div>
            </div>

        </div>

        {{-- RIGHT COLUMN --}}
        <div class="col-lg-4">

            {{-- ORDER SUMMARY --}}
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-light">
                    <strong>Order Summary</strong>
                </div>
                <div class="card-body">
                    <table class="table table-sm mb-0">
                        <tr>
                            <td>Subtotal</td>
                            <td class="text-right">
                                ₹{{ number_format($order->sub_total,2) }}
                            </td>
                        </tr>
                        <tr>
                            <td>Delivery</td>
                            <td class="text-right">
                                ₹{{ number_format($order->delivery_charge ?? 0,2) }}
                            </td>
                        </tr>
                        <tr class="font-weight-bold">
                            <td>Total</td>
                            <td class="text-right">
                                ₹{{ number_format($order->total_amount,2) }}
                            </td>
                        </tr>
                    </table>
                </div>
            </div>

            {{-- PAYMENT --}}
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-light">
                    <strong>Payment Information</strong>
                </div>
                <div class="card-body">
                    <table class="table table-sm mb-0">
                        <tr>
                            <td>Method</td>
                            <td class="text-right text-capitalize">
                                {{ $order->payment_method }}
                            </td>
                        </tr>
                        <tr>
                            <td>Status</td>
                            <td class="text-right">
                                <span class="badge badge-success">
                                    {{ $order->payment_status }}
                                </span>
                            </td>
                        </tr>

                        @if($order->razorpay_order_id)
                        <tr>
                            <td>Razorpay Order ID</td>
                            <td class="text-right small">
                                {{ $order->razorpay_order_id }}
                            </td>
                        </tr>
                        @endif

                        @if($order->razorpay_payment_id)
                        <tr>
                            <td>Payment ID</td>
                            <td class="text-right small">
                                {{ $order->razorpay_payment_id }}
                            </td>
                        </tr>
                        @endif
                    </table>
                </div>
            </div>

            {{-- STATUS --}}
            <div class="card shadow-sm">
                <div class="card-body text-center">

                    <div class="mb-2">Order Status</div>

                    <span class="badge badge-info text-uppercase px-3 py-2">
                        {{ $order->status }}
                    </span>


                    {{-- Exchange Status --}}
                    @if($order->exchange_requested)

                    <div class="mt-3">
                        <div class="text-muted small">Exchange Status</div>

                        <span class="badge badge-warning">
                            {{ $order->exchange_status ?? 'Requested' }}
                        </span>
                    </div>

                    @endif


                    {{-- Exchange Button --}}
                    @if(
                    $order->status == 'delivered' &&
                    !$order->exchange_requested
                    )

                    <form method="POST"
                        action="{{ route('user.order.exchange.request',$order->id) }}"
                        class="mt-3">

                        @csrf

                        <button class="btn btn-warning btn-sm">
                            Request Exchange
                        </button>

                    </form>

                    @endif

                </div>
            </div>

        </div>

    </div>
</div>
@endsection