<?php // vars: $rows, $customers, $filters ?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <h4 class="mb-0">سجل عروض الأسعار</h4>
  <?php if (can('quotations.create')): ?>
    <a class="btn btn-primary" href="<?= e(url($quotation_form_url ?? '/sales/quotations/form')) ?>"><i class="fa-solid fa-plus"></i> إنشاء عرض سعر</a>
  <?php endif; ?>
</div>

<form class="card card-body mb-3" method="get">
  <div class="row g-3 align-items-end">
    <div class="col-md-4">
      <label class="form-label">بحث</label>
      <input type="text" class="form-control" name="q" value="<?= e($filters['q'] ?? '') ?>" placeholder="رقم العرض / اسم العميل / الهاتف">
    </div>
    <div class="col-md-3">
      <label class="form-label">الحالة</label>
      <select class="form-select" name="status">
        <option value="">الكل</option>
        <?php foreach (['draft'=>'مسودة','sent'=>'مرسل','approved'=>'معتمد','rejected'=>'مرفوض','expired'=>'منتهي','converted'=>'تم تحويله'] as $k=>$v): ?>
          <option value="<?= e($k) ?>" <?= (($filters['status'] ?? '') === $k ? 'selected' : '') ?>><?= e($v) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-3">
      <label class="form-label">العميل</label>
      <select class="form-select" name="party_id" data-searchable-select="1" data-search-placeholder="اكتب أول حرفين من اسم العميل...">
        <option value="0">الكل</option>
        <?php foreach ($customers as $c): ?>
          <option value="<?= (int)$c['id'] ?>" <?= ((int)($filters['party_id'] ?? 0) === (int)$c['id'] ? 'selected' : '') ?>><?= e($c['name']) ?><?= !empty($c['phone']) ? ' — '.e($c['phone']) : '' ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-2 d-flex gap-2">
      <button class="btn btn-primary flex-fill" type="submit">عرض</button>
      <a class="btn btn-outline-secondary" href="<?= e(url($quotations_url ?? '/sales/quotations')) ?>">إعادة</a>
    </div>
  </div>
</form>

<div class="card">
  <div class="table-responsive">
    <table class="table table-hover align-middle mb-0">
      <thead>
      <tr>
        <th>#</th>
        <th>رقم العرض</th>
        <th>التاريخ</th>
        <th>الصلاحية</th>
        <th>العميل</th>
        <th>الإجمالي</th>
        <th>الحالة</th>
        <th class="text-center">إجراءات</th>
      </tr>
      </thead>
      <tbody>
      <?php if (!$rows): ?>
        <tr><td colspan="8" class="text-center text-muted py-4">لا توجد عروض أسعار.</td></tr>
      <?php else: foreach ($rows as $r): ?>
        <tr>
          <td><?= (int)$r['id'] ?></td>
          <td><?= e($r['quotation_no']) ?></td>
          <td><?= e($r['quotation_date']) ?></td>
          <td><?= e($r['valid_until'] ?: '-') ?></td>
          <td>
            <div class="fw-semibold"><?= e($r['party_name']) ?></div>
            <?php if (!empty($r['party_phone'])): ?><div class="small text-muted"><?= e($r['party_phone']) ?></div><?php endif; ?>
          </td>
          <td><?= e(number_format((float)$r['total_amount'], 2)) ?></td>
          <td><span class="badge text-bg-secondary"><?= e(quotation_status_label((string)$r['status'])) ?></span></td>
          <td class="text-center">
            <a class="btn btn-sm btn-outline-primary" href="<?= e((url($quotation_view_base_url ?? '/sales/quotations/view')) . '?id='.(int)$r['id']) ?>">عرض</a>
            <?php if (can('quotations.print')): ?><a class="btn btn-sm btn-outline-dark" target="_blank" rel="noopener" href="<?= e(url('/prints/quotation?id='.(int)$r['id'])) ?>">طباعة</a><?php endif; ?>
            <?php if (can('quotations.send')): ?><a class="btn btn-sm btn-outline-success" href="<?= e((url($quotation_email_base_url ?? '/sales/quotations/email')) . '?id='.(int)$r['id']) ?>">بريد</a><?php endif; ?>
            <?php if (can('quotations.send')): ?><a class="btn btn-sm btn-outline-success" target="_blank" rel="noopener" href="<?= e((url($quotation_whatsapp_base_url ?? '/sales/quotations/whatsapp')) . '?id='.(int)$r['id']) ?>">واتساب</a><?php endif; ?>
            <?php if (can('quotations.convert') && can('sales.invoices.create') && strtolower((string)$r['status']) !== 'converted'): ?><a class="btn btn-sm btn-primary" href="<?= e((url($quotation_convert_base_url ?? '/sales/quotations/convert')) . '?id='.(int)$r['id']) ?>">تحويل</a><?php endif; ?>
            <?php if (!empty($r['converted_invoice_id'])): ?><a class="btn btn-sm btn-outline-primary" href="<?= e((url($sales_invoice_view_base_url ?? '/sales/invoices/view')) . '?id='.(int)$r['converted_invoice_id']) ?>">الفاتورة</a><?php endif; ?>
            <?php if (can('quotations.edit')): ?>
              <a class="btn btn-sm btn-outline-secondary" href="<?= e((url($quotation_form_url ?? '/sales/quotations/form')) . '?id='.(int)$r['id']) ?>">تعديل</a>
            <?php endif; ?>
          </td>
        </tr>
      <?php endforeach; endif; ?>
      </tbody>
    </table>
  </div>
</div>
