<?php
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/functions.php';
$conn = offers_db();
if (!$conn) die('Database connection not found.');
$flash = offers_flash_get();

$filters = [
    'type' => $_GET['type'] ?? '',
    'status' => $_GET['status'] ?? '',
    'search' => $_GET['search'] ?? '',
];
$rows = offers_fetch_all($conn, $filters);
?>
<?php $page_title = 'العروض'; ?>
<?php include __DIR__ . '/../includes/header.php'; ?>
<?php include __DIR__ . '/../includes/sidebar.php'; ?>
<link rel="stylesheet" href="assets/offers.css">
<div class="ofr-wrap">
  <div class="ofr-page-title">
    <div>
      <h1>كل العروض</h1>
      <div class="ofr-muted">قائمة موحدة لكل أنواع العروض مع فلترة وبحث.</div>
    </div>
    <div class="ofr-actions">
      <a class="btn btn-primary" href="create.php">إضافة عرض</a>
      <a class="btn btn-light" href="index.php">رجوع للوحة العروض</a>
    </div>
  </div>

  <?php if ($flash): ?>
    <div class="notice <?= offers_h($flash['type']); ?>"><?= offers_h($flash['message']); ?></div>
  <?php endif; ?>

  <form class="ofr-filter" method="get">
    <div class="field">
      <label>النوع</label>
      <select name="type">
        <option value="">الكل</option>
        <option value="individual" <?= ($filters['type']==='individual')?'selected':''; ?>>فردي</option>
        <option value="bundle" <?= ($filters['type']==='bundle')?'selected':''; ?>>باقة</option>
        <option value="quantity" <?= ($filters['type']==='quantity')?'selected':''; ?>>كمية</option>
        <option value="company" <?= ($filters['type']==='company')?'selected':''; ?>>شركة/براند</option>
      </select>
    </div>
    <div class="field">
      <label>الحالة</label>
      <select name="status">
        <option value="">الكل</option>
        <option value="1" <?= ((string)$filters['status']==='1')?'selected':''; ?>>مفعّل</option>
        <option value="0" <?= ((string)$filters['status']==='0')?'selected':''; ?>>معطّل</option>
      </select>
    </div>
    <div class="field">
      <label>بحث</label>
      <input type="text" name="search" value="<?= offers_h($filters['search']); ?>" placeholder="عنوان، منتج، شركة...">
    </div>
    <div class="field" style="justify-content:end">
      <label>&nbsp;</label>
      <div class="actions-inline">
        <button class="btn btn-dark" type="submit">فلترة</button>
        <a class="btn btn-light" href="list.php">مسح</a>
      </div>
    </div>
  </form>

  <div class="table-wrap">
    <table class="table">
      <thead>
        <tr>
          <th>#</th>
          <th>النوع</th>
          <th>العنوان</th>
          <th>الهدف</th>
          <th>الأصلي</th>
          <th>العرض</th>
          <th>الفترة</th>
          <th>الحالة</th>
          <th>إجراءات</th>
        </tr>
      </thead>
      <tbody>
      <?php if (!$rows): ?>
        <tr><td colspan="9">لا توجد عروض حالياً.</td></tr>
      <?php else: ?>
        <?php foreach ($rows as $row): ?>
          <tr>
            <td><?= (int)$row['id']; ?></td>
            <td><span class="badge type"><?= offers_h(offers_type_label($row['type'])); ?></span></td>
            <td>
              <strong><?= offers_h($row['title']); ?></strong>
              <?php if (!empty($row['notes'])): ?>
                <div class="tiny"><?= offers_h(mb_strimwidth($row['notes'], 0, 120, '...')); ?></div>
              <?php endif; ?>
            </td>
            <td><?= offers_h($row['product_name'] ?? ''); ?></td>
            <td><?= is_numeric($row['old_price'] ?? null) ? offers_format_money($row['old_price']) : offers_h($row['old_price'] ?? '-'); ?></td>
            <td><?= is_numeric($row['new_price'] ?? null) ? offers_format_money($row['new_price']) : offers_h($row['new_price'] ?? '-'); ?></td>
            <td>
              <div><?= offers_h($row['start_date'] ?: '-'); ?></div>
              <div class="tiny"><?= offers_h($row['end_date'] ?: '-'); ?></div>
            </td>
            <td><span class="badge <?= offers_h(offers_status_class($row['status'])); ?>"><?= offers_h(offers_status_label($row['status'])); ?></span></td>
            <td>
              <div class="actions-inline">
                <a class="btn btn-light" href="view.php?type=<?= offers_h($row['type']); ?>&id=<?= (int)$row['id']; ?>">عرض</a>
                <a class="btn btn-warning" href="edit.php?type=<?= offers_h($row['type']); ?>&id=<?= (int)$row['id']; ?>">تعديل</a>
                <?= admin_post_action_form('toggle_status.php', 'حالة', 'btn btn-dark', 'offer_toggle', ['type' => $row['type'], 'id' => (int)$row['id']], 'تغيير حالة العرض؟'); ?>
                <?= admin_post_action_form('delete.php', 'حذف', 'btn btn-danger', 'offer_delete', ['type' => $row['type'], 'id' => (int)$row['id']], 'حذف العرض نهائياً؟'); ?>
              </div>
            </td>
          </tr>
        <?php endforeach; ?>
      <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>