package com.yzt.airconditionersystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzt.airconditionersystem.bean.Permission;
import com.yzt.airconditionersystem.bean.Role;
import com.yzt.airconditionersystem.result.Result;
import com.yzt.airconditionersystem.service.PermissionService;
import com.yzt.airconditionersystem.service.RoleService;
import com.yzt.airconditionersystem.util.BasePageVoUtil;
import com.yzt.airconditionersystem.vo.BasePageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author TzZach
 * @since 2022-04-11 12:38
 */
@Controller
public class AdminController {

    public static final String PREFIX = "admin/";

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RoleService roleService;

    @GetMapping("/admin-list")
    public String adminList() {
        return PREFIX + "admin-list";
    }

    @GetMapping("/admin-role")
    public String adminRole(Model model) {
        List<Permission> permissions = permissionService.list();
        List<Role> roles = roleService.list();
        model.addAttribute("permissions", permissions);
        model.addAttribute("roles", roles);
        return PREFIX + "admin-role";
    }

    @GetMapping("/admin-cate")
    public String adminCate() {
        return PREFIX + "admin-cate";
    }

    @GetMapping("/admin-rule")
    public String adminRule() {
        return PREFIX + "admin-rule";
    }


    @GetMapping("/permission/page")
    @ResponseBody
    public BasePageVo permissionPage(@RequestParam("page") Long page,
                                     @RequestParam("limit") Long limit) {
        Page<Permission> pageInfo = new Page<>(page, limit);
        pageInfo = permissionService.page(pageInfo);
        return BasePageVoUtil.getPageVo(pageInfo);
    }

    @PostMapping("/permission/add")
    @ResponseBody
    public Result permissionAdd(@RequestBody Permission permission) {
        permissionService.save(permission);
        return Result.success("添加成功");
    }

    @PostMapping("/permission/edit")
    @ResponseBody
    public Result permissionEdit(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return Result.success("修改成功");
    }

    @PostMapping("/permission/delete")
    @ResponseBody
    public Result permissionDelete(@RequestBody Permission permission) {
        permissionService.removeById(permission);
        return Result.success("删除成功");
    }

    @PostMapping("/permission/batchDelete")
    @ResponseBody
    public Result permissionBatchDelete(@RequestBody List<Integer> ids) {
        permissionService.removeBatchByIds(ids);
        return Result.success("删除成功");
    }


}
