{% 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>فاکتور سفارش - {{ order_number }}</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Vazir', 'Tahoma', Arial, sans-serif;
            direction: rtl;
            padding: 10px;
            background-color: #fff;
        }
        
        .invoice-container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 20px;
        }
        
        .header {
            text-align: center;
            margin-bottom: 15px;
            border-bottom: 2px solid #333;
            padding-bottom: 10px;
        }
        
        .header h1 {
            color: #333;
            font-size: 22px;
            margin-bottom: 5px;
        }
        
        .header h2 {
            color: #666;
            font-size: 14px;
        }
        
        .order-info {
            display: flex;
            justify-content: space-between;
            margin-bottom: 15px;
            background-color: #f9f9f9;
            padding: 12px;
            border-radius: 3px;
        }
        
        .info-section {
            flex: 1;
        }
        
        .info-section h3 {
            color: #333;
            margin-bottom: 6px;
            font-size: 13px;
            border-bottom: 1px solid #ddd;
            padding-bottom: 3px;
        }
        
        .info-row {
            margin: 4px 0;
            font-size: 11px;
        }
        
        .info-label {
            font-weight: bold;
            color: #555;
        }
        
        .info-value {
            color: #333;
        }
        
        .items-table {
            width: 100%;
            border-collapse: collapse;
            margin: 15px 0;
        }
        
        .items-table th {
            background-color: #333;
            color: white;
            padding: 6px;
            text-align: center;
            font-size: 11px;
        }
        
        .items-table td {
            padding: 6px;
            text-align: center;
            border-bottom: 1px solid #ddd;
            font-size: 10px;
        }
        
        .items-table tr:hover {
            background-color: #f5f5f5;
        }
        
        .summary {
            margin-top: 15px;
            float: left;
            width: 300px;
        }
        
        .summary-row {
            display: flex;
            justify-content: space-between;
            padding: 6px;
            border-bottom: 1px solid #ddd;
            font-size: 11px;
        }
        
        .summary-row.total {
            background-color: #333;
            color: white;
            font-weight: bold;
            font-size: 14px;
            margin-top: 6px;
        }
        
        .summary-label {
            font-weight: bold;
        }
        
        .summary-value {
            text-align: left;
        }
        
        .status-badge {
            display: inline-block;
            padding: 2px 8px;
            border-radius: 10px;
            font-size: 9px;
            font-weight: bold;
        }
        
        .status-paid {
            background-color: #4CAF50;
            color: white;
        }
        
        .status-unpaid {
            background-color: #f44336;
            color: white;
        }
        
        .status-pending {
            background-color: #ff9800;
            color: white;
        }
        
        .footer {
            margin-top: 30px;
            text-align: center;
            padding-top: 10px;
            border-top: 1px solid #ddd;
            color: #777;
            font-size: 9px;
        }
        
        .clearfix::after {
            content: "";
            display: table;
            clear: both;
        }
        
        .discount-highlight {
            color: #d32f2f;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="invoice-container">
        <div class="header">
            <h1>فاکتور فروش</h1>
            <h2>شماره سفارش: {{ order_number }}</h2>
        </div>
        
        <div class="order-info">
            <div class="info-section">
                <h3>اطلاعات مشتری</h3>
                {% if user %}
                <div class="info-row">
                    <span class="info-label">نام:</span>
                    <span class="info-value">{{ user.first_name }} {{ user.last_name }}</span>
                </div>
                <div class="info-row">
                    <span class="info-label">ایمیل:</span>
                    <span class="info-value">{{ user.email|default:"---" }}</span>
                </div>
                <div class="info-row">
                    <span class="info-label">تلفن:</span>
                    <span class="info-value">{{ user.phone }}</span>
                </div>
                {% endif %}
            </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>
        </div>
        
        {% if address %}
        <div class="order-info">
            <div class="info-section">
                <h3>آدرس تحویل</h3>
                <div class="info-row">
                    <span class="info-label">نام گیرنده:</span>
                    <span class="info-value">{{ address.name }}</span>
                </div>
                <div class="info-row">
                    <span class="info-label">آدرس:</span>
                    <span class="info-value">{{ address.address }}</span>
                </div>
                <div class="info-row">
                    <span class="info-label">شهر/استان:</span>
                    <span class="info-value">{{ address.city }}, {{ address.province }}</span>
                </div>
                <div class="info-row">
                    <span class="info-label">کد پستی:</span>
                    <span class="info-value">{{ address.postal_code }}</span>
                </div>
                <div class="info-row">
                    <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.item.product.product.name }}</td>
                    <td>{{ item_data.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.item.discount_percent > 0 %}
                        {{ item_data.item.discount_percent }}% ({{ item_data.discount_amount|price_format }} تومان)
                        {% else %}
                        ---
                        {% endif %}
                    </td>
                    <td class="discount-highlight">
                        {% if item_data.item.special_discount_amount > 0 %}
                        {{ item_data.item.special_discount_amount|price_format }} تومان
                        {% else %}
                        ---
                        {% endif %}
                    </td>
                    <td>{{ item_data.final_price|price_format }} تومان</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        
        <div class="clearfix">
            <div class="summary">
                <div class="summary-row">
                    <span class="summary-label">تعداد کل اقلام:</span>
                    <span class="summary-value">{{ total_items }}</span>
                </div>
                
                <!-- 1. Total Before Any Discount -->
                <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>
                
                <!-- 2. Product Discount -->
                <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> 
                
                <!-- 3. Special Product Discount -->
                <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>
                
                {% if discount_amount > 0 %}
                <div class="summary-row">
                    <span class="summary-label discount-highlight">تخفیف کد تخفیف:</span>
                    <span class="summary-value discount-highlight">-{{ discount_amount|price_format }} تومان</span>
                </div>
                {% endif %}
                
                <div class="summary-row">
                    <span class="summary-label">مالیات ({{ tax_rate }}%):</span>
                    <span class="summary-value">{{ tax|price_format }} تومان</span>
                </div>
                
                <!-- 4. Final Price -->
                <div class="summary-row total">
                    <span class="summary-label">۴. قیمت نهایی (قابل پرداخت):</span>
                    <span class="summary-value">{{ final_price|price_format }} تومان</span>
                </div>
            </div>
        </div>
        
        {% if discount_code %}
        <div class="clearfix" style="margin-top: 100px;">
            <div class="order-info">
                <div class="info-section">
                    <h3>کد تخفیف استفاده شده</h3>
                    <div class="info-row">
                        <span class="info-label">کد:</span>
                        <span class="info-value">{{ discount_code.code }}</span>
                    </div>
                    <div class="info-row">
                        <span class="info-label">درصد:</span>
                        <span class="info-value">{{ discount_code.percent }}%</span>
                    </div>
                </div>
            </div>
        </div>
        {% endif %}
        
        <div class="footer">
            <p>با تشکر از خرید شما</p>
            <p>این فاکتور به صورت الکترونیکی صادر شده و نیازی به امضا و مهر ندارد</p>
        </div>
    </div>
</body>
</html>
