<?php
// File: API_KONEKSI/customer/get_promo.php
header('Content-Type: application/json');
include '../config/koneksi.php';

$response = array("status" => "gagal", "pesan" => "Terjadi kesalahan sistem", "data" => array());

try {
    // Mengambil data promo yang aktif, belum dihapus, dan belum kadaluarsa
    $query = "SELECT promo_id, promo_nm, kode_promo, tipe_potongan, potongan_nominal, min_pembelian, maks_potongan, selesai_tgl, instruksi_syarat, wajib_upload_st 
              FROM mst_promo 
              WHERE active_st = 1 AND deleted_st = 0 AND selesai_tgl >= CURDATE() 
              ORDER BY selesai_tgl ASC";
              
    $stmt = $conn->prepare($query);
    $stmt->execute();
    $result = $stmt->get_result();

    $data = array();
    while ($row = $result->fetch_assoc()) {
        $data[] = $row;
    }

    if (count($data) > 0) {
        $response["status"] = "sukses";
        $response["pesan"] = "Data promo berhasil diambil";
        $response["data"] = $data;
    } else {
        $response["status"] = "sukses";
        $response["pesan"] = "Tidak ada promo yang tersedia saat ini";
        $response["data"] = array();
    }

    $stmt->close();
} catch (Exception $e) {
    $response["pesan"] = "Error: " . $e->getMessage();
}

echo json_encode($response);
$conn->close();
?>