<template>
  <el-dialog
    class="select-class-dialog"
    :visible.sync="modalIsShow"
    append-to-body
    top="20px"
    width="100%"
    title="选择班级"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
    @opened="visibleChange"
  >
    <el-transfer
      v-model="classIds"
      :data="dataList"
      :titles="['班级列表', '已选班级']"
      filterable
    ></el-transfer>

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

<script>
import { examStructureClassListPage } from "../api";

export default {
  name: "select-class-dialog",
  props: {
    filterData: {
      type: Object,
      default: null,
    },
    selectedIds: {
      type: Array,
      default() {
        return [];
      },
    },
    disableIds: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      dataList: [],
      classIds: [],
    };
  },
  mounted() {
    this.getClassData();
  },
  methods: {
    visibleChange() {
      this.classIds = [...this.selectedIds];
      this.dataList.forEach((item) => {
        item.disabled = this.disableIds.includes(item.id);
      });
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    // user
    async getClassData() {
      if (!this.filterData) return;
      const data = await examStructureClassListPage(this.filterData);
      this.dataList = data.map((item) => {
        return {
          id: item,
          key: item,
          label: item,
          disabled: false,
        };
      });
    },
    // confirm
    confirm() {
      this.$emit("confirm", this.classIds);
      this.cancel();
    },
  },
};
</script>
