<template>
  <div>
    <el-form :model="form" ref="form" label-suffix=": " inline label-width="90px">
      <el-row>
        <el-col :span="10">
          <el-form-item
            label="主教老师"
            prop="coreTeacher"
            v-if="!isOnlyChangeUser"
            :rules="[{ required: true, message: '请选择主教老师' }]"
          >
            <el-select
              v-model.trim="form.coreTeacher"
              placeholder="请选择主教老师"
              clearable
              filterable
            >
              <el-option
                v-for="(item, index) in teacherList"
                :key="index"
                :label="item.realName"
                :value="String(item.id)"
              >
                <span style="float: left">{{ item.realName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{
              String(item.id)
            }}</span>
              </el-option>
            </el-select>
            <!-- <remote-search :commit="'setTeachers'" v-model="form.coreTeacher"  /> -->
          </el-form-item>
        </el-col>
        <!-- :offset="4" -->
        <el-col :span="10" >
          <el-form-item
            label="助教老师"
            prop="assistant"
            v-if="!isOnlyChangeUser && activeType!='HIGH'&&activeType!='HIGH_ONLINE'&&activeType!='MUSIC_NETWORK'"
          >
            <!-- <remote-search :commit="'setTeachers'" v-model="form.assistant"  :multiple='true'/> -->
            <el-select
              v-model.trim="form.assistant"
              placeholder="请选择助教老师"
              filterable
              clearable
              collapse-tags
              multiple
            >
              <el-option
                v-for="(item, index) in cooperationList"
                :key="index"
                :label="item.realName"
                :value="item.id"
              >
                <span style="float: left">{{ item.realName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{
              String(item.id)
            }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <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>
        <!-- :offset="showName ? 4 : 0" -->
        <el-col :span="10" >
          <el-form-item label="声部" style="margin-right: 0;" >
            <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
      v-if="!showName"
        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', 'teacherList', 'cooperationList', 'isStudentRemove'],
  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() {
      // console.log(this.isStudentRemove)
      return this.$route.query.type !== 'resetTeam' || !this.isOnlyChangeUser || this.isStudentRemove
    }
  },
  data() {
    return {
      sound: [],
      form: {
        name: '',
        coreTeacher: '',
        assistant: '',
      },
      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 && !this.isStudentRemove) {
      //   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()
            this.$listeners.close()
          } else {
            this.$listeners.submited({
              seleched: this.seleched,
              ...this.form,
              soundList:this.sound
            })
          }
        }
      })
    }
  },
};
</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>
