<template>
  <section class="content" style="margin-top: -18px;">
    <div class="box box-info">
      <!-- 头信息 -->
      <div
        class="box-header with-border"
        style="background-color:#d3dce6;margin-bottom:20px;"
      >
        <h2 class="box-title">课程统计</h2>
        <div class="box-tools pull-right">
          <button type="button" class="btn btn-box-tool" data-widget="collapse">
            <i class="fa fa-minus"></i>
          </button>
        </div>
      </div>

      <!-- 正文信息 -->
      <div class="box-body">
        <el-form
          :model="formSearch"
          :inline="true"
          label-position="right"
          label-width="100px"
        >
          <el-form-item label="学校">
            <el-select
              v-model="formSearch.orgId"
              placeholder="请选择"
              clearable
              @change="searchExamList(formSearch.orgId);"
            >
              <el-option
                v-for="item in orgList"
                :label="item.orgName"
                :value="item.orgId"
                :key="item.orgId"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="考试">
            <el-select
              v-model="formSearch.examId"
              @change="searchRecords"
              placeholder="请选择"
            >
              <el-option
                v-for="item in examList"
                :label="item.examName"
                :value="item.examId"
                :key="item.examId"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="试卷状态">
            <el-select
              v-model="formSearch.paperStatus"
              clearable
              placeholder="请选择"
            >
              <el-option label="请选择" value=""></el-option>
              <el-option
                v-for="item in coursePaperStatusList"
                :label="item.label"
                :value="item.value"
                :key="item.value"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="课程名称">
            <el-select
              v-model="formSearch.courseId"
              placeholder="请选择"
              filterable
              clearable
              remote
              :remote-method="searchCourseList"
            >
              <el-option label="请选择" value=""></el-option>
              <el-option
                v-for="item in courseList"
                :label="item.name"
                :value="item.id"
                :key="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="试卷名称">
            <el-input
              v-model="formSearch.paperName"
              style="width: 221px"
            ></el-input>
          </el-form-item>

          <el-form-item style="padding-left: 30px">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-search"
              @click="searchRecords"
              >查询
            </el-button>

            <el-button
              size="small"
              type="primary"
              icon="el-icon-menu"
              @click="openAllotAllDialog"
              >分配待指定试卷
            </el-button>

            <el-button
              size="small"
              type="primary"
              icon="el-icon-download"
              @click="openExportAllDialog"
              >整体导出
            </el-button>

            <el-button
              size="small"
              type="primary"
              icon="el-icon-download"
              @click="openExportDialog"
              >批量导出
            </el-button>

            <el-button
              size="small"
              type="primary"
              icon="el-icon-refresh"
              @click="refreshStatistic"
              >刷新
            </el-button>
          </el-form-item>
        </el-form>

        <!-- 数据列表 -->
        <el-table
          v-loading="loading"
          :data="tableData"
          element-loading-text="数据加载中"
          style="width:100%;"
          border
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column label="课程名称" prop="courseName" />
          <el-table-column width="150" label="课程代码" prop="courseCode" />
          <el-table-column width="80" label="试卷类型" prop="paperType" />
          <el-table-column width="80" label="试卷状态">
            <template slot-scope="scope">
              <span
                @click="openAllotDialog(scope.row);"
                style="cursor: pointer"
                >{{ scope.row.paperStatusName }}</span
              >
            </template>
          </el-table-column>
          <el-table-column width="200" label="试卷名称" prop="paperName" />
          <el-table-column width="80" label="试卷P数" prop="paperP" />
          <el-table-column width="100" label="考生人数" prop="totalStudent" />

          <el-table-column width="180" label="操作" :context="_self">
            <template slot-scope="scope">
              <el-button
                size="mini"
                icon="el-icon-view"
                v-show="scope.row.paperStatus > 1"
                @click="preview(scope.row);"
                >预览
              </el-button>

              <el-button
                size="mini"
                icon="el-icon-download"
                v-show="scope.row.paperStatus > 1"
                @click="download(scope.row);"
                >下载
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="page pull-right">
          <el-pagination
            @current-change="handlePager"
            :current-page="formSearch.pageNo"
            :page-size="formSearch.pageSize"
            :total="totalElements"
            layout="total, prev, pager, next, jumper"
          ></el-pagination>
        </div>
      </div>

      <el-dialog
        title="试卷指定"
        width="380px"
        :visible.sync="allotDialog"
        @close="closeAllotDialog"
      >
        <el-form
          :model="allotForm"
          ref="allotForm"
          :rules="rules"
          label-position="right"
          label-width="80px"
        >
          <el-form-item label="课程名称" prop="courseName">
            <el-input
              v-model="allotForm.courseName"
              :disabled="true"
              class="w220"
            >
              {{ allotForm.courseName }}</el-input
            >
          </el-form-item>

          <el-form-item label="试卷类型" prop="paperType">
            <el-input
              v-model="allotForm.paperType"
              :disabled="true"
              class="w220"
            >
              {{ allotForm.paperType }}</el-input
            >
          </el-form-item>

          <el-form-item label="试卷名称" prop="coursePaperId">
            <el-select
              v-model="allotForm.coursePaperId"
              placeholder="请选择"
              class="w220"
            >
              <el-option
                v-for="item in coursePaperList"
                :value="item.coursePaperId"
                :key="item.coursePaperId"
                :label="item.paperName"
              ></el-option>
            </el-select>
          </el-form-item>

          <div style="text-align: center">
            <el-button type="primary" @click="saveAllot">确 定</el-button>
            <el-button @click="closeAllotDialog">取 消</el-button>
          </div>
        </el-form>
      </el-dialog>

      <el-dialog title="试卷整体分配" :visible.sync="allotAllDialog">
        <el-form
          :model="allotAllForm"
          label-position="right"
          label-width="100px"
        >
          <el-row :gutter="10">
            <el-col :md="20">
              <el-form-item label="学校名称">
                <el-select
                  v-model="allotAllForm.orgId"
                  @change="searchExamList(allotAllForm.orgId, 2);"
                >
                  <el-option label="请选择" value></el-option>
                  <el-option
                    v-for="item in orgList"
                    :label="item.orgName"
                    :value="item.orgId"
                    :key="item.orgId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :md="20">
              <el-form-item label="考试名称">
                <el-select v-model="allotAllForm.examId">
                  <el-option label="请选择" value></el-option>
                  <el-option
                    v-for="item in examList"
                    :label="item.examName"
                    :value="item.examId"
                    :key="item.examId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="20">
              <el-form-item>
                <span style="font-size:20px;color: red;"
                  >警告！确定后，选中考试中待指定的课程会在推送试卷中随机选取一套试卷，是否确定执行？</span
                >
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col
              :span="24"
              class="text-center"
              style="border-top: 2px solid;padding-top: 10px;"
            >
              <el-button
                type="primary"
                :disabled="
                  allotAllForm.orgId === '' || allotAllForm.examId === ''
                "
                @click="saveAllotAll"
                >确定</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </el-dialog>

      <el-dialog title="导出" :visible.sync="exportDialog">
        <el-form :model="exportForm" label-position="right" label-width="80px">
          <el-checkbox-group v-model="exportForm.types">
            <el-checkbox label="1">试卷</el-checkbox>
            <el-checkbox label="2">答案</el-checkbox>
            <el-checkbox label="3">试卷结构</el-checkbox>
          </el-checkbox-group>
          <div style="text-align: center">
            <el-button type="primary" @click="doExport">确定导出</el-button>
            <el-button @click="closeExportDialog">取 消</el-button>
          </div>
        </el-form>
      </el-dialog>

      <el-dialog title="试卷整体导出" :visible.sync="exportAllDialog">
        <el-form
          :model="exportAllForm"
          label-position="right"
          label-width="100px"
        >
          <el-row :gutter="10">
            <el-col :md="20">
              <el-form-item label="学校名称">
                <el-select
                  v-model="exportAllForm.orgId"
                  @change="searchExamList(exportAllForm.orgId, 3);"
                >
                  <el-option label="请选择" value></el-option>
                  <el-option
                    v-for="item in orgList"
                    :label="item.orgName"
                    :value="item.orgId"
                    :key="item.orgId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :md="20">
              <el-form-item label="考试名称">
                <el-select
                  v-model="exportAllForm.examId"
                  @change="searchExamCoursePaperTotal"
                >
                  <el-option label="请选择" value></el-option>
                  <el-option
                    v-for="item in examList"
                    :label="item.examName"
                    :value="item.examId"
                    :key="item.examId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :md="20">
              <el-form-item label="试卷总数">{{
                exportAllForm.allNum
              }}</el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :md="20">
              <el-form-item label="已有试卷数">{{
                exportAllForm.existNum
              }}</el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :md="20">
              <el-form-item label="缺少试卷数">{{
                exportAllForm.missNum
              }}</el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="10">
            <el-col :md="20">
              <el-form-item label="导出内容">
                <el-checkbox-group v-model="exportAllForm.types">
                  <el-checkbox label="1">试卷</el-checkbox>
                  <el-checkbox label="2">答案</el-checkbox>
                  <el-checkbox label="3">试卷结构</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="10">
            <el-col
              :span="24"
              class="text-center"
              style="border-top: 2px solid;padding-top: 10px;"
            >
              <el-button
                type="primary"
                :disabled="exportAllForm.existNum == 0"
                @click="doExportAll"
                >确定导出</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </el-dialog>
    </div>
  </section>
</template>

<script>
import { CORE_API, PRINT_API } from "@/constants/constants";
import { userRole, coursePaperStatusList } from "../constants/constants.js";
import { mapState } from "vuex";

export default {
  data() {
    return {
      formSearch: {
        orgId: "",
        examId: "",
        courseId: "",
        paperStatus: "",
        paperName: "",
        pageNo: 1,
        pageSize: 10
      },
      curUserRole: userRole,
      hasPermit: false,
      totalElements: 0,
      loading: false,
      tableData: [],
      orgList: [],
      examList: [],
      courseList: [],
      coursePaperList: [],
      coursePaperStatusList: coursePaperStatusList,
      allotDialog: false,
      allotForm: {
        id: "",
        coursePaperId: "",
        courseName: "",
        paperType: ""
      },
      allotAllDialog: false,
      allotAllForm: {
        orgId: "",
        examId: ""
      },
      exportDialog: false,
      exportForm: {
        orgId: "",
        examId: "",
        courseCode: "",
        courseName: "",
        paperType: "",
        coursePaperId: "",
        courseId: "",
        id: ""
      },
      exportAllDialog: false,
      exportAllForm: {
        orgId: "",
        examId: "",
        exportList: [],
        allNum: 0,
        existNum: 0,
        missNum: 0,
        types: []
      },
      rules: {
        coursePaperId: [
          { required: true, message: "试卷不能为空！", trigger: "change" }
        ],
        courseName: [
          { required: true, message: "课程不能为空！", trigger: "change" }
        ],
        paperType: [
          { required: true, message: "试卷类型不能为空！", trigger: "change" }
        ]
      }
    };
  },
  methods: {
    searchExamCoursePaperTotal() {
      /* xxx */
    },
    openAllotDialog(row) {
      /* 试卷状态为"已有"时，打开试卷指定弹窗 */
      if (row.paperStatus > 0) {
        this.allotDialog = true;
        this.allotForm.id = row.id;
        this.allotForm.coursePaperId = row.coursePaperId;
        this.allotForm.courseName = row.courseName;
        this.allotForm.paperType = row.paperType;
        let params = {
          orgId: row.orgId,
          examId: row.examId,
          courseId: row.courseId
        };
        let url = PRINT_API + "/course/paper/list";
        this.$http.post(url, params).then(
          response => {
            this.coursePaperList = response.data;
          },
          error => {
            console.log(error);
          }
        );
      }
    },
    closeAllotDialog() {
      /* 关闭试卷指定弹窗 */
      this.allotDialog = false;
    },
    saveAllot() {
      /* 保存试卷指定 */
      this.$refs.allotForm.validate(valid => {
        if (!valid) {
          return false;
        }
        let url =
          PRINT_API +
          "/course/paper/allot/" +
          this.allotForm.id +
          "/" +
          this.allotForm.coursePaperId;
        this.$http.post(url).then(
          () => {
            this.$notify({
              message: "试卷指定成功！",
              type: "success"
            });
            this.allotDialog = false;
            this.searchRecords();
          },
          () => {
            this.$notify({
              message: "试卷指定失败！",
              type: "error"
            });
          }
        );
      });
    },
    openAllotAllDialog() {
      /* 打开分配待指定试卷弹窗 */
      this.allotAllDialog = true;
    },
    closeAllotAllDialog() {
      /* 关闭分配待指定试卷弹窗 */
      this.allotAllDialog = false;
    },
    saveAllotAll() {
      /* 保存分配待指定试卷 */
    },
    openExportDialog() {
      /* 打开批量导出弹窗 */
      this.exportDialog = true;
    },
    closeExportDialog() {
      /* 关闭批量导出弹窗 */
      this.exportDialog = false;
    },
    doExport() {
      /* 批量导出 */
    },
    openExportAllDialog() {
      /* 打开整体导出弹窗 */
      this.exportAllDialog = true;
    },
    closeExportAllDialog() {
      /* 关闭整体导出弹窗 */
      this.exportAllDialog = false;
    },
    doExportAll() {
      /* 整体导出 */
    },
    handlePager(current) {
      /* 处理分页 */
      this.formSearch.pageNo = current;
      this.searchRecords();
    },
    searchRecords() {
      /* 查询记录列表 */
      let orgId = this.formSearch.orgId;
      if (this.isEmptyNumber(orgId)) {
        this.$notify({
          message: "请选择学校！",
          type: "warning"
        });
        return;
      }

      let examId = this.formSearch.examId;
      if (this.isEmptyNumber(examId)) {
        this.$notify({
          message: "请选择考试！",
          type: "warning"
        });
        return;
      }

      this.loading = true;
      let url = PRINT_API + "/course/statistic/list";
      this.$http.post(url, this.formSearch).then(
        response => {
          this.tableData = response.data.content;
          this.totalElements = response.data.totalElements;
          this.loading = false;
        },
        error => {
          console.log(error);
          this.loading = false;
        }
      );
    },
    searchExamList(orgId) {
      /* 查询考试列表 */
      this.formSearch.examId = "";
      this.examList = [];
      this.tableData = [];

      if (!this.isEmptyNumber(orgId)) {
        let url = PRINT_API + "/printing/project/exam/list?orgId=" + orgId;
        this.$http.post(url).then(response => {
          this.examList = response.data;
        });
      }
    },
    searchCourseList(query) {
      /* 查询课程列表 */
      this.courseList = [];
      let url = CORE_API + "/course/query?enable=true";

      if (!this.isEmptyStr(query)) {
        url += "&name=" + query;
      }

      this.$http.get(url).then(response => {
        this.courseList = response.data;
      });
    },
    refreshStatistic() {
      /* 刷新当前统计信息 */
      this.$notify({
        message: "Todo...",
        type: "warning"
      });
    },
    preview(row) {
      /* 预览试卷PDF */
      console.log(row);
      this.$notify({
        message: "Todo...",
        type: "warning"
      });
    },
    download(row) {
      /* 下载试卷结构 */
      console.log(row);
      this.$notify({
        message: "Todo...",
        type: "warning"
      });
    }
  },
  computed: {
    ...mapState({ user: state => state.user })
  },
  created() {
    this.loadOrgList();

    this.loadUserRole(this.user);

    if (this.curUserRole.isSuperLeader || this.curUserRole.isPM) {
      this.hasPermit = true;
    } else {
      this.hasPermit = false;
    }
  }
};
</script>

<style scoped>
.page {
  margin-top: 10px;
}
.pull-right {
  float: right;
}
.pull-left {
  float: left;
}
.w220 {
  width: 220px;
}
</style>
