<?php

declare(strict_types=1);

namespace prj\mockup\controllers\app;

class home_ctrl {
    
  public function index() {
      $html = new \tts\html_document();
      $html->set_author("Robert Strutts, plus ME!");

      $view = new \tts\view();
      $view->set('html', $html);
      $view->set_template('main');
      $view->include("app/header");
      $view->include("app/footer");
      $view->render($this, "app/home_index");
  }
    
  public function name_demo() {
      $view = new \tts\view();
      $view->set('html', new \tts\html_document());
      $view->set_template('main');
      $view->include("app/name_demo");
      $view->render($this);
  }
  
  public function name_demo_post() {      
      $input = \prj\mockup\inputs\app\home_in::name_demo();

      \tts\main\registry::set('db', \tts\main\registry::get('di')->get_service('db_mocker') ); // Grabs DB
      $model = new \prj\mockup\models\app\home_model(\tts\main\registry::get('db')); 
      $model->init_name_demo_table(); // Create Table if NOT exists!
      if ($model->is_valid($input)) {
        $model->populate(100); // INSERT 100 random rows of data
      }
      
      $success = $model->save_new_user($input); // Save user from Users Request Data
      // Only show users 12, if added new one
      $input['model'] = ($success===true) ? 
          $model->get_users(101) : 
          $model->get_users(0);

      $output = \prj\mockup\outputs\app\home_out::name_demo($input);
      
      $view = new \tts\view();
      $view->set('html', new \tts\html_document());
      $view->set('output', $output);
      $view->set_template('main');
      $view->include("app/name_demo_post");
      $view->render($this);
  }
  
  public function test($id) {
      \tts\common::real_time_output();
      
      echo "bla $id";
      echo PHP_EOL;
      
      for ($i=0; $i<10;$i++){
        echo $i;
        echo str_pad('', 14960);
        sleep(1);
      }
  }
  
  public function error() { tts_broken_error(); } // Needed: To show Live Page for Broken Pages

}