<template>
  <div>
    <el-form :model="form" ref="form" label-suffix=": " inline>
      <el-row>
        <el-col :span="10" v-if="showName">
          <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="showName ? 4 : 0">
          <el-form-item label="声部">
            <el-select
              v-model="sound"
              style="width: 100%"
              clearable
              multiple
              collapse-tags
              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"
        :class="{hideReturn: !canDelUser}"
      >
        <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";
import { updateClassGroupStudents } from '../../api'
import { uniqBy } from 'lodash'
export default {
  props: ["studentList", "soundList", "activeType", 'activeListStudent', 'isOnlyChangeUser', 'classGroupId', 'type'],
  computed: {
    data() {
      return uniqBy([...this.studentList, ...this.activeListStudent], 'userId')
        .filter(item => {
          if (this.sound.length) {
            return this.sound.includes(item.actualSubjectId) || this.seleched.includes(item.userId)
          }
          return true
        })
        .map((item) => ({
        value: item.userId,
        key: item.userId,
        name: item.name,
        subjectName: item.subjectName,
        gender: genderType[item.gender],
      }));
    },
    showName() {
      return !this.isOnlyChangeUser && this.activeType != 'MUSIC_NETWORK'
    },
    canDelUser() {
      return this.$route.query.type !== 'resetTeam' || !this.isOnlyChangeUser
    }
  },
  data() {
    return {
      sound: [],
      form: {
        name: '',
      },
      seleched: [],
    };
  },
  watch: {
    activeListStudent() {
      this.setSelectedUser()
    },
  },
  mounted() {
    this.setSelectedUser()
  },
  methods: {
    setSelectedUser() {
      const ids = this.activeListStudent.map(item => item.userId)
      this.seleched = [...ids]
    },
    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">
          <el-tooltip class="item" effect="dark" placement="top" open-delay={300}>
            <span slot="content">{option.name}</span>
            <span class="select-item">{option.name}</span>
          </el-tooltip>
          <span>{option.gender}</span>
          <el-tooltip class="item" effect="dark" placement="top" open-delay={300}>
            <span slot="content">{option.subjectName}</span>
            <span class="select-item">{option.subjectName}</span>
          </el-tooltip>
        </div>
      );
    },
    change(val) {
      this.$listeners.changeActiveChioseSound(val)
      this.$listeners.searchStudent()
    },
    submit() {
      if (!this.isOnlyChangeUser && 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('请至少选择一名学生')
      }
      if (this.activeType == 'MUSIC_NETWORK' && this.seleched.length > 1) {
        return this.$message.error('乐团网管课仅可添加一名学生')
      }
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.isOnlyChangeUser && this.type !== 'change') {
            try {
              await updateClassGroupStudents({
                classGroupId: this.classGroupId,
                studentIds: this.seleched.join(',')
              })
              this.$message.success('提交成功')
            } catch (error) {}
            this.$listeners.submited()
          } else {
            this.$listeners.submited({
              seleched: this.seleched,
              name: this.form.name
            })
          }
          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;
}
.hideReturn{
  /deep/ .el-transfer__buttons button:first-child{
    display: none;
  }
}
/deep/ .el-checkbox-group{
  padding-bottom: 40px;
}
/deep/ .line {
  width: 220px;
  display: flex;
  justify-content: space-around;
  .select-item{
    width: 33.33333%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
/deep/ .dialog-footer{
  margin-top: 20px;
  display: block;
  text-align: right;
}
</style>
