<?php
  /**
   * Common control library 0.1 for Conjuro.
   */
  return [
    new class('data') extends Control{
      protected function getControlPlaceholders(){
        return [];
      }

      protected function renderControl($meta,$data) {
        ?>
        <span id="<?=$meta['id']?>" class="<?=$meta['id']?>"><?=htmlentities($data[$meta['key']])?></span>
        <?php
      }
    },
    new class('html') extends Control{
      protected function getControlPlaceholders(){
        return [];
      }

      protected function renderControl($meta,$data) {
        echo $meta['html'];
      }
    },
    new class('img') extends Control{
      protected function getControlPlaceholders(){
        return [];
      }

      protected function renderControl($meta,$data) {
        c()->rhtml(
          'img',
          ['id'=>$meta['id'],'class'=>$meta['class'],'src'=>$meta['src']]
        );
      }
    },
    new class('form') extends Control{
      protected function getControlPlaceholders(){
        return [];
      }

      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['content'] 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 getControlPlaceholders(){
        return [];
      }

      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 getControlPlaceholders(){
        return [];
      }

      protected function renderControl($meta,$data) {
        $ctt=$meta["height"];
        echo "<div style='height:$ctt'></div>";
      }
    },
    new class('icon') extends Control{
      protected function getControlPlaceholders(){
        return [];
      }

      protected function renderControl($meta,$data) {
        ?><i class="<?=$meta['class']?> fa-solid fa-<?=$meta['icon']?>"></i><?php
      }
    },
    new class('table') extends Control{
      protected function getControlPlaceholders(){
        return [];
      }

      protected function renderControl($meta,$data) {
        ?><table class="tbl <?=$meta['class']?>"><tr><?php
        ?></tr></table><?php
      }
    },
    new class('list') extends Control{
      protected function getControlPlaceholders(){
        return [];
      }

      protected function renderControl($meta,$data) {
        $itemData=$data[$meta['data']];
        $itemTemplate=$meta['item'];
        $headerTemplate=$meta['header'];
        if (!$itemData)
          $itemData=[];
        ?>
          <ul class="list <?=$meta['class']?>">
            <?php
              foreach($itemData as $item){
                $data['item']=$item;
                c()->renderContent($itemTemplate,$data);
              }
            ?>
          </ul>
        <?php
      }
    },
    new class('list-item') extends Control{
      protected function getControlPlaceholders(){
        return [];
      }
      
      protected function renderControl($meta,$data) {
        $itemData=$data['item'];
        if (!$itemData)
          $itemData=[];
        $link=$meta['link'];
        if ($link){
          if ($link[0]=='&')
            $link=substr($link,1);
          if (($link.'?')[0]!='?')
            $link="?$link";
          $link.='&';
        }else
          $link='?';
        $linkdata=null;
        $linkname;
        if (isset($meta['link-field'])){
          $linkname=$meta['link-field'];
          $linkdata=$itemData[$meta['link-field']];
        }else if (count($itemData)>0){
          $linkname='data';
          $linkdata=$itemData;
        }
        if($linkdata){
          if (is_object($linkdata) || is_array($linkdata))
            $linkdata=urlencode(json_encode($linkdata));
          else
            $linkdata=urlencode($linkdata);
          $link.="${linkname}=${linkdata}";
        }
        ?>
          <a href="<?=$link?>"><li class="list-item <?=$meta['class']?>"><?=$itemData[$meta['field-title']]?></li></a>
        <?php
      }
    },
  ];
?>