<?php
declare (strict_types = 1);

namespace <#namespace#>;

use app\common\controller\Backend;
use think\annotation\route\Group;
<#if (!table && !form) || methods || relation || isTree || summary#>
use think\annotation\route\Route;
<#endif#>
<#if table || form#>
use app\admin\traits\Actions;
<#endif#>
use <#model#> as <#modelName#>Model;
<#if isTree#>
use app\common\library\Tree;
<#endif#>

#[Group("<#group#>")]
class <#controllerName#> extends Backend
{
    <#if table || form#>
    <#if relation && !isTree && !summary#>
    use Actions{
        index as private _index;
    }
    <#endif#>
    <#if !(relation && !isTree && !summary)#>
    use Actions;
    <#endif#>
    <#endif#>

    protected function _initialize()
    {
        parent::_initialize();
        $this->model = new <#modelName#>Model();
        <#if isTree#>
        if(!$this->request->isPost()){
            $list=$this->model->select()->toArray();
            $parentList=$this->toTree($list);
            $this->assign('parentList',$parentList);
        }
        <#endif#>
        <#if table && isset(actionList['recyclebin'])#>
        $this->recyclebinColumns=[
        <#recyclebinField#>
        ];
        $this->recyclebinColumnsType=[
        <#recyclebinType#>
        ];
        <#endif#>
    }

    <#if relation || isTree || summary#>
    #[Route("GET,JSON","index")]
    public function index()
    {
        <#if relation#>
        $this->relationField=<#relation#>;
        <#endif#>
        <#if !isTree && !summary#>
        return $this->_index();
        <#endif#>
        <#if isTree || summary#>
        if (false === $this->request->isAjax()) {
            return $this->fetch();
        }
        if($this->request->post('selectpage')){
            return $this->selectpage();
        }
        [$where, $order, $limit, $with] = $this->buildparams();
        $list = $this->model
            ->withJoin($with,'left')
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $result = [
            <#if summary#>
            'summary' => '自定义统计信息',
            <#endif#>
            'total' => $list->total(),
            <#if isTree#>
            'rows' => $this->toTree($list->items())
            <#endif#>
            <#if !isTree#>
            'rows' => $list->items()
            <#endif#>
        ];
        return json($result);
        <#endif#>
    }
    <#endif#>

    <#if isTree#>
    private function toTree($list)
    {
        $tree = Tree::instance();
        $tree->init($list, 'pid');
        return $tree->getTreeList($tree->getTreeArray(0), '<#treeTitle#>');
    }
    <#endif#>

    <#if methods#>
<#methods#>
    <#endif#>
}