<template>
  <el-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
      :props="dataProps"
    ></el-transfer>

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

<script>
export default {
  name: "select-class-by-course",
  props: {
    classList: {
      type: Array,
      default() {
        return [];
      },
    },
    selectedIds: {
      type: Array,
      default() {
        return [];
      },
    },
    disableIds: {
      type: Array,
      default() {
        return [];
      },
    },
    required: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      modalIsShow: false,
      dataList: [],
      classIds: [],
      dataProps: {
        key: "id",
        label: "name",
      },
    };
  },
  methods: {
    visibleChange() {
      this.classIds = [...this.selectedIds];
      this.dataList = this.classList.map((item) => {
        return {
          id: item,
          name: item,
          disabled: false,
        };
      });
      this.dataList.forEach((item) => {
        item.disabled = this.disableIds.includes(item.id);
      });
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    // confirm
    confirm() {
      if (this.required && !this.classIds.length) {
        this.$message.error("请选择班级");
        return;
      }
      this.$emit("confirm", this.classIds);
      this.cancel();
    },
  },
};
</script>
