@extends('admin.layouts.app')

@section('title', $title)

@section('content')
    <form class="form-horizontal" action="{{ route('ch-admin.subscription.order.update', ['subscription_id' => $order->subscription_id, 'order_id' => $order->id]) }}" method="post">
        @csrf
        @method('PUT')

        <div class="row">
            <!-- Left Section -->
            <div class="col-md-8">
                <div class="bgc-white bd bdrs-3 p-20 mB-20">
                    <section class="invoice mb-20">
                        <a href="{{ route('ch-admin.subscription.edit', $order->subscription_id) }}"><i class="ti-arrow-left me-2"></i>Back to Subscription</a>
                        <!-- Title -->
                        <div class="row">
                            <div class="col-xs-12">
                                <h3 class="c-grey-900 mT-10 mB-30">
                                    <i class="fa fa-cart-plus me-2"></i>Order #{{$order->order_reference}}
                                    <small class="pull-right">Date Time: {{$order->created_at}}</small>
                                </h3>
                            </div>
                        </div>

                        <!-- General & Billing Details -->
                        <div class="row invoice-info">
                            <!-- General Info -->
                            <div class="col-sm-6 invoice-col">
                                <p class="lead">General:</p>
                                <strong>Submitted: </strong><span title="{{$order->created_at}}">{{$order->created_at->diffForHumans()}}</span><br>
                                @php
                                    $statusDetails = getStatusDetails((int)$order->status);
                                @endphp
                                <strong>Order Status: </strong><span class="order-status badge {{ $statusDetails['class'] }}">{{ $statusDetails['label'] }}</span>
                            </div>
                            <!-- Billing Details -->
                            <div class="col-sm-6 invoice-col">
                                <p class="lead">Billing Details:</p>
                                <address>
                                    @if($order->user_id == 0)
                                        <strong>Guest Order</strong><br>
                                        <strong>Name:</strong> {{ $order->getMeta('billing_first_name') . ' ' . $order->getMeta('billing_last_name') }}<br>
                                        <strong>Email:</strong> {!! $order->getMeta('billing_email') ?? 'N/A' !!}<br>
                                    @else
                                        <strong>Name:</strong> <a href="{{ route('ch-admin.user.edit', [$order->user->id]) }}">{{ $order->billingInfo->first_name . ' ' . $order->billingInfo->last_name }}</a><br>
                                        <strong>Address:</strong> {!! $order->billingInfo->address !!}<br>
                                        <strong>City:</strong> {!! $order->billingInfo->city !!}<br>
                                        <strong>State:</strong> {!! $order->billingInfo->state->name ?? null !!}<br>
                                        <strong>Country:</strong> {!! $order->billingInfo->country->name ?? null !!}<br>
                                        <strong>Zip code:</strong> {!! $order->billingInfo->zip !!}<br>
                                    @endif

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

                        <!-- Items Table -->
                        <div class="row">
                            <div class="col-xs-12 table-responsive">
                                <p class="lead mt-30">Item Purchased:</p>
                                <table class="table table-striped">
                                    <thead>
                                    <tr>
                                        <th>Product</th>
                                        <th>Quantity</th>
                                        <th>Price</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    @foreach ( $order->items as $item )
                                        <tr>
                                            <td>{!! $item->name !!}</td>
                                            <td>{{ $item->quantity }}</td>
                                            <td>{!! ch_format_price( $item->price, $order->currency ) !!}</td>
                                        </tr>
                                    @endforeach
                                    @foreach ( $order->addons as $addon )
                                        <tr>
                                            <td>{!! $addon->name !!}</td>
                                            <td>{{ $item->quantity }}</td>
                                            <td>{!! ch_format_price( $addon->pivot->price, $order->currency ) !!}</td>
                                        </tr>
                                    @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>

                        <!-- Payment & Order Summary -->
                        <div class="row">
                            <!-- Payment Method -->
                            <div class="col-md-6">
                                <p class="lead">Payment Details:</p>

                                <div class="table-responsive">
                                    <table class="table">
                                        <tr>
                                            <th class="w-50p">Payment method:</th>
                                            <td>{{ $order->offline_payment_method ?? $order->payment_method }}</td>
                                        </tr>
                                        <tr>
                                            <th>Transaction ID</th>
                                            <td>{{ $order->transaction_id }}</td>
                                        </tr>
                                    </table>
                                </div>
                            </div>

                            <!-- Order Summary -->
                            <div class="col-md-6">
                                <p class="lead">Summary</p>

                                <div class="table-responsive">
                                    <table class="table">
                                        <tr>
                                            <th class="w-50p">Subtotal:</th>
                                            <td>{!! ch_format_price($order->subtotal, $order->currency) !!}</td>
                                        </tr>
                                        @if($order->discount_amount > 0)
                                            <tr>
                                                <th>Coupon code</th>
                                                <td>{!! $order->discount_code !!}</td>
                                            </tr>
                                            <tr>
                                                <th>Coupon amount</th>
                                                <td>{!! ch_format_price($order->discount_amount, $order->currency) !!}</td>
                                            </tr>
                                        @endif
                                        <tr>
                                            <th>Tax</th>
                                            <td>{!! ch_format_price($order->tax_amount, $order->currency) !!}</td>
                                        </tr>
                                        <tr>
                                            <th>Total</th>
                                            <td>{!! ch_format_price($order->total, $order->currency) !!}</td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </section>
                </div>
            </div>

            <!-- Right Section -->
            <div class="col-md-4">
                <div class="bgc-white bd bdrs-3 p-20 mB-20">
                    <h4 class="c-grey-900 mT-10">Update Order</h4>

                    <!-- Transaction Id -->
                    @if($order->payment_method == 'offline_payments')
                        <div class="mb-3">
                            <label for="transaction_id" class="col-sm-12 mb-2">Transaction Id</label>
                            <div class="col-sm-12">
                                <input type="text" name="transaction_id" id="transaction_id" class="form-control" value="{{$order->transaction_id}}"/>
                            </div>
                        </div>
                    @endif

                    <!-- Order Status -->
                    <div class="mb-3">
                        <label for="status" class="col-sm-12 mb-2">Order Status</label>
                        <div class="col-sm-12">
                            <select class="form-control" name="status">
                                @foreach(getConstants('order_status') as $key => $value)
                                    <option value="{{$key}}" {{ $order->status == $key ? 'SELECTED' : '' }}>
                                        {{$value}}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                    </div>

                    <button class="btn btn-block btn-primary" {{ $order->status === (int)getConstant('ORDER_STATUS_COMPLETED') ? 'disabled' : '' }}>Update Order</button>
                </div>
            </div>
        </div>
    </form>
@endsection
