<?php
  /**
   * Common control library 0.1 for Conjuro.
   */
  return [
    new class("html") extends Control{
      protected function renderControl($meta,$data) {
        echo $meta["html"];
      }
    },
    new class("img") extends Control{
      protected function renderControl($meta,$data) {
        c()->rhtml(
          'img',
          ['id'=>$meta['id'],'class'=>$meta['class'],'src'=>$meta['src']]
        );
      }
    },
    new class("builder") extends Control{
      protected function renderControl($meta,$data) {
        include "Conjuro/Builder.php";
      }
    },
    new class("form") extends Control{
      protected function renderControl($meta,$data) {
        $cols=$meta['columns'];
        c()->rhtml(
          'form',
          ['id'=>$meta['id'],'class'=>$meta['class'],'action'=>$meta['action'],'method'=>$meta['method']],
          fn()=>c()->rhtml(
            'table',
            ['class'=>'form-body-table'],
            function() use($meta,$data,$cols){
              echo '<tr>';
              $col=0;
              foreach($meta['childs'] as $child){
                c()->renderContent($child,$data);
                if(++$col>=$cols){
                  echo '</tr><tr>';
                }
              }
              if ($meta['accept']||$meta['reset']){ //Buttons}
                if ($col!=0)
                  echo '</tr><tr>';
                echo '<td colspan='.($cols*2).' class="form-body-buttons">';
                if ($meta['accept'])
                  c()->rhtml('input',['type'=>'submit','value'=>$meta['accept']]);
                if ($meta['reset'])
                  c()->rhtml('input',['type'=>'reset','value'=>$meta['reset']]);
                echo '</td>';
            }
              echo '</tr>';
            }
          )
        );
      }
    },
    new class("form-data") extends Control{
      protected function renderControl($meta,$data) {
        if ($meta['id']){
          $lid=$meta["id"].'-label';
          $vid=$meta["id"].'-value';
        }
        c()->rhtml('td',['id'=>$lid,'class'=>'form-data-label '.$meta["class"]],fn()=>print(htmlentities($meta["label"])));
        c()->rhtml('td',['id'=>$vid,'class'=>'form-data-value '.$meta["class"]],function() use($meta,$data){
          $type=$meta['type'];
          if (!$type)
            $type='input';
          switch($type){
            case 'input':
              echo c()->htmlopen('input',['id'=>$meta['id'],'name'=>$meta['id']]);
              break;
            case 'password':
              echo c()->htmlopen('input',['id'=>$meta['id'],'name'=>$meta['id'],'type'=>'password']);
              break;
            }
        });
      }
    },
    new class("jump") extends Control{
      protected function renderControl($meta,$data) {
        $ctt=$meta["height"];
        echo "<div style='height:$ctt'></div>";
      }
    }
  ];
?>