<?php
require_once("dbconn.php");
require_once("phpFunctions.php");

$customerName = isset($_POST["drsi_customer_name"]) ? $_POST["drsi_customer_name"] : null;

$result = executeQuery($conn, "SELECT pl_effectivity_date FROM pricelist WHERE pl_customer_name = ? AND is_deleted = FALSE AND is_current = TRUE ORDER BY pl_effectivity_date DESC", "s", [$customerName]);

if ($result === false) { 
    throw new Exception("Error executing query: " . $conn->error);
} 

$options = '<option value="" disabled selected>SELECT</option>';
$latestDate = null;

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $latestDate = $row["pl_effectivity_date"];
    $options .= '<option value="' . $latestDate . '" selected>' . $latestDate . '</option>';
    
    while ($row = $result->fetch_assoc()) {
        $options .= '<option value="' . $row['pl_effectivity_date'] . '">' . $row['pl_effectivity_date'] . '</option>';
    }
}

echo $options;
?>