<?php
namespace App\Admin\Extensions\Field;

use Encore\Admin\Admin;
use Encore\Admin\Form;
use Encore\Admin\Form\Field;
use Illuminate\Support\Collection;

/**
 * Class Template
 * 表单模板
 * @method Field\Text           text($column, $label = '')
 * @method Field\Checkbox       checkbox($column, $label = '')
 * @method Field\Radio          radio($column, $label = '')
 * @method Field\Select         select($column, $label = '')
 * @method Field\MultipleSelect multipleSelect($column, $label = '')
 * @method Field\Textarea       textarea($column, $label = '')
 * @method Field\Hidden         hidden($column, $label = '')
 * @method Field\Id             id($column, $label = '')
 * @method Field\Ip             ip($column, $label = '')
 * @method Field\Url            url($column, $label = '')
 * @method Field\Color          color($column, $label = '')
 * @method Field\Email          email($column, $label = '')
 * @method Field\Mobile         mobile($column, $label = '')
 * @method Field\Slider         slider($column, $label = '')
 * @method Field\Map            map($latitude, $longitude, $label = '')
 * @method Field\Editor         editor($column, $label = '')
 * @method Field\File           file($column, $label = '')
 * @method Field\Image          image($column, $label = '')
 * @method Field\Date           date($column, $label = '')
 * @method Field\Datetime       datetime($column, $label = '')
 * @method Field\Time           time($column, $label = '')
 * @method Field\Year           year($column, $label = '')
 * @method Field\Month          month($column, $label = '')
 * @method Field\DateRange      dateRange($start, $end, $label = '')
 * @method Field\DateTimeRange  datetimeRange($start, $end, $label = '')
 * @method Field\TimeRange      timeRange($start, $end, $label = '')
 * @method Field\Number         number($column, $label = '')
 * @method Field\Currency       currency($column, $label = '')
 * @method Field\HasMany        hasMany($relationName, $callback)
 * @method Field\SwitchField    switch($column, $label = '')
 * @method Field\Display        display($column, $label = '')
 * @method Field\Rate           rate($column, $label = '')
 * @method Field\Divide         divider()
 * @method Field\Password       password($column, $label = '')
 * @method Field\Decimal        decimal($column, $label = '')
 * @method Field\Html           html($html, $label = '')
 * @method Field\Tags           tags($column, $label = '')
 * @method Field\Icon           icon($column, $label = '')
 * @method Field\Embeds         embeds($column, $label = '')
 */
class Template
{
    /**
     * 对应表单
     * @var Collection
     */
    protected $templates;

    protected $nowId;

    public function __construct()
    {
        $this->templates=new Collection();
    }

    public function setTemplatesId($id)
    {
        $this->nowId=$id;
        if (!$this->templates->has($id)) {
            $this->templates->put($id, new Collection());
        }
        return $this;
    }

    protected function pushField(Field $field)
    {
        $this->templates->get($this->nowId)->push($field);
    }

    protected function getHtmlAndScript()
    {
        $htmls = [];
        $scripts = [];

        /* @var Field $field */
        foreach ($this->templates as $key => $template) {
            $html='';
            $script=[];
            foreach ($template as $field) {
                //when field render, will push $script to Admin
                $html.= $field->render();
                /*
                 * Get and remove the last script of Admin::$script stack.
                 */
                if ($field->getScript()) {
                    $script[] = array_pop(Admin::$script);
                }
            }
            $htmls[$key]=$html;
            $scripts[$key]=implode("\r\n", $script);
        }

        return [$htmls, $scripts];
    }

    /**
     * Add nested-form fields dynamically.
     *
     * @param string $method
     * @param array  $arguments
     *
     * @return mixed
     */
    public function __call($method, $arguments)
    {
        if ($className = Form::findFieldClass($method)) {
            $column = array_get($arguments, 0, '');

            /* @var Field $field */
            $field = new $className($column, array_slice($arguments, 1));

            $this->pushField($field);

            return $field;
        }

        return $this;
    }

    public function render()
    {
        list($template, $script) = $this->getHtmlAndScript();

        return view('admin.field.template')->with('templates', $template)->with('scripts', $script)->render();
    }

}