<?php
include 'renderer.php';

class Payings extends Renderer {
//        ['label' => 'Nachname', 'type' => 'text', 'size' => 50, 'name' => 'lastname', 'combine_with_next_line' => false, 'filter' => FILTER_SANITIZE_STRING, 'dbfield' => 'last_name', 'optional' => false],
    protected array $formFields = [
        ['label' => 'Datum', 'type' => 'date', 'size' => 10, 'name' => 'date', 'combine_with_next_line' => false, 
            'filter' => FILTER_SANITIZE_STRING, 'dbfield' => 'payment_date', 'optional' => false, 'encryption' => false],
        ['label' => 'Betrag', 'type' => 'number', 'size' => 10, 'name' => 'value', 'combine_with_next_line' => false, 
            'filter' => FILTER_SANITIZE_STRING, 'dbfield' => 'payment_height', 'optional' => false, 'encryption' => false],
        ['label' => '', 'type' => 'hidden', 'size'=> 1, 'name' => 'clubmember-id', 'value' => 0, 'combine_with_next_line' => false, 
            'filter' => FILTER_SANITIZE_NUMBER_INT, 'dbfield' => 'clubmember_id', 'optional' => false, 'encryption' => false],
    ];
    protected string $formSendButtonLabel = 'Zahlung eintragen';
    protected string $dbTable = 'paying_history';

    public function __construct(?string $templateName = null) {
        parent::__construct($templateName);
        $id = $this->getUriParams()['id'];
        $this->formFields[2]['value'] = $id;
        $this->loadUserData($id);
    }
    
    protected function loadUserData(string $id): void {
        $query = sprintf("SELECT c.first_name, c.last_name, c.salt "
                        . "FROM clubmember c  "
                        . "WHERE c.id = %d", $id);
        $dbResult = mysqli_query($this->dbConnection, $query);
        if (mysqli_num_rows($dbResult) === 0) {
            $this->templateName = 'notfound_error';
            return;
        }
        $line = mysqli_fetch_assoc($dbResult);
        $salt = $line['salt'];
        $this->content['member-name'] = $this->decode($line['last_name'], $salt) . ', ' . $this->decode($line['first_name'], $salt);
        $this->predefines = $line;
    }
    
    protected function generateContent(): void {
        $this->loadPayHistory($this->getUriParams()['id']);
    }

    protected function loadPayHistory(string $id): void {
        $query = sprintf("SELECT ph.payment_date, ph.payment_height, u.realname, u.salt  "
                        . "FROM paying_history ph  "
                        . "JOIN `user` u  "
                        . "  ON u.id = ph.registered_by_id  "
                        . "WHERE ph.clubmember_id = %d "
                        . "ORDER BY payment_date DESC", $id);
        $dbResult = mysqli_query($this->dbConnection, $query);
        $tableBody = '';
        while ($row = mysqli_fetch_assoc($dbResult)) {
            $tableBody .= '<tr>';
            $tableBody .= '<td>' . $row['payment_date'] . '</td>';
            $tableBody .= '<td>' . $row['payment_height'] . '</td>';
            $tableBody .= '<td>' . $this->decode($row['realname'], $row['salt']) . '</td>';
            $tableBody .= '</tr>';
        }
        $this->content['payings'] = $tableBody;
    }

    protected function formAction(): void {
        if (!$this->formCheckFields()) {
            return;
        }
        $this->saveToDb();
        $this->cleanFields = true;
    }

    protected function createDbKeyValues(): array {
        $data = parent::createDbKeyValues();
        $data['keys'][] = '`registered_by_id`';
        $data['values'][] = $_SESSION['userid'];
        return $data;
    }
}
