<template>
  <section class="content">
    <!-- 正文信息 -->
    <div class="part-box">
      <h2 class="part-box-title">精确结构预设</h2>

      <el-form class="part-filter-form" :inline="true" :model="formSearch">
        <el-form-item label="结构名称">
          <el-input
            v-model="formSearch.name"
            placeholder="请输入结构名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="创建人">
          <el-input
            v-model="formSearch.creator"
            placeholder="请输入创建人"
          ></el-input>
        </el-form-item>
        <el-form-item label="制定课程">
          <el-select
            v-model="formSearch.courseId"
            filterable
            :remote-method="getCourses"
            remote
            clearable
            placeholder="请选择"
            @clear="getCourses('')"
          >
            <el-option label="公用" value="-1"></el-option>
            <el-option
              v-for="item in courseInfoSelect"
              :key="item.courseId"
              :label="item.courseInfo"
              :value="item.courseId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="searchFrom">查询</el-button>
          <el-button type="danger" plain @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button
          type="danger"
          plain
          icon="icon icon-delete"
          :disabled="noBatchSelected"
          @click="deletePapers"
          >删除
        </el-button>
        <el-button
          type="primary"
          icon="icon icon-plus-white"
          @click="addPaperStruct"
          >新增</el-button
        >
      </div>
    </div>

    <div class="part-box">
      <!-- 页面列表 -->
      <el-table
        v-loading="loading"
        :data="tableData"
        element-loading-text="拼命加载中"
        @selection-change="selectionChange"
      >
        <el-table-column
          type="selection"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column label="预设精确结构名称">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="关联课程" width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.courseName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="课程代码" width="90">
          <template slot-scope="scope">
            <span>{{ scope.row.courseNo }}</span>
          </template>
        </el-table-column>
        <el-table-column label="类型" width="80">
          <template slot-scope="scope">
            <span>{{ getType(scope.row.courseNo) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="大题数" width="88" sortable prop="detailCount">
        </el-table-column>
        <el-table-column
          label="小题数"
          width="88"
          sortable
          prop="detailUnitCount"
        >
        </el-table-column>
        <el-table-column label="总分" width="78" sortable prop="totalScore">
        </el-table-column>
        <el-table-column label="创建人" width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.creator }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="175" fixed="right">
          <template slot-scope="scope">
            <div class="operate_left">
              <el-button
                size="mini"
                type="primary"
                plain
                @click="editPaperStruct(scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                plain
                @click="deletePaper(scope.$index, scope.row)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100, 200, 300]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        ></el-pagination>
      </div>
    </div>
  </section>
</template>

<script>
import { QUESTION_API } from "@/constants/constants";

export default {
  data() {
    return {
      courseLoading: false,
      formSearch: {
        name: "",
        creator: "",
        courseId: null,
        type: "EXACT",
        courseName: "",
      },
      paperStructId: "",
      loading: false,
      tableData: [],
      selectedList: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      courseList: [],
      isClear: 0,
    };
  },
  computed: {
    selectedIds() {
      var selectedIdsStr = "";
      for (let id of this.selectedList) {
        if (!selectedIdsStr) {
          selectedIdsStr += id;
        } else {
          selectedIdsStr += "," + id;
        }
      }
      return selectedIdsStr;
    },
    courseInfoSelect() {
      var courseList = [];
      for (let course of this.courseList) {
        var courseInfo = course.name + "(" + course.code + ")";
        var courseId = course.id;
        courseList.push({ courseId: courseId, courseInfo: courseInfo });
      }
      return courseList;
    },
    noBatchSelected() {
      return this.selectedList.length === 0;
    },
  },
  watch: {
    $route: "initVue",
  },
  created() {
    this.initVue();
    this.removeItem();
  },
  methods: {
    //重置
    resetForm() {
      this.formSearch = {
        name: "",
        creator: "",
        courseId: null,
        type: "EXACT",
      };
    },
    //查询
    searchFrom() {
      this.currentPage = 1;
      this.searchPaperStructs();
    },
    searchPaperStructs() {
      var pageNo = Number(this.currentPage);
      this.currentPage = 1;
      this.loading = true;
      var url = QUESTION_API + "/paperStruct/" + pageNo + "/" + this.pageSize;
      this.$http.get(url, { params: this.formSearch }).then((response) => {
        this.tableData = response.data.content;
        this.total = response.data.totalElements;
        this.currentPage = pageNo;
      });
      this.loading = false;
    },
    //全选
    selectionChange(val) {
      this.selectedList = [];
      var selectedList = this.selectedList;
      val.forEach((element) => {
        selectedList.push(element.id);
      });
      this.selectedList = selectedList;
    },
    getCourseName(courseId) {
      for (let course of this.courseList) {
        if (course.courseId == courseId) {
          this.formSearch.courseName = course.name;
        }
      }
    },
    editPaperStruct(row) {
      this.getCourseName(this.formSearch.courseId);
      sessionStorage.setItem("paper_stucture", JSON.stringify(this.formSearch));
      sessionStorage.setItem("paper_stucture_currentPage", this.currentPage);
      sessionStorage.setItem("paperStruct", JSON.stringify(row));
      this.$router.push({
        path: "/questions/insert_paper_structure/" + row.id,
      });
    },
    //删除单条数据
    deletePaper(index, row) {
      this.$confirm("是否删除试卷结构?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        var url = QUESTION_API + "/paperStruct/" + row.id;
        this.loading = true;
        this.$http.delete(url).then(
          () => {
            this.$notify({
              type: "success",
              message: "删除成功!",
            });
            this.searchPaperStructs();
          },
          () => {
            this.$notify({
              type: "error",
              message: "删除失败!",
            });
          }
        );
      });
      this.loading = false;
    },
    //删除多条数据
    deletePapers() {
      this.$confirm("是否删除试卷结构？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      }).then(() => {
        this.loading = true;
        var url = QUESTION_API + "/paperStruct/" + this.selectedIds;
        this.$http
          .delete(url)
          .then(() => {
            this.$notify({
              type: "success",
              message: "删除成功！",
            });
            this.searchPaperStructs();
            this.selectedList = [];
          })
          .catch(() => {
            this.$notify({
              type: "error",
              message: "删除失败！",
            });
            this.selectedList = [];
          });
      });
      this.loading = false;
    },

    //新增
    addPaperStruct() {
      sessionStorage.setItem("paper_stucture", JSON.stringify(this.formSearch));
      sessionStorage.setItem("paper_stucture_currentPage", this.currentPage);
      this.$router.push({
        path: "/questions/insert_paper_structure/add",
      });
    },

    //分页跳转
    handleCurrentChange(val) {
      this.currentPage = val;
      this.searchPaperStructs();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.searchPaperStructs();
    },
    //查询所有课程
    getCourses(query) {
      this.courseLoading = true;
      this.$http
        .get(QUESTION_API + "/course/query?name=" + query + "&enable=true")
        .then((response) => {
          this.courseList = response.data;
          this.courseLoading = false;
        });
    },
    getType(val) {
      if (val == "ENSEMBLE") {
        return "宏观结构";
      }
      return "微观结构";
    },
    removeItem() {
      sessionStorage.removeItem("paperStruct");
    },
    initVue() {
      this.isClear = this.$route.params.isClear;
      if (this.isClear == 0 || !this.isClear) {
        sessionStorage.removeItem("paper_stucture");
        sessionStorage.removeItem("paper_stucture_currentPage");
        this.formSearch = {
          name: "",
          creator: "",
          courseId: null,
          type: "EXACT",
          courseName: "",
        };
        this.currentPage = 1;
        this.getCourses("");
      } else {
        this.formSearch = JSON.parse(sessionStorage.getItem("paper_stucture"));
        this.currentPage = parseInt(
          sessionStorage.getItem("paper_stucture_currentPage")
        );
      }
      if (this.formSearch.courseName) {
        this.getCourses(this.formSearch.courseName);
      } else {
        this.getCourses("");
      }
      this.searchPaperStructs();
    },
  },
};
</script>
