package org.cs.mgr.admin.ctl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.cs.core.ctl.BaseCtl;
import org.cs.mgr.admin.model.Action;
import org.cs.mgr.admin.model.Role;
import org.cs.mgr.admin.service.IActionService;
import org.cs.mgr.admin.service.IMenuService;
import org.cs.mgr.admin.service.IRoleMenuService;
import org.cs.mgr.admin.service.IRoleService;
import org.cs.mgr.admin.vo.ZTree;
import org.cs.mgr.product.model.MgmProductCategory;
import org.cs.mgr.product.service.IMgmProductCategoryService;
import org.cs.util.Pager;
import org.cs.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("/role")
public class RoleCtl  extends BaseCtl{
	
	Logger log = Logger.getLogger(RoleCtl.class);
	
	@Autowired
	IRoleService roleService;
	
	@Autowired
	IMenuService menuService;
	
	@Autowired
	IActionService actionService;
	
	@Autowired
	private IMgmProductCategoryService mgmProductCategoryService;
	
	@Autowired
	IRoleMenuService roleMenuService;
	
	@RequestMapping("list")
	public String list(ModelMap mm, @RequestParam(required=false)String menuCode){
		mm.put(MENU_CODE, menuCode);
		return "sys/role/list";	
	}
	
	@RequestMapping(value="list2", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String list2(int page, int pageSize, String search, ModelMap mm){
		Map<String, String> map = getMapFromJson(search);
		if(map == null){
			map = new HashMap<>();
		}
		Pager pager = this.roleService.getRoleList(new Pager(page, pageSize), map);
		JSONObject json = new JSONObject();
		json.put("rows", pager.getResults());
		json.put("total", pager.getTotal());
		return JSONObject.toJSONString(json, features);
	}
	
	
	@RequestMapping("transfer")
	public String transfer(String id, String action, ModelMap mm){
		Role role = null;
		if(StringUtil.isNotBlank(id)){
			role = this.roleService.findById(Integer.valueOf(id.trim()));
			mm.put("role", role);
		}
		
		mm.put("firstCategory", this.mgmProductCategoryService.firstCategory());
		if("edit".equals(action)){
			Map<String, String> map = new HashMap<>();
			if(role.getOneCategoryId() != null && role.getOneCategoryId().intValue()!=0){
				map.put("id", role.getOneCategoryId().toString());
				mm.put("twoCategory", this.mgmProductCategoryService.queryShowCategory(map));
			}else{
				mm.put("twoCategory", new ArrayList<MgmProductCategory>());
			}
			if(role.getTwoCategoryId() != null && role.getTwoCategoryId().intValue()!=0){
				map = new HashMap<>();
				map.put("id", role.getTwoCategoryId().toString());
				mm.put("threeCategory", this.mgmProductCategoryService.queryShowCategory(map));
			}else{
				mm.put("threeCategory", new ArrayList<MgmProductCategory>());
			}
			
			if(role.getThreeCategoryId() != null && role.getThreeCategoryId().intValue()!=0){
				map = new HashMap<>();
				map.put("id", role.getThreeCategoryId().toString());
				mm.put("fourCategory", this.mgmProductCategoryService.queryShowCategory(map));
			}else{
				mm.put("fourCategory", new ArrayList<MgmProductCategory>());
			}
		}
		return "sys/role/" + action;
	}
	
	@RequestMapping(value="getListMenu", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String getListMenu(int roleId, ModelMap mm){
		List<Map<String, Object>> list = this.menuService.getRoleMenu(roleId);
		List<Map<String, Object>> list2 = this.actionService.getAllAction(roleId+"");
		
		List<ZTree> zts = new ArrayList<ZTree>();
		ZTree zt = null;
		for(Map<String, Object> mo : list){
			int menu_id = Integer.valueOf(mo.get("menu_id").toString()).intValue();
			int pId = Integer.valueOf(mo.get("menu_pid").toString()).intValue();
			String name = mo.get("menu_name").toString();
			String code = mo.get("menu_code").toString();
			int set = Integer.valueOf(mo.get("a1a")+"");
			zt = new ZTree(menu_id, pId , name, code);
			if(pId == 0){
				zt.setParent(true);
			}
			zt.setOpen(true);
			if(set >= 0){
				zt.setChecked(true);
				zt.setVal(set);
			}
			zts.add(zt);
		}
		
		for(Map<String, Object> mo : list2){
			int action_id = Integer.valueOf(mo.get("action_id").toString()).intValue();
			int action_val = Integer.valueOf(mo.get("action_val").toString()).intValue();
			int pId = Integer.valueOf(mo.get("menu_id").toString()).intValue();
			String name = mo.get("action_name").toString();
			
			zt = new ZTree(action_id * 1000, pId , name);
			
			zt.setAction(true);
			
			int set = Integer.valueOf(mo.get("a1a")+"");
			if(set > 0){
				zt.setChecked(true);
			}
			zt.setVal(action_val);
			zts.add(zt);
		}
		
		
		return JSONObject.toJSONString(zts, features);
	}
	
	
	@RequestMapping(value="getMenuAction", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String getMenuAction(String menuCode){
		List<Action> list = this.actionService.getActionByMenuCode(menuCode);
		return JSONObject.toJSONString(list, features);
	}
	@RequestMapping(value="getAllAction", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String getAllAction(String roleId){
		List<Map<String, Object>> list = this.actionService.getAllAction(roleId);
		return JSONObject.toJSONString(list, features);
	}
	
	
	@RequestMapping(value="setPermission", method = RequestMethod.POST, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String setPermission(String acs, int roleId, ModelMap mm){
		JSONArray ja = JSONArray.parseArray(acs);
		
		this.roleMenuService.setPermission(roleId, ja);
		
		return AJAX_SUCCESS; 
	}
	
	
	@RequestMapping(value="add", method = RequestMethod.POST)
	@ResponseBody
	public String add(Role role, ModelMap mm){
		if(role != null ){
			String num = "0";
			if(role.getOneCategoryId() != null && role.getOneCategoryId().intValue() != 0){
				num = "1";
			}
			if(role.getTwoCategoryId() != null && role.getTwoCategoryId().intValue() != 0){
				num = "2";
			}
			if(role.getThreeCategoryId() != null && role.getThreeCategoryId().intValue() != 0){
				num = "3";
			}
			if(role.getFourCategoryId() != null && role.getFourCategoryId().intValue() != 0){
				num = "4";
			}
			role.setNum(num);
			this.roleService.add(role);
		}
		return AJAX_SUCCESS;
	}
	
	@RequestMapping(value="edit", method = RequestMethod.POST)
	@ResponseBody
	public String edit(Role role, ModelMap mm){
		if(role != null ){
			Role role1 = this.roleService.findById(role.getId());
			String num = "0";
			if(role.getOneCategoryId() != null && role.getOneCategoryId().intValue() != 0){
				role1.setOneCategoryId(role.getOneCategoryId());
				num = "1";
			}
			if(role.getTwoCategoryId() != null && role.getTwoCategoryId().intValue() != 0){
				role1.setTwoCategoryId(role.getTwoCategoryId());
				num = "2";
			}
			if(role.getThreeCategoryId() != null && role.getThreeCategoryId().intValue() != 0){
				role1.setThreeCategoryId(role.getThreeCategoryId());
				num = "3";
			}
			if(role.getFourCategoryId() != null && role.getFourCategoryId().intValue() != 0){
				role1.setFourCategoryId(role.getFourCategoryId());
				num = "4";
			}
			role1.setNum(num);
			
			role1.setCode(role.getCode());
			role1.setName(role.getName());
			role1.setNote(role.getNote());
			this.roleService.update(role1);
		}
		return AJAX_SUCCESS;
	}
	
	
	@RequestMapping("del")
	@ResponseBody
	public String del(int id, ModelMap mm){
		
//		mm.put("superMenuList", this.menuService.getSuperMenuList());
		if(id != 0){
			this.roleService.deleteById(id);
		}
		
		return AJAX_SUCCESS;
	}
	
	
	

}
