<?php
$page_title = 'عرض فاتورة شراء';
$UA_FORCE_DESIGN_V2 = true;
include __DIR__ . '/header.php';
include __DIR__ . '/sidebar.php';

$finance_active = 'purchases_report';
$finance_group = 'purchases';

require_once __DIR__ . '/purchases_lib.php';
$conn = $GLOBALS['conn'] ?? null;
if (!($conn instanceof mysqli)) {
  echo '<div class="content-wrapper ua-extracted-page"><div class="alert alert-danger">لا يوجد اتصال بقاعدة البيانات.</div></div>';
  include __DIR__ . '/footer.php';
  exit;
}
ensure_purchase_tables($conn);

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$purchase = null;
$items = [];

if ($id > 0) {
  $stmt = $conn->prepare("SELECT id,supplier_name,invoice_no,currency,usd_rate,total,paid,notes,created_at FROM shop_purchases WHERE id=? LIMIT 1");
  $stmt->bind_param("i",$id);
  $stmt->execute();
  $purchase = $stmt->get_result()->fetch_assoc();
  $stmt->close();

  $stmt = $conn->prepare("SELECT item_name,qty,unit_price,total FROM shop_purchase_items WHERE purchase_id=? ORDER BY id ASC");
  $stmt->bind_param("i",$id);
  $stmt->execute();
  $items = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
  $stmt->close();
}
?>
<div class="content-wrapper ua-extracted-page">
  <section class="content-header">
    <h1><?= htmlspecialchars($page_title, ENT_QUOTES, 'UTF-8') ?></h1>
  </section>

  <section class="content">
    <div class="container-fluid">
      <?php include __DIR__ . '/finance_tabs.php'; ?>
    <?php if(!$purchase): ?>
      <div class="alert alert-warning">الفاتورة غير موجودة أو لم يتم تحديد رقم صحيح.</div>
    <?php else: ?>
      <div class="card">
        <div class="card-body">
          <div class="row">
            <div class="col-md-6">
              <p><strong>المورّد:</strong> <?= htmlspecialchars($purchase['supplier_name'] ?? '-', ENT_QUOTES, 'UTF-8') ?></p>
              <p><strong>رقم الفاتورة:</strong> <?= htmlspecialchars($purchase['invoice_no'] ?? '-', ENT_QUOTES, 'UTF-8') ?></p>
              <p><strong>التاريخ:</strong> <?= htmlspecialchars($purchase['created_at'] ?? '-', ENT_QUOTES, 'UTF-8') ?></p>
            </div>
            <div class="col-md-6">
              <p><strong>العملة:</strong> <?= htmlspecialchars($purchase['currency'] ?? 'SYP', ENT_QUOTES, 'UTF-8') ?></p>
              <p><strong>سعر الصرف:</strong> <?= htmlspecialchars((string)($purchase['usd_rate'] ?? ''), ENT_QUOTES, 'UTF-8') ?></p>
              <p><strong>الإجمالي:</strong> <?= (float)($purchase['total'] ?? 0) ?></p>
              <p><strong>المدفوع:</strong> <?= (float)($purchase['paid'] ?? 0) ?></p>
            </div>
          </div>

          <?php if(!empty($purchase['notes'])): ?>
            <hr>
            <p><strong>ملاحظات:</strong><br><?= nl2br(htmlspecialchars($purchase['notes'], ENT_QUOTES, 'UTF-8')) ?></p>
          <?php endif; ?>
        </div>
      </div>

      <div class="card mt-3">
        <div class="card-header"><strong>الأصناف</strong></div>
        <div class="card-body">
          <div class="table-responsive">
            <table class="table table-striped table-sm">
              <thead>
                <tr>
                  <th>الصنف</th>
                  <th class="text-center">الكمية</th>
                  <th class="text-center">السعر</th>
                  <th class="text-center">الإجمالي</th>
                </tr>
              </thead>
              <tbody>
                <?php if(!$items): ?>
                  <tr><td colspan="4" class="text-center text-muted">لا توجد أصناف</td></tr>
                <?php else: foreach($items as $it): ?>
                  <tr>
                    <td><?= htmlspecialchars($it['item_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                    <td class="text-center"><?= (float)($it['qty'] ?? 0) ?></td>
                    <td class="text-center"><?= (float)($it['unit_price'] ?? 0) ?></td>
                    <td class="text-center"><?= (float)($it['total'] ?? 0) ?></td>
                  </tr>
                <?php endforeach; endif; ?>
              </tbody>
            </table>
          </div>

          <a class="btn btn-secondary" href="purchases_report.php">رجوع</a>
        </div>
      </div>
    <?php endif; ?>
    </div>
  </section>
</div>

<?php include __DIR__ . '/footer.php'; ?>
