package com.qmth.exam.reserve.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qmth.boot.api.annotation.Aac;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.boot.core.collection.PageResult;
import com.qmth.boot.core.exception.StatusException;
import com.qmth.exam.reserve.bean.applytask.TaskNoticeReq;
import com.qmth.exam.reserve.bean.applytask.TaskTimeReq;
import com.qmth.exam.reserve.bean.applytask.TimePeriodReq;
import com.qmth.exam.reserve.bean.login.LoginUser;
import com.qmth.exam.reserve.bean.task.ApplyTaskPageReq;
import com.qmth.exam.reserve.bean.task.ApplyTaskReq;
import com.qmth.exam.reserve.bean.task.ApplyTaskVO;
import com.qmth.exam.reserve.controller.BaseController;
import com.qmth.exam.reserve.service.ApplyTaskService;
import com.qmth.exam.reserve.service.TimePeriodService;
import com.qmth.exam.reserve.util.JsonHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@Api(tags = "预约任务相关接口")
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/admin/apply/task")
@Aac(strict = false, auth = true)
public class ApplyTaskController extends BaseController {

    @Autowired
    private ApplyTaskService applyTaskService;

    @Autowired
    private TimePeriodService timePeriodService;

    @ApiOperation(value = "预约任务查询")
    @PostMapping(value = "/page")
    public PageResult<ApplyTaskVO> page(@RequestBody ApplyTaskPageReq req) {
        return applyTaskService.page(req);
    }

    @ApiOperation(value = "预约任务编辑查询")
    @PostMapping(value = "/find")
    public ApplyTaskVO find(@ApiParam("预约任务ID") @RequestParam Long id) {
        return applyTaskService.find(id);
    }

    @ApiOperation(value = "基础规则新增/编辑")
    @PostMapping(value = "/rule/save")
    public void ruleSave(@RequestBody ApplyTaskReq req) {
        LoginUser user = this.curLoginUser();
        applyTaskService.saveRule(req, user);
    }

    @ApiOperation(value = "预约时段新增/编辑")
    @PostMapping(value = "/time/save")
    public void timeSave(@ApiParam("预约时段") @RequestBody TaskTimeReq req) {
        List<TimePeriodReq> timeList = JsonHelper.toList(req.getTimeJson(), TimePeriodReq.class);
        if (timeList.isEmpty())
            throw new StatusException("请填写时段");
        LoginUser user = this.curLoginUser();
        timePeriodService.saveTimePeriod(user.getId(), req.getId(), timeList);
    }

    @ApiOperation(value = "考试说明新增/编辑")
    @PostMapping(value = "/notice/save")
    public void noticesave(@ApiParam("预约说明") @RequestBody TaskNoticeReq req) {
        LoginUser user = this.curLoginUser();
        applyTaskService.saveNotice(user.getId(), req.getId(), req.getNotice());
    }

    @ApiOperation(value = "预约任务启用/禁用")
    @PostMapping(value = "/enable")
    public void enable(@ApiParam("预约任务ID") @RequestParam Long id, @ApiParam("预约任务的状态") @RequestParam Boolean enable) {
        LoginUser user = this.curLoginUser();
        applyTaskService.enable(user.getId(), id, enable);
    }

    @ApiOperation(value = "预约时段单个删除")
    @PostMapping(value = "/rule/delete")
    public void deleteRule(@ApiParam("时段ID") @RequestParam Long timeId) {
        applyTaskService.deleteRule(timeId);
    }

}
