<?php

declare(strict_types=1);

namespace app\admin\controller\user;

use ba\Tree;
use app\common\controller\Backend;
use app\admin\model\UserRule;
use Webman\Http\Request;
use support\Response;

class Rule extends Backend
{
    protected ?object $model = null;
    protected Tree $tree;
    protected array $initValue = [];
    protected bool $assembleTree = true;
    protected string $keyword = '';

    protected array|string $preExcludeFields = ['create_time', 'update_time'];
    protected array|string $defaultSortField = ['weigh' => 'desc'];
    protected array|string $quickSearchField = 'title';

    protected function initController(Request $request): ?Response
    {
        $this->model = new UserRule();
        $this->tree = Tree::instance();
        $isTree = filter_var($request->get('isTree', $request->post('isTree', true)), FILTER_VALIDATE_BOOLEAN);
        $this->initValue = $request->get('initValue', $request->post('initValue', []));
        $this->initValue = is_array($this->initValue) ? array_filter($this->initValue) : [];
        $this->keyword = $request->get('quickSearch', $request->post('quickSearch', ''));
        $this->assembleTree = $isTree && !$this->initValue;
        return null;
    }

    public function index(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) return $response;

        if ($request->get('select') || $request->post('select')) {
            return $this->select($request);
        }

        list($where, , , ) = $this->queryBuilder();
        $list = $this->model->where($where)->order($this->defaultSortField)->select();

        if ($this->assembleTree) {
            $list = $this->tree->assembleChild($list->toArray());
        } else {
            $list = $list->toArray();
        }

        return $this->success('', [
            'list' => $list,
            'remark' => get_route_remark(),
        ]);
    }

    public function add(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) return $response;
        return $this->_add();
    }

    public function edit(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) return $response;
        return $this->_edit();
    }

    public function del(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) return $response;
        return $this->_del();
    }

    public function select(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) return $response;

        list($where, , , ) = $this->queryBuilder();
        $list = $this->model->where($where)->order($this->defaultSortField)->select();

        if ($this->assembleTree) {
            $list = $this->tree->assembleChild($list->toArray());
        } else {
            $list = $list->toArray();
        }

        return $this->success('', ['list' => $list]);
    }
}
