<?php // vars: $quotation, $lines, $dispatch_logs ?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <h4 class="mb-1">عرض سعر: <?= e($quotation['quotation_no']) ?></h4>
    <div class="text-muted small">الحالة: <span class="badge text-bg-secondary"><?= e(quotation_status_label((string)$quotation['status'])) ?></span><?php if (!empty($quotation['converted_invoice_id'])): ?> <span class="ms-2">• فاتورة البيع: <a href="<?= e((url($sales_invoice_view_base_url ?? '/sales/invoices/view')) . '?id=' . (int)$quotation['converted_invoice_id']) ?>">#<?= (int)$quotation['converted_invoice_id'] ?></a></span><?php endif; ?></div>
  </div>
  <div class="d-flex gap-2 flex-wrap">
    <?php if (can('quotations.print')): ?><a class="btn btn-outline-dark" target="_blank" rel="noopener" href="<?= e(url('/prints/quotation?id=' . (int)$quotation['id'])) ?>">طباعة / PDF</a><?php endif; ?>
    <?php if (can('quotations.send')): ?><a class="btn btn-outline-success" href="<?= e(url($quotation_email_url ?? '/sales/quotations/email?id=' . (int)$quotation['id'])) ?>">إرسال بريد</a><?php endif; ?>
    <?php if (can('quotations.send')): ?><a class="btn btn-outline-success" target="_blank" rel="noopener" href="<?= e(url($quotation_whatsapp_url ?? '/sales/quotations/whatsapp?id=' . (int)$quotation['id'])) ?>">واتساب</a><?php endif; ?>
    <?php if (can('quotations.convert') && can('sales.invoices.create') && strtolower((string)$quotation['status']) !== 'converted'): ?>
      <a class="btn btn-primary" href="<?= e(url($quotation_convert_url ?? '/sales/quotations/convert?id=' . (int)$quotation['id'])) ?>">تحويل إلى فاتورة بيع</a>
    <?php elseif (!empty($quotation['converted_invoice_id'])): ?>
      <a class="btn btn-outline-primary" href="<?= e((url($sales_invoice_view_base_url ?? '/sales/invoices/view')) . '?id=' . (int)$quotation['converted_invoice_id']) ?>">فتح فاتورة البيع</a>
    <?php endif; ?>
    <?php if (can('quotations.edit')): ?>
      <a class="btn btn-outline-primary" href="<?= e((url($quotation_form_url ?? '/sales/quotations/form')) . '?id=' . (int)$quotation['id']) ?>">تعديل</a>
    <?php endif; ?>
    <a class="btn btn-outline-secondary" href="<?= e(url($quotations_url ?? '/sales/quotations')) ?>">رجوع</a>
  </div>
</div>

<div class="row g-3 mb-3">
  <div class="col-md-4">
    <div class="card p-3 h-100">
      <div class="text-muted small mb-1">العميل</div>
      <div class="fw-semibold fs-5"><?= e($quotation['party_name']) ?></div>
      <?php if (!empty($quotation['party_phone'])): ?><div class="small text-muted">📞 <?= e($quotation['party_phone']) ?></div><?php endif; ?>
      <?php if (!empty($quotation['party_email'])): ?><div class="small text-muted">✉️ <?= e($quotation['party_email']) ?></div><?php endif; ?>
    </div>
  </div>
  <div class="col-md-4">
    <div class="card p-3 h-100">
      <div class="text-muted small mb-1">التواريخ</div>
      <div>تاريخ العرض: <strong><?= e($quotation['quotation_date']) ?></strong></div>
      <div>ساري حتى: <strong><?= e($quotation['valid_until'] ?: '-') ?></strong></div>
    </div>
  </div>
  <div class="col-md-4">
    <div class="card p-3 h-100">
      <div class="text-muted small mb-1">الإجمالي</div>
      <div class="fw-bold fs-4"><?= e(number_format((float)$quotation['total_amount'], 2)) ?></div>
    </div>
  </div>
</div>

<div class="card mb-3">
  <div class="table-responsive">
    <table class="table table-bordered align-middle mb-0">
      <thead>
      <tr>
        <th>#</th>
        <th>الصنف</th>
        <th>الباركود</th>
        <th>الكمية</th>
        <th>السعر</th>
        <th>الإجمالي</th>
        <th>ملاحظات</th>
      </tr>
      </thead>
      <tbody>
      <?php foreach ($lines as $i => $ln): ?>
        <tr>
          <td><?= $i + 1 ?></td>
          <td><?= e($ln['item_name'] ?: ('#' . $ln['item_id'])) ?></td>
          <td><?= e($ln['barcode'] ?: '-') ?></td>
          <td><?= e(number_format((float)$ln['qty'], 3)) ?></td>
          <td><?= e(number_format((float)$ln['unit_price'], 2)) ?></td>
          <td><?= e(number_format((float)$ln['line_total'], 2)) ?></td>
          <td><?= e($ln['notes'] ?: '-') ?></td>
        </tr>
      <?php endforeach; ?>
      </tbody>
      <tfoot>
        <tr>
          <th colspan="5" class="text-end">الإجمالي</th>
          <th><?= e(number_format((float)$quotation['total_amount'], 2)) ?></th>
          <th></th>
        </tr>
      </tfoot>
    </table>
  </div>
</div>

<div class="row g-3">
  <div class="col-md-6">
    <div class="card p-3 h-100">
      <h6 class="mb-2">طريقة الدفع</h6>
      <div class="text-muted"><?= nl2br(e((string)($quotation['payment_terms'] ?? '-'))) ?></div>
    </div>
  </div>
  <div class="col-md-6">
    <div class="card p-3 h-100">
      <h6 class="mb-2">مدة / شروط التوريد</h6>
      <div class="text-muted"><?= nl2br(e((string)($quotation['delivery_terms'] ?? '-'))) ?></div>
    </div>
  </div>
  <div class="col-md-6">
    <div class="card p-3 h-100">
      <h6 class="mb-2">الشروط والأحكام</h6>
      <div class="text-muted"><?= nl2br(e((string)($quotation['terms_conditions'] ?? '-'))) ?></div>
    </div>
  </div>
  <div class="col-md-6">
    <div class="card p-3 h-100">
      <h6 class="mb-2">ملاحظات إضافية</h6>
      <div class="text-muted"><?= nl2br(e((string)($quotation['notes'] ?? '-'))) ?></div>
    </div>
  </div>
</div>

<?php if (!empty($dispatch_logs)): ?>
<div class="card p-3 mt-3">
  <h6 class="mb-3">سجل الإرسال</h6>
  <div class="table-responsive">
    <table class="table table-sm align-middle mb-0">
      <thead><tr><th>القناة</th><th>المستلم</th><th>الحالة</th><th>التاريخ</th></tr></thead>
      <tbody>
      <?php foreach ($dispatch_logs as $log): ?>
        <tr>
          <td><?= e($log['channel']) ?></td>
          <td><?= e($log['recipient']) ?></td>
          <td><?= e($log['status']) ?></td>
          <td><?= e($log['sent_at'] ?: $log['created_at']) ?></td>
        </tr>
      <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
<?php endif; ?>
