{% load price_filters %}
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>فاکتور فروشگاه - {{ shop_order_id }}</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Vazir', 'Tahoma', Arial, sans-serif;
            direction: rtl;
            padding: 5px;
            background-color: #fff;
            font-size: 9px;
        }
        
        .invoice-container {
            max-width: 850px;
            margin: 0 auto;
            background: white;
            padding: 10px;
        }
        
        .header {
            text-align: center;
            margin-bottom: 8px;
            border-bottom: 1.5px solid #333;
            padding-bottom: 5px;
        }
        
        .header h1 {
            color: #333;
            font-size: 16px;
            margin-bottom: 3px;
        }
        
        .header h2 {
            color: #666;
            font-size: 10px;
        }
        
        .shop-info {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 6px 8px;
            border-radius: 3px;
            margin-bottom: 8px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .shop-info h3 {
            font-size: 11px;
            margin: 0;
        }
        
        .shop-info p {
            font-size: 9px;
            margin: 0;
        }
        
        .order-info {
            display: flex;
            justify-content: space-between;
            margin-bottom: 8px;
            background-color: #f9f9f9;
            padding: 6px 8px;
            border-radius: 3px;
        }
        
        .info-section {
            flex: 1;
            margin: 0 3px;
        }
        
        .info-section h3 {
            color: #333;
            margin-bottom: 3px;
            font-size: 10px;
            border-bottom: 1px solid #ddd;
            padding-bottom: 2px;
        }
        
        .info-row {
            margin: 2px 0;
            font-size: 8px;
        }
        
        .info-label {
            font-weight: bold;
            color: #555;
        }
        
        .info-value {
            color: #333;
        }
        
        .items-table {
            width: 100%;
            border-collapse: collapse;
            margin: 8px 0;
        }
        
        .items-table th {
            background-color: #333;
            color: white;
            padding: 3px 2px;
            text-align: center;
            font-size: 8px;
        }
        
        .items-table td {
            padding: 3px 2px;
            text-align: center;
            border-bottom: 1px solid #ddd;
            font-size: 8px;
        }
        
        .items-table tr:hover {
            background-color: #f5f5f5;
        }
        
        .financial-summary {
            margin-top: 8px;
            background-color: #f9f9f9;
            padding: 6px 8px;
            border-radius: 3px;
        }
        
        .summary-section {
            margin-bottom: 6px;
        }
        
        .summary-section h3 {
            color: #333;
            margin-bottom: 4px;
            font-size: 10px;
            border-bottom: 1px solid #667eea;
            padding-bottom: 2px;
        }
        
        .summary-row {
            display: flex;
            justify-content: space-between;
            padding: 3px 4px;
            border-bottom: 1px solid #ddd;
            font-size: 8px;
        }
        
        .summary-row.highlight {
            background-color: #e3f2fd;
            font-weight: bold;
        }
        
        .summary-row.total {
            background-color: #333;
            color: white;
            font-weight: bold;
            font-size: 10px;
            margin-top: 3px;
        }
        
        .summary-row.payable {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-weight: bold;
            font-size: 11px;
            margin-top: 3px;
        }
        
        .summary-label {
            font-weight: bold;
        }
        
        .summary-value {
            text-align: left;
        }
        
        .status-badge {
            display: inline-block;
            padding: 1px 5px;
            border-radius: 8px;
            font-size: 7px;
            font-weight: bold;
        }
        
        .status-paid {
            background-color: #4CAF50;
            color: white;
        }
        
        .status-unpaid {
            background-color: #f44336;
            color: white;
        }
        
        .status-pending {
            background-color: #ff9800;
            color: white;
        }
        
        .status-settled {
            background-color: #2196F3;
            color: white;
        }
        
        .footer {
            margin-top: 10px;
            text-align: center;
            padding-top: 5px;
            border-top: 1px solid #ddd;
            color: #777;
            font-size: 7px;
        }
        
        .clearfix::after {
            content: "";
            display: table;
            clear: both;
        }
        
        .discount-highlight {
            color: #d32f2f;
            font-weight: bold;
        }
        
        .commission-highlight {
            color: #1976d2;
            font-weight: bold;
        }
        
        .address-box {
            background-color: #fff3cd;
            border: 1px solid #ffc107;
            padding: 5px 8px;
            border-radius: 3px;
            margin: 6px 0;
        }
        
        .address-box h3 {
            color: #856404;
            margin-bottom: 3px;
            font-size: 9px;
        }
    </style>
</head>
<body>
    <div class="invoice-container">
        <div class="header">
            <h1>فاکتور فروشگاه</h1>
            <h2>شماره سفارش اصلی: {{ order_number }} | شماره فاکتور فروشگاه: {{ shop_order_id }}</h2>
        </div>
        
        <div class="shop-info">
            <div>
                <h3>{{ shop.shop_name }}</h3>
                {% if shop.shop_description %}
                <p>{{ shop.shop_description|truncatewords:10 }}</p>
                {% endif %}
            </div>
            <p style="font-size: 10px; font-weight: bold;">کمیسیون: {{ commission_percent }}%</p>
        </div>
        
        <div class="order-info">
            <div class="info-section">
                <h3>اطلاعات مشتری</h3>
                <div class="info-row">
                    <span class="info-label">نام:</span>
                    <span class="info-value">{{ customer_name|default:"---" }}</span>
                </div>
                <div class="info-row">
                    <span class="info-label">تلفن:</span>
                    <span class="info-value">{{ customer_phone|default:"---" }}</span>
                </div>
            </div>
            
            <div class="info-section">
                <h3>اطلاعات سفارش</h3>
                <div class="info-row">
                    <span class="info-label">تاریخ:</span>
                    <span class="info-value">{{ created_at_jalali|default:"---" }}</span>
                </div>
                <div class="info-row">
                    <span class="info-label">وضعیت پرداخت:</span>
                    <span class="info-value">
                        {% if is_paid %}
                        <span class="status-badge status-paid">پرداخت شده</span>
                        {% else %}
                        <span class="status-badge status-unpaid">پرداخت نشده</span>
                        {% endif %}
                    </span>
                </div>
                <div class="info-row">
                    <span class="info-label">وضعیت سفارش:</span>
                    <span class="info-value">
                        <span class="status-badge status-pending">{{ status }}</span>
                    </span>
                </div>
                <div class="info-row">
                    <span class="info-label">وضعیت تسویه:</span>
                    <span class="info-value">
                        {% if is_settled %}
                        <span class="status-badge status-settled">تسویه شده</span>
                        {% else %}
                        <span class="status-badge status-unpaid">تسویه نشده</span>
                        {% endif %}
                    </span>
                </div>
            </div>
        </div>
        
        {% if address_text %}
        <div class="address-box">
            <h3>آدرس تحویل</h3>
            <div style="display: flex; flex-wrap: wrap; gap: 8px;">
                <div class="info-row" style="flex: 1 1 45%;">
                    <span class="info-label">گیرنده:</span>
                    <span class="info-value">{{ address_recipient_name }}</span>
                </div>
                <div class="info-row" style="flex: 1 1 45%;">
                    <span class="info-label">شهر/استان:</span>
                    <span class="info-value">{{ address_city }}, {{ address_province }}</span>
                </div>
                <div class="info-row" style="flex: 1 1 60%;">
                    <span class="info-label">آدرس:</span>
                    <span class="info-value">{{ address_text }}</span>
                </div>
                <div class="info-row" style="flex: 1 1 18%;">
                    <span class="info-label">کد پستی:</span>
                    <span class="info-value">{{ address_postal_code }}</span>
                </div>
                <div class="info-row" style="flex: 1 1 18%;">
                    <span class="info-label">تلفن:</span>
                    <span class="info-value">{{ address_phone }}</span>
                </div>
            </div>
        </div>
        {% endif %}
        
        <table class="items-table">
            <thead>
                <tr>
                    <th>ردیف</th>
                    <th>نام محصول</th>
                    <th>تنوع</th>
                    <th>تعداد</th>
                    <th>قیمت واحد</th>
                    <th>جمع</th>
                    <th>تخفیف محصول</th>
                    <th>تخفیف ویژه</th>
                    <th>قیمت نهایی</th>
                </tr>
            </thead>
            <tbody>
                {% for item_data in items_with_discount %}
                <tr>
                    <td>{{ forloop.counter }}</td>
                    <td>{{ item_data.order_item.product.product.name }}</td>
                    <td>{{ item_data.order_item.product.title }}</td>
                    <td>{{ item_data.item.quantity }}</td>
                    <td>{{ item_data.unit_price|price_format }} تومان</td>
                    <td>{{ item_data.price_before_discount|price_format }} تومان</td>
                    <td class="discount-highlight">
                        {% if item_data.order_item.discount_percent > 0 %}
                        {{ item_data.order_item.discount_percent }}% ({{ item_data.discount_amount|price_format }} تومان)
                        {% else %}
                        ---
                        {% endif %}
                    </td>
                    <td class="discount-highlight">
                        {% if item_data.special_discount_amount > 0 %}
                        {{ item_data.special_discount_amount|price_format }} تومان
                        {% else %}
                        ---
                        {% endif %}
                    </td>
                    <td>{{ item_data.final_price|price_format }} تومان</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        
        <div class="financial-summary">
            <div class="summary-section">
                <h3>خلاصه مالی</h3>
                <div class="summary-row">
                    <span class="summary-label">تعداد کل اقلام:</span>
                    <span class="summary-value">{{ total_items }}</span>
                </div>
                
                <div class="summary-row" style="background-color: #e8f5e9; font-weight: bold;">
                    <span class="summary-label">۱. جمع سبد خرید (بعد از تخفیف محصولات):</span>
                    <span class="summary-value">{{ subtotal|price_format }} تومان</span>
                </div>
                
                <div class="summary-row" style="background-color: #fff3e0;">
                    <span class="summary-label discount-highlight">۲. تخفیف محصولات:</span>
                    <span class="summary-value discount-highlight">
                        {% if items_discount_amount > 0 %}
                        -{{ items_discount_amount|price_format }} تومان
                        {% else %}
                        0 تومان
                        {% endif %}
                    </span>
                </div>
                
                <div class="summary-row" style="background-color: #fce4ec;">
                    <span class="summary-label discount-highlight">۳. تخفیف ویژه محصولات:</span>
                    <span class="summary-value discount-highlight">
                        {% if special_discount_total > 0 %}
                        -{{ special_discount_total|price_format }} تومان
                        {% else %}
                        0 تومان
                        {% endif %}
                    </span>
                </div>
                
                {% comment %} <div class="summary-row">
                    <span class="summary-label">مالیات:</span>
                    <span class="summary-value">{{ tax_amount|price_format }} تومان</span>
                </div> {% endcomment %}
                
                <div class="summary-row highlight">
                    <span class="summary-label commission-highlight">کمیسیون ({{ commission_percent }}%):</span>
                    <span class="summary-value commission-highlight">-{{ commission_amount|price_format }} تومان</span>
                </div>
                
                <div class="summary-row payable">
                    <span class="summary-label">قیمت نهایی (قابل پرداخت به فروشگاه):</span>
                    <span class="summary-value">{{ payable_amount|price_format }} تومان</span>
                </div>
            </div>
        </div>
        
        <div class="footer">
            <p>با تشکر از همکاری شما</p>
            <p>این فاکتور به صورت الکترونیکی صادر شده و نیازی به امضا و مهر ندارد</p>
            <p>در صورت هرگونه سوال یا مشکل با پشتیبانی تماس بگیرید</p>
        </div>
    </div>
</body>
</html>
