<template>
  <div id="cascaderMap">
    <span class="buildFloor" style="margin-right: 12px">设备类别</span>
    <slot></slot>
    <el-select
      v-model="value"
      placeholder="请选择"
      clearable
      :props="props"
      filterable
      :style="isWidth ? '' : 'width:160px;'"
      size="small"
      @change="changeVal"
    >
      <el-option
        v-for="item in trueOptions"
        :key="item.code"
        :label="item.facility"
        :value="item.code"
      ></el-option>
    </el-select>
    <el-checkbox v-model="checked" @change="checkChange"
      >仅项目交付范围内</el-checkbox
    >
    <!-- <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 { mapGetters } from "vuex";
import DicController from "@/controller/old-adm/dicController";
import EquipController from "@/controller/old-adm/equipController";

export default {
  name: "getCode",
  props: {
    isWidth: {
      type: Boolean,
      default: true,
    },
    all: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    ...mapGetters("layout", ["projectId"]),
  },
  data() {
    return {
      value: "",
      options: [],
      props: {
        value: "code",
        label: "facility",
      },
      falg: true,
      content: [],
      checked: true,
    };
  },
  computed: {
    trueOptions() {
      if (this.options.length) {
        return this.options.filter((i) => {
          return this.checked
            ? i.hasOwnProperty("isDelivery") && i.isDelivery === 1
            : i;
        });
      }
      return [];
    },
  },
  created() {
    this.getData();
  },
  watch: {
    projectId() {
      this.value = "";
      this.getData();
    },
  },
  methods: {
    setValue(val) {
      this.value = val;
    },
    //修改val
    changeVal(val) {
      let value = {};
      let flag = true;
      this.options.map((item) => {
        if (item.code == val) {
          flag = false;
          value = item;
        }
      });
      this.value = val;
      if (flag) {
        this.value = "";
      }
      this.$emit("change", value);
    },
    //获取当前项目下的设备类型(只拿到编码-需要过滤)
    getData(val) {
      let param2 = {
        distinct: true,
        pageNumber: 1,
        pageSize: 1000,
        projection: ["classCode"],
      };
      if (val && val[0] != "all") {
        param2.filters = "";
        if (val[0] == "noKnow") {
          param2.filters += `;buildingId isNull`;
        } else if (val[0] && val[0] != "all") {
          param2.filters += `;buildingId='${val[0]}'`;
        }
        if (val[1] == "noKnow") {
          param2.filters += `;floorId isNull`;
        } else if (val[1] && val[1] != "all") {
          param2.filters += `;floorId='${val[1]}'`;
        }
        param2.filters = param2.filters.slice(1);
      }

      let param1 = {
        type: "equipment",
      };

      Promise.all([
        EquipController.queryEquip(param2),
        DicController.queryPhysicsAllType(param1),
      ]).then((res) => {
        let allData = res[0],
          data = res[1];
        this.options = this.formatOptions(data.content);
        if (!this.all) {
          this.content = data.content.map((t) => {
            return t.classCode;
          });
          this.filterForOptions();
        }
        if (this.value) {
          this.changeVal(this.value);
        }
      });
    },
    //格式化options数据
    formatOptions(arr) {
      let data = [];
      arr &&
        arr.map((t) => {
          let temp = t;
          // temp.code = t.code;
          temp.facility =
            t.name === t.aliasName ? t.name : `${t.name}(${t.aliasName})`;
          data.push(temp);
        });
      return data;
    },
    //过滤
    filterForOptions() {
      this.options = this.options.filter((item) => {
        if (this.content.indexOf(item.code) > -1) {
          return item;
        }
      });
    },
    checkChange(val) {
      this.value = "";
    },
  },
};
</script>
<style lang="less" scoped>
#cascaderMap {
  float: left;
  margin-left: 10px;

  .buildFloor {
    color: #999999;
    font-size: 14px;
  }
}
</style>
