package com.iamberry.wechat.handles.coupon;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
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.banner.MessageDTO;
import com.iamberry.wechat.core.entity.coupon.AdminCouponItemDto;
import com.iamberry.wechat.face.coupon.AdminCouponItemService;

@Controller
@RequestMapping("/admin/couponItem")
public class AdminCouponItemHandler {
	
	@Autowired
	private AdminCouponItemService adminCouponItemService;
	
	/**
	 * 分页查询优惠券详情信息
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getAllCouponItem")
	public ModelAndView getAllCouponItem(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/coupon/couponItemList");
		String beginTime = request.getParameter("beginDate");
		String text = request.getParameter("text");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		AdminCouponItemDto adminCouponItemDto = new AdminCouponItemDto();
		adminCouponItemDto.getPage().setPageNumber(pageNO);
		adminCouponItemDto.getPage().setPageSize(pageSize);
		adminCouponItemDto.getPage().initRecordBegin();
		StringBuffer url = new StringBuffer("/admin/couponItem/getAllCouponItem?&pageSize=" + pageSize);
		if (null != beginTime && !"".equals(beginTime)) {
			if (!request.getMethod().equalsIgnoreCase("post")) {
				adminCouponItemDto.setCouponUseEndDate(format.parse(new String(beginTime.getBytes("ISO-8859-1"), "UTF-8")));
			} else {
				adminCouponItemDto.setCouponUseEndDate(format.parse(beginTime));
			}
			url.append("&beginDate=" + adminCouponItemDto.getCouponUseEndDate());
		}
		if (null != text && !"".equals(text)) {
			if (!request.getMethod().equalsIgnoreCase("post")) {
				text = new String(text.getBytes("ISO-8859-1"), "UTF-8");
				adminCouponItemDto.setCouponName(text);
				text = URLEncoder.encode(text, "UTF-8");
				adminCouponItemDto.setUserName(text);
			} else {
				adminCouponItemDto.setCouponName(text);
				text = URLEncoder.encode(text, "UTF-8");
				adminCouponItemDto.setUserName(text);
				
			}
		}
		List<AdminCouponItemDto> couponItemList = adminCouponItemService.getAllCouponItem(adminCouponItemDto);
		if (null != couponItemList && couponItemList.size() > 0) {
			for (AdminCouponItemDto couponItem : couponItemList) {
				if (null != couponItem.getUserName() && !"".equals(couponItem.getUserName())) {
					couponItem.setUserName(URLDecoder.decode(couponItem.getUserName(), "UTF-8"));
				}
			}
		}
		if (null != adminCouponItemDto.getUserName() && !"".equals(adminCouponItemDto.getUserName())) {
			mv.addObject("text", URLDecoder.decode(adminCouponItemDto.getUserName(), "UTF-8"));
			url.append("&text=" + URLDecoder.decode(adminCouponItemDto.getUserName(), "UTF-8"));
		}
		mv.addObject("beginDate", adminCouponItemDto.getCouponUseEndDate());
		url.append("&pageNO=");
		mv.addObject("couponItemList", couponItemList);
		mv.addObject("pageNO", pageNO);
		mv.addObject("url", url.toString());
		mv.addObject("pageSize", adminCouponItemService.getCouponItemCount(adminCouponItemDto));
		return mv;
	}
	
	/**
	 * 跳转到优惠券详情修改页面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateCouponItem")
	public ModelAndView updateCouponItem(HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView("/admin/coupon/updateCouponItem");
		String couponItemId = request.getParameter("couponItemId");
		if (null == couponItemId || "".equals(couponItemId)) {
			mv = new ModelAndView("admin/msg/msg");
			mv.addObject("msgObj", new MessageDTO(false, "对不起，操作有误", "对不起，您刚才的操作流程有误，请按规定填写数据！"));
			return mv;
		}
		AdminCouponItemDto couponItemDto = adminCouponItemService.getCouponItemByCouponItemId(couponItemId);
		if (couponItemDto == null) {
			mv = new ModelAndView("admin/msg/msg");
			mv.addObject("msgObj", new MessageDTO(false, "对不起，操作有误", "对不起，您刚才的操作流程有误，请按规定填写数据！"));
			return mv;
		}
		mv.addObject("couponItemDto", couponItemDto);
		return mv;
	}
	
	/**
	 * 修改优惠券详细信息
	 * @param request
	 * @param couponItemDto
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateCouponItemInfo")
	public ModelAndView updateCouponItemInfo(HttpServletRequest request,
			AdminCouponItemDto couponItemDto) throws Exception {
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		Integer num = adminCouponItemService.updateCouponItem(couponItemDto);
		if (num > 0) {
			mv.addObject("msgObj", new MessageDTO(true, "恭喜您,操作成功", "模板信息已添加成功,刷新即可!"));
		} else {
			mv.addObject("msgObj", new MessageDTO(false, "对不起，操作有误", "对不起，您刚才的操作流程有误，请按规定填写数据！"));
		}
		return mv;
	}
}
