<template>
  <div id="busIns">
    <el-form label-position="right" label-width="110px" :model="listData">
      <el-form-item
        v-for="(items, indexs) in listitem"
        :key="indexs"
        :label="items.label"
        :prop="items.prop"
      >
        <div v-if="items.scope === 'buss'">
          <el-select
            v-model="eduType"
            placeholder="请选择教育类型"
            @change="changeEduType"
          >
            <el-option
              v-for="(item, index) in eduTypeOptions"
              :key="index"
              :label="item.educationName"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <el-select
            v-model="courType"
            placeholder="请选择业务层次"
            @change="changecourseType"
          >
            <el-option
              v-for="(item, index) in newCourTypeOptions"
              :key="index"
              :label="item.businessName"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <el-popover
            ref="popovers"
            placement="bottom"
            trigger="click"
            @show="showHandle"
            @hide="hideHandle"
            :disabled="courType ? false : true"
          >
            <el-checkbox-group v-model="sujectArray" class="checkboxSty">
              <el-checkbox
                v-for="(item, index) in newSujectOption"
                :label="item.newId"
                :key="index"
                >{{ item.subjectName }}</el-checkbox
              >
            </el-checkbox-group>
            <div style="display: block; text-align: center; margin-top: 10px">
              <el-button size="mini" type="primary" @click="submitSujectArray"
                >确定</el-button
              >
            </div>
            <el-button slot="reference" style="margin-left: 12px"
              >请选择科目</el-button
            >
          </el-popover>
        </div>
        <div v-else>
          <div :class="changeHeight ? 'ach' : 'clh'">
            <div
              v-for="(item, index) in newSujectApis"
              :key="index"
              class="listBoxStys"
            >
              {{
                item.educationName +
                " - " +
                item.projectName +
                " - " +
                item.businessName +
                " - " +
                item.subjectName
              }}
              <i class="el-icon-error closeIcons" @click="closeType(index)"></i>
            </div>
          </div>
          <el-button
            size="mini"
            v-if="newSujectApis.length > 1"
            @click="changeType"
            >{{ changeHeight ? "展开" : "关闭" }}</el-button
          >
          <span v-if="newSujectApis.length === 0">未选项目类型</span>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: ["typeBus","sujectApisTable","newSujectApisTable"],
  data() {
    return {
      changeHeight: false,
      eduTypeOptions: [], //教育类型数据
      projectTypeOptions: [], //项目类型数据
      courTypeOptions: [], //业务层次数据
      newCourTypeOptions: [], //当前业务层次数据
      sujectOption: [], //科目数据
      newSujectOption: [], //当前科目数据数据
      eduType: "", //当前选中教育类型
      courType: "", //当前选中业务层次
      sujectApis: [], //当前存在的科目
      newSujectApis: [],
      sujectArray: [], //选中的科目
      listData: {},
      listitem: [
        {
          label: "适用业务层级",
          scope: "buss",
        },
        {
          label: "已选科目",
          scope: "activeBox",
        },
      ],
    };
  },
  watch:{
    "sujectApisTable": {
      handler(newVal, oldVal) {
        this.sujectApis = newVal
      },
      // 立即处理 进入页面就触发
      immediate: true,
    },
    "newSujectApisTable": {
      handler(newVal, oldVal) {
        this.newSujectApis = newVal
      },
      // 立即处理 进入页面就触发
      immediate: true,
    },
  },
  mounted() {
    this.getDict();
  },
  methods: {
    changeTypes() {
      var self = this;
      var arrays = [];
      this.sujectApis.map((item, index) => {
        this.courTypeOptions.map((items) => {
          if (items.id === item.split("-").map(Number)[0]) {
            var obj = {
              type: self.typeBus,
              educationTypeId: items.educationId,
              educationName: items.educationName,
              projectId: items.projectId,
              projectName: items.projectName,
              businessId: items.id,
              businessName: items.businessName,
            };
            self.sujectOption.map((i) => {
              if (
                i.id === item.split("-").map(Number)[1] &&
                i.courseArrays.indexOf(items.projectId) !== -1
              ) {
                obj.subjectName = i.subjectName;
                obj.subjectId = i.id;
              }
            });
            arrays.push(obj);
          }
        });
      });
      this.newSujectApis = arrays;
    },
    getDict() {
      this.$api.inquireCourseEducationType({ status: 1 }).then((res) => {
        this.eduTypeOptions = res.rows;
      });
      this.$api.inquireCourseProjectType({ status: 1 }).then((res) => {
        this.projectTypeOptions = res.rows;
      });
      this.$api.inquirebusinessList({ status: 1 }).then((res) => {
        this.courTypeOptions = res.rows;
        this.newCourTypeOptions = res.rows;
      });
      this.$api.inquireCourseSubject({ status: 1 }).then((res) => {
        res.rows.map((item, index) => {
          var array = [];
          item.courseProjectTypes.map((items, indexs) => {
            array.push(items.id);
          });
          item.courseArrays = array;
        });
        this.sujectOption = res.rows;
      });
    },

    changeType() {
      this.changeHeight = !this.changeHeight;
    },
    changeEduType() {
      if (!(this.courType === undefined || this.courType === "")) {
        this.courType = "";
      }
      var arrays = [];
      this.courTypeOptions.map((item) => {
        if (item.educationId === this.eduType) {
          arrays.push(item);
        }
      });
      this.newCourTypeOptions = arrays;
    },
    changecourseType() {
      this.newCourTypeOptions.map((item, index) => {
        if (item.id === this.courType) {
          this.eduType = item.educationId;
          var array = [];
          this.sujectOption.map((items, indexs) => {
            if (items.courseArrays.indexOf(item.projectId) !== -1) {
              array.push(items);
            }
          });
          this.newSujectOption = array;
        }
      });
      var arrays = [];
      this.courTypeOptions.map((item) => {
        if (item.educationId === this.eduType) {
          arrays.push(item);
        }
      });
      this.newCourTypeOptions = arrays;
      this.$refs.popovers[0].doClose();
      this.$emit("amdis", this.courType);
    },
    submitSujectArray() {
      var self = this;
      this.sujectApis = this.sujectApis.filter((item, index) => {
        return item.split("-").map(Number)[0] !== Number(self.courType);
      });
      for (let i = 0; i < this.sujectArray.length; i++) {
        this.sujectApis.push(this.sujectArray[i]);
      }
      this.$refs.popovers[0].doClose();
      this.changeTypes();
    },
    hideHandle() {},
    showHandle() {
      var array = [];
      for (let i = 0; i < this.sujectApis.length; i++) {
        if (
          this.sujectApis[i].split("-").map(Number)[0] === Number(this.courType)
        ) {
          array.push(this.sujectApis[i]);
        }
      }
      this.sujectArray = array;
      this.newSujectOption.map((item) => {
        item.newId = this.courType + "-" + item.id;
      });
    },
    closeType(index) {
      this.sujectApis.splice(index, 1);
      this.changeTypes();
    },
  },
};
</script>

<style lang="less" scoped>
.listBoxStys {
  flex-shrink: 0;
  padding: 0px 10px;
  border-radius: 8px;
  border: 1px solid #eee;
  margin-right: 10px;
  margin-bottom: 6px;
}
.closeIcons {
  color: red;
  cursor: pointer;
  margin-left: 6px;
}
.ach {
  display: flex;
  align-items: center;
  overflow: hidden;
}
.clh {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}
</style>
