package com.ruoyi.web.controller.exam;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.exam.dto.ExamDTO;
import com.ruoyi.system.domain.paper.dto.request.PaperAnswerDTO;
import com.ruoyi.system.domain.paper.dto.request.PaperListReqDTO;
import com.ruoyi.system.domain.paper.dto.request.PaperQuQueryDTO;
import com.ruoyi.system.domain.paper.dto.response.ExamResultRespDTO;
import com.ruoyi.system.domain.paper.dto.response.PaperListRespDTO;
import com.ruoyi.system.domain.repo.ElRepo;
import com.ruoyi.web.controller.manager.PaperManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * packageName com.ruoyi.web.controller.examination
 *
 * @author wangxy
 * @version JDK 8
 * @className PaperController
 * @date 2024/6/28
 * @description 试卷
 */
@Api("在线考试")
@Controller
@RequestMapping("/system/paper")
public class PaperController extends BaseController {

    private String prefix = "system/elExam/paper";


    @Resource
    private PaperManager paperManager;


    @ApiOperation("去考试")
    @GetMapping("/toPaper/{examId}/{paperId}")
    public String toPaper(@PathVariable("examId") String examId, @PathVariable String paperId,ModelMap mmap ) {
        mmap.put("examId", examId);
        mmap.put("paperId", paperId);
        return prefix + "/paper";
    }

    @ApiOperation("在线考试")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PaperListReqDTO paperListReqDTO) {
        startPage();
        List<PaperListRespDTO> list = paperManager.paging(paperListReqDTO);
        return getDataTable(list);
    }



    @ApiOperation("创建试卷")
    @Log(title = "创建试卷", businessType = BusinessType.INSERT)
    @GetMapping("/create/{examId}")
    @ResponseBody
    public AjaxResult create(@PathVariable("examId") String examId) {
        return AjaxResult.success(paperManager.createPaper(examId,getUserId()));
    }

    @ApiOperation("试卷详情")
    @GetMapping("/detail/{paperId}")
    @ResponseBody
    public AjaxResult detail(@PathVariable("paperId") String paperId) {
        return AjaxResult.success(paperManager.paperDetail(paperId));
    }

    @ApiOperation("试题详情")
    @PostMapping("/quDetail")
    @ResponseBody
    public AjaxResult quDetail(@RequestBody @Validated PaperQuQueryDTO reqDTO) {
        return AjaxResult.success(paperManager.findQuDetail(reqDTO.getPaperId(), reqDTO.getQuId()));
    }

    @ApiOperation("保存答案")
    @PostMapping("/fillAnswer")
    @ResponseBody
    public AjaxResult fillAnswer(@RequestBody @Validated PaperAnswerDTO reqDTO) {
        return toAjax(paperManager.fillAnswer(reqDTO));
    }


    @ApiOperation("交卷")
    @GetMapping("/submitExam/{paperId}")
    @Log(title = "交卷", businessType = BusinessType.INSERT)
    @ResponseBody
    public AjaxResult submitExam(@PathVariable("paperId") String paperId) {
        return toAjax(paperManager.submitExam(paperId));
    }


    @ApiOperation("详情")
    @GetMapping("/toView/{paperId}")
    public String toView(@PathVariable("paperId") String paperId, ModelMap mmap) {
        mmap.put("paperId", paperId);
        return prefix + "/view";

    }

    @ApiOperation("详情")
    @GetMapping("/paperResult/{paperId}")
    @ResponseBody
    public AjaxResult paperResult(@PathVariable("paperId") String paperId) {
        return AjaxResult.success( paperManager.paperResult(paperId));
    }


    @ApiOperation("检测进行中的考试")
    @GetMapping("/checkProcess")
    @ResponseBody
    public AjaxResult checkProcess() {
        return AjaxResult.success(paperManager.checkProcess(getUserId()));
    }












}
