<?php
declare (strict_types = 1);
namespace app\admin\controller;
use app\admin\BaseController;
use think\App;
use think\facade\Db;

class Action extends BaseController{
    public function __construct(App $app) {parent::__construct($app);}

    //获取素有菜单列表数据
    public function index()
    {
        $post = $this->request->post();

        $where = [["status", "=", 1], ["is_del", "=", 0], ['menu_type', "=", 2]];

        if (isset($post['level']) && $post['level'] !== '') $where[] = ['level', 'in', $post['level']];

        $data = Db::name("admin_menu")
            ->where($where)
            ->order("weight desc")
            ->column("id,menu_name,menu_img,menu_route,menu_url,pid,level,is_show,is_private,menu_type,status");
        $result = [];
        if (empty($data)) {
            return app_show(0, "获取成功", $result);
        }
        $list = [];
        $menu = [];
        foreach ($data as $k => $value) {
            $action = Db::name("action")
                ->alias("a")
                ->leftJoin("action_list b", "a.action_code=b.action_code")
                ->where(["menuid" => $value['id'], "a.status" => 1, "a.is_del" => 0, "b.is_del" => 0])
                ->column("a.id,a.action_code,b.action_name");
            if ($value['menu_type'] == 2) $value['action'] = $action;
            $list[] = $value;
        }
        menuAction($list, $menu);
        return app_show(0, "获取成功", array_values($menu));
    }

    public function ActionList(){
        $post=$this->post;
        $page = isset($post['page']) ? intval($post['page']) : 1;
        $size = isset($post['size']) ? intval($post['size']) : 10;
        $count = Db::name("action_list")->where(["is_del"=>0])->count();
        $total = ceil($count/$size)>1 ? ceil($count/$size) : 1;
        $page = $page>=$total?intval($total):$page;
        $list = Db::name("action_list")->where(["is_del"=>0])->page($page,$size)->select();
        $data =['list'=>$list,"count"=>$count];
        return app_show(0,'获取成功',$data);
    }


    /** 菜单下功能信息状态修改
     * @return \think\response\Json|void
     * @throws \think\exception\DbException
     */
    public function ActionAdd(){
        $post  =$this->post;
        $action_name = isset($post['action_name']) ? trim($post['action_name']) : "";
        if($action_name==""){
            return error_show(1001,'功能名称不能为空');
        }
        $action_desc = isset($post['action_desc']) ? trim($post['action_desc']) : "";
        $code = isset($post['action_code']) ? trim($post['action_code']) : "";
        if($code==""){
            return error_show(1001,'功能代码不能为空');
        }
        $status = isset($post['status']) ? intval($post['status']) : 1;
        try{
            $action=[
                "action_name"=>$action_name,
                "action_code"=>$code,
                "action_desc"=>$action_desc,
                "status"=>$status,
                "is_show"=>1,
                "addtime"=>date("Y-m-d H:i:s"),
                "updatetime"=>date("Y-m-d H:i:s")
            ];
            $up =Db::name("action_list")->insert($action);
            return $up ? app_show(0,"新建成功") :error_show(1004,"新建失败");
        }catch (\Exception $e){
            return error_show(1005,$e->getMessage());
        }
    }


    /** 菜单下功能信息状态修改
     * @return \think\response\Json|void
     * @throws \think\exception\DbException
     */
    public function ActionDel(){
        $post  =$this->post;
        $action_id = isset($post['action_id']) ? intval($post['action_id']) : "";
        if($action_id===""){
            return error_show(1001,'参数action_id不能为空');
        }
       $action =Db::name("action_list")->where(["id"=>$action_id,"is_del"=>0])->find();
        if($action==false){
            return error_show(1004,"未找到功能数据");
        }
        $upda=["is_del"=>0,"updatetime"=>date("Y-m-d H:i:s")];
        Db::startTrans();
        try {
            $up =Db::name("action_list")->where($action)->update($upda);
            if($up){
               $upall =Db::name("action")->where(["action_code"=>$action['action_code'],"is_del"=>0])->update($upda);
               Db::commit();
               return app_show(0,"删除成功");
            }
            Db::rollback();
            return error_show(1005,"删除失败");
         }catch (\Exception $e){
            Db::rollback();
            return error_show(1005,$e->getMessage());
         }
    }
}