package com.cab.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

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

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cab.model.CabPush;
import com.cab.service.CabPushServiceImpl;
import com.cku.core.PageBeanResult;
import com.cku.core.RESTResponse;
import com.cku.util.PageBean;
import com.cku.util.ServletUtils;
import com.sys.model.SysUser;

@Controller
@RequestMapping(value="cabPush")
public class CabPushController {
	private static final Logger logger = Logger.getLogger(CabPushController.class);
	@Autowired
	public CabPushServiceImpl cabPushService;

	@InitBinder
	public void InitBinder(ServletRequestDataBinder bin) {
		bin.registerCustomEditor(Date.class, new CustomDateEditor(
				new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
	}

	@RequestMapping(value="/getList")
	public ModelAndView getList(HttpServletResponse response,HttpServletRequest request, ModelMap modelMap) throws IOException {
		request.getSession().getAttribute("user");
		String content = ServletUtils.getParameter(request, "content",null);
		PageBean pb = ServletUtils.getParameterBean(request);
		PageBeanResult<CabPush> s = cabPushService.getList(content,pb);
		modelMap.addAttribute("pageList",s);
		SysUser user = (SysUser)request.getSession().getAttribute("user");
		modelMap.addAttribute("user",user);
		ModelAndView mv = new ModelAndView("admin/cabPush/cabPushList",modelMap);
		return mv;
	
	}

	@RequestMapping(value="/toUpdate")
	public ModelAndView toUpdate(HttpServletResponse response,HttpServletRequest request, ModelMap modelMap) throws IOException{
		 String type = ServletUtils.getParameter(request, "type","add");
		 if("edit".equals(type)){
			 long id = ServletUtils.getParameterLong(request, "id",null);
			 CabPush cp = cabPushService.getModelById(id);
			 modelMap.addAttribute("cabPush",cp);
		 }else{
			 modelMap.addAttribute("cabPush",null);
		 }
		ModelAndView mv = new ModelAndView("admin/cabPush/updateCabPush",modelMap);
		return mv;
	}

	@RequestMapping(value="/addCabPush")
	public ModelAndView addCabPush(HttpServletResponse response,HttpServletRequest request,CabPush cabPush, ModelMap modelMap) throws IOException{
		cabPush.setCreateUser(((SysUser)request.getSession().getAttribute("user")).getUserId());
		cabPush.setCreateDate(new Date());
		cabPush.setStatus(0);
		cabPush.setIsPush(0);
		int i = cabPushService.addCabPush(cabPush);
		String result = i>0?"success":"error";
		modelMap.addAttribute("toPage","cabPush/getList.do");
		modelMap.addAttribute("message",i>0?"添加推送消息成功!":"添加推送消息失败!");
		ModelAndView mv = new ModelAndView(result,modelMap);
		return mv;
	}
	

	@RequestMapping(value="/updateCabPush")
	public ModelAndView updateCaiPan(HttpServletResponse response,HttpServletRequest request,CabPush cabPush, ModelMap modelMap)throws IOException{
		int i = cabPushService.updateCabPush(cabPush);
		String result = i>0?"success":"error";
		modelMap.addAttribute("toPage","cabPush/getList.do");
		modelMap.addAttribute("message",i>0?"修改推送消息成功!":"修改推送消息失败!");
		ModelAndView mv = new ModelAndView(result,modelMap);
		return mv;
	}
	@RequestMapping(value="/deleteCabPush")
	@ResponseBody
	public void deleteCabPush(HttpServletResponse response,HttpServletRequest request, ModelMap modelMap)throws IOException{
		RESTResponse result = null;
		try{
			long id = ServletUtils.getParameterLong(request, "id",null);
			int i = cabPushService.deleteCabPush(id);
			if(i>0){
				result = new RESTResponse(0,"删除推送消息成功！");
			}else{
				result = new RESTResponse(1,"删除推送消息失败！");
			}
		}catch (Exception e){
			result = new RESTResponse(e);
			logger.error(e);
		}
		ServletUtils.writeResponse(response, result);
	}
	@RequestMapping(value="/changeStatus")
	@ResponseBody
	public void changeStatus(HttpServletResponse response,HttpServletRequest request, ModelMap modelMap)throws IOException{
		RESTResponse result = null;
		try{
			long id = ServletUtils.getParameterLong(request, "id",null);
			int i = cabPushService.changeStatus(id);
			if(i>0){
				result = new RESTResponse(0,"审核推送消息成功！");
			}else{
				result = new RESTResponse(1,"审核推送消息失败！");
			}
		}catch (Exception e){
			result = new RESTResponse(e);
			logger.error(e);
		}
		ServletUtils.writeResponse(response, result);
	}
}
