<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <title>فاکتور پرده</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Nastaliq+Urdu:wght@400;700&family=Vazirmatn:wght@400;500;700&display=swap" rel="stylesheet">
    <style>
        @page {
            size: A4;
            margin: 7mm;
        }

        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            direction: rtl;
            background: #fff;
            color: #1e1d20;
            font-family: 'Vazirmatn', Tahoma, Arial, sans-serif;
            -webkit-print-color-adjust: exact;
            print-color-adjust: exact;
        }

        .invoice {
            width: 196mm;
            min-height: 283mm;
            margin: 0 auto;
            background: #fff;
            overflow: hidden;
            position: relative;
            padding-bottom: 5mm;
        }

        .curtain-header {
            height: 63mm;
            position: relative;
            overflow: hidden;
            border-radius: 0 0 2mm 2mm;
            background: #c9a96e;
        }

        .curtain-header-img {
            position: absolute;
            inset: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
            object-position: center top;
            opacity: 0.92;
        }

        .curtain-header:before,
        .curtain-header:after {
            content: "";
            position: absolute;
            top: -7mm;
            width: 56mm;
            height: 74mm;
            background:
                radial-gradient(ellipse at center, rgba(255,255,255,.22) 0 18%, transparent 19%),
                repeating-radial-gradient(ellipse at top, rgba(95, 58, 16, .45) 0 1.5px, rgba(215, 178, 101, .48) 2px 5px);
            opacity: .86;
            transform: rotate(9deg);
            border-radius: 0 0 32mm 32mm;
        }

        .curtain-header:before {
            right: -7mm;
        }

        .curtain-header:after {
            left: -7mm;
            transform: rotate(-9deg);
        }

        .header-center {
            position: absolute;
            inset: 7mm 24mm auto;
            text-align: center;
            z-index: 1;
        }

        .shop-title {
            margin: 0;
            font-family: 'Noto Nastaliq Urdu', 'Times New Roman', serif;
            font-size: 36px;
            font-weight: 700;
            line-height: 1.5;
            color: #34302d;
            text-shadow: 0 1px 0 rgba(255,255,255,.7);
        }

        .ornament {
            width: 76mm;
            margin: 2mm auto 3mm;
            height: 5mm;
            position: relative;
        }

        .ornament:before,
        .ornament:after {
            content: "";
            position: absolute;
            top: 50%;
            width: 31mm;
            border-top: 1px solid #2d2a2a;
        }

        .ornament:before {
            right: 0;
        }

        .ornament:after {
            left: 0;
        }

        .ornament span {
            position: absolute;
            left: 50%;
            top: 50%;
            width: 18mm;
            height: 5mm;
            transform: translate(-50%, -50%);
            border-top: 1.5px solid #2d2a2a;
            border-bottom: 1.5px solid #2d2a2a;
            border-radius: 50%;
        }

        .contacts {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 8mm;
            font-size: 15px;
            font-weight: 700;
            color: #242323;
        }

        .phone-icon,
        .social {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 4.5mm;
            height: 4.5mm;
            margin-left: 1mm;
            border-radius: 50%;
            color: #fff;
            background: #232323;
            font-size: 10px;
            line-height: 1;
            vertical-align: middle;
        }

        .socials {
            position: absolute;
            left: 25mm;
            bottom: 16mm;
            display: flex;
            gap: 1.2mm;
            z-index: 1;
        }

        .social.instagram { background: #c13584; }
        .social.facebook { background: #1877f2; }
        .social.tiktok { background: #111; }

        .meta {
            height: 12mm;
            display: grid;
            grid-template-columns: 1fr 35mm 45mm;
            align-items: center;
            gap: 4mm;
            padding: 0 8mm;
            font-size: 14px;
            font-weight: 700;
        }

        .meta-field {
            display: flex;
            align-items: flex-end;
            gap: 1.5mm;
            min-width: 0;
        }

        .meta-field span {
            flex: 1;
            min-height: 6mm;
            border-bottom: 1.5px dotted #58515a;
            color: #23356e;
            padding: 0 2mm;
            font-weight: 500;
        }

        .bill-grid {
            width: calc(100% - 16mm);
            margin: 0 8mm;
            display: grid;
            grid-template-columns: 43mm 27mm 1fr 55mm 10mm;
            grid-auto-rows: 12.7mm;
            border-right: 1px solid #777;
            border-bottom: 1px solid #777;
            direction: ltr;
        }

        .cell {
            border-left: 1px solid #777;
            border-top: 1px solid #777;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 1.5mm 2.2mm;
            font-size: 13.5px;
            line-height: 1.45;
            direction: rtl;
            text-align: center;
            background: rgba(255,255,255,.92);
        }

        .head {
            height: 11mm;
            color: #fff;
            font-weight: 700;
            background: #27272f;
            border-color: #27272f;
        }

        .head.gold {
            background: linear-gradient(#b68038, #9e6d2b);
            border-color: #9e6d2b;
        }

        .head.first {
            border-radius: 7mm 0 0 0;
        }

        .head.last {
            border-radius: 0 7mm 0 0;
        }

        .payment-label {
            font-weight: 700;
            color: #202020;
            background: #f8f8f8;
        }

        .payment-value {
            color: #283672;
            font-size: 16px;
            font-weight: 500;
        }

        .total-due-label {
            font-weight: 700;
            background: #f7f7f7;
        }

        .detail-label {
            justify-content: center;
            padding: 0 2mm;
            font-weight: 700;
            font-size: 13px;
            text-align: center;
            direction: rtl;
        }

        .value {
            color: #273574;
            font-size: 15px;
            font-weight: 500;
        }

        .marker {
            position: relative;
            overflow: visible;
            border-left: 0;
            border-top-color: transparent;
            background: transparent;
        }

        .marker span {
            width: 8mm;
            height: 8mm;
            display: flex;
            align-items: center;
            justify-content: center;
            transform: rotate(45deg);
            color: #fff;
            font-size: 12px;
            font-weight: 700;
            background: #b48039;
            border: 1px solid rgba(255,255,255,.8);
        }

        .marker span b {
            transform: rotate(-45deg);
        }

        .marker.dark span {
            background: #303039;
        }

        .bottom {
            width: calc(100% - 16mm);
            margin: 2mm 8mm 0;
            display: grid;
            grid-template-columns: 28mm 1fr;
            align-items: end;
            gap: 4mm;
        }

        .window-details {
            width: calc(100% - 16mm);
            margin: 2mm 8mm 0;
            border: 1px solid #777;
            border-bottom: 0;
            font-size: 11px;
        }

        .window-details .title {
            padding: 1.5mm 2mm;
            text-align: center;
            font-weight: 700;
            background: #f2eadb;
            border-bottom: 1px solid #777;
        }

        .window-details-row {
            display: grid;
            grid-template-columns: 1.1fr 1.5fr 1.5fr .6fr .6fr .7fr .7fr;
        }

        .window-details-row > div {
            min-height: 7mm;
            padding: 1mm 1.5mm;
            display: flex;
            align-items: center;
            justify-content: center;
            border-left: 1px solid #777;
            border-bottom: 1px solid #777;
            text-align: center;
        }

        .window-details-row > div:first-child {
            border-left: 0;
        }

        .window-details-row.head-row {
            font-weight: 700;
            background: #fafafa;
        }

        .bedding-details .window-details-row {
            grid-template-columns: .7fr .8fr 1.4fr .45fr .55fr .55fr .65fr .9fr 1.5fr;
        }

        .door-details .window-details-row {
            grid-template-columns: 1.2fr 2fr .7fr .8fr;
        }

        .qr {
            width: 24mm;
            height: 24mm;
            border: 1.5px solid #b9893d;
            padding: 1mm;
            background: #fff;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .qr img {
            width: 100%;
            height: 100%;
            object-fit: contain;
        }

        .note {
            height: 8.5mm;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 0 7mm;
            color: #1f1f1f;
            font-size: 13.5px;
            font-weight: 700;
            background: linear-gradient(90deg, #b37a28, #efc45d 12%, #f8d36f 88%, #b37a28);
            border-radius: 9mm;
        }

        .address {
            height: 9mm;
            margin-top: 2.5mm;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #fff;
            font-size: 13px;
            font-weight: 700;
            background: #2a2a31;
            border-radius: 9mm;
        }

        @media print {
            .invoice {
                width: auto;
                min-height: auto;
            }
        }
    </style>
</head>
<body onload="window.print()">
@php
    $curtain = $product->curtain ?: new \App\Curtain;
    $customerName = optional($product->customer)->name ?: '';
    $issueDate = $product->created_at ? $product->created_at->format('Y/m/d') : date('Y/m/d');
    $deliveryDate = $product->completion_date ? $product->completion_date->format('Y/m/d') : '';
    $itemName = optional($product->product)->name ?: 'پرده';
    $panelText = trim(($curtain->panel_count ?? '') . ' عدد' . (($curtain->panel_meter ?? null) !== null ? ' (' . $curtain->panel_meter . ' متر)' : ''));
    $middleNetting = ($curtain->middle_netting_meter ?? '') !== '' ? $curtain->middle_netting_meter . ' متر' : '';
    $mattressText = trim(($curtain->mattress_count ?? 0) . ' توشک، ' . ($curtain->pillow_count ?? 0) . ' بالشت، ' . ($curtain->bolster_count ?? 0) . ' بالشت دراز');
    $windowDetails = $curtain->windowDetails ?? collect();
    $doorDetails = $curtain->doorDetails ?? collect();
    $beddingDetails = $curtain->beddingDetails ?? collect();
    $sewingType = collect([$curtain->pleating_type, $curtain->ring_stitch_type])->filter()->implode('، ');
    $rows = [
        ['تعداد کلکین', $curtain->window_count],
        ['تعداد دروازه', $curtain->door_count],
        ['نوع تکه', $curtain->fabric_type],
        ['نوع جالی', $curtain->netting_type],
        ['تخته پرده', $panelText],
        ['جالی وسط پرده', $middleNetting],
        ['نوع دوخت', $sewingType],
        ['مودل پرده', $curtain->curtain_model],
        ['تعداد توشک و بالشت', $mattressText],
        ['اندازه توشک و بالشت', $curtain->mattress_pillow_size],
        ['مودل بالشت و توشک', $curtain->pillow_mattress_model],
    ];
@endphp
    <main class="invoice">
        <section class="curtain-header">
            <img class="curtain-header-img" src="{{ asset('images/curtain_header.jpg') }}" alt="پرده نورا">
            <div class="header-center">
                <h1 class="shop-title">بزرگترین نمایشگاه پرده نورا</h1>
                <div class="ornament"><span></span></div>
                <div class="contacts">
                    <span><i class="phone-icon">☎</i>شماره دفتر 0795045555</span>
                    <span><i class="phone-icon">☎</i>سر عزیز 0773631102</span>
                </div>
            </div>
            <div class="socials">
                <i class="social instagram">◎</i>
                <i class="social facebook">f</i>
                <i class="social tiktok">♪</i>
            </div>
        </section>

        <section class="meta">
            <div class="meta-field"><strong>اسم مشتری:</strong><span>{{ $customerName }}</span></div>
            <div class="meta-field"><strong>نمبر بل:</strong><span>{{ $product->code }}</span></div>
            <div class="meta-field"><strong>تاریخ:</strong><span>{{ $issueDate }}</span></div>
        </section>

        <section class="bill-grid">
            <div class="cell head first">قیمت مجموع</div>
            <div class="cell head gold">قیمت</div>
            <div class="cell head">جمله</div>
            <div class="cell head gold last">اسم جنس</div>
            <div class="cell marker"></div>

            @foreach($rows as $index => $row)
                <div class="cell payment-value">
                    @if($index === 0)
                        {{ number_format($product->total_price, 2, '.', '') }}
                    @elseif($index === 1)
                        {{ number_format($product->paid_amount, 2, '.', '') }}
                    @elseif($index === 2)
                        {{ number_format($product->due_amount, 2, '.', '') }}
                    @elseif($index === 9)
                        {{ number_format($product->due_amount, 2, '.', '') }}
                    @elseif($index === 10)
                        {{ $deliveryDate }}
                    @endif
                </div>
                <div class="cell {{ $index < 3 ? 'payment-label' : ($index === 9 ? 'total-due-label' : '') }}">
                    @if($index === 0)
                        جمله
                    @elseif($index === 1)
                        رسید
                    @elseif($index === 2)
                        باقی
                    @elseif($index === 9)
                        جمله باقی
                    @elseif($index === 10)
                        تاریخ تحویل
                    @endif
                </div>
                <div class="cell value">{{ $row[1] }}</div>
                <div class="cell detail-label">{{ $row[0] }}:</div>
                <div class="cell marker {{ ($index + 1) % 2 === 0 ? 'dark' : '' }}"><span><b>{{ $index + 1 }}</b></span></div>
            @endforeach
        </section>

        @if($windowDetails->count())
            <section class="window-details">
                <div class="title">جزئیات اندازه کلکین</div>
                <div class="window-details-row head-row">
                    <div>نام / محل</div>
                    <div>تکه</div>
                    <div>جالی</div>
                    <div>تعداد کلکین</div>
                    <div>پنل های پرده</div>
                    <div>متر هر پنل</div>
                    <div>جالی وسط</div>
                </div>
                @foreach($windowDetails as $detail)
                    <div class="window-details-row">
                        <div>{{ $detail->label ?: 'گروپ ' . $loop->iteration }}</div>
                        <div>{{ optional($detail->fabricProduct)->name ?: '—' }} — {{ optional($detail->fabricProduct)->code ?: '—' }}</div>
                        <div>{{ optional($detail->nettingProduct)->name ?: '—' }} — {{ optional($detail->nettingProduct)->code ?: '—' }}</div>
                        <div>{{ $detail->window_count }}</div>
                        <div>{{ $detail->panel_count }}</div>
                        <div>{{ $detail->panel_meter }}</div>
                        <div>{{ $detail->middle_netting_meter }}</div>
                    </div>
                @endforeach
            </section>
        @endif

        @if($doorDetails->count())
            <section class="window-details door-details">
                <div class="title">مواد مورد نیاز دروازه</div>
                <div class="window-details-row head-row">
                    <div>نام دروازه</div>
                    <div>محصول</div>
                    <div>تعداد دروازه</div>
                    <div>متراژ</div>
                </div>
                @foreach($doorDetails as $detail)
                    <div class="window-details-row">
                        <div>{{ $detail->label ?: 'گروپ ' . $loop->iteration }}</div>
                        <div>{{ optional($detail->product)->name ?: '—' }} — {{ optional($detail->product)->code ?: '—' }}</div>
                        <div>{{ $detail->door_count }}</div>
                        <div>{{ $detail->meterage }}</div>
                    </div>
                @endforeach
            </section>
        @endif

        @if($beddingDetails->count())
            <section class="window-details bedding-details">
                <div class="title">جزئیات دوشک و بالشت</div>
                <div class="window-details-row head-row">
                    <div>نوع</div>
                    <div>نام</div>
                    <div>محصول</div>
                    <div>تعداد</div>
                    <div>طول</div>
                    <div>عرض</div>
                    <div>متراژ</div>
                    <div>مودل</div>
                    <div>{{ trans('file.Under Cover') }}</div>
                </div>
                @foreach($beddingDetails as $detail)
                    @php
                        $beddingType = $detail->type == 'mattress' ? 'دوشک' : ($detail->type == 'bolster' ? 'بالشت دراز' : 'بالشت');
                    @endphp
                    <div class="window-details-row">
                        <div>{{ $beddingType }}</div>
                        <div>{{ $detail->label ?: 'گروپ ' . $loop->iteration }}</div>
                        <div>{{ optional($detail->product)->name ?: '—' }} — {{ optional($detail->product)->code ?: '—' }}</div>
                        <div>{{ $detail->count }}</div>
                        <div>{{ $detail->height }}</div>
                        <div>{{ $detail->width }}</div>
                        <div>{{ $detail->meterage ?? '' }}</div>
                        <div>{{ $detail->model }}</div>
                        <div>@if($detail->has_under_cover){{ optional($detail->underCoverProduct)->name ?: '—' }}، {{ $detail->under_cover_count }} عدد، {{ $detail->under_cover_meterage }} متر @else—@endif</div>
                    </div>
                @endforeach
            </section>
        @endif

        <section class="bottom">
            <div class="qr">
                <img src="https://api.qrserver.com/v1/create-qr-code/?size=200x200&data={{ urlencode(route('tailoringproducts.print', $product->id)) }}" alt="QR Code">
            </div>
            <div>
                <div class="note">نوت: قبل از تسلیمی و نصب پرده حساب را تصفیه نمایید</div>
                <div class="address">بازار لیسه مریم، جوار رستورانت کفتریا، مرکز، منزل دوم</div>
            </div>
        </section>
    </main>
</body>
</html>
