<template>
  <div style="width: 100%;">
    <div class="showDiv">
      <div class="flex" style="flex-wrap: wrap;">
        <div class="flex" style="margin-right: 20px;align-items: center;padding: 8px 0;" v-for="(item, index) in activeTeacherData" :key="index">
          <div class="typeItem">{{ item.user_realname }}</div>
          <el-icon color="#00A3FF" class="pointer" @click="handleRemoveTeacher(index)">
            <CircleClose />
          </el-icon>
        </div>
      </div>
      <el-icon class="drop pointer" color="#a8abb2" :size="12" @click="showTeacher = !showTeacher">
        <ArrowDownBold />
      </el-icon>
    </div>
    <div class="jdlsDiv" v-show="showTeacher">
      <div class="operate">
        <div class="searchValue">
          <input type="text" placeholder="搜索关键字" v-model="searchKey">
          <div class="searchBtn" @click="initTeacherData(false)">
            <img src="/kczy/searchIcon.png" alt="">
          </div>
        </div>
        <div>
          <el-button @click="backTeacher">
            <el-icon>
              <Back />
            </el-icon>
          </el-button>
          <el-button @click="nextTeacher">
            <el-icon>
              <Right />
            </el-icon>
          </el-button>
        </div>
      </div>
      <div v-if="teacherData.length != 0">
        <el-checkbox-group v-model="activeTeacher" @change="initActiveTeacher">
          <el-checkbox v-for="(item, index) in teacherData" :key="index" :label="JSON.stringify(item)">{{
                    item.user_realname
                  }}({{ item.user_name }})</el-checkbox>
        </el-checkbox-group>
      </div>
      <el-empty v-if="teacherData.length == 0" :image-size="100" description="无数据" style="padding: 10px 0;" />
    </div>
  </div>
</template>

<script>
import { Picture, FolderOpened, ArrowDownBold, CircleClose, Back, Right } from "@element-plus/icons-vue";
export default {
  data() {
    return {
      showTeacher: true,
      teacherData: [],
      activeTeacher: [],
      activeTeacherData: [],
      searchKey: "",
      currentPage: 1,
      totalPage: 0,
      compareData: [],
    }
  },
  props: ["modelValue", "compare"],
  watch: {
    modelValue: {
      immediate: true,
      handler(val) {
        if (val) {
          this.activeTeacherData = JSON.parse(val);
        }
        this.activeTeacher = this.activeTeacherData.map(item => JSON.stringify(item));
      }
    },
    compare: {
      immediate: true,
      handler(val) {
        if (val) {
          this.compareData = JSON.parse(val);
        }
      }
    }
  },
  methods: {
    backTeacher() {
      if (this.currentPage == 1) {
        return false;
      }
      this.currentPage--;
      this.initTeacherData();
    },
    nextTeacher() {
      if (this.currentPage == this.totalPage) {
        return false;
      }
      this.currentPage++;
      this.initTeacherData();
    },
    // 老师
    initTeacherData(flag = true) {
      if (!flag) {
        this.currentPage = 1;
      }
      request({
        url: '/user/main/index',
        data: {
          // user_role_id: "75",
          is_not_stu: "1",
          user_realname: this.searchKey,
          page: this.currentPage,
          limit: 20
        }
      }).then(res => {
        if (res.code == 1) {
          this.teacherData = [];
          res.data.page_data.map((item) => {
            let obj = {
              user_id: item.user_id,
              user_realname: item.user_realname,
              user_name: item.user_name
            }
            this.teacherData.push(obj);
          })
          this.totalPage = res.data.total_page;
        }
      })
    },
    handleRemoveTeacher(index) {
      this.activeTeacherData.splice(index, 1);
      this.activeTeacher.splice(index, 1);
      let json = this.activeTeacherData.length != 0 ? JSON.stringify(this.activeTeacherData) : "";
      this.$emit('update:modelValue', json);
    },
    initActiveTeacher(value) {
      let activeTeacherData = value.map(item => JSON.parse(item));
      this.activeTeacherData = activeTeacherData.filter((val, i) => {
        let index = this.compareData.findIndex(item => val.user_id == item.user_id);
        if (index > 0) {
          this.$message.error("客座名师与全科教师不能重复！");
          this.activeTeacher.splice(i, 1);
        }
        return index < 0
      })
      let json = this.activeTeacherData.length != 0 ? JSON.stringify(this.activeTeacherData) : "";
      this.$emit('update:modelValue', json);
    },
  },
  created() {
    this.initTeacherData();
  }
}
</script>

<style lang="scss" scoped>
.pointer {
  cursor: pointer;
}
.showDiv {
  position: relative;
  width: 100%;
  min-height: 44px;
  background: #f9f9f9;
  border-radius: 6px;
  padding: 0 30px 0 15px;

  .el-icon.drop {
    position: absolute;
    right: 15px;
    top: 15px;
  }

  .typeItem {
    background: #00a3ff;
    color: #fff;
    font-size: 13px;
    padding: 3px 8px;
    border-radius: 6px;
    line-height: 22px;
    margin-right: 8px;
  }
}

.jdlsDiv {
  border: 1px solid #e4e6e8;
  border-radius: 6px;
  padding: 10px 12px;
  width: 100%;

  .operate {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;

    .el-button {
      width: 32px;
      height: 32px;
    }
  }

  .searchValue {
    width: 278px;
    height: 40px;
    background: #ffffff;
    border-radius: 8px;
    overflow: hidden;
    position: relative;

    input {
      width: 100%;
      height: 100%;
      outline: 0;
      box-sizing: border-box;
      padding-left: 10px;
      background: #f6f8fa;
    }

    .searchBtn {
      width: 26px;
      height: 26px;
      background: #00a3ff;
      border-radius: 6px;
      position: absolute;
      right: 5px;
      top: 50%;
      transform: translate(-50%, -50%);
      text-align: center;
      vertical-align: middle;
      cursor: pointer;

      img {
        display: block;
        text-align: center;
        margin: 6px auto 0;
      }
    }
  }

  .el-checkbox.el-checkbox--large {
    min-width: 80px;
    margin-right: 25px;
  }
}
</style>