package com.ozs.rpm.controller;

import java.util.List;

import com.ozs.rpm.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ozs.rpm.dto.MenuDTO;
import com.ozs.rpm.util.AccountApi;
import com.ozs.rpm.util.R;
import com.ozs.rpm.util.S;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = { "报表配置—>分类管理接口" })
@RestController
public class MenuController {

	@Autowired
	private MenuService menuService;
	@Autowired
	private AccountApi accountApi;

	@ApiOperation("通过菜单名模糊搜索获取菜单树状展示")
	@GetMapping("/rpm/menu/tree")
	@ApiImplicitParam(name = "menuName", value = "菜单名（模糊搜索）", required = false, paramType = "query", dataType = "String")
	public R<List<MenuDTO>> getMenuTree(@RequestParam(value = "menuName", required = false) String menuName) {
		try {
			return menuService.getMenuTree(menuName);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("通过菜单编码获取菜单信息")
	@GetMapping("/rpm/menu/{menuCode}")
	@ApiImplicitParam(name = "menuCode", value = "菜单编码", paramType = "path", dataType = "String")
	public R<MenuDTO> getMenu(@PathVariable(value = "menuCode") String menuCode) {
		try {
			return menuService.getMenu(menuCode);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("菜单信息修改接口")
	@ApiImplicitParam(name = "menuCode", value = "菜单编码", paramType = "path", dataType = "String")
	@PutMapping("/rpm/menu/{menuCode}")
	public R<String> updateMenu(@RequestBody MenuDTO menuDTO, @PathVariable(value = "menuCode") String menuCode,
			@RequestHeader String token) {
		try {
			menuDTO.setMenuCode(menuCode);
			menuDTO.setUpdateUserId(accountApi.parseTokenId(token));
			return menuService.updateMenu(menuDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("新建菜单接口")
	@PostMapping("/rpm/menu")
	public R<String> saveMenu(@RequestBody MenuDTO menuDTO, @RequestHeader String token) {
		try {
			menuDTO.setUpdateUserId(accountApi.parseTokenId(token));
			return menuService.saveMenu(menuDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("菜单删除接口")
	@DeleteMapping("/rpm/menu/{menuCode}")
	public R<String> removeMenu(@PathVariable(value = "menuCode", required = true) String menuCode) {
		try {
			return menuService.removeMenu(menuCode);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("获取菜单树状--未赋报表的叶子节点")
	@GetMapping("/rpm/menu/leafTree")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "menuName", value = "菜单名（模糊搜索）", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "reportId", value = "报表ID-用于已选分类回显", required = false, paramType = "query", dataType = "String") })

	public R<List<MenuDTO>> listMenuTreeOnlyLeaf(@RequestParam(value = "menuName", required = false) String menuName,
			@RequestParam(value = "reportId", required = false) String reportId) {
		try {
			return menuService.listMenuTreeOnlyLeaf(menuName, reportId);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}
}
