@extends('backend.layouts.master')

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

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

  <h5 class="card-header">
Order

@if($order->shiprocket_invoice_url)
<a href="{{$order->shiprocket_invoice_url}}"
target="_blank"
class="btn btn-sm btn-success shadow-sm float-right">
<i class="fas fa-download"></i> Shiprocket Invoice
</a>
@else
<span class="badge badge-secondary float-right">
Invoice Not Generated
</span>
@endif

</h5>
  <div class="card-body">
    @if($order)
    <table class="table table-striped table-hover">
      <thead>
        <tr>
          <th>ID</th>
          <th>Order No</th>
          <th>Customer</th>
          <th>Payment</th>
          <th>Qty</th>
          <th>Subtotal</th>
          <th>Delivery</th>
          <th>Total</th>
          <th>Status</th>
        </tr>
      </thead>

      <tbody>
        <tr>
          <td>{{$order->id}}</td>

          <td>{{$order->order_number}}</td>

          <td>
            {{$order->first_name}} {{$order->last_name}}
            <br>
            <small>{{$order->email}}</small>
          </td>

          <td>
            {{$order->payment_method}}
            <br>
            <small>{{$order->payment_status}}</small>
          </td>

          <td>{{$order->quantity}}</td>

          <td>₹ {{number_format($order->sub_total,2)}}</td>

          <td>₹ {{number_format($order->delivery_charge,2)}}</td>

          <td><strong>₹ {{number_format($order->total_amount,2)}}</strong></td>

          <td>
            @if($order->status=='new')
            <span class="badge badge-primary">New</span>

            @elseif($order->status=='process')
            <span class="badge badge-warning">Processing</span>

            @elseif($order->status=='delivered')
            <span class="badge badge-success">Delivered</span>

            @else
            <span class="badge badge-danger">Cancelled</span>
            @endif
          </td>

        </tr>
      </tbody>
    </table>
    <section class="confirmation_part section_padding">
      <div class="order_boxes">
        <div class="row">
          <div class="col-lg-6 col-lx-4">
            <div class="order-info">
              <h4 class="text-center pb-4">ORDER INFORMATION</h4>

              <table class="table">

                <tr>
                  <td>Order Number</td>
                  <td>{{$order->order_number}}</td>
                </tr>

                <tr>
                  <td>Order Date</td>
                  <td>{{$order->created_at->format('d M Y H:i')}}</td>
                </tr>

                <tr>
                  <td>Subtotal</td>
                  <td>₹ {{number_format($order->sub_total,2)}}</td>
                </tr>

                <tr>
                  <td>Delivery Charge</td>
                  <td>₹  {{number_format($order->delivery_charge,2)}}</td>
                </tr>

                <tr>
                  <td>Coupon</td>
                  <td>₹  {{number_format($order->coupon ?? 0,2)}}</td>
                </tr>

                <tr>
                  <td>Total Amount</td>
                  <td><strong>₹{{number_format($order->total_amount,2)}}</strong></td>
                </tr>

                <tr>
                  <td>Quantity</td>
                  <td>{{$order->quantity}}</td>
                </tr>

                <tr>
                  <td>Payment Method</td>
                  <td>{{$order->payment_method}}</td>
                </tr>

                <tr>
                  <td>Payment Status</td>
                  <td>{{$order->payment_status}}</td>
                </tr>

              </table>
              <hr>
              <h4 class="text-center pb-4">PAYMENT DETAILS</h4>

              <table class="table">

                <tr>
                  <td>Razorpay Order ID</td>
                  <td>{{$order->razorpay_order_id ?? 'N/A'}}</td>
                </tr>

                <tr>
                  <td>Razorpay Payment ID</td>
                  <td>{{$order->razorpay_payment_id ?? 'N/A'}}</td>
                </tr>

                <tr>
                  <td>Signature</td>
                  <td>{{$order->razorpay_signature ?? 'N/A'}}</td>
                </tr>

              </table>
              <hr>


              <h4 class="text-center pb-4">SHIPMENT INFORMATION</h4>

              <table class="table">

                <tr>
                  <td>Shiprocket Order</td>
                  <td>{{$order->shiprocket_order_ref ?? 'Not Created'}}</td>
                </tr>

                <tr>
                  <td>Shipment ID</td>
                  <td>{{$order->shipment_id ?? 'Not Created'}}</td>
                </tr>

                <tr>
                  <td>Courier</td>
                  <td>{{$order->courier_name ?? 'Not Assigned'}}</td>
                </tr>

                <tr>
                  <td>AWB Code</td>
                  <td>{{$order->awb_code ?? 'Not Generated'}}</td>
                </tr>

                <tr>
                  <td>Status</td>
                  <td>{{$order->shiprocket_status ?? 'Pending'}}</td>
                </tr>

                <tr>
                  <td>Tracking</td>
                  <td>
                    @if($order->tracking_url)
                    <a href="{{$order->tracking_url}}" target="_blank" class="btn btn-sm btn-info">
                      Track Shipment
                    </a>
                    @else
                    Not Available
                    @endif
                  </td>
                </tr>

<tr>
<td>Order Invoice</td>  
<td>
@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</span>
@endif
</td>
</tr>

<tr>
<td>Shipping Label</td>
<td>
@if($order->label_url)
<a href="{{$order->label_url}}" target="_blank" class="btn btn-sm btn-secondary">
Download Label
</a>
@else
Not Generated
@endif
</td>
</tr>




                @if(!$order->shipment_id)

                <tr>
                  <td>Create Shipment</td>

                  <td>
                    <form method="POST" action="{{route('admin.order.createShipment',$order->id)}}">
                      @csrf
                      <button class="btn btn-primary">
                        Create Shiprocket Shipment
                      </button>
                    </form>
                  </td>

                </tr>

                @endif




                <tr>
                  <td colspan="2">
                    <hr>
                  </td>
                </tr>

                @if($order->return_shipment_id)

                <tr>
                  <td>Return Shipment ID</td>
                  <td>{{$order->return_shipment_id}}</td>
                </tr>

                <tr>
                  <td>Return AWB</td>
                  <td>{{$order->return_awb_code}}</td>
                </tr>

                @endif

                <tr>
                  <td colspan="2">
                    <h4 class="text-center">EXCHANGE</h4>
                  </td>
                </tr>

                @if($order->exchange_requested && $order->exchange_status == 'requested')

                <tr>
                  <td>Exchange Request</td>

                  <td>
                    <form method="POST" action="{{route('admin.order.schedulePickup',$order->id)}}">
                      @csrf

                      <button class="btn btn-warning">
                        Schedule Return Pickup
                      </button>

                    </form>
                  </td>

                </tr>

                @endif

                @if($order->exchange_requested)

                <tr>
                  <td>Exchange Status</td>
                  <td>{{$order->exchange_status ?? 'Requested'}}</td>
                </tr>

                @endif

                @if($order->exchange_status == 'pickup_scheduled' && !$order->shipment_id)

                <tr>
                  <td>Create Replacement</td>

                  <td>
                    <form method="POST" action="{{route('admin.order.exchange',$order->id)}}">
                      @csrf

                      <button class="btn btn-primary">
                        Create Replacement Order
                      </button>

                    </form>
                  </td>

                </tr>

                @endif
              </table>
            </div>
          </div>

          <div class="col-lg-6 col-lx-4">
            <div class="shipping-info">
              <h4 class="text-center pb-4">CUSTOMER ADDRESS</h4>

              <table class="table">

                <tr>
                  <td>Name</td>
                  <td>{{$order->first_name}} {{$order->last_name}}</td>
                </tr>

                <tr>
                  <td>Email</td>
                  <td>{{$order->email}}</td>
                </tr>

                <tr>
                  <td>Phone</td>
                  <td>{{$order->phone}}</td>
                </tr>

                <tr>
                  <td>Country</td>
                  <td>{{$order->country}}</td>
                </tr>

                <tr>
                  <td>State</td>
                  <td>{{$order->state ?? 'N/A'}}</td>
                </tr>

                <tr>
                  <td>City</td>
                  <td>{{$order->city ?? 'N/A'}}</td>
                </tr>

                <tr>
                  <td>Postal Code</td>
                  <td>{{$order->post_code ?? 'N/A'}}</td>
                </tr>

                <tr>
                  <td>Address</td>
                  <td>{{$order->address1}} {{$order->address2}}</td>
                </tr>

              </table>
            </div>
          </div>
        </div>
      </div>
    </section>
    @endif

  </div>
</div>
@endsection

@push('styles')
<style>
  .order-info,
  .shipping-info {
    background: #ECECEC;
    padding: 20px;
  }

  .order-info h4,
  .shipping-info h4 {
    text-decoration: underline;
  }
</style>
@endpush