<template>
  <div>
    <el-dialog
      title="选择学员"
      append-to-body
      width="800px"
      :visible.sync="transStudentVisible"
    >
      <el-form
        :model="form"
        ref="form"
        label-suffix=": "
        inline
        label-width="90px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item
              :label="'声部筛选'"
              style="margin-right: 0"
              prop="sound"
            >
              <select-all
                v-model="form.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>
              </select-all>
            </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>
    </el-dialog>
  </div>
</template>
<script>
import { genderType } from "@/constant";
import { uniqBy } from "lodash";
export default {
  props: ["studentList", "soundList", "activeType", "activeListStudent"],
  computed: {
    data() {
      return uniqBy([...this.studentList, ...this.activeListStudent], "userId")
        .filter((item) => {
          if (this.form.sound.length) {
            return (
              this.form.sound.includes(item.subjectId) ||
              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: "",
        sound: [],
      },
      seleched: [],
      transStudentVisible: true,
    };
  },
  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 < 1 || this.seleched.length > 6)
      ) {
        return this.$message.error("线上技能班必须为1-6人");
      }
      this.$emit("submit", this.seleched);
      this.transStudentVisible = false
    },
    openDialog() {
      this.transStudentVisible = true;
    },
  },
};
</script>
<style lang="less" scoped>
::v-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 {
  ::v-deep .el-transfer__buttons button:first-child {
    display: none;
  }
}
::v-deep .el-checkbox-group {
  padding-bottom: 40px;
}
::v-deep .line {
  width: 220px;
  display: flex;
  justify-content: space-around;
  height: 30px;
  line-height: 30px;
  .select-item {
    width: 33.33333%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
::v-deep .dialog-footer {
  margin-top: 20px;
  display: block;
  text-align: right;
}
</style>
