<?php $__env->startSection('title', 'جزئیات حواله'); ?>

<?php $__env->startSection('content'); ?>
<?php echo $__env->make('admin.partials.iran-plate', ['loadStylesOnly' => true], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php
    $jalali = static fn ($value, $withTime = true) => $value ? \Morilog\Jalali\Jalalian::fromDateTime($value)->format($withTime ? 'Y/m/d H:i' : 'Y/m/d') : '-';
    $enum = [
        'status' => ['issued' => 'صادر شده', 'in_transit' => 'در حال حمل', 'delivered' => 'تحویل شده', 'cancelled' => 'لغو شده'],
        'state' => ['issued' => 'صادر شده', 'registered' => 'ثبت‌شده', 'cancelled' => 'لغو شده'],
        'transport_type' => ['domestic' => 'داخلی', 'export' => 'صادرات', 'import' => 'واردات', 'transit' => 'ترانزیت'],
        'cargo_kind' => ['general' => 'عمومی', 'dangerous' => 'خطرناک', 'perishable' => 'فاسدشدنی', 'oil' => 'نفتی'],
        'service_type' => ['retail' => 'خرده بار', 'full_truck' => 'دربستی', 'combined' => 'ترکیبی'],
        'oil_type' => ['oil' => 'نفتی', 'non_oil' => 'غیر نفتی'],
    ];
    $label = static fn ($group, $value) => $value ? ($enum[$group][$value] ?? $value) : '-';
    $sbk = static fn ($value) => $value ? ('SBK-' . ltrim((string) $value, '-')) : '-';
    $driverInitial = static fn ($name) => $name ? mb_substr(trim($name), 0, 1) : '؟';
?>

<style>
    .dispatch-hero {
        background: linear-gradient(180deg, #1e3a8a 0%, #1e40af 100%);
        color: #fff;
        border-radius: 16px;
        overflow: hidden;
        box-shadow: 0 14px 30px rgba(30, 64, 175, 0.22);
    }
    .dispatch-meta-card {
        border: 1px solid #dbe4f0;
        border-radius: 14px;
        overflow: hidden;
    }
    .dispatch-meta-card .card-header {
        background: #f8fbff;
        font-weight: 700;
    }
    .dispatch-table th {
        width: 220px;
        background: #f8fbff;
        color: #334155;
        font-weight: 700;
    }
    .dispatch-driver-avatar {
        width: 88px;
        height: 88px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 2rem;
        font-weight: 700;
        color: #fff;
        background: linear-gradient(135deg, #2563eb, #1d4ed8);
        box-shadow: inset 0 0 0 4px rgba(255,255,255,.25);
    }
    .dispatch-driver-photo {
        width: 88px;
        height: 88px;
        border-radius: 50%;
        object-fit: cover;
        border: 3px solid #dbe4f0;
        box-shadow: 0 6px 16px rgba(15, 23, 42, 0.15);
    }
    .dispatch-soft-label {
        color: rgba(255,255,255,.78);
        font-size: .9rem;
    }
</style>

<div class="dispatch-hero mb-4">
    <div class="p-4 p-lg-5">
        <div class="d-flex justify-content-between align-items-start flex-wrap gap-3">
            <div>
                <div class="dispatch-soft-label mb-2">سبک بار</div>
                <h2 class="h3 mb-2"><?php echo e(($bill->rmto_registered_at || $bill->rmto_bill_number) ? 'جزئیات بارنامه RMTO' : 'جزئیات حواله داخلی'); ?></h2>
                <div class="d-flex gap-2 flex-wrap">
                    <?php if($bill->rmto_registered_at || $bill->rmto_bill_number): ?>
                        <span class="badge bg-success">بارنامه ثبت‌شده در RMTO</span>
                    <?php else: ?>
                        <span class="badge bg-light text-primary">حواله داخلی</span>
                    <?php endif; ?>
                    <span class="badge bg-dark-subtle text-dark"><?php echo e($label('status', $bill->status)); ?></span>
                    <span class="badge bg-warning text-dark">صادر شده از راهداری</span>
                </div>
            </div>
            <div class="text-lg-end">
                <div class="small mb-1">شماره حواله: <strong><?php echo e($sbk($bill->dispatch_order_number ?? $bill->bill_number ?? $bill->id)); ?></strong></div>
                <div class="small mb-1">کد رهگیری: <strong><?php echo e($sbk($bill->tracking_code)); ?></strong></div>
                <div class="small mb-1">سریال داخلی: <strong><?php echo e($sbk($bill->internal_serial)); ?></strong></div>
                <?php if($bill->rmto_bill_number): ?>
                    <div class="small">شماره RMTO: <strong><?php echo e($sbk($bill->rmto_bill_number)); ?></strong></div>
                <?php endif; ?>
            </div>
        </div>
        <div class="mt-4 d-flex gap-2 flex-wrap">
            <?php if($bill->tracking_code): ?>
                <a href="<?php echo e(route('waybill.print', $bill->tracking_code)); ?>" target="_blank" class="btn btn-light btn-sm">نسخه چاپی</a>
                <a href="<?php echo e(url('/api/waybills/public/' . $bill->tracking_code)); ?>" target="_blank" class="btn btn-outline-light btn-sm">API عمومی</a>
            <?php endif; ?>
            <a href="<?php echo e(route('admin.bills.index')); ?>" class="btn btn-outline-light btn-sm">بازگشت</a>
        </div>
    </div>
</div>

<div class="row g-4 mb-4">
    <div class="col-lg-4">
        <div class="dispatch-meta-card h-100">
            <div class="card-header px-3 py-2">کد رهگیری و بارکد</div>
            <div class="card-body text-center">
                <?php if($bill->tracking_code): ?>
                    <div class="mb-3">
                        <?php
                            $qrService = app(\App\Services\QRCodeService::class);
                            $qrCode = $qrService->generateDataUrl($bill->tracking_code);
                        ?>
                        <img src="<?php echo e($qrCode); ?>" alt="QR Code" style="max-width: 200px; width: 100%;" class="mb-2">
                    </div>
                    <div class="mb-3">
                        <h5 style="font-size: 2rem; font-weight: bold; font-family: monospace; word-break: break-all;">
                            <?php echo e($bill->tracking_code); ?>

                        </h5>
                    </div>
                    <div class="text-muted small">
                        <p class="mb-1">صادر شده از سازمان راهداری و حمل‌ونقل جاده‌ای</p>
                        <p class="mb-0">سیستم سبک بار (SABOK)</p>
                    </div>
                <?php else: ?>
                    <p class="text-muted">کد رهگیری موجود نیست</p>
                <?php endif; ?>
            </div>
        </div>
    </div>


    <div class="col-lg-4">
        <div class="dispatch-meta-card h-100">
            <div class="card-header px-3 py-2">مشخصات صدور و وضعیت</div>
            <div class="table-responsive">
                <table class="table table-bordered dispatch-table mb-0 align-middle">
                    <tbody>
                        <tr><th>وضعیت حواله</th><td><?php echo e($label('status', $bill->status)); ?></td></tr>
                        <tr><th>وضعیت سند</th><td><?php echo e(($bill->rmto_registered_at || $bill->rmto_bill_number) ? 'بارنامه RMTO' : 'حواله داخلی'); ?></td></tr>
                        <tr><th>تاریخ صدور</th><td><?php echo e($jalali($bill->issue_date)); ?></td></tr>
                        <tr><th>زمان صدور</th><td><?php echo e($bill->issue_time ?: '-'); ?></td></tr>
                        <tr><th>شروع حمل</th><td><?php echo e($jalali($bill->transport_start_date)); ?></td></tr>
                        <tr><th>تاریخ تحویل</th><td><?php echo e($jalali($bill->delivery_date)); ?></td></tr>
                        <tr><th>نوع حمل</th><td><?php echo e($label('transport_type', $bill->transport_type)); ?></td></tr>
                        <tr><th>نوع بار</th><td><?php echo e($label('cargo_kind', $bill->cargo_kind)); ?></td></tr>
                        <tr><th>نوع سرویس</th><td><?php echo e($label('service_type', $bill->service_type)); ?></td></tr>
                        <tr><th>مرحله داخلی</th><td><?php echo e($label('state', $bill->state)); ?></td></tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-lg-4">
        <div class="dispatch-meta-card h-100">
            <div class="card-header px-3 py-2">شرکت حمل و ناوگان</div>
            <div class="table-responsive">
                <table class="table table-bordered dispatch-table mb-0 align-middle">
                    <tbody>
                        <tr><th>شرکت حمل</th><td><?php echo e($bill->company_name ?: '-'); ?></td></tr>
                        <tr><th>شناسه ملی</th><td><?php echo e($bill->company_national_id ?: '-'); ?></td></tr>
                        <tr><th>کد اقتصادی</th><td><?php echo e($bill->company_economic_code ?: '-'); ?></td></tr>
                        <tr><th>تلفن شرکت</th><td><?php echo e($bill->company_phone ?: '-'); ?></td></tr>
                        <tr><th>مدیرعامل</th><td><?php echo e($bill->company_ceo_name ?: '-'); ?></td></tr>
                        <tr><th>نوع وسیله</th><td><?php echo e($bill->vehicle_type ?: '-'); ?></td></tr>
                        <tr><th>پلاک</th><td><?php echo $__env->make('admin.partials.iran-plate', ['plate' => $bill->transport_license_plate], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?></td></tr>
                        <tr><th>کارت هوشمند خودرو</th><td><?php echo e($bill->vehicle_smart_card_no ?: '-'); ?></td></tr>
                        <tr><th>ظرفیت مجاز</th><td><?php echo e($bill->vehicle_allowed_capacity ? number_format($bill->vehicle_allowed_capacity) . ' کیلوگرم' : '-'); ?></td></tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div class="row g-4 mb-4">
    <div class="col-lg-6">
        <div class="dispatch-meta-card h-100">
            <div class="card-header px-3 py-2">رانندگان</div>
            <div class="card-body">
                <div class="row g-4">
                    <div class="col-md-6 text-center">
                        <?php if($bill->first_driver_photo_path): ?>
                            <img src="<?php echo e(asset('storage/' . $bill->first_driver_photo_path)); ?>" alt="عکس راننده اول" class="dispatch-driver-photo mx-auto mb-3">
                        <?php else: ?>
                            <div class="dispatch-driver-avatar mx-auto mb-3"><?php echo e($driverInitial($bill->first_driver_name)); ?></div>
                        <?php endif; ?>
                        <h6 class="mb-2">راننده اول</h6>
                        <div class="small text-muted mb-2"><?php echo e($bill->first_driver_photo_path ? 'تصویر ثبت شده' : 'تصویر ثبت نشده'); ?></div>
                        <table class="table table-sm table-bordered dispatch-table mb-0">
                            <tbody>
                                <tr><th>نام</th><td><?php echo e($bill->first_driver_name ?: '-'); ?></td></tr>
                                <tr><th>کد ملی</th><td><?php echo e($bill->first_driver_national_code ?: '-'); ?></td></tr>
                                <tr><th>موبایل</th><td><?php echo e($bill->first_driver_mobile ?: '-'); ?></td></tr>
                                <tr><th>حساب</th><td><?php echo e($bill->first_driver_account_number ?: '-'); ?></td></tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="col-md-6 text-center">
                        <?php if($bill->second_driver_photo_path): ?>
                            <img src="<?php echo e(asset('storage/' . $bill->second_driver_photo_path)); ?>" alt="عکس راننده دوم" class="dispatch-driver-photo mx-auto mb-3">
                        <?php else: ?>
                            <div class="dispatch-driver-avatar mx-auto mb-3" style="background:linear-gradient(135deg,#0f766e,#0d9488);"><?php echo e($driverInitial($bill->second_driver_name)); ?></div>
                        <?php endif; ?>
                        <h6 class="mb-2">راننده دوم</h6>
                        <div class="small text-muted mb-2"><?php echo e($bill->second_driver_photo_path ? 'تصویر ثبت شده' : 'تصویر ثبت نشده'); ?></div>
                        <table class="table table-sm table-bordered dispatch-table mb-0">
                            <tbody>
                                <tr><th>نام</th><td><?php echo e($bill->second_driver_name ?: '-'); ?></td></tr>
                                <tr><th>کد ملی</th><td><?php echo e($bill->second_driver_national_code ?: '-'); ?></td></tr>
                                <tr><th>موبایل</th><td><?php echo e($bill->second_driver_mobile ?: '-'); ?></td></tr>
                                <tr><th>وضعیت</th><td><?php echo e($bill->second_driver_name ? 'ثبت شده' : 'ثبت نشده'); ?></td></tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-6">
        <div class="dispatch-meta-card h-100">
            <div class="card-header px-3 py-2">فرستنده، گیرنده و مسیر</div>
            <div class="table-responsive">
                <table class="table table-bordered dispatch-table mb-0 align-middle">
                    <tbody>
                        <tr><th>فرستنده</th><td><?php echo e($bill->sender_name ?: '-'); ?></td></tr>
                        <tr><th>کدملی/شناسه فرستنده</th><td><?php echo e($bill->sender_national_id ?: '-'); ?></td></tr>
                        <tr><th>تلفن فرستنده</th><td><?php echo e($bill->sender_phone ?: '-'); ?></td></tr>
                        <tr><th>آدرس فرستنده</th><td><?php echo e($bill->sender_address ?: '-'); ?></td></tr>
                        <tr><th>گیرنده</th><td><?php echo e($bill->receiver_name_full ?: '-'); ?></td></tr>
                        <tr><th>کدملی گیرنده</th><td><?php echo e($bill->receiver_national_id ?: '-'); ?></td></tr>
                        <tr><th>موبایل گیرنده</th><td><?php echo e($bill->receiver_mobile ?: '-'); ?></td></tr>
                        <tr><th>آدرس گیرنده</th><td><?php echo e($bill->receiver_address_full ?: '-'); ?></td></tr>
                        <tr><th>کدپستی مبدا / مقصد</th><td><?php echo e($bill->origin_postal_code ?: '-'); ?> / <?php echo e($bill->destination_postal_code ?: '-'); ?></td></tr>
                        <tr><th>مسافت</th><td><?php echo e($bill->distance_km ? number_format($bill->distance_km) . ' کیلومتر' : '-'); ?></td></tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div class="dispatch-meta-card mb-4">
    <div class="card-header px-3 py-2">ردیف‌های کالا</div>
    <div class="table-responsive">
        <table class="table table-bordered mb-0 align-middle text-center">
            <thead class="table-light">
                <tr>
                    <th>#</th>
                    <th>نام کالا</th>
                    <th>کد کالا</th>
                    <th>بسته‌بندی</th>
                    <th>تعداد</th>
                    <th>وزن</th>
                    <th>ارزش</th>
                    <th>نوع کالا</th>
                </tr>
            </thead>
            <tbody>
                <?php $__empty_1 = true; $__currentLoopData = $bill->cargoItems; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <tr>
                        <td><?php echo e($index + 1); ?></td>
                        <td><?php echo e($item->cargo_name); ?></td>
                        <td><?php echo e($item->cargo_code ?: '-'); ?></td>
                        <td><?php echo e($item->packaging_type ?: '-'); ?></td>
                        <td><?php echo e(number_format($item->quantity ?? 0)); ?></td>
                        <td><?php echo e(number_format($item->weight ?? 0, 2)); ?></td>
                        <td><?php echo e(number_format($item->value ?? 0)); ?></td>
                        <td><?php echo e($label('oil_type', $item->oil_type)); ?></td>
                    </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr><td colspan="8" class="text-center text-muted py-4">ردیف کالایی ثبت نشده است.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="row g-4">
    <div class="col-lg-6">
        <div class="dispatch-meta-card h-100">
            <div class="card-header px-3 py-2">اطلاعات مالی و قرارداد</div>
            <div class="table-responsive">
                <table class="table table-bordered dispatch-table mb-0 align-middle">
                    <tbody>
                        <tr><th>ارزش اظهار شده</th><td><?php echo e(number_format($bill->declared_value ?? 0)); ?> تومان</td></tr>
                        <tr><th>کرایه</th><td><?php echo e(number_format($bill->freight_amount ?? 0)); ?> تومان</td></tr>
                        <tr><th>بیمه</th><td><?php echo e(number_format($bill->insurance_amount ?? 0)); ?> تومان</td></tr>
                        <tr><th>مالیات</th><td><?php echo e(number_format($bill->tax_amount ?? 0)); ?> تومان</td></tr>
                        <tr><th>عوارض</th><td><?php echo e(number_format($bill->toll_amount ?? 0)); ?> تومان</td></tr>
                        <tr><th>جمع کل</th><td><?php echo e(number_format($bill->total_amount ?? 0)); ?> تومان</td></tr>
                        <tr><th>نحوه پرداخت</th><td><?php echo e($bill->payment_method ?: '-'); ?></td></tr>
                        <tr><th>شرایط قرارداد</th><td><?php echo e($bill->contract_terms ?: '-'); ?></td></tr>
                        <tr><th>توضیحات حمل</th><td><?php echo e($bill->transport_notes ?: '-'); ?></td></tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-lg-6">
        <div class="dispatch-meta-card h-100">
            <div class="card-header px-3 py-2">تاییدات و اطلاعات تکمیلی</div>
            <div class="table-responsive">
                <table class="table table-bordered dispatch-table mb-0 align-middle">
                    <tbody>
                        <tr><th>امضای دیجیتال</th><td><?php echo e($bill->digital_signature ?: '-'); ?></td></tr>
                        <tr><th>امضای راننده</th><td><?php echo e($bill->driver_signature ?: '-'); ?></td></tr>
                        <tr><th>تایید سازمان</th><td><?php echo e($bill->organization_approval ? 'تایید شده' : 'تایید نشده'); ?></td></tr>
                        <tr><th>کد تایید سازمان</th><td><?php echo e($bill->organization_confirmation_code ?: '-'); ?></td></tr>
                        <tr><th>شماره کانتینر</th><td><?php echo e($bill->container_no ?: '-'); ?></td></tr>
                        <tr><th>کد باسکول</th><td><?php echo e($bill->weighbridge_code ?: '-'); ?></td></tr>
                        <tr><th>شماره قبض باسکول</th><td><?php echo e($bill->weighbridge_receipt_no ?: '-'); ?></td></tr>
                        <tr><th>نوع سفارش</th><td><?php echo e($bill->order_type ?: '-'); ?></td></tr>
                        <tr><th>ثبت RMTO</th><td><?php echo e($bill->rmto_registered_at ? $jalali($bill->rmto_registered_at) : 'هنوز ثبت نشده'); ?></td></tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('admin.layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH S:\wamp\www\sabok\temp-laravel\resources\views/admin/bills/show.blade.php ENDPATH**/ ?>