<template>
  <el-form
    ref="form"
    :model="value"
    :rules="rules"
    :label-width="labelWidth + 'px'"
  >
    <el-row>
      <el-col :span="24">
        <el-form-item label="所属领域:" required>
          <el-row type="flex" justify="space-between">
            <el-col :span="6">
              <el-form-item prop="industrys">
                <el-select
                  v-model="fields[0]"
                  value-key="id"
                  placeholder="请选择"
                  @change="levelIChange"
                >
                  <el-option
                    v-for="item in levelI"
                    :key="item.id"
                    :label="item.name"
                    :value="item"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-select
                v-model="fields[1]"
                value-key="id"
                placeholder="请选择"
                @change="levelIIChange"
              >
                <el-option
                  v-for="item in levelII"
                  :key="item.id"
                  :label="item.name"
                  :value="item"
                >
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="6">
              <el-select
                v-model="fields[2]"
                value-key="id"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in levelIII"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="3">
              <el-button type="primary" @click="fieldAdd">添加</el-button>
            </el-col>
          </el-row>
          <div class="e_tag">
            <el-tag
              v-for="(tag, index) in industrysTags"
              :key="index"
              closable
              @close="handleFieldClose(index)"
            >
              <template v-if="Array.isArray(tag)">
                <span v-for="(item, i) in tag" :key="item.id">
                  {{ item.name }}
                  <span v-if="tag.length != i + 1">></span>
                </span>
              </template>
              <template v-else>
                <span>{{ tag }}</span>
              </template>
            </el-tag>
          </div>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { industry } from "@/api/config";
export default {
  props: {
    value: Object,
    labelWidth: {
      type: Number,
      default: 120,
    },
  },
  data() {
    return {
      levelI: [], // I级数据
      levelII: [], // II级数据
      levelIII: [], // III级数据
      fields: [], // 当前下拉框选中的集合
      industrysTags: [], // 点击添加按钮后临时存储的数据集合
      rules: {
        industrys: [
          {
            type: "array",
            required: true,
            message: "请选择并添加",
            trigger: "change",
          },
        ],
      },
    };
  },
  watch: {
    value(newVal, oldVal) {
      let _key = [];
      let _value = [];
      for (let i = 0; i < newVal.industrys.length; i++) {
        const item = newVal.industrys[i];
        _key.push(item.key);
        _value.push(item.value);
      }
      newVal.industrys = _key;
      this.industrysTags = _value;
    },
  },
  methods: {
    getFieldByParent(id) {
      return new Promise((resolve, reject) => {
        industry({ parent_id: id })
          .then(({ code, msg, data }) => {
            if (code == 200) {
              resolve(data);
            } else {
              this.$modal.msgError(msg);
              reject({ msg, code });
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    async levelIChange(item) {
      delete this.fields[1];
      delete this.fields[2];
      this.levelII = await this.getFieldByParent(item.id);
    },
    async levelIIChange(item) {
      delete this.fields[2];
      this.levelIII = await this.getFieldByParent(item.id);
    },
    // 所属领域添加按钮
    fieldAdd() {
      if (!this.fields.length) return this.$modal.msgError("请选择领域类型");
      this.value.industrys = [];
      this.industrysTags.push(this.fields);
      for (let i = 0; i < this.industrysTags.length; i++) {
        this.value.industrys.push("");
        const item = this.industrysTags[i];
        for (let j = 0; j < item.length; j++) {
          const item2 = item[j];
          if (this.value.industrys[i] == "") {
            this.value.industrys[i] = item2.id;
          } else {
            this.value.industrys[i] = this.value.industrys[i] + "-" + item2.id;
          }
        }
      }
      this.fields = [];
      this.levelII = [];
      this.levelIII = [];
    },
    handleFieldClose(index) {
      this.industrysTags.splice(index, 1);
      this.value.industrys.splice(index, 1);
    },
    submitForm() {
      let flag = false;
      this.$refs["form"].validate((valid) => {
        flag = valid;
      });
      return flag;
    },
  },
  created() {
    industry().then((res) => {
      this.levelI = res.data;
    });
  },
};
</script>
<style lang="scss" scoped>
.e_tag {
  .el-tag {
    margin-right: 10px;
  }
}
</style>