package cn.feast.coding.controller.generator;

import cn.feast.coding.controller.BaseController;
import cn.feast.coding.model.generator.PageTemplate;
import cn.feast.coding.model.generator.Project;
import cn.feast.coding.service.generator.PageTemplateService;
import cn.feast.coding.service.generator.ProjectService;
import cn.feast.coding.tools.JsonResult;
import com.github.pagehelper.PageInfo;
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.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/pageTemplate")
public class PageTemplateController extends BaseController {
	@Autowired
	private PageTemplateService pageTemplateService;
	@Autowired
	private ProjectService projectService;
	private static final int PAGE_IS_DEFAULT = 1;   //默认页面模板
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(PageTemplate pageTemplate, Integer curPage, Integer pageSize, ModelMap model, HttpServletRequest request){
		PageInfo<PageTemplate> pageInfo = pageTemplateService.queryPage(pageTemplate, pageNum(curPage), pageSize(request, pageSize));
		model.put("pages", pageInfo);
		model.put("pageTemplate", pageTemplate);
		return "pageTemplate/pageTemplate_list";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model){
    	return "/pageTemplate_add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(PageTemplate pageTemplate,HttpServletRequest request, ModelMap model){
		pageTemplate.setPageDefault(PAGE_IS_DEFAULT);
		pageTemplateService.save(pageTemplate);
		model.put("flag", 1);
		return "pageTemplate/pageTemplate_add";
	}
	
	
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String update(PageTemplate pageTemplate,ModelMap model){
		pageTemplate = pageTemplateService.queryById(pageTemplate.getId());
		model.put("pageTemplate", pageTemplate);
		return "pageTemplate/pageTemplate_edit";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(PageTemplate pageTemplate,HttpServletRequest request, ModelMap model){
		pageTemplateService.update(pageTemplate);
		model.put("flag", 1);
		return "pageTemplate/pageTemplate_edit";
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult delete(PageTemplate pageTemplate){
		JsonResult jsonResult = new JsonResult();
		jsonResult.setState(JsonResult.SUCCESS);
		pageTemplate = pageTemplateService.queryById(pageTemplate.getId());
		if(pageTemplate.getPageDefault().equals(0)){
			jsonResult.setState(JsonResult.ERROR);
			jsonResult.setMessage("默认页面模板，不能删除");
			return jsonResult;
		}
		List<Project> list = projectService.queryByPageTemplate(pageTemplate.getId());
		if(list.size() > 0){
			jsonResult.setState(JsonResult.ERROR);
			jsonResult.setMessage("存在关联项目，不能删除");
			return jsonResult;
		}
		pageTemplateService.deleteById(pageTemplate.getId());
		return jsonResult;
	}

	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String view(PageTemplate pageTemplate,ModelMap model){
		pageTemplate = pageTemplateService.queryById(pageTemplate.getId());
		model.put("pageTemplate", pageTemplate);
		return "pageTemplate/pageTemplate_view";
	}
}
