package org.cs.mgr.admin.ctl;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.cs.constants.Constants;
import org.cs.core.ctl.BaseCtl;
import org.cs.mgr.admin.model.Admin;
import org.cs.mgr.admin.service.IAdminService;
import org.cs.mgr.admin.service.IUserRoleService;
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.JSONObject;

@Controller
@RequestMapping("/user")
public class UserCtl extends BaseCtl{
	
	Logger log = Logger.getLogger(UserCtl.class);
	
	@Autowired
	IAdminService adminService;
	
	
	@Autowired
	IUserRoleService userRoleService;

	@RequestMapping("list")
	public String list(ModelMap mm, @RequestParam(required=false)String menuCode){
		mm.put(MENU_CODE, menuCode);
		return "sys/user/list";	
	}
	
	@RequestMapping(value="list2", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String list2(int page, int pageSize, String search){
		Map<String, String> map = getMapFromJson(search)
				;
		if(map == null){
			map = new HashMap<String, String>();
		}
		Pager pager = this.adminService.getList(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){
		Admin admin = null;
		if(StringUtil.isNotBlank(id)){
			admin = this.adminService.findById(Integer.valueOf(id.trim()));
			mm.put("user", admin);
		}else{
			admin = (Admin)get(Constants.SESSION_USER);
			mm.put("user", admin);
		}

		return "sys/user/" + action;
	}
	
	
	@RequestMapping(value="add", method = RequestMethod.POST)
	@ResponseBody
	public String add(Admin admin,ModelMap mm) throws UnsupportedEncodingException{
		Admin admins=this.adminService.getByAccount(admin.getAccount());
        if(admins!=null){
        	return  AJAX_SUCCESS;  
        } 
		if(admin != null ){
			admin.setAttr1("0");
			this.adminService.addUser(admin);
		}
		return AJAX_SUCCESS;
	} 
	
	
	@RequestMapping(value="edit", method = RequestMethod.POST)
	@ResponseBody
	public String edit(Admin admin){
		if(admin != null ){
			this.adminService.updateUser(admin);
		}
		return AJAX_SUCCESS;
	}
	
	@RequestMapping("del")
	@ResponseBody
	public String del(int id){
		
//		mm.put("superMenuList", this.menuService.getSuperMenuList());
		if(id != 0){
			this.adminService.deleteById(id);
		}
		
		return AJAX_SUCCESS;
	}
	
	
	@RequestMapping("getRoleList")
	@ResponseBody
	public String getRoleList(int userId, String name){
		List<Map<String, Object>> list = this.adminService.getRoleList(userId, name);
		return JSONObject.toJSONString(list, features);
	}
	
	@RequestMapping("initPwd")
	@ResponseBody
	public String initPwd(int id){
		
		this.adminService.initPwd(id);
		
		return AJAX_SUCCESS;
	}

	@RequestMapping("changePwd")
	@ResponseBody
	public String changePwd(int id,String pwd){
		if(id==0){
			Admin admin = (Admin)get(Constants.SESSION_USER);
		}
		this.adminService.changePwd(id,pwd);

		return AJAX_SUCCESS;
	}

	@RequestMapping("setUserRole")
	@ResponseBody
	public String setUserRole(String roleCodes, int userId){
		
		this.userRoleService.setUserRole(roleCodes, userId);
		
		return AJAX_SUCCESS;
	}
	
	

}
