<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;"
      >
        <h3 class="box-title">课程统计</h3>
        <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="请选择"
              filterable
              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" 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"></el-input>
          </el-form-item>

          <el-form-item class="pull-right">
            <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="openAllotPaperModel"
              >分配待指定试卷
            </el-button>

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

            <el-button
              size="small"
              type="primary"
              icon="el-icon-download"
              @click="openExportBatchModel"
              >批量导出
            </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">
              {{ scope.row.paperStatusName }}
            </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"
                type="primary"
                icon="el-icon-view"
                v-show="scope.row.paperStatus == '2'"
                @click="preview(scope.row);"
                >预览
              </el-button>

              <el-button
                size="mini"
                type="primary"
                icon="el-icon-download"
                v-show="scope.row.paperStatus == '2'"
                @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>
    </div>
  </section>
</template>

<script>
import {
  print_api,
  core_api,
  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: [],
      coursePaperStatusList: coursePaperStatusList,
      rules: {}
    };
  },
  methods: {
    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 = [];

      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"
      });
    },
    openAllotPaperModel() {
      /* 分配待指定试卷 */
      this.$notify({
        message: "Todo...",
        type: "warning"
      });
    },
    openExportBatchModel() {
      /* 批量导出 */
      this.$notify({
        message: "Todo...",
        type: "warning"
      });
    },
    openExportAllModel() {
      /* 整体导出 */
      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;
}
</style>
