package com.iamberry.wechat.handles.questionnaire;

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

import com.alibaba.dubbo.common.extension.Activate;
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;

@Controller
@RequestMapping("/option")
public class optionHander {
	@Autowired
	private QuestionOptionService questionOptionService;
	@Autowired
	private QuestionnaireService questionnaireService;
	
	/**
	 * 根据题目id查询所有选项
	 * @param request
	 * @param response
	 * @param questionnaireOption
	 * @return
	 */
	@RequestMapping("/selectOptionById")
	public ModelAndView selectOptionById(HttpServletRequest request,HttpServletResponse response,QuestionnaireOption questionnaireOption){
		ModelAndView mv = new ModelAndView("/admin/questionnaire/questionOption");
		String optionQuesionId = request.getParameter("questionId");
		if("".equals(optionQuesionId) || "".equals(optionQuesionId)){
			return  mv;
		}
		List<QuestionnaireOption> questionnaireOptionList = questionOptionService.selectOptionById(Integer.valueOf(optionQuesionId));
		mv.addObject("questionnaireOptionList", questionnaireOptionList);
		
		QuestionnaireQuestionDto questionnaireQuestionDto =	questionnaireService.getProblemById(Integer.valueOf(optionQuesionId));
		mv.addObject("questionnaireQuestionDto", questionnaireQuestionDto);
		return mv;
		
	}
	
	/**
	 * 根据选项ID修改选项内容
	 * @param request
	 * @param response
	 * @param questionnaireOption
	 * @return
	 */
	@RequestMapping("/updateOptionById")
	@ResponseBody
	public Map updateOptionById(HttpServletRequest request,HttpServletResponse response,QuestionnaireOption questionnaireOption){
		Map map = new HashMap();
		String optionId = request.getParameter("optionId");
		String optionName = request.getParameter("optionName");
		String optionScore = request.getParameter("score");
		String optionIsChekcbox = request.getParameter("optionIsChekcbox");
		if("".equals(optionId) || "".equals(optionId)){
			map.put("status", "修改失败！");
			return  map;
		}
		if("".equals(optionName) || "".equals(optionName)){
			map.put("status", "修改失败！");
			return  map;
		}
		if("".equals(optionScore) || "".equals(optionScore)){
			map.put("status", "修改失败！");
			return  map;
		}
		questionnaireOption.setOptionId(Integer.valueOf(optionId));
		questionnaireOption.setOptionName(optionName);
		questionnaireOption.setOptionScore(Integer.valueOf(optionScore));
		questionnaireOption.setOptionIsChekcbox(Integer.valueOf(optionIsChekcbox));
		Integer msg = questionOptionService.updateOptionById(questionnaireOption);
		if(msg == 0){
			map.put("status", "修改失败！");
			return  map; 
		}
		map.put("status", "修改成功！");
		return map;
		
		
	}
	
	
	/**
	 * 根据选项ID排序
	 * @param request
	 * @param response
	 * @param questionnaireOption
	 * @return
	 */
	@RequestMapping("/optionSortingById")
	@ResponseBody
	public Map optionSortingById(HttpServletRequest request,HttpServletResponse response,QuestionnaireOption questionnaireOption){
		Map map = new HashMap();
		Integer msg = 0;
		String optionId = request.getParameter("optionId");
		String orderId = request.getParameter("orderId");
		String optionId2 = request.getParameter("optionId2");
		String orderId2 = request.getParameter("orderId2");
		if("".equals(optionId) || "".equals(optionId)){
			map.put("status", "修改失败！");
			return  map;
		}
		if("".equals(orderId) || "".equals(orderId)){
			map.put("status", "修改失败！");
			return  map;
		}
		
		questionnaireOption.setOptionId(Integer.valueOf(optionId));
		questionnaireOption.setOptionOrder(Integer.valueOf(orderId));
		msg = questionOptionService.optionSortingById(questionnaireOption);
		if(msg == 0){
			map.put("status", "修改失败！");
			return  map; 
		}
		if("".equals(optionId2) || "".equals(optionId2)){
			map.put("status", "修改失败！");
			return  map;
		}
		if("".equals(orderId2) || "".equals(orderId2)){
			map.put("status", "修改失败！");
			return  map;
		}
		
		questionnaireOption.setOptionId(Integer.valueOf(optionId2));
		questionnaireOption.setOptionOrder(Integer.valueOf(orderId2));
		msg = questionOptionService.optionSortingById(questionnaireOption);
		if(msg == 0){
			map.put("status", "修改失败！");
			return  map; 
		}
		map.put("status", "修改成功！");
		return map;
		
		
	}
	

}
