<?php
  $page_title = 'Daftar Penjualan';
  require_once('includes/load.php');
  // Level pengguna yang diizinkan untuk melihat halaman ini
  page_require_level(5);

  /**
   * Fungsi untuk menemukan semua data penjualan.
   * Fungsi ini digabungkan langsung ke dalam file sales.php
   * untuk kemudahan.
   */
  function find_all_sales_local() {
    global $db;
    $sql = "SELECT s.id, s.qty, s.price, s.total_price, s.date,
            p.name AS product_name, c.name AS customer_name
            FROM sales s
            LEFT JOIN products p ON s.product_id = p.id
            LEFT JOIN customers c ON s.customer_id = c.id
            ORDER BY s.date DESC";
    return find_by_sql($sql);
  }

  // Ambil semua data penjualan dari database menggunakan fungsi lokal
  $sales = find_all_sales_local();
?>

<?php include_once('layouts/header.php'); ?>

<div class="row">
  <div class="col-md-12">
    <?php echo display_msg($msg); ?>
  </div>
</div>
<div class="row">
  <div class="col-md-12">
    <div class="panel panel-default">
      <div class="panel-heading clearfix">
        <strong>
          <span class="glyphicon glyphicon-th"></span>
          <span>Daftar Penjualan</span>
        </strong>
        <a href="add_sale.php" class="btn btn-info pull-right">Tambah Penjualan</a>
      </div>
      <div class="panel-body">
        <table class="table table-bordered table-striped">
          <thead>
            <tr>
              <th class="text-center" style="width: 50px;">#</th>
              <th>Nama Produk</th>
              <th>Nama Pelanggan</th>
              <th class="text-center" style="width: 10%;">Jumlah</th>
              <th class="text-center" style="width: 15%;">Harga Satuan</th>
              <th class="text-center" style="width: 15%;">Total Harga</th>
              <th class="text-center" style="width: 15%;">Tanggal</th>
              <th class="text-center" style="width: 100px;">Aksi</th>
            </tr>
          </thead>
          <tbody>
            <?php if (!empty($sales)): ?>
              <?php foreach ($sales as $sale) : ?>
                <tr>
                  <td class="text-center"><?php echo (int)$sale['id']; ?></td>
                  <td><?php echo remove_junk($sale['product_name']); ?></td>
                  <td><?php echo remove_junk($sale['customer_name']); ?></td>
                  <td class="text-center"><?php echo (int)$sale['qty']; ?></td>
                  <td class="text-center">Rp <?php echo number_format($sale['price'], 2, ',', '.'); ?></td>
                  <td class="text-center">Rp <?php echo number_format($sale['total_price'], 2, ',', '.'); ?></td>
                  <td class="text-center"><?php echo read_date($sale['date']); ?></td>
                  <td class="text-center">
                    <div class="btn-group">
                      <a href="edit_sale.php?id=<?php echo (int)$sale['id'];?>" class="btn btn-warning btn-xs" title="Edit" data-toggle="tooltip">
                        <span class="glyphicon glyphicon-edit"></span>
                      </a>
                      <a href="delete_sale.php?id=<?php echo (int)$sale['id'];?>" class="btn btn-danger btn-xs" title="Hapus" data-toggle="tooltip" onclick="return confirm('Apakah Anda yakin ingin menghapus penjualan ini?');">
                        <span class="glyphicon glyphicon-trash"></span>
                      </a>
                    </div>
                  </td>
                </tr>
              <?php endforeach; ?>
            <?php else: ?>
              <tr>
                <td colspan="8" class="text-center">Tidak ada data penjualan ditemukan.</td>
              </tr>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

<?php include_once('layouts/footer.php'); ?>