<?php
$page_title = 'فاتورة مبيعات';

if (session_status() === PHP_SESSION_NONE) session_start();

require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/db_bootstrap.php';
require_once __DIR__ . '/app_assets.php';

require_login();
$conn = ensure_db(true);
if(function_exists('require_perm')) require_perm('can_manage_sales');

$UA_FORCE_DESIGN_V2 = true;
include __DIR__ . '/header.php';
include __DIR__ . '/sidebar.php';

$id = (int)($_GET['id'] ?? 0);
$sale = $conn->query("SELECT * FROM shop_sales WHERE id=$id LIMIT 1");
if(!$sale || !$sale->num_rows){ die('الفاتورة غير موجودة'); }
$sale = $sale->fetch_assoc();
$items = $conn->query("SELECT * FROM shop_sale_items WHERE sale_id=$id");

$active_tab = 'sales';
?>
<div class="content-wrapper ua-extracted-page">
  <section class="content-header">
    <div class="container-fluid d-flex justify-content-between">
      <h1 class="mb-0">فاتورة مبيعات #<?= (int)$sale['id'] ?></h1>
      <button class="btn btn-primary" onclick="window.print()"><i class="fa fa-print"></i> طباعة 80mm</button>
    </div>
  </section>

  <section class="content">
    <div class="container-fluid">
      <div class="card">
        <div class="card-body" id="printArea">
          <h4 class="text-center mb-1"><?= h(defined('APP_NAME')?APP_NAME:'') ?></h4>
          <div class="text-center text-muted mb-3">فاتورة مبيعات</div>

          <div class="d-flex justify-content-between">
            <div>العميل: <?= h($sale['customer_name'] ?? '-') ?></div>
            <div><?= h($sale['created_at'] ?? '') ?></div>
          </div>
          <div>الهاتف: <?= h($sale['customer_phone'] ?? '-') ?></div>

          <hr>
          <table class="table table-sm">
            <thead><tr><th>الصنف</th><th>كمية</th><th>سعر</th><th>إجمالي</th></tr></thead>
            <tbody>
              <?php while($it=$items->fetch_assoc()){ ?>
                <tr>
                  <td><?= h($it['name']) ?></td>
                  <td><?= (int)$it['qty'] ?></td>
                  <td><?= number_format((float)$it['price'],2) ?></td>
                  <td><?= number_format((float)$it['line_total'],2) ?></td>
                </tr>
              <?php } ?>
            </tbody>
          </table>
          <hr>
          <div class="d-flex justify-content-between">
            <b>المجموع</b>
            <b><?= number_format((float)$sale['total'],2) ?> ل.س</b>
          </div>

          <div class="text-center mt-3">
            <div id="qrcode"></div>
            <div class="text-muted ua-u-small">QR: SALE-<?= (int)$sale['id'] ?></div>
          </div>
        </div>
      </div>
    </div>
  </section>
</div>

<?php echo ua_asset_tag_js('qrcode_js'); ?>
<script>
new QRCode(document.getElementById("qrcode"), {
  text: "SALE-<?= (int)$sale['id'] ?>",
  width: 128,
  height: 128
});
</script>
<?php require_once __DIR__ . '/output_devices_lib.php'; ua_output_print_bootstrap($conn, 'sales_receipt', (string)$id); ?>
<?php include __DIR__ . '/footer.php'; ?>
