<?php
if ($member == NULL)
{
//  echo "<span style=\"color: red;\">Your session has expired!</span><br />Please log in to be able to download your wordlist<br />and access dynamic links in the left column.";
}
else
{

$file_payment_and_attendance_costs2 = "tmp_files/payment_and_attendance_costs2_$member.csv";

if (file_exists($file_payment_and_attendance_costs2 )) {
$file_payment_and_attendance_costs2 = fopen($file_payment_and_attendance_costs2, "r");

// empty array before each loop or will add up
 $csv = array();

while(!feof($file_payment_and_attendance_costs2)) {
  // [] mandatory
    $csv[] = fgetcsv($file_payment_and_attendance_costs2,1024,";");
//    var_dump ($csv[0][4]);
}

$total_ach = array_sum(array_column($csv,'2'));
$total_cost = array_sum(array_column($csv,'4'));
$total_paid = array_sum(array_column($csv,'5'));
$final_balance = $total_cost + $total_paid;

$session_rate_for_attendance_history = (-$total_cost) / $total_ach; // approximate bc actually depends if f2f or on-line
$final_balance_ach = $final_balance / $session_rate_for_attendance_history;

echo "<div class=\"popupPh\" onclick=\"popupFunctionPh()\">";
echo "<img src=\"pix/icons8-payment-history-100.png\" width=\"35\" border=\"0\" alt=\"payment history\">";

echo "<div class=\"popuptextPh\" id=\"myPopupPh\">";

echo "total ach: ".$total_ach."<br />";
echo "total cost: ".$total_cost."<br />";
echo "total paid: ".$total_paid."<br />";
echo "final balance: ".$final_balance."<br />";
echo "lessons left (approx ach): ".$final_balance_ach."<br />";
echo "<a download class=\"clog\" target=\"about_blank\" href=\"tmp_files/payment_and_attendance_costs2_$member.csv\"><img src=\"pix/libre_office_calc01.png\" width=\"20\" height=\"20\" alt=\"logo\"> payment history</a>";

echo "</div><!-- close popuptext -->";
echo "</div><!-- close popup -->";

}// if (file_exists($file_payment_and_attendance_costs2 ))

else
{
  echo "Please, reload page to access payment history\n"; // needed when run for the 1st time
}

} //if ($member == NULL)

?>

<script>
// https://www.w3schools.com/howto/howto_js_popup.asp
// When the user clicks on <div>, open the popup
function popupFunctionPh() {
  var popupPh = document.getElementById("myPopupPh");
  popup.classList.toggle("showPh");
}
</script>
