<?php
/**
 * Created by PhpStorm.
 * User: maliang
 * Date: 2019-03-30
 * Time: 14:29
 */

namespace Modules\Admin\Auxiliary\View;


class FromAuxiliary
{
    /**
     * 提交路由
     * @var string
     */
    public $path = '';

    /**
     * 表单标题
     * @var string
     */
    public $title = '表单';

    /**
     * 显示数据
     * @var
     */
    public $item;

    /**
     * 主视图显示字段
     * @var
     */
    public $columns = [];

    /**
     * 标题宽度
     * @var int
     */
    public $colWidth = 80;
    /**
     * 主键
     * @var string
     */
    public $primaryKey = 'id';

    /**
     * 当前字段
     * @var string
     */
    protected $currentColumn = '';

    /**
     * 底部显示按钮
     * @var array
     */
    public $footBtns = ['goback', 'save'];

    /**
     * 是否有分割线
     * @var bool
     */
    public $hasHr = false;

    public function __construct($path = '', $item = '')
    {
        $this->item = $item;
        $this->path = $path;
    }

    /**
     * 设置默认值
     * @param $default
     * @return $this
     */
    public function default($default)
    {
        $this->columns[$this->currentColumn]['default'] = $default;
        return $this;
    }

    /**
     * 设置提示语
     * @param $placeholder
     * @return $this
     */
    public function placeholder($placeholder)
    {
        $this->columns[$this->currentColumn]['placeholder'] = $placeholder;
        return $this;
    }

    public function text($varName, $label, $default = '')
    {
        $arguments = [$varName, $label, $default, ''];
        return $this->method('text', $arguments);
    }

    public function input($varName, $label, $default = '', $placeholder = '')
    {
        $arguments = [$varName, $label, $default, $placeholder];
        return $this->method('input', $arguments);
    }

    public function password($varName, $label, $default = '', $placeholder = '')
    {
        $arguments = [$varName, $label, $default, $placeholder];
        return $this->method('password', $arguments);
    }

    public function textarea($varName, $label, $default = '', $placeholder = '')
    {
        $arguments = [$varName, $label, $default, $placeholder];
        return $this->method('textarea', $arguments);
    }

    public function icon($varName, $label, $default = '', $placeholder = '')
    {
        $arguments = [$varName, $label, $default, $placeholder];
        return $this->method('icon', $arguments);
    }

    public function date($varName, $label, $default = '', $placeholder = '')
    {
        $arguments = [$varName, $label, $default, $placeholder];
        return $this->method('date', $arguments);
    }

    public function dateMonth($varName, $label, $default = '', $placeholder = '')
    {
        $arguments = [$varName, $label, $default, $placeholder];
        return $this->method('dateMonth', $arguments);
    }

    public function dateYear($varName, $label, $default = '', $placeholder = '')
    {
        $arguments = [$varName, $label, $default, $placeholder];
        return $this->method('dateYear', $arguments);
    }

    public function dateTime($varName, $label, $default = '', $placeholder = '')
    {
        $arguments = [$varName, $label, $default, $placeholder];
        return $this->method('dateTime', $arguments);
    }

    public function time($varName, $label, $default = '', $placeholder = '')
    {
        $arguments = [$varName, $label, $default, $placeholder];
        return $this->method('time', $arguments);
    }

    public function radio($varName, $label, $options, $default = '')
    {
        $arguments = [$varName, $label, $options, $default];
        return $this->method('radio', $arguments);
    }

    public function checkbox($varName, $label, $options, $default = '')
    {
        $arguments = [$varName, $label, $options, $default];
        return $this->method('checkbox', $arguments);
    }

    public function select($varName, $label, $options, $default = '')
    {
        $arguments = [$varName, $label, $options, $default];
        return $this->method('select', $arguments);
    }

    public function xselect($varName, $label, $options, $default = '')
    {
        $arguments = [$varName, $label, $options, $default];
        return $this->method('xselect', $arguments);
    }

    public function sselect($varName, $label, $options, $default = '')
    {
        $arguments = [$varName, $label, $options, $default];
        return $this->method('sselect', $arguments);
    }

    public function mselect($varName, $label, $options, $default = '')
    {
        $arguments = [$varName, $label, $options, $default];
        return $this->method('mselect', $arguments);
    }

    public function tselect($varName, $label, $options, $default = '')
    {
        $arguments = [$varName, $label, $options, $default];
        return $this->method('tselect', $arguments);
    }

    public function trselect($varName, $label, $options, $default = '')
    {
        $arguments = [$varName, $label, $options, $default];
        return $this->method('trselect', $arguments);
    }

    public function trmselect($varName, $label, $options, $default = '')
    {
        $arguments = [$varName, $label, $options, $default];
        return $this->method('trmselect', $arguments);
    }

    public function tree_select($varName, $label, $options, $default = '', $isShowNull = true)
    {
        $arguments = [$varName, $label, $options, $default, $isShowNull];
        return $this->method('tree_select', $arguments);
    }

    public function uploadImg($varName, $label, $uploadURLPath, $default = '')
    {
        $arguments = [$varName, $label, $uploadURLPath, $default];
        return $this->method('uploadImg', $arguments);
    }

    public function uploadFile($varName, $label, $uploadURLPath)
    {
        $arguments = [$varName, $label, $uploadURLPath, ''];
        return $this->method('uploadFile', $arguments);
    }

    public function uploadImgs($varName, $label, $uploadURLPath, $default = '')
    {
        $arguments = [$varName, $label, $uploadURLPath, $default];
        return $this->method('uploadImgs', $arguments);
    }

    public function uploadFiles($varName, $label, $uploadURLPath, $default = '')
    {
        $arguments = [$varName, $label, $uploadURLPath, $default];
        return $this->method('uploadFiles', $arguments);
    }

    public function hidden($varName, $value, $default = '')
    {
        $arguments = [$varName, $value, $default];
        return $this->method('hidden', $arguments);
    }

    public function editor($varName, $label, $default = '', $uploadURLPath = '')
    {
        $arguments = [$varName, $label, $uploadURLPath, $default];
        return $this->method('editor', $arguments);
    }

    public function includeBlock($varName, $label, $itemValue, $path)
    {
        $arguments = [$varName, $label, $itemValue, $path];
        return $this->method('includeBlock', $arguments);
    }

    /**
     * 调用方法
     * @param $method
     * @param $arguments
     * @return FromAuxiliary
     */
    protected function method($method, $arguments)
    {
        $this->columns[$arguments[0]]['type'] = $method;
        /**
         * $arguments[0]  $varName
         * $arguments[1]  $label
         * $arguments[2]  $default = ''
         * $arguments[3]  $placeholder = ''
         */
        if (in_array($method, ['text', 'input', 'password', 'textarea', 'icon', 'date', 'dateMonth', 'dateYear', 'dateTime', 'time', 'hidden'])) {
            $var = $this->columns[$arguments[0]]['placeholder'] = $arguments[3] ?? null;
            return $this->baseColumn($arguments[0], $arguments[1], $arguments[2] ?? '');
        }

        /**
         * $arguments[0]  $varName
         * $arguments[1]  $label
         * $arguments[2]  $uploadURLPath
         * $arguments[3]  $default = ''
         */
        if (in_array($method, ['uploadImg', 'uploadFile', 'uploadImgs', 'uploadFiles', 'editor'])) {
            $this->columns[$arguments[0]]['uploadURLPath'] = $arguments[2] ?? null;
            return $this->baseColumn($arguments[0], $arguments[1], $arguments[3] ?? '');
        }

        /**
         * $arguments[0]  $varName
         * $arguments[1]  $label
         * $arguments[2]  $options
         * $arguments[3]  $default = ''
         */
        if (in_array($method, ['radio', 'checkbox', 'trselect', 'trmselect', 'tselect', 'xselect', 'sselect', 'mselect', 'select'])) {
            $this->columns[$arguments[0]]['options'] = $arguments[2] ?? [];
            return $this->baseColumn($arguments[0], $arguments[1], $arguments[3] ?? '');
        }

        /**
         * $arguments[0]  $varName
         * $arguments[1]  $label
         * $arguments[2]  $options
         * $arguments[3]  $default = ''
         * $arguments[4]  $isShow = ''
         */
        if (in_array($method, ['tree_select'])) {
            $this->columns[$arguments[0]]['options'] = $arguments[2] ?? [];
            $col                                     = $this->treeSelectColumn($arguments[0], $arguments[1], $arguments[3] ?? '', $arguments[4]);
            return $col;
        }

        /**
         * $arguments[0]  $varName
         * $arguments[1]  $label
         * $arguments[2]  $itemValue
         * $arguments[3]  $default = ''
         */
        if (in_array($method, ['includeBlock'])) {
            $this->columns[$arguments[0]]['itemValue'] = $arguments[2] ?? [];
            $this->columns[$arguments[0]]['path']      = $arguments[3] ?? '';
            return $this->baseColumn($arguments[0], $arguments[1], '');
        }


    }

    protected function baseColumn($varName, $label, $default = '')
    {
        $this->columns[$varName]['value']   = $this->item[$varName] ?? '';
        $this->columns[$varName]['label']   = $label;
        $this->columns[$varName]['default'] = $default ?? '';
        $this->currentColumn                = $varName;
        return $this;
    }

    protected function treeSelectColumn($varName, $label, $default = '', $isShowNull = true)
    {
        $this->columns[$varName]['value']      = $this->item[$varName] ?? '';
        $this->columns[$varName]['label']      = $label;
        $this->columns[$varName]['default']    = $default ?? '';
        $this->columns[$varName]['isShowNull'] = $isShowNull ?? true;
        $this->currentColumn                   = $varName;
        return $this;
    }

}
