<?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.');

$type = $_GET['type'] ?? '';
$id = (int)($_GET['id'] ?? 0);
$item = offers_find($conn, $type, $id);
if (!$item) die('Offer not found.');
$bundleItems = $type === 'bundle' ? offers_fetch_bundle_items($conn, $id) : [];
?>
<?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><?= offers_h($item['title']); ?></h1>
      <div class="ofr-muted">عرض رقم <?= (int)$id; ?> | النوع: <?= offers_h(offers_type_label($type)); ?></div>
    </div>
    <div class="ofr-actions">
      <a class="btn btn-warning" href="edit.php?type=<?= offers_h($type); ?>&id=<?= (int)$id; ?>">تعديل</a>
      <a class="btn btn-light" href="list.php">كل العروض</a>
    </div>
  </div>

  <div class="ofr-grid">
    <div class="ofr-card">
      <h3>الحالة</h3>
      <span class="badge <?= offers_h(offers_status_class($item['status'] ?? 0)); ?>"><?= offers_h(offers_status_label($item['status'] ?? 0)); ?></span>
    </div>
    <div class="ofr-card">
      <h3>البداية</h3>
      <div><?= offers_h($item['start_date'] ?? '-'); ?></div>
    </div>
    <div class="ofr-card">
      <h3>النهاية</h3>
      <div><?= offers_h($item['end_date'] ?? '-'); ?></div>
    </div>
  </div>

  <div class="separator"></div>

  <div class="box">
    <h3>تفاصيل العرض</h3>
    <?php if ($type === 'individual'): ?>
      <div class="row3">
        <div class="field"><label>المنتج</label><input readonly value="<?= offers_h($item['product_name'] ?? ''); ?>"></div>
        <div class="field"><label>السعر الأصلي</label><input readonly value="<?= offers_h(offers_format_money($item['old_price'] ?? 0)); ?>"></div>
        <div class="field"><label>سعر العرض</label><input readonly value="<?= offers_h(offers_format_money($item['new_price'] ?? 0)); ?>"></div>
      </div>
    <?php elseif ($type === 'bundle'): ?>
      <div class="row3">
        <div class="field"><label>عدد العناصر</label><input readonly value="<?= (int)($item['item_count'] ?? 0); ?>"></div>
        <div class="field"><label>المجموع الأصلي</label><input readonly value="<?= offers_h(offers_format_money($item['old_price_total'] ?? 0)); ?>"></div>
        <div class="field"><label>سعر الباقة</label><input readonly value="<?= offers_h(offers_format_money($item['new_price_total'] ?? 0)); ?>"></div>
      </div>
      <div class="separator"></div>
      <div class="table-wrap">
        <table class="table">
          <thead><tr><th>#</th><th>المنتج</th><th>الكمية</th><th>السعر</th><th>الإجمالي</th></tr></thead>
          <tbody>
            <?php foreach ($bundleItems as $i => $bundleItem): ?>
              <tr>
                <td><?= $i+1; ?></td>
                <td><?= offers_h($bundleItem['product_name'] ?? ('#'.$bundleItem['product_id'])); ?></td>
                <td><?= (int)$bundleItem['qty']; ?></td>
                <td><?= offers_h(offers_format_money($bundleItem['unit_price'])); ?></td>
                <td><?= offers_h(offers_format_money($bundleItem['line_total'])); ?></td>
              </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    <?php elseif ($type === 'quantity'): ?>
      <div class="row4">
        <div class="field"><label>المنتج</label><input readonly value="<?= offers_h($item['product_name'] ?? ''); ?>"></div>
        <div class="field"><label>الكمية المطلوبة</label><input readonly value="<?= (int)($item['required_qty'] ?? 0); ?>"></div>
        <div class="field"><label>الأصلي الإجمالي</label><input readonly value="<?= offers_h(offers_format_money($item['old_price_total'] ?? 0)); ?>"></div>
        <div class="field"><label>سعر العرض</label><input readonly value="<?= offers_h(offers_format_money($item['new_price_total'] ?? 0)); ?>"></div>
      </div>
    <?php else: ?>
      <div class="row3">
        <div class="field"><label>الشركة / البراند</label><input readonly value="<?= offers_h($item['company_name'] ?? ''); ?>"></div>
        <div class="field"><label>نوع الخصم</label><input readonly value="<?= ($item['discount_type'] ?? 'percent') === 'percent' ? 'نسبة مئوية' : 'مبلغ ثابت'; ?>"></div>
        <div class="field"><label>قيمة الخصم</label><input readonly value="<?= ($item['discount_type'] ?? 'percent') === 'percent' ? offers_h($item['discount_value'].'%') : offers_h(offers_format_money($item['discount_value'])); ?>"></div>
      </div>
    <?php endif; ?>
  </div>

  <?php if (!empty($item['notes'])): ?>
    <div class="box">
      <h3>ملاحظات</h3>
      <div><?= nl2br(offers_h($item['notes'])); ?></div>
    </div>
  <?php endif; ?>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>