package cn.com.qmth.stmms.subjectheader.marker;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

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

import cn.com.qmth.stmms.admin.exam.parameter.BaseParameterController;
import cn.com.qmth.stmms.biz.exam.model.Marker;
import cn.com.qmth.stmms.biz.exam.model.SubjectiveBlock;
import cn.com.qmth.stmms.biz.exam.service.ExamSubjectService;
import cn.com.qmth.stmms.biz.exam.service.MarkerService;
import cn.com.qmth.stmms.biz.exam.service.SubjectiveBlockService;
import cn.com.qmth.stmms.biz.subjectheader.model.SubjectHeader;
import cn.com.qmth.stmms.biz.subjectheader.service.SubjectHeaderService;
import cn.com.qmth.stmms.common.session.model.StmmsSession;
import cn.com.qmth.stmms.common.utils.RequestUtils;

@Controller
@RequestMapping("/subjectheader/marker")
public class MarkerController extends BaseParameterController {

    @Autowired
    private MarkerService markerService;

    @Autowired
    private SubjectHeaderService subjectHeaderService;

    @Autowired
    private SubjectiveBlockService subjectiveBlockService;

    @Autowired
    private ExamSubjectService examSubjectService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView markerList(HttpServletRequest request) {
        StmmsSession session = RequestUtils.getSession(request);
        SubjectHeader subjectHeader = RequestUtils.getWebUser(request).getSubjectHeader();
        int examId = Integer.parseInt(session.getParameter("examId"));
        String subjectCode = session.getParameter("subjectCode");
        List<SubjectiveBlock> sList = null;
        sList = subjectiveBlockService.findBlockByExamIdAndSubjectCode(examId, subjectCode);

        List<Marker> mList = markerService.findByExamAndSubject(subjectHeader.getExamId(),
                subjectHeader.getSubjectCode());
        boolean flag = false;
        if (examId > 0) {
            flag = isMarking(examId, subjectHeader.getSubjectCode());
        }
        ModelAndView view = new ModelAndView("subjectheader/marker/edit");
        view.addObject("sList", sList);
        view.addObject("subjectHeaderId", subjectHeader.getId());
        view.addObject("examId", subjectHeader.getExamId());
        view.addObject("flag", flag);
        return view;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject saveMarker(@RequestParam Integer id, @RequestParam String password, @RequestParam Boolean enable) {
        Marker marker = markerService.findById(id);
        JSONObject obj = new JSONObject();
        if (marker != null) {
            marker.setPassword(password);
            marker.setEnable(enable);
            markerService.save(marker);
            obj.accumulate("success", true);
        } else {
            obj.accumulate("success", false);
            obj.accumulate("message", "该评卷员不存在");
        }
        return obj;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject deleteMarker(@RequestParam Integer markerId) {
        Marker marker = markerService.findById(markerId);
        JSONObject obj = new JSONObject();
        if (marker != null) {
            markerService.deleteById(markerId);
            obj.accumulate("success", true);
        } else {
            obj.accumulate("success", false);
            obj.accumulate("message", "该评卷员不存在");
        }
        return obj;
    }

    @RequestMapping(value = "/multi-delete", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject deleteMarkers(@RequestParam Integer[] markerIds) {
        JSONObject obj = new JSONObject();
        if (markerIds != null) {
            for (int id : markerIds) {
                Marker marker = markerService.findById(id);
                if (marker != null) {
                    markerService.deleteById(id);
                }
            }
            obj.accumulate("success", true);
        } else {
            obj.accumulate("success", false);
        }
        return obj;
    }

    @RequestMapping("/remove")
    @ResponseBody
    public JSONObject removeMarker(@RequestParam Integer[] markerIds) {
        for (Integer markerId : markerIds) {
            markerService.deleteById(markerId);
        }
        return null;
    }
}
