<?php

declare(strict_types=1);

namespace App\Controller;

use App\JsonRpc\AuthorityServiceInterface;
use App\Tools\PublicData;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Tools\Result;
use App\Constants\ErrorCode;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
/**
 * Class AuthorityController
 * @package App\Controller
 */
#[AutoController]
class AuthorityController extends AbstractController
{
    #[Inject]
    protected ValidatorFactoryInterface $validationFactory;
    /**
     * @var AuthorityServiceInterface
     */
    #[Inject]
    private $authorityServiceClient;


    /**
     * 获取菜单列表
     * @return array
     */
    public function getMenuList()
    {
        $id =  (int)$this->request->input("id",0);
        $page =  (int)$this->request->input("page",1);
        $pageSize =  (int)$this->request->input("pageSize",10);
        $data = [
            'id'=>$id,
            'page'=>$page,
            'pageSize'=>$pageSize
        ];
        $result = $this->authorityServiceClient->getMenuList($data);
        if($result['data']){
            foreach ($result['data']['rows'] as $k=>$v) {
                $result['data']['rows'][$k]['pid_arr'] = $v['pid_arr']?json_decode($v['pid_arr']):[0];
            }
        }
        return $result?Result::success($result['data']):Result::error($result['message']);
    }

    /**
     * 删除菜单
     * @return array
     */
    public function delMenu()
    {
        $id =  (int)$this->request->input("id",0);
        $data = [
            'id'=>$id,
            'page'=>1,
            'pageSize'=>2
        ];
        $result = $this->authorityServiceClient->getMenuList($data);
        if(count($result['data']['rows'])>0){
            return Result::error("有子菜单不能删除");
        }
        return $this->authorityServiceClient->delMenu($data);

    }

    /**
     * 添加菜单
     * @return array|void
     */
    public function addMenu()
    {
        $requireData = $this->request->all();
        $validator = $this->validationFactory->make(
            $requireData,
            [

                'label' => 'required',
//                'url' => 'required',

            ],
            [

                'label.required' => '菜单名称不能为空',
//                'url.required' => '路由地址不能为空',
            ]
        );
        if ($validator->fails()){
            $errorMessage = $validator->errors()->first();
            return Result::error($errorMessage);
        }
        if(isset($requireData['pid']) && is_array($requireData['pid'])){
            $requireData['pid_arr'] = json_encode($requireData['pid']);
            $requireData['pid'] = end($requireData['pid']);
        }else{
            $id = [0];
            $requireData['pid_arr'] = json_encode($id);
            $requireData['pid'] = end($id);
        }
        $result = $this->authorityServiceClient->addMenu($requireData);
        return $result?Result::success($result['data']):Result::error($result['message']);
    }
    /**
     * 修改菜单
     * @return array|void
     */
    public function updateMenu()
    {
        $requireData = $this->request->all();
        $validator = $this->validationFactory->make(
            $requireData,
            [
                'id' => 'required',
//                'pid' => 'required',
                'label' => 'required',
//                'url' => 'required',

            ],
            [
                'id.required' => 'id不能为空',
//                'pid.required' => '父级id不能为空',
                'label.required' => '菜单名称不能为空',
//                'url.required' => '路由地址不能为空',
            ]
        );
        if ($validator->fails()){
            $errorMessage = $validator->errors()->first();
            return Result::error($errorMessage);
        }
        unset($requireData['pid_arr']);
        var_dump("提交数据前：",$requireData);
        if(isset($requireData['pid']) && is_array($requireData['pid'])){
            $requireData['pid_arr'] = json_encode($requireData['pid']);
            $requireData['pid'] = end($requireData['pid']);
        }else if(isset($requireData['pid']) && $requireData['pid']>0){
//            array_push($requireData['pid'],$requireData['pid']);
            $requireData['pid'] = [$requireData['pid']];
            $requireData['pid_arr'] = json_encode($requireData['pid']);
            $requireData['pid'] = end($requireData['pid']);
        }else{
            $id = [0];
            $requireData['pid_arr'] = json_encode($id);
            $requireData['pid'] = end($id);
        }
        var_dump("提交数据后",$requireData);
        $result = $this->authorityServiceClient->updateMenu($requireData);
        return $result?Result::success($result['data']):Result::error($result['message']);
    }

    /**
     * 菜单递归查询处理
     * @return void
     */
    public  function  getRecursionMenu()
    {
       $result =  $this->authorityServiceClient->getRecursionMenu([]);
       $rep = PublicData::buildMenuTree($result['data']);
       return $rep?Result::success($rep):Result::error("查询失败");
    }

}