<?PHP

function sortByColumn($url, $get, $sortBy)
// Fonction pour trier un tableau
{
	$url = $url . "?";
	foreach ($get as $param => $value) {
		if ($param != 'sortBy' & $param != 'sortDir' &  !empty($value))
			$url = $url . $param . "=" . urlencode($value) . "&";
	}

	echo "&nbsp;&nbsp;";
	if ($get['sortBy'] == $sortBy) {
		if ($get['sortDir'] == 'ASC') {
			echo "<a href=" . $url . "sortBy=$sortBy&sortDir=DESC>";
			echo "<span class='fas fa-sort-up'></span>";
			echo "</a>";
		} elseif ($get['sortDir'] == 'DESC') {
			echo "<a href=" . $url . "sortBy=$sortBy&sortDir=ASC>";
			echo "<span class='fas fa-sort-down'></span>";
			echo "</a>";
		}
	} else {
		echo "<a href=" . $url . "sortBy=$sortBy&sortDir=ASC>";
		echo "<span class='fas fa-sort text-muted'></span>";
		echo "</a>";
	}
}
function dropDownList($conn, $name, $tableName, $primaryKey, $columnName, $selected, $attr = '', $condition = '')
{
	$sql = "SELECT $primaryKey, $columnName FROM $tableName ";
	if ($condition != '')
		$sql .= 'WHERE ' . $condition;

	$sql .= " ORDER BY $columnName ASC";

	$conn->exec($sql);
	$str = "<select name=$name $attr><option value=''></option>";
	foreach ($conn->data as $row) {
		if ($row[$primaryKey] == $selected)
			$tmp = "<option selected='selected' value='$row[$primaryKey]'>$row[$columnName]</option>";
		else
			$tmp = "<option value='$row[$primaryKey]'>$row[$columnName]</option>";
		$str = $str . $tmp;
	}
	$str = $str . "</select>";
	echo $str;
}
function dropDownListMulti($conn, $name, $tableName, $primaryKey, $columnName, $selected, $charSelected, $attr = '', $condition = '')
{
	$tabSelection = explode($charSelected, $selected);


	$sql = "SELECT $primaryKey, $columnName FROM $tableName ";
	if ($condition != '')
		$sql .= 'WHERE ' . $condition;

	$sql .= " ORDER BY $columnName ASC";

	$conn->exec($sql);
	$str = "<select name=$name $attr multiple style='min-height:150px'><option value=''></option>";
	foreach ($conn->data as $row) {
		$selection = false;
		foreach ($tabSelection as $r) {

			if ($row[$primaryKey] == $r) {
				$selection = true;
				break;
			}
		}
		if ($selection)
			$tmp = "<option selected='selected' value='$row[$primaryKey]'>$row[$columnName]</option>";
		else
			$tmp = "<option value='$row[$primaryKey]'>$row[$columnName]</option>";
		$str = $str . $tmp;
	}
	$str = $str . "</select>";
	echo $str;
}
function formatDateWithOur($date) {
    if ($date === "0000-00-00 00:00:00" || empty($date)) {
        return ""; // Don't display anything if it's an empty or invalid date
    }
    return date("Y-m-d", strtotime($date)); // Extract only the date part
}
function searchCompanies($conn)
{
	$sql = "SELECT id_fournisseur, nom_compagnie FROM fournisseur ";

	$conn->exec($sql);
	$str = '<select class="form-control capitalize selectpicker" id="select-country companyName" name="companyName" data-live-search="true"><option value=""></option>';
	foreach ($conn->data as $row) {
		$str .= '<option selected="selected" value="' . $row["id_fournisseur"] . '">' . $row["nom_compagnie"] . '</option>';
	}
	$str .= '</select>';
	echo $str;
}


?>
