@extends('layouts.store')

@section('title', $order->order_number)

@section('content')
    <section class="dashboard-head">
        <div>
            <span class="eyebrow">Order</span>
            <h1>{{ $order->order_number }}</h1>
        </div>
        <div class="table-actions">
            <a class="button ghost" href="{{ route('customer.orders.index') }}">Back</a>
            <a class="button ghost" href="{{ route('orders.track', ['order_number' => $order->order_number, 'delivery_phone' => $order->delivery_phone]) }}">Track</a>
            <a class="button primary" href="{{ route('customer.orders.invoice', $order) }}">Invoice</a>
        </div>
    </section>

    @if (session('status'))
        <p class="form-status">{{ session('status') }}</p>
    @endif

    <section class="admin-grid">
        <article class="panel">
            <h2>Items</h2>
            <div class="table-wrap">
                <table>
                    <thead><tr><th>Product</th><th>Qty</th><th>Total</th></tr></thead>
                    <tbody>
                        @foreach ($order->items as $item)
                            <tr>
                                <td>
                                    {{ $item->product_name }}
                                    @if ($item->variant_label)
                                        <small>{{ $item->variant_label }}</small>
                                    @endif
                                </td>
                                <td>{{ $item->quantity }}</td>
                                <td>{{ \App\Models\SiteSetting::formatPrice($item->line_total) }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </article>
        <article class="panel">
            <h2>Status</h2>
            <dl class="account-details">
                <div><dt>Status</dt><dd>{{ $order->statusLabel() }}</dd></div>
                <div><dt>Payment</dt><dd>{{ strtoupper($order->payment_method) }}</dd></div>
                <div><dt>Subtotal</dt><dd>{{ \App\Models\SiteSetting::formatPrice($order->subtotal) }}</dd></div>
                <div><dt>Shipping</dt><dd>{{ \App\Models\SiteSetting::formatPrice($order->shipping_amount) }}</dd></div>
                @if ((float) $order->discount_amount > 0)
                    <div><dt>Coupon</dt><dd>{{ $order->coupon_code }} - {{ \App\Models\SiteSetting::formatPrice($order->discount_amount) }}</dd></div>
                @endif
                <div><dt>Total</dt><dd>{{ \App\Models\SiteSetting::formatPrice($order->total) }}</dd></div>
                <div>
                    <dt>Delivery Address</dt>
                    <dd>{{ $order->delivery_recipient_name }} - {{ $order->delivery_phone }}<br>{{ collect([$order->delivery_address_line, $order->delivery_area, $order->delivery_city])->filter()->implode(', ') }}</dd>
                </div>
                @if ($order->payment_approval_note)
                    <div><dt>Payment Approval Note</dt><dd>{{ $order->payment_approval_note }}</dd></div>
                @endif
                @if ($order->payment_rejection_reason)
                    <div><dt>Payment Rejection Reason</dt><dd>{{ $order->payment_rejection_reason }}</dd></div>
                @endif
            </dl>
            @if ($order->paymentScreenshotUrl())
                <div class="media-list">
                    <a class="auth-link" href="{{ $order->paymentScreenshotUrl() }}" target="_blank" rel="noopener">View payment screenshot</a>
                </div>
            @endif
        </article>
    </section>

    @if ($order->canReceiveFeedback())
        <section class="panel">
            <h2>Product Feedback</h2>

            @if ($errors->any())
                <p class="form-error">{{ $errors->first() }}</p>
            @endif

            <div class="feedback-list">
                @foreach ($order->items as $item)
                    @php
                        $review = $reviewsByProduct->get($item->product_id);
                        $complaint = $complaintsByProduct->get($item->product_id);
                    @endphp

                    <article class="feedback-item">
                        <h3>{{ $item->product_name }}{{ $item->variant_label ? ' - '.$item->variant_label : '' }}</h3>
                        <div class="admin-grid">
                            <form method="POST" action="{{ route('customer.orders.review', $order) }}" class="admin-form" enctype="multipart/form-data">
                                @csrf
                                <input name="product_id" type="hidden" value="{{ $item->product_id }}">

                                <label>
                                    <span>Rating</span>
                                    <select name="rating" required>
                                        @for ($rating = 5; $rating >= 1; $rating--)
                                            <option value="{{ $rating }}" @selected((int) old('rating', $review?->rating ?? 5) === $rating)>{{ $rating }} Star</option>
                                        @endfor
                                    </select>
                                </label>

                                <label>
                                    <span>Review</span>
                                    <textarea name="comment" placeholder="Share your experience">{{ old('comment', $review?->comment) }}</textarea>
                                </label>

                                <div class="form-grid two-columns">
                                    <label>
                                        <span>Review image</span>
                                        <input name="review_image" type="file" accept="image/jpeg,image/png,image/webp">
                                    </label>

                                    <label>
                                        <span>Review video</span>
                                        <input name="review_video" type="file" accept="video/mp4,video/quicktime,video/webm">
                                    </label>
                                </div>

                                <button class="button primary" type="submit">Submit Product Review</button>
                            </form>

                            <form method="POST" action="{{ route('customer.orders.complaint', $order) }}" class="admin-form">
                                @csrf
                                <input name="product_id" type="hidden" value="{{ $item->product_id }}">

                                <label>
                                    <span>Subject</span>
                                    <input name="subject" value="{{ old('subject', $complaint?->subject) }}" maxlength="120" required>
                                </label>

                                <label>
                                    <span>Complaint</span>
                                    <textarea name="message" required>{{ old('message', $complaint?->message) }}</textarea>
                                </label>

                                <button class="button primary" type="submit">Submit Product Complaint</button>
                            </form>
                        </div>

                        @if ($review || $complaint)
                            <div class="feedback-summary">
                                @if ($review)
                                    <div>
                                        <strong>Your Review</strong>
                                        <p>{{ $review->rating }} Star{{ $review->comment ? ' - '.$review->comment : '' }}</p>
                                        <div class="review-media">
                                            @if ($review->imageUrl())
                                                <img src="{{ $review->imageUrl() }}" alt="Review image">
                                            @endif
                                            @if ($review->videoUrl())
                                                <video controls src="{{ $review->videoUrl() }}"></video>
                                            @endif
                                        </div>
                                    </div>
                                @endif

                                @if ($complaint)
                                    <div>
                                        <strong>Your Complaint</strong>
                                        <p>{{ $complaint->subject }} - {{ $complaint->message }}</p>
                                        @if ($complaint->admin_response)
                                            <strong>Admin Response</strong>
                                            <p>{{ $complaint->admin_response }}</p>
                                        @endif
                                    </div>
                                @endif
                            </div>
                        @endif
                    </article>
                @endforeach
            </div>
        </section>
    @endif
@endsection
