<template>
  <el-dialog
    :visible.sync="modalIsShow"
    title="选择课程"
    top="10vh"
    width="800px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @opened="visibleChange"
  >
    <div class="tips-markedness mb-2">
      <p>
        请先选择开课学院后点击查询按钮，在查询结果列表里勾上需要添加的课程，再点击确认按钮。
      </p>
      <p>已经被添加的课程，不允许重复添加，复选框为不允许勾选状态！！！</p>
    </div>
    <el-form ref="FilterForm" label-position="left" inline label-width="0px">
      <el-form-item>
        <org-select v-model="filter.orgId" placeholder="开课学院"></org-select>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model.trim="filter.courseCodeOrName"
          placeholder="课程名称或代码"
          clearable
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" :disabled="!canSearch" @click="toPage(1)"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <div class="box-justify mb-2">
      <div>全部共{{ dataList.length }}门课</div>
      <div>已选{{ multipleSelection.length }}门</div>
    </div>
    <el-table
      ref="TableList"
      :data="dataList"
      border
      max-height="500px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        fixed="left"
        width="55"
        align="center"
        :selectable="(row) => row.canSelect"
      ></el-table-column>
      <el-table-column
        type="index"
        label="序号"
        width="70"
        :index="indexMethod"
      ></el-table-column>
      <el-table-column
        prop="courseName"
        label="课程名称"
        min-width="120"
      ></el-table-column>
      <el-table-column
        prop="courseCode"
        label="课程编码"
        min-width="120"
      ></el-table-column>
      <el-table-column
        prop="orgName"
        label="开课学院"
        min-width="120"
      ></el-table-column>
    </el-table>
    <div class="part-page">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :pager-count="5"
        :current-page="current"
        :total="total"
        :page-size="size"
        @current-change="toPage"
        @size-change="pageSizeChange"
      >
      </el-pagination>
    </div>

    <div slot="footer">
      <el-button type="primary" :disabled="isSubmit" @click="submit"
        >确认</el-button
      >
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { trainingPlanCourseQueryList, trainingPlanCourseSave } from "../../api";

export default {
  name: "add-training-plan-course",
  props: {
    rowData: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  computed: {
    canSearch() {
      return this.filter.orgId;
    },
  },
  data() {
    return {
      modalIsShow: false,
      filter: {
        cultureProgramId: "",
        orgId: "",
        courseCodeOrName: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      isSubmit: false,
      dataList: [],
      multipleSelection: [],
    };
  },
  methods: {
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    visibleChange() {
      this.filter.orgId = this.$ls.get("orgId", "");
      this.filter.cultureProgramId = this.rowData.id;
      this.toPage(1);
      this.$nextTick(() => {
        this.$refs.TableList?.clearSelection();
      });
    },
    async getList() {
      this.multipleSelection = [];
      this.dataList = [];
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await trainingPlanCourseQueryList(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    async submit() {
      if (!this.multipleSelection.length) {
        this.$message.error("请选择课程！");
        return;
      }

      if (this.isSubmit) return;
      this.isSubmit = true;
      const data = await trainingPlanCourseSave({
        cultureProgramId: this.rowData.id,
        courses: this.multipleSelection.map((item) => {
          return {
            courseId: item.courseId,
          };
        }),
      }).catch(() => {});
      this.isSubmit = false;

      if (!data) return;
      this.multipleSelection = [];
      this.$message.success("选择成功！");
      this.$emit("modified");
      this.cancel();
    },
  },
};
</script>
