@extends('admin.layouts.layout')
@section('title', 'جزئیات تراکنش #' . $transaction->id)

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

    <div class="d-flex align-items-center justify-content-between mb-4 flex-wrap gap-2">
        <div>
            <h4 class="mb-1">تراکنش #{{ $transaction->id }}</h4>
            <p class="text-muted mb-0 small">اطلاعات کامل تراکنش درگاه</p>
        </div>
        <a href="{{ route('admin.finance.transactions.index') }}" class="btn btn-outline-secondary">
            <i class="ti ti-arrow-right ti-xs me-1"></i>بازگشت به لیست
        </a>
    </div>

    <div class="row g-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-credit-card ti-md text-primary"></i>
                    <h5 class="mb-0">اطلاعات تراکنش</h5>
                </div>
                <div class="card-body">
                    <div class="row g-3">
                        <div class="col-sm-6">
                            <div class="text-muted small mb-1">شناسه تراکنش</div>
                            <div class="fw-medium font-monospace" dir="ltr">#{{ $transaction->id }}</div>
                        </div>
                        <div class="col-sm-6">
                            <div class="text-muted small mb-1">وضعیت</div>
                            <span class="badge bg-label-{{ $transaction->statusColor() }}">{{ $transaction->statusLabel() }}</span>
                        </div>
                        <div class="col-sm-6">
                            <div class="text-muted small mb-1">درگاه</div>
                            <div class="fw-medium">{{ $transaction->gateway_name ?: ($gatewayMeta['name'] ?? $transaction->gateway) }}</div>
                            <div class="text-muted small font-monospace" dir="ltr">{{ $transaction->gateway }}</div>
                        </div>
                        <div class="col-sm-6">
                            <div class="text-muted small mb-1">نوع درگاه</div>
                            @if($isInstallment)
                            <span class="badge bg-label-info">اقساطی</span>
                            @else
                            <span class="badge bg-label-secondary">غیر اقساطی</span>
                            @endif
                        </div>
                        <div class="col-sm-6">
                            <div class="text-muted small mb-1">مبلغ</div>
                            <div class="fw-bold fs-5">{{ number_format($transaction->amount) }} تومان</div>
                        </div>
                        <div class="col-sm-6">
                            <div class="text-muted small mb-1">شناسه پیگیری (Track ID)</div>
                            <div class="font-monospace" dir="ltr">{{ $transaction->track_id ?: '—' }}</div>
                        </div>
                        <div class="col-sm-6">
                            <div class="text-muted small mb-1">شماره مرجع</div>
                            <div class="font-monospace" dir="ltr">{{ $transaction->ref_number ?: '—' }}</div>
                        </div>
                        <div class="col-sm-6">
                            <div class="text-muted small mb-1">شماره کارت</div>
                            <div class="font-monospace" dir="ltr">{{ $transaction->card_number ?: '—' }}</div>
                        </div>
                        <div class="col-sm-6">
                            <div class="text-muted small mb-1">تاریخ ایجاد</div>
                            <div>{{ $transaction->shamsiCreatedAt() }}</div>
                        </div>
                        <div class="col-sm-6">
                            <div class="text-muted small mb-1">زمان پرداخت</div>
                            <div>{{ $transaction->shamsiPaidAt() ?: '—' }}</div>
                        </div>
                        @if($transaction->error_code || $transaction->error_message)
                        <div class="col-12">
                            <div class="alert alert-danger mb-0 py-2">
                                @if($transaction->error_code)
                                <div class="small"><span class="text-muted">کد خطا:</span> <span class="font-monospace" dir="ltr">{{ $transaction->error_code }}</span></div>
                                @endif
                                @if($transaction->error_message)
                                <div class="small mt-1">{{ $transaction->error_message }}</div>
                                @endif
                            </div>
                        </div>
                        @endif
                    </div>
                </div>
            </div>

            @if(!empty($transaction->gateway_response))
            <div class="card mb-4">
                <div class="card-header d-flex align-items-center gap-2">
                    <i class="ti ti-code ti-md text-primary"></i>
                    <h5 class="mb-0">پاسخ خام درگاه</h5>
                </div>
                <div class="card-body">
                    <pre class="bg-light border rounded p-3 mb-0 small overflow-auto" dir="ltr" style="max-height:420px;white-space:pre-wrap;word-break:break-word">{{ json_encode($transaction->gateway_response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) }}</pre>
                </div>
            </div>
            @endif

        </div>

        <div class="col-lg-4">

            <div class="card mb-4">
                <div class="card-header d-flex align-items-center gap-2">
                    <i class="ti ti-user ti-md text-primary"></i>
                    <h5 class="mb-0">کاربر</h5>
                </div>
                <div class="card-body">
                    @if($transaction->user)
                    <div class="fw-medium mb-1">{{ $transaction->user->first_name }} {{ $transaction->user->last_name }}</div>
                    <div class="text-muted small mb-2" dir="ltr">{{ $transaction->user->mobile }}</div>
                    <a href="{{ route('admin.users.show', $transaction->user) }}" class="btn btn-sm btn-outline-primary">
                        مشاهده کاربر
                    </a>
                    @else
                    <span class="text-muted">کاربر یافت نشد</span>
                    @endif
                </div>
            </div>

            <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">
                    @if($transaction->order)
                    @php $order = $transaction->order; @endphp
                    <div class="d-flex justify-content-between py-1 border-bottom">
                        <span class="text-muted small">شماره سفارش</span>
                        <span class="font-monospace small fw-medium" dir="ltr">{{ $order->order_number }}</span>
                    </div>
                    <div class="d-flex justify-content-between py-1 border-bottom">
                        <span class="text-muted small">وضعیت سفارش</span>
                        <span class="badge bg-label-{{ $order->statusColor() }}">{{ $order->statusLabel() }}</span>
                    </div>
                    <div class="d-flex justify-content-between py-1 border-bottom">
                        <span class="text-muted small">مبلغ قابل پرداخت</span>
                        <span class="small fw-medium">{{ number_format($order->payable_amount) }} تومان</span>
                    </div>
                    <div class="d-flex justify-content-between py-1 border-bottom">
                        <span class="text-muted small">روش پرداخت</span>
                        <span class="small">{{ $order->payment_method_name ?? '—' }}</span>
                    </div>
                    <div class="d-flex justify-content-between py-1 mb-3">
                        <span class="text-muted small">تاریخ</span>
                        <span class="small">{{ $order->shamsi_date }}</span>
                    </div>
                    <a href="{{ route('admin.orders.show', $order) }}" class="btn btn-sm btn-outline-primary w-100">
                        مشاهده سفارش
                    </a>
                    @else
                    <span class="text-muted">سفارش یافت نشد</span>
                    @endif
                </div>
            </div>

        </div>
    </div>

</div>
@endsection
