@extends('admin.layouts.layout')
@section('title', 'درخواست لغو ' . $order->order_number)

@section('content')
<div class="container-xxl flex-grow-1 container-p-y">

    <div class="d-flex align-items-center gap-3 mb-4 flex-wrap">
        <a href="{{ route('admin.orders.returns.index') }}" class="btn btn-icon btn-outline-secondary">
            <i class="ti ti-arrow-right ti-sm"></i>
        </a>
        <div>
            <h4 class="mb-0">درخواست لغو سفارش</h4>
            <p class="text-muted mb-0 small font-monospace" dir="ltr">{{ $order->order_number }}</p>
        </div>
        <span class="badge bg-label-{{ $cancelRequest->statusColor() }}">{{ $cancelRequest->statusLabel() }}</span>
        <span class="badge bg-label-{{ $order->statusColor() }}">{{ $order->statusLabel() }}</span>
    </div>

    @if(session('success'))
    <div class="alert alert-success alert-dismissible mb-4" role="alert">
        {{ session('success') }}
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @endif

    @if($errors->any())
    <div class="alert alert-danger alert-dismissible mb-4" role="alert">
        <ul class="mb-0 ps-3">
            @foreach($errors->all() as $e)
            <li>{{ $e }}</li>
            @endforeach
        </ul>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @endif

    <div class="row gy-4">

        <div class="col-lg-8">
            <div class="card mb-4">
                <div class="card-header d-flex align-items-center gap-2">
                    <i class="ti ti-shopping-cart ti-md text-primary"></i>
                    <h5 class="mb-0">محصولات سفارش</h5>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table align-middle mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>محصول</th>
                                    <th>تعداد</th>
                                    <th>جمع</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($order->items as $item)
                                <tr>
                                    <td>
                                        <div class="fw-medium">{{ $item->product_title }}</div>
                                        @if($item->variant_title)
                                        <div class="text-muted small">{{ $item->variant_title }}</div>
                                        @endif
                                    </td>
                                    <td>{{ $item->qty }}</td>
                                    <td class="fw-bold">{{ number_format($item->subtotal) }} تومان</td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="card mb-4">
                <div class="card-header d-flex align-items-center gap-2">
                    <i class="ti ti-message-report ti-md text-primary"></i>
                    <h5 class="mb-0">جزئیات درخواست لغو</h5>
                </div>
                <div class="card-body">
                    <dl class="row mb-0">
                        <dt class="col-sm-3 text-muted">دلیل لغو</dt>
                        <dd class="col-sm-9 fw-medium">{{ $cancelRequest->reasonLabel() }}</dd>

                        <dt class="col-sm-3 text-muted">توضیحات کاربر</dt>
                        <dd class="col-sm-9" style="white-space:pre-wrap">{{ $cancelRequest->description }}</dd>

                        <dt class="col-sm-3 text-muted">تاریخ درخواست</dt>
                        <dd class="col-sm-9">{{ $cancelRequest->shamsiDate() }} — {{ $cancelRequest->created_at?->format('H:i') }}</dd>

                        @if($cancelRequest->admin_response)
                        <dt class="col-sm-3 text-muted">پاسخ ادمین</dt>
                        <dd class="col-sm-9" style="white-space:pre-wrap">{{ $cancelRequest->admin_response }}</dd>
                        @endif

                        @if($cancelRequest->reviewed_at)
                        <dt class="col-sm-3 text-muted">بررسی‌کننده</dt>
                        <dd class="col-sm-9">
                            {{ $cancelRequest->reviewer?->first_name }} {{ $cancelRequest->reviewer?->last_name }}
                            <span class="text-muted small">({{ $cancelRequest->reviewed_at->format('Y-m-d H:i') }})</span>
                        </dd>
                        @endif
                    </dl>
                </div>
            </div>

            @if($cancelRequest->status === 'pending')
            <div class="card">
                <div class="card-header d-flex align-items-center gap-2">
                    <i class="ti ti-gavel ti-md text-primary"></i>
                    <h5 class="mb-0">پاسخ و تصمیم</h5>
                </div>
                <div class="card-body">
                    <form method="POST" action="{{ route('admin.orders.returns.decide', $order) }}">
                        @csrf
                        <div class="mb-3">
                            <label class="form-label">پاسخ برای کاربر <span class="text-danger">*</span></label>
                            <textarea name="admin_response" rows="4" class="form-control @error('admin_response') is-invalid @enderror"
                                      required minlength="3" maxlength="2000"
                                      placeholder="پاسخ خود را برای کاربر بنویسید...">{{ old('admin_response') }}</textarea>
                            @error('admin_response')
                            <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                        <div class="d-flex flex-wrap gap-2">
                            <button type="submit" name="decision" value="approved" class="btn btn-success">
                                <i class="ti ti-check me-1"></i>تأیید لغو سفارش
                            </button>
                            <button type="submit" name="decision" value="rejected" class="btn btn-danger">
                                <i class="ti ti-x me-1"></i>رد درخواست
                            </button>
                            <a href="{{ route('admin.orders.show', $order) }}" class="btn btn-outline-secondary ms-auto">
                                مشاهده سفارش
                            </a>
                        </div>
                    </form>
                </div>
            </div>
            @endif
        </div>

        <div class="col-lg-4">
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">اطلاعات کاربر</h5>
                </div>
                <div class="card-body">
                    <div class="fw-medium mb-1">{{ $order->user?->first_name }} {{ $order->user?->last_name }}</div>
                    <div class="text-muted small font-monospace mb-2" dir="ltr">{{ $order->user?->mobile }}</div>
                    @if($order->user)
                    <a href="{{ route('admin.users.show', $order->user) }}" class="btn btn-sm btn-outline-primary">
                        مشاهده کاربر
                    </a>
                    @endif
                </div>
            </div>

            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">اطلاعات سفارش</h5>
                </div>
                <div class="card-body">
                    <dl class="row mb-0 small">
                        <dt class="col-6 text-muted">مبلغ قابل پرداخت</dt>
                        <dd class="col-6 fw-bold">{{ number_format($order->payable_amount) }} تومان</dd>
                        <dt class="col-6 text-muted">روش پرداخت</dt>
                        <dd class="col-6">{{ $order->payment_method_name ?? '—' }}</dd>
                        <dt class="col-6 text-muted">روش ارسال</dt>
                        <dd class="col-6">{{ $order->shipping_method_name ?? '—' }}</dd>
                        <dt class="col-6 text-muted">تاریخ سفارش</dt>
                        <dd class="col-6">{{ $order->shamsi_date }}</dd>
                    </dl>
                    <a href="{{ route('admin.orders.show', $order) }}" class="btn btn-sm btn-outline-secondary w-100 mt-3">
                        جزئیات کامل سفارش
                    </a>
                </div>
            </div>
        </div>

    </div>
</div>
@endsection
