<?php $__env->startSection('title', 'سفارش ' . $order->order_number); ?>

<?php $__env->startSection('content'); ?>
<div class="container-xxl flex-grow-1 container-p-y">

    <div class="d-flex align-items-center gap-3 mb-4">
        <a href="<?php echo e(route('admin.orders.index')); ?>" class="btn btn-icon btn-outline-secondary">
            <i class="ti ti-arrow-right ti-sm"></i>
        </a>
        <div>
            <h4 class="mb-0" dir="ltr"><?php echo e($order->order_number); ?></h4>
            <p class="text-muted mb-0 small"><?php echo e($order->shamsi_date); ?></p>
        </div>
        <span class="badge bg-label-<?php echo e($order->statusColor()); ?> ms-2"><?php echo e($order->statusLabel()); ?></span>
    </div>

    <?php if(session('success')): ?>
    <div class="alert alert-success alert-dismissible mb-4" role="alert">
        <?php echo e(session('success')); ?>

        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php 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>
                                    <th>تعداد</th>
                                    <th>جمع</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $order->items; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <tr>
                                    <td>
                                        <div class="d-flex align-items-center gap-2">
                                            <?php if($item->product && $item->product->featured_image): ?>
                                            <a href="<?php echo e(route('product.show', $item->product->slug)); ?>" target="_blank">
                                                <img src="<?php echo e(asset('uploads/products/' . $item->product->featured_image)); ?>"
                                                     alt="<?php echo e($item->product_title); ?>"
                                                     class="product-list-thumb rounded">
                                            </a>
                                            <?php endif; ?>
                                            <div>
                                                <?php if($item->product): ?>
                                                <a href="<?php echo e(route('product.show', $item->product->slug)); ?>" class="fw-medium text-dark text-decoration-none" target="_blank"><?php echo e($item->product_title); ?></a>
                                                <?php else: ?>
                                                <div class="fw-medium"><?php echo e($item->product_title); ?></div>
                                                <?php endif; ?>
                                                <?php if($item->variant_title): ?>
                                                <div class="text-muted small"><?php echo e($item->variant_title); ?></div>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    </td>
                                    <td class="text-muted"><?php echo e(number_format($item->original_price)); ?> تومان</td>
                                    <td class="<?php echo e($item->final_price < $item->original_price ? 'text-danger fw-bold' : ''); ?>">
                                        <?php echo e(number_format($item->final_price)); ?> تومان
                                    </td>
                                    <td><?php echo e($item->qty); ?></td>
                                    <td class="fw-bold"><?php echo e(number_format($item->subtotal)); ?> تومان</td>
                                </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            
            <div class="card">
                <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 p-0">
                    <?php if($order->transactions->isEmpty()): ?>
                    <div class="text-center py-4 text-muted small">تراکنشی ثبت نشده است.</div>
                    <?php else: ?>
                    <div class="table-responsive">
                        <table class="table align-middle mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>درگاه</th>
                                    <th>مبلغ</th>
                                    <th>وضعیت</th>
                                    <th>شناسه پیگیری</th>
                                    <th>شماره مرجع</th>
                                    <th>کارت</th>
                                    <th>زمان</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $order->transactions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tx): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <tr>
                                    <td><?php echo e($tx->gateway_name ?? $tx->gateway); ?></td>
                                    <td class="fw-bold"><?php echo e(number_format($tx->amount)); ?> تومان</td>
                                    <td>
                                        <span class="badge bg-label-<?php echo e($tx->status === 'success' ? 'success' : ($tx->status === 'failed' ? 'danger' : 'warning')); ?>">
                                            <?php echo e(['pending'=>'در انتظار','success'=>'موفق','failed'=>'ناموفق'][$tx->status] ?? $tx->status); ?>

                                        </span>
                                    </td>
                                    <td class="font-monospace small" dir="ltr"><?php echo e($tx->track_id ?? '—'); ?></td>
                                    <td class="font-monospace small" dir="ltr"><?php echo e($tx->ref_number ?? '—'); ?></td>
                                    <td class="font-monospace small" dir="ltr"><?php echo e($tx->card_number ?? '—'); ?></td>
                                    <td class="text-muted small font-monospace" dir="ltr">
                                        <?php echo e($tx->paid_at ? $tx->paid_at->format('Y-m-d H:i') : '—'); ?>

                                    </td>
                                </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                        </table>
                    </div>
                    <?php endif; ?>
                </div>
            </div>

        </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-receipt ti-md text-primary"></i>
                    <h5 class="mb-0">خلاصه مالی</h5>
                </div>
                <div class="card-body">
                    <div class="d-flex justify-content-between py-1 border-bottom">
                        <span class="text-muted small">قیمت اصلی محصولات:</span>
                        <span class="small"><?php echo e(number_format($order->subtotal)); ?> تومان</span>
                    </div>
                    <?php if($order->product_discount > 0): ?>
                    <div class="d-flex justify-content-between py-1 border-bottom">
                        <span class="text-muted small">تخفیف محصولات:</span>
                        <span class="small text-danger">−<?php echo e(number_format($order->product_discount)); ?> تومان</span>
                    </div>
                    <?php endif; ?>
                    <?php if($order->coupon_discount > 0): ?>
                    <div class="d-flex justify-content-between py-1 border-bottom">
                        <span class="text-muted small">کد تخفیف (<?php echo e($order->coupon_code); ?>):</span>
                        <span class="small text-danger">−<?php echo e(number_format($order->coupon_discount)); ?> تومان</span>
                    </div>
                    <?php endif; ?>
                    <div class="d-flex justify-content-between py-1 border-bottom">
                        <span class="text-muted small">هزینه ارسال:</span>
                        <span class="small <?php echo e($order->shipping_cost === 0 ? 'text-success' : ''); ?>">
                            <?php echo e($order->shipping_cost > 0 ? number_format($order->shipping_cost).' تومان' : 'رایگان'); ?>

                        </span>
                    </div>
                    <?php if($order->wallet_used > 0): ?>
                    <div class="d-flex justify-content-between py-1 border-bottom">
                        <span class="text-muted small">اعتبار کیف پول:</span>
                        <span class="small text-info">−<?php echo e(number_format($order->wallet_used)); ?> تومان</span>
                    </div>
                    <?php endif; ?>
                    <div class="d-flex justify-content-between py-2 mt-1">
                        <span class="fw-bold">مبلغ پرداختی:</span>
                        <span class="fw-bold text-primary"><?php echo e(number_format($order->payable_amount)); ?> تومان</span>
                    </div>
                </div>
            </div>

            
            <div class="card mb-4">
                <div class="card-header d-flex align-items-center gap-2">
                    <i class="ti ti-truck ti-md text-primary"></i>
                    <h5 class="mb-0">آدرس و ارسال</h5>
                </div>
                <div class="card-body">
                    <p class="mb-1 small"><span class="text-muted">روش ارسال:</span> <?php echo e($order->shipping_method_name ?? '—'); ?></p>
                    <?php if($order->address_snapshot): ?>
                    <?php $addr = $order->address_snapshot; ?>
                    <hr class="my-2">
                    <p class="mb-1 small fw-medium"><?php echo e($addr['name'] ?? ''); ?></p>
                    <p class="mb-1 small text-muted"><?php echo e(($addr['province'] ?? '')); ?> — <?php echo e(($addr['city'] ?? '')); ?></p>
                    <p class="mb-1 small text-muted"><?php echo e($addr['address'] ?? ''); ?></p>
                    <p class="mb-0 small text-muted">کد پستی: <?php echo e($addr['postal_code'] ?? ''); ?></p>
                    <?php endif; ?>
                </div>
            </div>

            
            <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">
                    <p class="mb-1 small fw-medium"><?php echo e($order->user->first_name); ?> <?php echo e($order->user->last_name); ?></p>
                    <p class="mb-3 small text-muted" dir="ltr"><?php echo e($order->user->mobile); ?></p>
                    <hr class="my-2">
                    <p class="mb-1 small text-muted">رفرر (منبع ورود):</p>
                    <?php if($order->referrer): ?>
                        <?php if(str_starts_with($order->referrer, 'http')): ?>
                            <a href="<?php echo e($order->referrer); ?>" target="_blank" rel="noopener noreferrer"
                               class="small d-inline-block text-break" dir="ltr"><?php echo e($order->referrer); ?></a>
                        <?php else: ?>
                            <p class="mb-0 small font-monospace text-break" dir="ltr"><?php echo e($order->referrer); ?></p>
                        <?php endif; ?>
                        <?php if($order->referrerLabel()): ?>
                            <div class="text-muted small mt-1"><?php echo e($order->referrerLabel()); ?></div>
                        <?php endif; ?>
                    <?php else: ?>
                        <p class="mb-0 small text-muted">ثبت نشده</p>
                    <?php endif; ?>
                </div>
            </div>

            
            <div class="card mb-4">
                <div class="card-header d-flex align-items-center gap-2">
                    <i class="ti ti-truck-delivery ti-md text-success"></i>
                    <h5 class="mb-0">کد رهگیری پستی</h5>
                </div>
                <div class="card-body">
                    <?php if($order->tracking_code): ?>
                    <div class="alert alert-success py-2 mb-3 d-flex align-items-center gap-2">
                        <i class="ti ti-check-circle"></i>
                        <span class="font-monospace fw-bold" dir="ltr"><?php echo e($order->tracking_code); ?></span>
                    </div>
                    <?php endif; ?>
                    <form method="POST" action="<?php echo e(route('admin.orders.saveTracking', $order)); ?>">
                        <?php echo csrf_field(); ?>
                        <div class="mb-3">
                            <label class="form-label small"><?php echo e($order->tracking_code ? 'ویرایش کد رهگیری' : 'وارد کردن کد رهگیری'); ?></label>
                            <input type="text" name="tracking_code" class="form-control <?php $__errorArgs = ['tracking_code'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                   value="<?php echo e(old('tracking_code', $order->tracking_code)); ?>"
                                   placeholder="کد رهگیری پستی" dir="ltr">
                            <?php $__errorArgs = ['tracking_code'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?><div class="invalid-feedback"><?php echo e($message); ?></div><?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>
                        <button type="submit" class="btn btn-success w-100">
                            <i class="ti ti-device-floppy me-1"></i>ذخیره و تغییر وضعیت به ارسال شده
                        </button>
                    </form>
                </div>
            </div>

            
            <?php if($order->payment_method === 'torobpay' && in_array($order->status, ['paid', 'sent'], true)): ?>
            <div class="card border-danger">
                <div class="card-header d-flex align-items-center gap-2">
                    <i class="ti ti-arrow-back-up ti-md text-danger"></i>
                    <h5 class="mb-0">عودت سفارش ترب‌پی</h5>
                </div>
                <div class="card-body">
                    <p class="small text-muted mb-3">
                        با عودت، درخواست لغو به API ترب‌پی ارسال می‌شود و وضعیت سفارش به «عودت شده» تغییر می‌کند.
                        مبلغ توسط ترب‌پی (حداکثر ۴۸ ساعت) به مشتری بازمی‌گردد.
                    </p>
                    <form method="POST" action="<?php echo e(route('admin.settings.torobpay.orders.refund')); ?>"
                          onsubmit="return confirm('آیا از عودت این سفارش در ترب‌پی مطمئن هستید؟');">
                        <?php echo csrf_field(); ?>
                        <input type="hidden" name="order_id" value="<?php echo e($order->id); ?>">
                        <button type="submit" class="btn btn-danger w-100">
                            <i class="ti ti-arrow-back-up me-1"></i>عودت سفارش ترب‌پی
                        </button>
                    </form>
                    <a href="<?php echo e(route('admin.settings.torobpay.orders')); ?>" class="btn btn-sm btn-outline-secondary w-100 mt-2">
                        لیست سفارش‌های ترب‌پی
                    </a>
                </div>
            </div>
            <?php endif; ?>

            
            <div class="card">
                <div class="card-header d-flex align-items-center gap-2">
                    <i class="ti ti-settings ti-md text-primary"></i>
                    <h5 class="mb-0">تغییر وضعیت</h5>
                </div>
                <div class="card-body">
                    <?php if(session('error')): ?>
                    <div class="alert alert-danger py-2 small"><?php echo e(session('error')); ?></div>
                    <?php endif; ?>
                    <form method="POST" action="<?php echo e(route('admin.orders.updateStatus', $order)); ?>">
                        <?php echo csrf_field(); ?>
                        <div class="mb-3">
                            <select name="status" class="form-select">
                                <?php $__currentLoopData = ['failed'=>'در انتظار پرداخت','paid'=>'در انتظار ارسال','sent'=>'ارسال شده','canceled'=>'لغو شده','returned'=>'عودت شده']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $val => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($val); ?>" <?php echo e($order->status === $val ? 'selected' : ''); ?>><?php echo e($label); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="ti ti-device-floppy me-1"></i>ذخیره
                        </button>
                    </form>
                </div>
            </div>

        </div>

    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('styles'); ?>
<style>
.order-item-thumb { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; flex-shrink: 0; }
</style>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('admin.layouts.layout', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/louishop/public_html/engine/resources/views/admin/orders/show.blade.php ENDPATH**/ ?>