package com.iamberry.wechat.handles.questionnaire;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.apache.commons.lang.StringUtils;
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 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.order.MessageDto;
import com.iamberry.wechat.core.entity.questionnaire.QuestionnaireOption;
import com.iamberry.wechat.core.entity.questionnaire.QuestionnairePaper;
import com.iamberry.wechat.core.entity.questionnaire.QuestionnairePaperDto;
import com.iamberry.wechat.core.entity.questionnaire.QuestionnaireQuestion;
import com.iamberry.wechat.core.entity.questionnaire.QuestionnaireQuestionDto;
import com.iamberry.wechat.face.questionnaire.QuestionnairePaperService;
import com.iamberry.wechat.face.questionnaire.QuestionnaireService;
import com.iamberry.wechat.tools.ResultInfo;



/**
 * 后台管理-试卷管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin/questionPaper")
public class AdminPaperHandler {
	
	
	@Autowired
	private QuestionnairePaperService questionnairePaperService; //试卷管理
	
	@Autowired
	private QuestionnaireService questionnaireService; //试卷题目管理
	
	
	@RequestMapping("/list")
	public ModelAndView getQuestionnairePaperList(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/questionnaire/questionnairePaperList");
		String paperName = req.getParameter("paperName");
		String paperType = req.getParameter("paperType");
		QuestionnairePaper ct = new QuestionnairePaper();
		ct.setPage(new PageBean());
		ct.getPage().setPageSize(pageSize);
		ct.getPage().setPageNumber(pageNO);
		if(StringUtils.isNotEmpty(paperName)){ //试卷名称
			ct.setPaperName(paperName);
		}
		if(StringUtils.isNotEmpty(paperType)){ //试卷状态 1 有效 2无效
			ct.setPaperStatus(Integer.parseInt(paperType));
		}
		
		List<QuestionnairePaper> paperList = questionnairePaperService.getQuestionnairePaperList(ct);
		Integer count = questionnairePaperService.getQuestionnairePaperCount();
		
		mv.addObject("paperName", paperName);
		mv.addObject("paperType", paperType);
		mv.addObject("paperList", paperList);
		int pageCount =  count % ct.getPage().getPageSize() == 0? count/ct.getPage().getPageSize():(count/ct.getPage().getPageSize())+1;
		//分页用
		mv.addObject("pageNO", pageNO);
		mv.addObject("url", "/admin/questionPaper/list?pageSize=" + pageSize + "&pageNO=");
		mv.addObject("pageSize", pageCount);
		return mv;
	}
	
	
	/**
	 * 获取题目信息
	 * @param req
	 * @param pageSize
	 * @param pageNO
	 * @return
	 */
	@RequestMapping("/Questionlist")
	@ResponseBody
	public HashMap getQuestionnaireQuestionList(HttpServletRequest req,
			@RequestParam(value="pageSize",defaultValue= "100", required=false)Integer pageSize,
			@RequestParam(value="pageNO",defaultValue= "1", required=false)Integer pageNO){
//		ModelAndView mv = new ModelAndView("admin/questionnaire/questionnaireQuestionList");
//		String paperName = req.getParameter("paperName");
//		String paperStatus = req.getParameter("paperStatus");
//		PageBean ct = new PageBean();
//		ct.setPageSize(pageSize);
//		ct.setPageNumber(pageNO);
//		
//		List<QuestionnaireQuestion> questionList = questionnairePaperService.getQuestionnaireQuestionList(ct);
//		Integer count = questionnairePaperService.getQuestionnaireQuestionCount();
//		
//		mv.addObject("paperName", paperName);
//		mv.addObject("paperStatus", paperStatus);
//		mv.addObject("questionList", questionList);
//		int pageCount =  count % ct.getPageSize() == 0? count/ct.getPageSize():(count/ct.getPageSize())+1;
//		//分页用
//		mv.addObject("pageNO", pageNO);
//		mv.addObject("url", "/admin/questionPaper/Questionlist?pageSize=" + pageSize + "&pageNO=");
//		mv.addObject("pageSize", pageCount);
//		
		
		HashMap map=new HashMap();
		PageBean ct = new PageBean();
		ct.setPageSize(pageSize);
		ct.setPageNumber(pageNO);
		List<QuestionnaireQuestion> questionList = questionnairePaperService.getQuestionnaireQuestionList();
		Integer count = questionnairePaperService.getQuestionnaireQuestionCount();

		map.put("data",questionList);
		map.put("totalNumber", count);
		
		return map;
	}
	
	
	/**
	 * 根据题目id获取选项
	 * @return
	 */
	@RequestMapping("/getOptionByquestionID")
	@ResponseBody
	public ResultMsg getOptionByquestionID(HttpServletRequest req,Integer questionId){
		ResultMsg rm = new ResultMsg();
		List<QuestionnaireOption> optionList = questionnairePaperService.getOptionByquestionID(questionId);
		rm.setStatus(true);
		rm.setData(optionList);
		return rm;
	}
	
	
	
	/**
	 * 显示试卷的题目，答案，分数
	 * @param req
	 * @param pageSize
	 * @param pageNO
	 * @return
	 */
	@RequestMapping("/listPaper/{paperId}")
	public ModelAndView getQuestionnairePaperQuesOptiList(HttpServletRequest req,
			@PathVariable("paperId") Integer paperId){
		ModelAndView mv = new ModelAndView("admin/questionnaire/questionnairePaperQuesOptiList");
		
	
		List<QuestionnairePaperDto> paperList = questionnairePaperService.getQuestionnairePaperQuesOptiList(paperId);
		
		/*Integer listSize  = 1;
		for(int i = 0;i<=paperList.size()-1;i++ ){
			if(i!=paperList.size()-1){
				String flag  = paperList.get(i).getQuestionId();
				String flag2  = paperList.get(i+1).getQuestionId();
				if(!flag.equals(flag2)){
					listSize++;
				}
			}else{
				String flag  = paperList.get(i).getQuestionId();
				String flag2  = paperList.get(i-1).getQuestionId();
				if(!flag.equals(flag2)){
					listSize++;
				}
			}
		}*/
		mv.addObject("questionId", paperList.get(0).getQuestionId() == null? 1:2 );
		mv.addObject("paperList", paperList);
		mv.addObject("paperId", paperId);
		//mv.addObject("listSize", listSize);
		return mv;
	}
	
	

	/**
	 * 根据不同的类型，修改不同表的数据，用于修改试卷界面中的，题目，选项，分数
	 * @param req
	 * @return
	 */
	@RequestMapping("/toPaperUpdate")
	public ModelAndView toPaperUpdateQuestionnairePaper(HttpServletRequest req){
		String content = req.getParameter("content");
		
		try {
			content = new String(content.getBytes("iso_8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ModelAndView mv = new ModelAndView("admin/questionnaire/toPaperUpdate");
		mv.addObject("type", req.getParameter("type")); //1题目、2选项、3分数
		mv.addObject("id", req.getParameter("id"));   //主键
		mv.addObject("content", content); //修改的内容
		return mv;
	}
	
	/**
	 * 修改试卷
	 * @param id  主键id
	 * @param content 内容
	 * @param type 1 试题，2、选项 ，3、分数，4、题目是否多选
	 * @return
	 */

	@RequestMapping("/updatePaper")
	@ResponseBody
	public MessageDTO updateQuestionnairePaperAll(HttpServletRequest req ,
			QuestionnairePaperDto paperDto
			){
		
		Integer i = questionnairePaperService.updateQuestionnairePaperByType(paperDto);
		if(i != null && i.intValue() > 0){
			return new MessageDTO(true, "操作结果", "修改成功！");
		}
		return new MessageDTO(false, "操作结果", "修改失败！");
		
	}
	
	
	
	@RequestMapping("/toAdd")
	public ModelAndView toAddQuestionnairePaper(){
		ModelAndView mv = new ModelAndView("admin/questionnaire/addQuestionnairePaper");
		return mv;
	}
	@RequestMapping("/add")
	public ModelAndView addQuestionnairePaper(HttpServletRequest req,QuestionnairePaper QuestionnairePaper){
		ModelAndView mv = new ModelAndView("admin/msg/msg");
		
		int count = questionnairePaperService.insertQuestionnairePaper(QuestionnairePaper);
		if(count > 0){
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加成功！"));
		}else{
			mv.addObject("msgObj", new MessageDTO(true, "操作结果", "添加失败！"));
		}
		System.out.println(QuestionnairePaper);
		return mv;
	}
	
	/*
	 * 进入添加试卷题目页面
	 */
	@RequestMapping("/toPaperAdd")
	public ModelAndView toPaperAddQuestionnairePaper(String paperId){
		ModelAndView mv = new ModelAndView("admin/questionnaire/addPaperQuestion");
		HashMap map=new HashMap();
		List<QuestionnaireQuestion> questionList = questionnairePaperService.getQuestionnaireQuestionList();
		mv.addObject("questionList",questionList);
		mv.addObject("paperId",paperId);
		return mv;
	}
	
	/*
	 * 添加试卷题目
	 */
	@RequestMapping("/updatePaperQuestionById")
	@ResponseBody
	public Map updatePaperQuestionById(HttpServletRequest req){
		Map map = new HashMap();
		QuestionnaireQuestion questionnaireQuestion = new QuestionnaireQuestion();
		String questionPaperId = req.getParameter("questionPaperId");//题目ID
		String questionId = req.getParameter("questionId");//试卷ID
		String questionIsChekcbox = req.getParameter("questionIsChekcbox");//是否多选
		
		if("".equals(questionPaperId) || "".equals(questionPaperId)){
			map.put("status", "修改失败！");
			return  map;
		}
		if("".equals(questionId) || "".equals(questionId)){
			map.put("status", "修改失败！");
			return  map;
		}
		if("".equals(questionIsChekcbox) || "".equals(questionIsChekcbox)){
			map.put("status", "修改失败！");
			return  map;
		}
		questionnaireQuestion.setQuestionId(Integer.valueOf(questionId));
		questionnaireQuestion.setQuestionPaperId(Integer.valueOf(questionPaperId));
		questionnaireQuestion.setQuestionIsChekcbox(Integer.valueOf(questionIsChekcbox));
		
		Integer msg = questionnairePaperService.updatePaperQuestionById(questionnaireQuestion);
		if(msg == 0){
			map.put("status", "添加失败！");
			return  map; 
		}
		map.put("status", "添加成功！");
		return  map;
	}
	
	

	
	
	
	
	
	
	/**
	 * 后台进入修改页面
	 * 2016年7月8日
	 * @param paperId
	 * @return
	 */
	@RequestMapping(value = "/toUpdateType/{paperId}", method = RequestMethod.GET)
	public ModelAndView toUpdateQuestionnairePaper(@PathVariable("paperId")Integer paperId){
		ModelAndView mv = new ModelAndView("admin/questionnaire/updateQuestionnairePaper");
		if(paperId != null){
			QuestionnairePaper cType = questionnairePaperService.getQuestionnairePaperById(paperId);
			if(cType != null){
				mv.addObject("cType", cType);
			}
		}
		return mv;
	}
	/**
	 * 删除试卷题目
	 * @param request
	 * @param response
	 * @param questionnaireQuestionDto
	 * @return
	 */
	@RequestMapping("/deletePaperQuestion")
	@ResponseBody
	public Map deletePaperQuestion(HttpServletRequest request,HttpServletResponse response){
		Map map = new HashMap();
		String questionId = request.getParameter("questionId");
		if(questionId == null || questionId.equals("")){
			map.put("status", "删除失败！");
			return map;
		}
		Integer msg = 	questionnaireService.deletePaperQuestion(Integer.valueOf(questionId));
		if(msg == 0){
			map.put("status", "删除失败！");
			return  map; 
		}
		map.put("status", "删除成功！");
		return map;
		
	}
	
	
	
	
	
	/**
	 * 后台进入详情页面
	 * 2016年7月8日
	 * @param paperId
	 * @return
	 */
	@RequestMapping(value = "/toTypeDetail/{paperId}", method = RequestMethod.GET)
	public ModelAndView toQuestionnairePaperDetail(@PathVariable("paperId")Integer paperId){
		ModelAndView mv = new ModelAndView("admin/questionnaire/QuestionnairePaperDetail");
		if(paperId != null){
			QuestionnairePaper cType = questionnairePaperService.getQuestionnairePaperById(paperId);
			if(cType != null){
				mv.addObject("cType", cType);
			}
		}
		return mv;
	}
	
}
