package com.iamberry.wechat.handles.questionnaire;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

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.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iamberry.wechat.core.entity.questionnaire.QuestionnaireOption;
import com.iamberry.wechat.core.entity.questionnaire.QuestionnaireQuestionDto;
import com.iamberry.wechat.face.questionnaire.QuestionOptionService;
import com.iamberry.wechat.face.questionnaire.QuestionnaireService;

/**
 * @company	深圳爱贝源科技有限公司
 * @website www.iamberry.com
 * @author 	献
 * @tel		18271840547
 * @date	2016年10月26日
 * @explain	答题handler处理类，不需要微信的支持过滤
 */
@Controller
@RequestMapping("/admin/answer")
public class AnswerTitleHander {

	@Autowired
	private QuestionnaireService questionnaireService;
	@Autowired
	private QuestionOptionService questionOptionService;
	
	
	/**
	 * 分页查询题库所有题目信息
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getAllProblem")
	public ModelAndView getAllProblem(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/questionnaire/problemList");
		String text = request.getParameter("text");//题目标题
		String IsAllocated = request.getParameter("IsAllocated");//是否已分配
		
		QuestionnaireQuestionDto questionnaireQuestionDto = new QuestionnaireQuestionDto();
		questionnaireQuestionDto.getPage().setPageNumber(pageNO);
		questionnaireQuestionDto.getPage().setPageSize(pageSize);
		questionnaireQuestionDto.getPage().initRecordBegin();
		StringBuffer url = new StringBuffer("/admin/answer/getAllProblem?&pageSize=" + pageSize);
		if (null != text && !"".equals(text)) {
			if (!request.getMethod().equalsIgnoreCase("post")) {
				text = new String(text.getBytes("ISO-8859-1"), "UTF-8");
				questionnaireQuestionDto.setQuestionName(text);
				text = URLEncoder.encode(text, "UTF-8");
			} else {
				questionnaireQuestionDto.setQuestionName(text);
				text = URLEncoder.encode(text, "UTF-8");
				
			}
		}
		if (null != IsAllocated && !"".equals(IsAllocated)) {
				questionnaireQuestionDto.setQuestionPaperId(IsAllocated);
		
		}
		List<QuestionnaireQuestionDto> questionList = questionnaireService.getAllProblem(questionnaireQuestionDto);
		
		if (null != questionnaireQuestionDto.getQuestionName() && !"".equals(questionnaireQuestionDto.getQuestionName())) {
			mv.addObject("text", URLDecoder.decode(questionnaireQuestionDto.getQuestionName(), "UTF-8"));
			url.append("&text=" + URLDecoder.decode(questionnaireQuestionDto.getQuestionName(), "UTF-8"));
		}
		if (null !=IsAllocated && !"".equals(IsAllocated)) {
			mv.addObject("IsAllocated", IsAllocated);
		}
		url.append("&pageNO=");
		mv.addObject("questionList", questionList);
		mv.addObject("pageNO", pageNO);
		mv.addObject("url", url.toString());
		mv.addObject("pageSize", questionnaireService.getAllProblemCount(questionnaireQuestionDto));
		return mv;
	}
	
	/**
	 * 修改题目状态
	 * @param request
	 * @param response
	 * @param questionnaireQuestionDto
	 * @return
	 */
	@RequestMapping("/updateQuestionStatu")
	@ResponseBody
	public Map updateQuestionStatu(HttpServletRequest request,HttpServletResponse response,QuestionnaireQuestionDto questionnaireQuestionDto){
				Map map = new HashMap();
				String questionId = request.getParameter("questionId");
				String questionStatu = request.getParameter("questionStatu");
				if("".equals(questionId) || "".equals(questionStatu)){
					map.put("status", "修改失败！");
					return  map;
				}
				questionnaireQuestionDto.setQuestionId(Integer.valueOf(questionId));
				questionnaireQuestionDto.setQuestionStatu(Integer.parseInt(questionStatu));
				
				Integer msg = questionnaireService.updateQuestionStatu(questionnaireQuestionDto);
				if(msg == 0){
					map.put("status", "修改失败！");
					return  map; 
				}
				map.put("status", "修改成功！");
				return  map;
		
	}
	
	/**
	 * 进入添加页面
	 * @return
	 */
	@RequestMapping("/toAddInsertQuestion")
	public ModelAndView toAddInsertQuestion(){
		ModelAndView mv = new ModelAndView("/admin/questionnaire/addProblem");
		return mv;
	}
	
	/**
	 * 新增题目
	 * @param request
	 * @param response
	 * @param questionnaireQuestionDto
	 * @return
	 */
	@RequestMapping("/insertQuestion")
	@ResponseBody
	public Map insertQuestion(HttpServletRequest request,HttpServletResponse response){
		Map map = new HashMap();
		QuestionnaireQuestionDto questionnaireQuestionDto = new QuestionnaireQuestionDto();
		String questionName = request.getParameter("questionName");
		String questionPic = request.getParameter("questionPic");//图片顺序
		String questionDesc = request.getParameter("questionDesc");
		String questionStatu = request.getParameter("questionStatu");
		String questionCalculation = request.getParameter("questionCalculation");
		String questionType = request.getParameter("questionType");
		//获取问题选项
		String[] options = request.getParameterValues ("options");//题目选项
		String[] optionScore = request.getParameterValues ("score");//题目分数
		String[] optionIsChekcbox = request.getParameterValues ("optionIsChekcbox");//是否多选
		System.out.println(options);
		if(questionName == null || questionName.equals("")){
			map.put("status", "添加失败，题目名称不能为空！");
			return  map;
		}
		if(questionPic == null || questionPic.equals("")){
			map.put("status", "添加失败，题目图片不能为空！");
			return  map;
		}
		if(questionDesc == null || questionDesc.equals("")){
			map.put("status", "添加失败，题目简介不能为空！");
			return  map;
		}
		if(questionStatu == null || questionStatu.equals("")){
			map.put("status", "添加失败，题目状态不能为空！");                          
			return  map;
		}
		questionnaireQuestionDto.setQuestionName(questionName);
		questionnaireQuestionDto.setQuestionDesc(questionDesc);
		questionnaireQuestionDto.setQuestionCreateDate(new Date());
		questionnaireQuestionDto.setQuestionStatu(1);
		questionnaireQuestionDto.setQuestionCalculation(Integer.valueOf(questionCalculation));
		questionnaireQuestionDto.setQuestionOrder(Integer.valueOf(questionPic));
		questionnaireQuestionDto.setQuestionPic("http://static.iamberry.com/super_baby/images/q-"+questionPic+".jpg");
		questionnaireQuestionDto.setQuestionType(Integer.valueOf(questionType));
		questionnaireService.insertQuestion(questionnaireQuestionDto,options,optionScore,optionIsChekcbox);
		
		

	/*	if(msg == 0){
			map.put("status", "添加失败！");
			return  map; 
		}*/
		map.put("status", "添加成功！");
		return  map;
		
	}
	/**
	 * 进入修改页面
	 * @return
	 */
	@RequestMapping("/toAddUpdateQuestion")
	public ModelAndView toAddUpdateQuestion(HttpServletRequest request,HttpServletResponse response,QuestionnaireQuestionDto questionnaireQuestionDto){
		ModelAndView mv = new ModelAndView("/admin/questionnaire/updateProblem");
		String questionId = request.getParameter("questionId");
		if (null != questionId && !"".equals(questionId)) {
			questionnaireQuestionDto =	questionnaireService.getProblemById(Integer.valueOf(questionId));
		}
		mv.addObject("questionnaireQuestionDto", questionnaireQuestionDto);
		return mv;
	}
	
	
	
	/**
	 * 修改题目信息
	 * @param request
	 * @param response
	 * @param questionnaireQuestionDto
	 * @return
	 */
	@RequestMapping("/updateQuestion")
	@ResponseBody
	public Map updateQuestion(HttpServletRequest request,HttpServletResponse response,QuestionnaireQuestionDto questionnaireQuestionDto){
		Map map = new HashMap();
		String questionId = request.getParameter("questionId");
		String questionName = request.getParameter("questionName");
		String questionPic = request.getParameter("questionPic");
		String questionDesc = request.getParameter("questionDesc");
		String questionCreateDate = request.getParameter("questionCreateDate");
		String questionStatu = request.getParameter("questionStatu");
		String questionType = request.getParameter("questionType");
		if(questionId == null || questionId.equals("")){
			map.put("status", "修改失败！");
			return  map;
		}
		if(questionName == null || questionName.equals("")){
			map.put("status", "修改失败，题目名称不能为空！");
			return  map;
		}
		if(questionPic == null || questionPic.equals("")){
			map.put("status", "修改失败，题目图片不能为空！");
			return  map;
		}
		if(questionCreateDate == null || questionCreateDate.equals("")){
			map.put("status", "修改失败！");
			return  map;
		}
		if(questionDesc == null || questionDesc.equals("")){
			map.put("status", "修改失败，题目简介不能为空！");
			return  map;
		}
		questionnaireQuestionDto.setQuestionId(Integer.valueOf(questionId));
		questionnaireQuestionDto.setQuestionName(questionName);
		questionnaireQuestionDto.setQuestionDesc(questionDesc);
		questionnaireQuestionDto.setQuestionUpdateDate(new Date());
		questionnaireQuestionDto.setQuestionCreateDate(new Date(questionCreateDate));
		questionnaireQuestionDto.setQuestionStatu(Integer.valueOf(questionStatu));
		questionnaireQuestionDto.setQuestionOrder(Integer.valueOf(questionPic));
		questionnaireQuestionDto.setQuestionPic("http://static.iamberry.com/super_baby/images/q-"+questionPic+".jpg");
		questionnaireQuestionDto.setQuestionType(Integer.valueOf(questionType));
		Integer msg = questionnaireService.updateQuestion(questionnaireQuestionDto);
		
		if(msg == 0){
			map.put("status", "修改失败！");
			return  map; 
		}
		map.put("status", "修改成功！");
		return  map;
		
	}
	
	/**
	 * 删除题目
	 * @param request
	 * @param response
	 * @param questionnaireQuestionDto
	 * @return
	 */
	@RequestMapping("/deleteQuestion")
	@ResponseBody
	public Map deleteQuestion(HttpServletRequest request,HttpServletResponse response,QuestionnaireQuestionDto questionnaireQuestionDto){
		Map map = new HashMap();
		String questionId = request.getParameter("questionId");
		if(questionId == null || questionId.equals("")){
			map.put("status", "删除失败！");
			return map;
		}
		Integer msg = 	questionnaireService.deleteQuestion(Integer.valueOf(questionId));
		if(msg == 0){
			map.put("status", "删除失败！");
			return  map; 
		}
		map.put("status", "删除成功！");
		return map;
		
	}
	/**
	 * 修改题目顺序
	 * @param request
	 * @param response
	 * @param questionnaireQuestionDto
	 * @return
	 */
	@RequestMapping("/updatePaperOrder")
	@ResponseBody
	public Map updatePaperOrder(HttpServletRequest request,HttpServletResponse response,QuestionnaireQuestionDto questionnaireQuestionDto){
		Map map = new HashMap();
		String questionId1 = request.getParameter("questionId1");
		String questionId2 = request.getParameter("questionId2");
		String questionOrder1 = request.getParameter("questionOrder1");
		String questionOrder2 = request.getParameter("questionOrder2");
		if(questionId1 == null || questionId1.equals("")){
			map.put("status", "修改失败！");
			return map;
		}
		if(questionId2 == null || questionId2.equals("")){
			map.put("status", "修改失败！");
			return map;
		}
		if(questionOrder1 == null || questionOrder1.equals("")){
			map.put("status", "修改失败！");
			return map;
		}
		if(questionOrder2 == null || questionOrder2.equals("")){
			map.put("status", "修改失败！");
			return map;
		}
		questionnaireQuestionDto.setQuestionId(Integer.valueOf(questionId1));
		questionnaireQuestionDto.setQuestionOrder(Integer.valueOf(questionOrder1));
		Integer msg = 	questionnaireService.updatePaperOrder(questionnaireQuestionDto);
		questionnaireQuestionDto.setQuestionId(Integer.valueOf(questionId2));
		questionnaireQuestionDto.setQuestionOrder(Integer.valueOf(questionOrder2));
		msg = 	questionnaireService.updatePaperOrder(questionnaireQuestionDto);
		if(msg == 0){
			map.put("status", "修改失败！");
			return  map; 
		}
		map.put("status", "修改成功！");
		return map;
		
	}
	
}
