package cn.com.countrygarden.bi.main.controller.project;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.model.AccessToken;

import cn.com.countrygarden.bi.main.model.project.ProjectMenu;
import cn.com.countrygarden.bi.main.service.AccessTokenService;

import cn.com.countrygarden.bi.main.service.project.ProjectMenuService;

@RestController("ProjectMenuController")
@RequestMapping("/api/project/menu")
public class ProjectMenuController {
	  @Autowired
	    private ProjectMenuService projectMenu;
	  @Autowired
	    private AccessTokenService accessTokenService;
	  
	  @RequestMapping(value = "/list", method = {RequestMethod.GET}, produces = {"application/json;charset=utf-8"})
	  public Object search( ){
		  return Response.response(projectMenu.search());
	  }
	  @ResponseBody
	  @RequestMapping(value = "/add", method = {RequestMethod.POST},produces = "application/json;charset=utf-8")
	  public Object create(
			  @RequestParam(value = "id", required = false) Integer id,
			  @RequestParam(value = "name", required = false, defaultValue = "") String name,
			  @RequestParam(value = "parentId", required = false, defaultValue = "") Integer parentId,
			  HttpServletRequest request, HttpServletResponse response) {
		  ProjectMenu p = new ProjectMenu();
		  p.setId(id);
		  p.setName(name);
		  p.setParentId(parentId);
		  p.setIsDelete(0);
		  String accessToken = request.getParameter("AccessToken");
		  AccessToken token = accessTokenService.findByToken(accessToken);
		  return Response.response( projectMenu.save(p, token.getUserCode()) );
	  }
	  @RequestMapping(value = "/delete/{id}", method = {RequestMethod.GET}, produces = {"application/json;charset=utf-8"})
	  public Object delete(@PathVariable Integer id) {
		  projectMenu.delete(id);
		  return Response.response( new Boolean(true) );
	  }
	  @RequestMapping(value = "/findOne/{id}", method = {RequestMethod.GET}, produces = {"application/json;charset=utf-8"})
	  public Object findOne(@PathVariable Integer id) {
		  return Response.response(projectMenu.findOne(id));
		//  return projectJointPlan.findOne(id);
	  }
}
