package com.iamberry.wechat.handles.coupon;

import java.util.Date;
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.PathVariable;
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.servlet.ModelAndView;

import org.apache.commons.lang.StringUtils;
import com.iamberry.wechat.core.entity.PageBean;
import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.banner.MessageDTO;
import com.iamberry.wechat.core.entity.coupon.CouponItemDto;
import com.iamberry.wechat.core.entity.coupon.CouponType;
import com.iamberry.wechat.face.coupon.CouponItemService;
import com.iamberry.wechat.face.coupon.CouponTypeService;
import com.iamberry.wechat.tools.DateTimeUtil;
import com.iamberry.wechat.tools.ResultInfo;

@Controller
@RequestMapping("/admin/couponType")
public class AdminCouponTypeHandler {
	@Autowired
	private CouponTypeService couponTypeService;
	@Autowired
	private CouponItemService couponItemService;
	
	@RequestMapping("/typeList")
	public ModelAndView getCouponTypeList(HttpServletRequest req,
			@RequestParam(value="pageSize",defaultValue= "6", required=false)Integer pageSize,
			@RequestParam(value="pageNO",defaultValue= "1", required=false)Integer pageNO){
		ModelAndView mv = new ModelAndView("admin/coupon/couponTypeList");
		String couponName = req.getParameter("couponName");
		String couponType = req.getParameter("couponType");
		CouponType ct = new CouponType();
		ct.setPage(new PageBean());
		ct.getPage().setPageSize(pageSize);
		ct.getPage().setPageNumber(pageNO);
		if(StringUtils.isNotEmpty(couponName)){
			ct.setCouponName(couponName);
		}
		if(StringUtils.isNotEmpty(couponType)){
			ct.setCouponType(Integer.parseInt(couponType));
		}
		List<CouponType> typeList = couponTypeService.getCouponTypeList(ct);
		Integer count = couponTypeService.getCouponTypeCount();
		
		mv.addObject("couponName", couponName);
		mv.addObject("couponType", couponType);
		mv.addObject("typeList", typeList);
		int pageCount =  count % ct.getPage().getPageSize() == 0? count/ct.getPage().getPageSize():(count/ct.getPage().getPageSize())+1;
		//分页用
		mv.addObject("pageNO", pageNO);
		mv.addObject("url", "/admin/couponType/typeList?pageSize=" + pageSize + "&pageNO=");
		mv.addObject("pageSize", pageCount);
		return mv;
	}
	@RequestMapping("/toAdd")
	public ModelAndView toAddCouponType(){
		ModelAndView mv = new ModelAndView("admin/coupon/addCouponType");
		return mv;
	}
	@RequestMapping("/add")
	public ModelAndView addCouponType(HttpServletRequest req,CouponType couponType){
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		String couponEndDate = req.getParameter("couponEndTime");
		if(StringUtils.isNotEmpty(couponEndDate)){
			couponType.setCouponEndDate(DateTimeUtil.parse(couponEndDate, DateTimeUtil.defaultSimpleFormater));
		}
		couponType.setCouponCreateDate(new Date());
		couponType.setCouponUpdateDate(new Date());
		int count = couponTypeService.insertCouponType(couponType);
		if(count > 0){
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加成功！"));
		}else{
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加失败！"));
		}
		System.out.println(couponType);
		return mv;
	}
	
	/**
	 * 后台进入修改页面
	 * 2016年7月8日
	 * @param couponId
	 * @return
	 */
	@RequestMapping(value = "/toUpdateType/{couponId}", method = RequestMethod.GET)
	public ModelAndView toUpdateCouponType(@PathVariable("couponId")Integer couponId){
		ModelAndView mv = new ModelAndView("admin/coupon/updateCouponType");
		if(couponId != null){
			CouponType cType = couponTypeService.getCouponTypeById(couponId);
			if(cType != null){
				mv.addObject("cType", cType);
			}
		}
		return mv;
	}
	
	/**
	 * 修改一种优惠券类型
	 * @param cType
	 * @return
	 */
	@RequestMapping("/updateType")
	public ModelAndView updateCouponType(HttpServletRequest req ,CouponType cType){
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		if(cType != null){
			String couponEndDate = req.getParameter("couponEndTime");
			if(StringUtils.isNotEmpty(couponEndDate)){
				cType.setCouponEndDate(DateTimeUtil.parse(couponEndDate, DateTimeUtil.defaultSimpleFormater));
			}
			Integer i = couponTypeService.updateCouponTypeById(cType);
			if(i != null && i.intValue() > 0){
				mv.addObject("msgObj", new MessageDTO(true, "操作结果", "修改成功！"));
			}else{
				mv.addObject("msgObj", new MessageDTO(false, "操作结果", "修改失败！"));
			}
		}else{
			mv.addObject("msgObj", new MessageDTO(false, "操作结果", "修改失败！"));
		}
		return mv;
	}
	
	/**
	 * 后台删除一个优惠券类型
	 * 2016年7月8日
	 * @return
	 */
	@RequestMapping(value = "/deleteType/{couponId}", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg deleteCouponType(@PathVariable("couponId")Integer couponId){
		ResultMsg rm = new ResultMsg();
//System.out.println(couponId);
		if(couponId != null){
			CouponItemDto cDto = new CouponItemDto();
			cDto.setCouponId(couponId);
			List<CouponItemDto> list = couponItemService.getCouponItemDtoList(cDto);
			if(list != null && list.size() > 0){
				rm.setStatus(false);
				rm.setMessage("该类型的优惠券已经被领取,不能删除!");
			}else{
				Integer i = couponTypeService.deleteCouponTypeById(couponId);
				if(i != null && i.intValue() > 0){
					rm.setStatus(true);
					rm.setMessage(ResultInfo.SUCCESSINFO);
				}else{
					rm.setStatus(false);
					rm.setMessage(ResultInfo.ERRORINFO);
				}
			}
		}else{
			rm.setStatus(false);
			rm.setMessage(ResultInfo.ERRORINFO);
		}
		return rm;
	}
	
	/**
	 * 后台进入详情页面
	 * 2016年7月8日
	 * @param couponId
	 * @return
	 */
	@RequestMapping(value = "/toTypeDetail/{couponId}", method = RequestMethod.GET)
	public ModelAndView toCouponTypeDetail(@PathVariable("couponId")Integer couponId){
		ModelAndView mv = new ModelAndView("admin/coupon/couponTypeDetail");
		if(couponId != null){
			CouponType cType = couponTypeService.getCouponTypeById(couponId);
			if(cType != null){
				mv.addObject("cType", cType);
			}
		}
		return mv;
	}
}
