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

namespace Modules\Admin\Auxiliary\View;


use Closure;

class TreeAuxiliary
{
    /**
     * 增删查改前缀路由
     * @var string
     */
    public $path = '';

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

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

    /**
     * 顶部操作按钮
     * @var array
     */
    public $topActions = [];

    /**
     * 头部自定义按钮
     * $displayActionOthers = [
     *      [
     *          'name' => '撤销',
     *          'path' => 'order/list/check',
     *          'class' => 'layui-btn-normal',
     *          'isShow' => function($item) {},
     *          'isJump' => 1,
     *      ]
     * ];
     * @var null
     */
    public $displayActionOthers = null;

    /**
     * 显示操作按钮定义
     * @var array
     */
    public $actionBtns = ['edit', 'del'];

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

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

    /**
     * 显示字段
     * @param              $varName
     * @param string       $showName
     * @param Closure|null $callback
     * @return $this
     */
    public function column($varName, $showName = '', Closure $callback = null)
    {
        $this->columns[$varName]['name'] = $showName;
        $this->currentColumn             = $varName;  // 记录当前字段
        if (isset($callback)) {
            $this->display($callback);
        }
        return $this;
    }

    /**
     * 处理字段结果
     * @param Closure $callback
     * @return $this
     */
    public function display(Closure $callback)
    {
        $this->columns[$this->currentColumn]['value'] = $callback;
        return $this;
    }

}
