<template>
  <div class="select-goodsClass">
    <el-cascader
     v-loading="loading"
      style="width: 100%"
      :disabled="disabled"
      v-model="value"
      :options="options"
      :placeholder="placeholder || ''"
      :size="size || 'medium'"
      :props="props"
      @change="handleChange"
    ></el-cascader>
  </div>
</template>

<script>
import asyncRequest from "@/apis/components/goods-class";
import resToken from "@/mixins/resToken";
import { isArray } from "@/utils/validate";
export default {
  name: "goodClass",
  mixins: [resToken],
  props: ["size", "value", "placeholder", "isDetail", "disabled", "type"],
  /**
   * 属性集合
   * @param {String}        size             : 组件大小            非必填
   * @param {Array}         value            : 选中值              必填
   * @param {String}        placeholder      : 提示信息            非必填
   * @param {Boolean}       isDetail         : 是否是详情逻辑       必填
   * @param {Boolean}       disabled         : 是否禁用            必填
   * @param {String}        type             : 平台类型            必填
   */
  /**
   * 事件集合
   * @searchChange             : 选中值变化调用   抛出选中数据
   */
  data() {
    return {
      loading: false,
      options: [],
      selectLoading: false,
      props: {
        label: "name",
        value: "id",
        children: "child",
        // checkStrictly: true
      },
    };
  },
  // watch: {
  //   // value: function (val, old) {
  //   //   if (val) {
  //   //     this.loading = true;
  //   //     setTimeout(() => {
  //   //       this.loading = false;
  //   //     }, 10);
  //   //   }
  //   // },
  // },
  mounted() {
    this.options = [];
    this.getInfo();
    this.selectLoading = false;
  },
  methods: {
    handleChange(value) {
      this.$emit("handleChange", value);
    },
    delChild(list) {
      let arr = [];
      list.forEach((v) => {
        let sitem = JSON.parse(JSON.stringify(v));
        if (isArray(sitem.child) && sitem.child.length === 0) {
          delete sitem["child"];
        } else if (isArray(sitem.child) && sitem.child.length > 0) {
          sitem.child = this.delChild(sitem.child);
        }
        arr.push(sitem);
      });
      return arr;
    },
    async getInfo() {
      this.selectLoading = true;
      let res = await asyncRequest.list({company_type:this.type});
      if (res && res.code === 0 && res.data) {
        let arr = res.data;
        let list = [];

        list = this.delChild(arr);
        this.options = list;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
      this.selectLoading = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.select-goodsClass {
  .loading-input {
    background-color: transparent;
    border: 1px solid #dfe4ed;
    color: #c0c4cc;
    width: 100%;
    height: 36px;
    line-height: 36px;
    padding: 0 30px 0 12px;
    border-radius: 4px;
    &.disabled {
      background-color: #f5f7fa;
    }
    span {
      font-size: 16px;
      height: 36px;
      line-height: 36px;
      padding: 0 0 0 3px;
      vertical-align: top;
    }
  }
}
</style>