<template>
  <div>
    <el-form :model="form" ref="form" label-suffix=": " inline>
      <el-row>
        <el-col :span="10">
          <el-form-item
            label="班级名称"
            prop="name"
            :rules="[
              { required: true, message: '请输入班级名称', trigger: 'blur' },
            ]"
          >
            <el-input
              v-model="form.name"
              style="width: 100%"
              placeholder="请输入班级名称"
            />
          </el-form-item>
        </el-col>
        <el-col :span="10" :offset="4">
          <el-form-item label="声部">
            <el-select
              v-model="sound"
              style="width: 100%"
              clearable
              filterable
              placeholder="请选择声部"
            >
              <el-option
                v-for="(item, index) in soundList"
                :key="index"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-transfer
        filterable
        :titles="['所有学员', '已选学员']"
        filter-placeholder="请输入学生姓名"
        :filter-method="filterStudent"
        v-model="seleched"
        :render-content="renderFunc"
        :data="data"
      >
        <template #left-footer>
          <div class="footer line">
            <span>姓名</span>
            <span>性别</span>
            <span>专业</span>
          </div>
        </template>
        <template #right-footer>
          <div class="footer line">
            <span>姓名</span>
            <span>性别</span>
            <span>专业</span>
          </div>
        </template>
      </el-transfer>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$listeners.close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </div>
</template>
<script>
import { genderType } from "@/constant";
export default {
  props: ["studentList", "soundList", "activeType"],
  computed: {
    data() {
      return this.studentList.map((item) => ({
        value: item.id,
        key: item.id,
        name: item.name,
        subjectName: item.subjectName,
        gender: genderType[item.gender],
      }));
    },
  },
  data() {
    return {
      sound: [],
      form: {
        name: '',
      },
      seleched: [],
    };
  },
  methods: {
    filterStudent(query, item) {
      return (
        item.name.indexOf(query) > -1 ||
        item.subjectName.indexOf(query) > -1 ||
        item.gender.indexOf(query) > -1
      );
    },
    renderFunc(h, option) {
      return (
        <div class="line">
          <span>{option.name}</span>
          <span>{option.gender}</span>
          <span>{option.subjectName}</span>
        </div>
      );
    },
    submit() {
      if (this.activeType == 'HIGH_ONLINE' && (this.seleched.length < 3 || this.seleched.length > 5)) {
        return this.$message.error('线上技能班必须为3-5人')
      }
      if (this.seleched.length < 1) {
        return this.$message.error('请至少选择一名学生')
      }
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$listeners.close()
        }
      })
    }
  },
};
</script>
<style lang="less" scoped>
/deep/ .el-transfer {
  display: flex;
  align-items: center;
  .el-transfer__buttons {
    display: flex;
    width: 110px;
    flex-direction: column;
    > button {
      &:last-child {
        margin-left: 0;
      }
    }
  }
  .el-transfer-panel {
    width: 300px;
  }
}
.footer {
  margin-left: 35px;
  margin-right: auto;
  height: 40px;
  line-height: 40px;
}
/deep/ .line {
  width: 220px;
  display: flex;
  justify-content: space-around;
}
/deep/ .dialog-footer{
  margin-top: 20px;
  display: block;
  text-align: right;
}
</style>
