package io.renren.modules.admin.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.admin.entity.CategoryEntity;
import io.renren.modules.admin.service.CategoryService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;



/**
 * 材料类型表
 *
 * @author huwhois
 * @email huwhois@gmail.com
 * @date 2020-07-17 09:55:33
 */
@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:category:list")
    public R list(){
        List<CategoryEntity> categoryList = categoryService.list();

		for(CategoryEntity categoryMenuEntity : categoryList){
			CategoryEntity parentMenuEntity = categoryService.getById(categoryMenuEntity.getParentId());
			if(parentMenuEntity != null){
				categoryMenuEntity.setParentName(parentMenuEntity.getName());
			}
        }
        
        return R.ok().put("list", categoryList);
    }

    /**
	 * 选择上级材料(添加、修改类型)
	 */
	@GetMapping("/select")
	@RequiresPermissions("admin:category:list")
	public R select(){
		//查询列表数据
		List<CategoryEntity> categoryList = categoryService.queryNotLastList();
		
		//添加顶级菜单
		CategoryEntity root = new CategoryEntity();
		root.setId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		categoryList.add(root);
		
		return R.ok().put("categoryList", categoryList);
	}

    /**
	 * 选择上级材料(添加、修改类型)
	 */
	@GetMapping("/selecttree")
	@RequiresPermissions("admin:category:list")
	public R selectTree(){
		//查询列表数据
		List<CategoryEntity> categoryList = categoryService.queryNotLastTreeList();
		
		//添加顶级菜单
		CategoryEntity root = new CategoryEntity();
		root.setId(0L);
		root.setName("一级菜单");
		root.setParentId(0L);
		root.setOpen(true);
		categoryList.add(root);
		
		return R.ok().put("categoryList", categoryList);
	}


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("admin:category:info")
    public R info(@PathVariable("id") Integer id){
		CategoryEntity category = categoryService.getById(id);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("admin:category:save")
    public R save(@RequestBody CategoryEntity category){
        ValidatorUtils.validateEntity(category, AddGroup.class);

        category.setCreateTime(new Date());
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("admin:category:update")
    public R update(@RequestBody CategoryEntity category){
        ValidatorUtils.validateEntity(category, UpdateGroup.class);

		categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("admin:category:delete")
    public R delete(@RequestBody Integer[] ids){
		categoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * treeList
     */
    @GetMapping("/treelist")
    @RequiresPermissions("admin:category:list")
    public R treeList(){
        List<CategoryEntity> list = categoryService.queryTreeList();

        return R.ok().put("list", list);
    }

    /**
     * namelist
     */
    @GetMapping("/namelist")
    @RequiresPermissions("admin:category:list")
    public R namelist(){
        List<CategoryEntity> list = categoryService.list();
        Map<String, Object> namelist = new HashMap<>();
        if (list != null) {
            for (CategoryEntity category : list) {
                namelist.put(category.getId().toString(), category.getName());
            }
        }

        return R.ok().put("list", namelist);
    }
}
