package com.iamberry.wechat.handles.flow;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.iamberry.wechat.core.entity.admin.AdminInfo;
import com.iamberry.wechat.core.entity.banner.MessageDTO;
import com.iamberry.wechat.core.entity.flow.TemplateInfo;
import com.iamberry.wechat.core.entity.product.ProductType;
import com.iamberry.wechat.face.flow.TemplateFlowInfoService;
import com.iamberry.wechat.face.flow.TemplateInfoService;
import com.iamberry.wechat.face.porduct.ProductTypeService;

@Controller
@RequestMapping("/admin/template")
public class TemplateInfoHandler {
	
	@Autowired
	private TemplateInfoService templateInfoService;
	
	@Autowired
	private TemplateFlowInfoService templateFlowInfoService;
	
	@Autowired
	private ProductTypeService productTypeService;
	
	/**
	 * 分页获取模板列表数据
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getTemplateList")
	public ModelAndView getTemplateList(HttpServletRequest request,
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO) throws Exception {
		ModelAndView mv = new ModelAndView("admin/flow/templateList");
		TemplateInfo templateInfo = new TemplateInfo();
		String text = request.getParameter("text");
		StringBuffer url = new StringBuffer("/admin/template/getTemplateList?pageSize=" + pageSize);
		if (null != text && !"".equals(text)) {
			if (!request.getMethod().equalsIgnoreCase("post")) {
				text = new String(text.getBytes("ISO-8859-1"), "UTF-8");
			}
			templateInfo.setTemplateName(text);
			url.append("&text=" + text);
		}
		url.append("&pageNO=");
		templateInfo.getPage().setPageSize(pageSize);
		templateInfo.getPage().setPageNumber(pageNO);
		templateInfo.getPage().initRecordBegin();
		List<TemplateInfo> templateList = templateInfoService.getTemplateList(templateInfo);
		
		// 判断当前是否支持修改
		for (TemplateInfo templateInfo2 : templateList) {
			List<Integer> integers = templateFlowInfoService.getTypeId(templateInfo2.getTemplateId());
			if (integers == null || integers.size() == 0) {
				templateInfo2.setFlowNum(-2);			// 添加
			} else {
				List<ProductType> types = productTypeService.getAll();
				if (types.size() == integers.size()) {
					templateInfo2.setFlowNum(1);		// 修改
				} else {
					templateInfo2.setFlowNum(-1);		// 添加
				}
			}
		}
		
		mv.addObject("text", text);
		mv.addObject("templateList", templateList);
		mv.addObject("pageNO", pageNO);
		mv.addObject("url", url.toString());
		mv.addObject("pageSize", templateInfoService.getTemplateCount(templateInfo));
		return mv;
	}
	
	/**
	 * 跳转到添加模板页面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addTemplate")
	public ModelAndView addTemplate(HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView("admin/flow/addTemplateInfo");
		return mv;
	}
	
	/**
	 * 添加模板数据
	 * @param request
	 * @param templateInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addTemplateInfo")
	public ModelAndView addTemplateInfo(HttpServletRequest request,TemplateInfo templateInfo) throws Exception {
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		AdminInfo adminInfo = (AdminInfo)request.getSession().getAttribute("login_admin_userinfo");
		templateInfo.setCreaterId(adminInfo.getAdminId());
		Integer num = templateInfoService.addTemplateInfo(templateInfo);
		if (num > 0) {
			mv.addObject("msgObj", new MessageDTO(true, "恭喜您,操作成功", "模板信息已添加成功,刷新即可!"));
		} else {
			mv.addObject("msgObj", new MessageDTO(false, "对不起，操作有误", "对不起，您刚才的操作流程有误，请按规定填写数据！"));
		}
		return mv;
	}
	
	/**
	 * 跳转到修改模板页面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateTemplate")
	public ModelAndView updateTemplate(HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView("admin/flow/updateTemplateInfo");
		String templateId = request.getParameter("templateId");
		TemplateInfo templateInfo = new TemplateInfo();
		if (null == templateId || "".equals(templateId)) {
			return null;
		}
		templateInfo.setTemplateId(Integer.parseInt(templateId));
		templateInfo = templateInfoService.getTemplateInfoById(templateInfo);
		if (templateInfo == null) {
			mv = new ModelAndView("admin/msg/msg");
			mv.addObject("msgObj", new MessageDTO(false, "对不起，操作有误", "对不起，您刚才的操作流程有误，请按照系统流程操作！"));
			return mv;
		}
		mv.addObject("templateInfo", templateInfo);
		return mv;
	}
	
	/**
	 * 修改模板数据
	 * @param request
	 * @param templateInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateTemplateInfo")
	public ModelAndView updateTemplateInfo(HttpServletRequest request,TemplateInfo templateInfo) throws Exception {
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		Integer num = templateInfoService.updateTemplateInfo(templateInfo);
		if (num > 0) {
			mv.addObject("msgObj", new MessageDTO(true, "恭喜您,操作成功", "模板信息已修改成功,刷新即可!"));
		} else {
			mv.addObject("msgObj", new MessageDTO(false, "对不起，操作有误", "对不起，您刚才的操作流程有误，请按规定填写数据！"));
		}
		return mv;
	}
}
