<?php
include 'renderer.php';

class Members extends Renderer {
    public function __construct(?string $templateName = null) {
        parent::__construct($templateName);
        $result = mysqli_query($this->dbConnection, 
                'SELECT c.*, cs.`status_text`, (SELECT ph.`payment_date` FROM `paying_history` ph WHERE ph.`clubmember_id` = c.`id` ORDER BY `payment_date` DESC LIMIT 1) as last_payment '
                . 'FROM `clubmember` c '
                . 'JOIN `clubmember_status` cs '
                . '  ON cs.`id` = c.`membership_status` '
                . 'WHERE cs.`status_text` NOT IN ("Mitgliedschaft abgelehnt", "Mitgliedschaft gekündigt") '
                . 'ORDER BY c.`last_name`, c.`first_name`');
        $tableBody = '<tbody>';
        while ($row = mysqli_fetch_assoc($result)) {
            $tableBody .= '<tr>'
                    . '<td>' . $row['id'] . '</td>'
                    . '<td>' . $this->decode($row['last_name'], $row['salt']) . ', ' . $this->decode($row['first_name'], $row['salt']) . '</td>'
                    . '<td>' . $this->decode($row['email'], $row['salt']) . '</td>'
                    . '<td>' . $row['last_payment'] . '</td>'
                    . '<td>' . $row['membership_start'] . '</td>'
                    . '<td>' . $row['status_text'] . '</td>'
                    . '<td><a href="editmember?id=' . $row['id'] . '">Bearbeiten</a>&nbsp;'
                    . '<a href="payings?id=' . $row['id'] . '">Zahlungen</a></td>' 
                    . '</tr>';
        }
        $tableBody .= '</tbody>';
        $this->content['memberlist'] = $tableBody;
    }
}
