<template>
  <div id="cascaderMap">
    <span class="buildFloor">设备类别：</span>
    <el-cascader
      placeholder="请选择"
      :options="options"
      v-model="value"
      :props="props"
      filterable
      :style="isWidth ? '' : 'width:160px;'"
      size="small"
      @change="changeVal"
      change-on-select
    ></el-cascader>
  </div>
</template>
<script>
import { getEqCode } from "@/api/request";
export default {
  name: "getCode",
  props: {
    isWidth: {
      type: Boolean,
      default: true
    },
    isAll: {
      type: Boolean,
      default: false,
    }
  },
  data() {
    return {
      value: [""],
      options: [],
      props: {
        value: "code",
        label: "facility"
      },
      falg: true,
      content: []
    };
  },
  created() {
    this.getData();
  },
  mounted() { },
  methods: {
    setValue(val) {
      this.value = val
    },
    //修改val
    changeVal(val) {
      let value = {}
      this.options.map(item => {
        if (item.code == val) {
          value = item
        }
      })
      this.value = val
      this.$emit("change", value)
    },
    async getData() {
      await getEqCode().then(res => {
        if (this.isAll) {
          let data = this.changeArr(res.data.Content)
          data.unshift({
            code: "all",
            facility: "全部"
          });
          this.options = data
          console.log(this.options, "options")
          this.value = ["all"]
        } else {
          this.options = this.changeArr(res.data.Content);
        }
        if (!!this.value && this.value.length) {
          let value = {}
          this.options.map(item => {
            if (item.code == this.value[0]) {
              value = item
            }
          })
          this.$emit("change", value)
        }
        if (!this.falg) {
          this.changeList()
        }
      });
    },
    changeArr(arr) {
      let data = [];
      arr.map(item => {
        if (item.content && item.content.length) {
          return item.content.map(children => {
            if (children.content && children.content.length) {
              return children.content.map(i => {
                data.push(i);
              });
            }
          });
        }
      });
      return data;
    },

    pushData(content) {
      this.content = content
      if (this.options.length) {
        this.falg = true
        this.changeList()
      } else {
        this.falg = false
      }
    },

    changeList() {
      this.options.map(item => {
        item.disabled = true
        this.content.map(child => {
          if (item.code == child) {
            item.disabled = false
          }
        })
      })
    }
  }
};
</script>
<style lang="less" scoped>
#cascaderMap {
  float: left;
  margin-left: 10px;
  .buildFloor {
    color: #999999;
    font-size: 14px;
  }
}
</style>
