<!DOCTYPE html>
<html lang="fa" dir="rtl">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>فاکتور سفارش # <?php echo $order->get_id(); ?> </title>
    <style>
      * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
      }

      body {
        font-family: 'yekan', sans-serif;
        direction: rtl;
        text-align: right;
      }

      .invoice-container {
        margin: 0 auto;
        background-color: white;
      }

      .invoice-title {
        text-align: center;
        font-size: 28px;
        font-weight: bold;
        margin-bottom: 20px;
      }

      .info-row {
        display: flex;
        margin: 5px 0;
      }

      .info-label {
        font-size: 14px;
        font-weight: bold;
        margin-left: 5px;
        margin-bottom: 10px;
        line-height: 20px;
      }

      .invoice-table {
        width: 100%;
        border-collapse: collapse;
        margin-bottom: 20px;
      }

      .invoice-table th,
      .invoice-table td {
        border: 1px solid #ccc;
        padding: 8px;
        text-align: center;
      }

      .invoice-table th {
        background-color: <?php echo esc_attr($invoice_data['color'] ?: '#E8E8E8'); ?>;
        color: <?php echo esc_attr($invoice_data['title_color'] ?: '#000'); ?>;
      }

      .invoice-details {
        width: 100%;
        border-collapse: collapse;
        margin-bottom: 20px;
      }

      .invoice-details th,
      .invoice-details td {
        border: 1px solid #ccc;
        padding: 8px;
        text-align: right;
      }

      .invoice-details th {
        background-color: <?php echo esc_attr($invoice_data['color'] ?: '#E8E8E8'); ?>;
        color: <?php echo esc_attr($invoice_data['title_color'] ?: '#000'); ?>;
      }

      .invoice-totals {
        width: 100%;
        border-collapse: collapse;
        margin-top: 20px;
      }

      .invoice-totals th,
      .invoice-totals td {
        border: 1px solid #ccc;
        padding: 8px;
        text-align: center;
      }

      .invoice-totals th {
        background-color: <?php echo esc_attr($invoice_data['color'] ?: '#E8E8E8'); ?>;
        color: <?php echo esc_attr($invoice_data['title_color'] ?: '#000'); ?>;
      }

      .manager-note {
        margin-top: 20px;
        padding: 10px;
        border: 1px solid #ccc;
        border-radius: 4px;
      }

      .manager-note img {
        max-width: 100%;
        height: auto;
      }
    </style>
  </head>
  <body>
    <div class="invoice-container">
      <!-- هدر فاکتور -->
      <table style="width: 100%; border-collapse: collapse; margin-bottom: 50px;">
        <tr>
          <!-- ستون لوگو -->
          <td style="width: 30%; vertical-align: middle; padding: 5px;">
            <div class="logo" style="text-align: center;">
              <?php if (!empty($invoice_data['logo'])): ?>
                <img src="<?php echo esc_url($invoice_data['logo']); ?>" alt="لوگوی فروشگاه" style="width:70px;">
              <?php else: ?>
                <img src="<?php echo esc_url(plugins_url('assets/images/default-logo.png', dirname(dirname(__FILE__)))); ?>" alt="لوگوی پیش‌فرض" style="width:70px;">
              <?php endif; ?>
            </div>
          </td>
          <!-- ستون عنوان فاکتور -->
          <td style="width: 40%; vertical-align: middle; padding: 5px;">
            <div class="invoice-title" style="  text-align: center; font-size: 24px; font-weight: bold;"> صـــورتــحـسـاب ســفـارش </div>
          </td>
          <!-- ستون اطلاعات فاکتور -->
          <td style="width: 30%; vertical-align: middle; padding: 5px; text-align: left;">
            <div class="barcode-container" style="text-align: left;">
              <div style="margin-bottom: 5px;">
                <span>شماره فاکتور:</span>
                <span> <?php echo $order->get_id(); ?> </span>
              </div>
              <div style="margin-bottom: 5px;">
                <span>تاریخ:</span>
                <span> <?php echo $order->get_date_created()->date('Y/m/d'); ?> </span>
              </div>
              <div style="margin-bottom: 5px;">
                <span>وضعیت:</span>
                <span> <?php echo esc_html(wc_get_order_status_name($order->get_status())); ?> </span>
              </div>
            </div>
          </td>
        </tr>
      </table>
      <!-- اطلاعات مشتری و فروشنده -->
      <table style="margin-bottom: 35px;" class="invoice-table">
        <thead>
          <tr>
            <th>فــروشــنده</th>
            <th>خــریـدار</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>
              <br>
              <?php if (!empty($invoice_data['seller_name'])): ?>
              <div class="info-row">
                <span class="info-label">نام فروشگاه: </span>
                <span> <?php echo esc_html($invoice_data['seller_name']); ?> </span>
              </div>
              <br>
              <?php endif; ?>
              
              <div class="info-row">
                <?php if (!empty($invoice_data['seller_national_id'])): ?>
                <span class="info-label">کد ملی:</span>
                <span> <?php echo esc_html($invoice_data['seller_national_id']); ?> </span>
                <?php endif; ?>
                
                <?php if (!empty($invoice_data['seller_registration_number'])): ?>
                <span class="info-label" style="margin-right: <?php echo !empty($invoice_data['seller_national_id']) ? '20px' : '0'; ?>">شماره ثبت/ملی:</span>
                <span> <?php echo esc_html($invoice_data['seller_registration_number']); ?> </span>
                <?php endif; ?>
              </div>
              
              <?php if (!empty($invoice_data['seller_national_id']) || !empty($invoice_data['seller_registration_number'])): ?>
              <br>
              <?php endif; ?>
              
              <div class="info-row">
                <?php if (!empty($invoice_data['seller_postcode'])): ?>
                <span class="info-label">کدپستی:</span>
                <span> <?php echo esc_html($invoice_data['seller_postcode']); ?> </span>
                <?php endif; ?>
                
                <?php if (!empty($invoice_data['seller_phone'])): ?>
                <span class="info-label" style="margin-right: <?php echo !empty($invoice_data['seller_postcode']) ? '20px' : '0'; ?>">شماره تماس:</span>
                <span> <?php echo esc_html($invoice_data['seller_phone']); ?> </span>
                <?php endif; ?>
              </div>
              
              <?php if (!empty($invoice_data['seller_postcode']) || !empty($invoice_data['seller_phone'])): ?>
              <br>
              <?php endif; ?>
              
              <?php if (!empty($invoice_data['seller_address'])): ?>
              <div class="info-row">
                <span class="info-label">آدرس فروشگاه:</span>
                <span> <?php echo esc_html($invoice_data['seller_address']); ?> </span>
              </div>
              <?php endif; ?>
            </td>
            <br>
            <td>
              <br>
              <?php if (!empty($order->get_billing_first_name()) || !empty($order->get_billing_last_name())): ?>
              <div class="info-row">
                <span class="info-label">نام و نام خانوادگی:</span>
                <span> <?php echo esc_html($order->get_billing_first_name() . ' ' . $order->get_billing_last_name()); ?> </span>
              </div>
              <br>
              <?php endif; ?>
              
              <div class="info-row">
                <?php if (!empty($order->get_billing_phone())): ?>
                <span class="info-label">شماره تماس:</span>
                <span> <?php echo esc_html($order->get_billing_phone()); ?> </span>
                <?php endif; ?>
                
                <?php if (!empty($order->get_billing_postcode())): ?>
                <span class="info-label" style="margin-right: <?php echo !empty($order->get_billing_phone()) ? '20px' : '0'; ?>">کدپستی:</span>
                <span> <?php echo esc_html($order->get_billing_postcode()); ?> </span>
                <?php endif; ?>
              </div>
              
              <?php if (!empty($order->get_billing_phone()) || !empty($order->get_billing_postcode())): ?>
              <br>
              <?php endif; ?>
              
              <?php if (!empty($order->get_billing_address_1())): ?>
              <div class="info-row">
                <span class="info-label">آدرس:</span>
                <span> <?php echo esc_html($order->get_billing_address_1()); ?> </span>
              </div>
              <br>
              <?php endif; ?>
              
              <?php if (!empty($order->get_billing_email())): ?>
              <div class="info-row">
                <span class="info-label">ایمیل:</span>
                <span> <?php echo esc_html($order->get_billing_email()); ?> </span>
              </div>
              <?php endif; ?>
            </td>
          </tr>
        </tbody>
      </table>
      <!-- جدول محصولات -->
      <table style="margin-bottom: 25px;" class="invoice-table">
        <thead>
          <tr>
            <th>#</th>
            <th>شناسه</th>
            <th>تصویر محصول</th>
            <th>نام محصول</th>
            <th>قیمت واحد</th>
            <th>تخفیف</th>
            <th>تعداد</th>
            <th>مبلغ کل</th>
          </tr>
        </thead>
        <tbody> <?php
                $items = $order->get_items();
                $counter = 1;
                foreach ($items as $item_id => $item):
                    $product = $item->get_product();
                    $product_image = $product ? wp_get_attachment_image_src($product->get_image_id(), 'thumbnail') : false;
                ?> <tr>
            <td> <?php echo $counter++; ?> </td>
            <td> <?php echo $product ? $product->get_id() : '-'; ?> </td>
            <td> <?php if ($product_image): ?> <img src="<?php echo esc_url($product_image[0]); ?>" alt="<?php echo esc_attr($item->get_name()); ?>" width="50"> <?php else: ?> — <?php endif; ?> </td>
            <td> <?php echo esc_html($item->get_name()); ?> </td>
            <td> <?php echo wc_price($item->get_subtotal() / $item->get_quantity()); ?> </td>
            <td> <?php echo wc_price($item->get_subtotal() - $item->get_total()); ?> </td>
            <td> <?php echo esc_html($item->get_quantity()); ?> </td>
            <td> <?php echo wc_price($item->get_total()); ?> </td>
          </tr> <?php endforeach; ?> </tbody>
      </table>
      <!-- جمع‌بندی مبالغ -->
      <table style="margin-bottom: 25px;" class="invoice-totals">
        <tr>
          <th>مبلغ کل</th>
          <td> <?php echo wc_price($order->get_subtotal()); ?> </td>
        </tr>
        <tr>
          <th>تخفیف</th>
          <td> <?php echo wc_price($order->get_total_discount()); ?> </td>
        </tr>
        <tr>
          <th>هزینه حمل و نقل</th>
          <td> <?php echo wc_price($order->get_shipping_total()); ?> </td>
        </tr>
        <tr>
          <th>مبلغ نهایی</th>
          <td> <?php echo wc_price($order->get_total()); ?> </td>
        </tr>
      </table>
      <!-- یادداشت مدیر -->
      <?php if (!empty($invoice_data['manager_note'])): ?>
      <table style="width: 100%; border-collapse: collapse; margin-bottom: 10px;">
        <tr>
          <td style="width: 100%; vertical-align: middle; padding: 5px;">
            <div class="manager-note">
              <?php echo wpautop($invoice_data['manager_note']); ?>
            </div>
          </td>
        </tr>
      </table>
      <?php endif; ?>
    </div>
  </body>
</html>