<?php

declare(strict_types=1);

namespace prj\{{.Root}}\outputs\{{.Subfolder}};

use \bs_tts\safer_io as SafeIO;

class {{.File}}_out {
    
    public static function {{.Method}}(array & $input): array {
        $ret['model'] = $input['model'];
        unset($input['model']); // Free up some space
        
        $submitted = $input['submitted'] ?? false;
        
        if ($submitted) {
            $html_output = [];
            $errors = [];
            foreach(SafeIO::html_escape_and_sanitize($input) as $html) {
                $key = $html['name'] ?? "";
                $html_output[$key] = $html['html'];

                if (\bs_tts\common::get_count($html['errors'])) {
                    $errors[$key] = $html['errors'][$key];
                }
            }

            $age = $html_output['age'] ?? 0;        
            $first_name = $html_output['first_name'] ?? "Unknown";
            $last_name = $html_output['last_name'] ?? "Unknown";

            $ret['main'] = (\bs_tts\common::get_count($errors)) ? "" : "Hello, {$first_name} {$last_name}." . PHP_EOL;
            $ret['errors'] = $errors;
        } else {
            $ret['main'] = "Welcome";
            $ret['errors'] = [];
        }
        
        unset($input); // Free up some space
        
        return $ret;
    }

    public static function {{.Method}}_show_errors(array $errors): string {
        if ($errors) {
            $message = "Please correct the following errors: ";
            $i = 0;
            foreach($errors as $error) {
                $i++;
                $message .= "<br/>{$i}) " . $error;
            }
            return $message;
        }
        return ""; // No Errors to return here
    }

    public static function {{.Method}}_display(\Generator $input): void {
        if (! $input->current()) {
            return; // No Data, so Bail here
        }
        \tts\html::show_table_from_generator(
            ['ID', 'First Name', 'Last Name'],
            $input
        );
    }

}
