<template>
  <div style="display: flex;">
    <el-select
        v-model="value"
        placeholder="请选择"
        filterable
        clearable
        allow-create
        default-first-option
        @change="getKeyId(value)" :disabled="disabled">
      <template>
        <el-option v-for="item in corpList" :key="item.id" :label="corpType == 'DD'?item.name:corpType == 'DD-ID'?item.name:item.cname" :value="item.id">
        </el-option>
      </template>
    </el-select>
    <el-tooltip class="item" effect="dark" content="获取最新客户资料" placement="top-start">
      <el-button icon="el-icon-refresh" size="mini" v-if="label != 'shortName'" :disabled="disabled"
                 @click="refreshData"></el-button>
    </el-tooltip>
  </div>
</template>

<script>
import {allCropList} from "@/api/basicData/customerInformation";
import {kMaxLength} from "buffer";
import {getAllList} from "@/api/basicData/portinformation";

export default {
  name: "main",
  data() {
    return {
      form: {},
      corpList: []
    };
  },
  props: {
    value: String,
    name: String,
    label: String,
    corpType: String,
    disabled: Boolean
  },
  model: {
    prop: "value",
    event: "balabala",
  },
  created() {
    this.getCorpList()
  },
  methods: {
    getCorpList(type) {
      let userObj = JSON.parse(localStorage.getItem("saber-userInfo")).content;
      if (this.corpType == 'DD'){
        getAllList().then(res => {
          this.corpList = res.data.data;
          if (type) {
            for (let item of this.corpList) {
              if (item.name == this.value) {
                return this.getKeyId(item.id)
              }
            }
            for (let item of this.corpList) {
              if (item.id == this.value) {
                return this.getKeyId(item.id)
              }
            }
          }
        });
      }else if(this.corpType == 'DD-ID'){
        getAllList().then(res => {
          this.corpList = res.data.data;
          if (type) {
            for (let item of this.corpList) {
              if (item.name == this.value) {
                return this.getKeyId(item.id)
              }
            }
            for (let item of this.corpList) {
              if (item.id == this.value) {
                return this.getKeyId(item.id)
              }
            }
          }
        });
      }else {
        allCropList({
          corpType: this.corpType ? this.corpType : 'KH',
          adminProfiles: userObj.role_name != "admin" ? userObj.user_id : null
        }).then(res => {
          this.corpList = res.data.data;
          if (type) {
            for (let item of this.corpList) {
              if (item.cname == this.value) {
                return this.getKeyId(item.id)
              }
            }
            for (let item of this.corpList) {
              if (item.id == this.value) {
                return this.getKeyId(item.id)
              }
            }
          }
        });
      }
    },
    refreshData() {
      this.getCorpList(true)
    },
    getKeyId(row) {
      console.log(row)
      if (row) {
        let res = this.corpList.find(e => {
          return e.id == row
        })
        if (res) {
          if (this.corpType == 'DD'){
            this.$emit('balabala',res.name)
            this.$emit('getCropId', res)
          }else if (this.corpType == 'DD-ID'){
            this.$emit('balabala',res.id)
            this.$emit('getCropId', res)
          }else {
            this.$emit('balabala',res.id)
            this.$emit('getCropId', res)
          }
        } else {
          if (this.corpType == 'DD'){
            this.$emit('update:name', row);
          }else if (this.corpType == 'DD-ID'){
            this.$emit('update:name', row);
          }else {
            this.$emit('update:name', row);
          }
          this.$emit('balabala',row)
          this.$emit('getCropId', null)
        }
      } else {
        this.$emit('balabala', null)
        this.$emit('getCropId', null)
      }

    }
  },
  watch: {}
};
</script>

<style lang="scss" scoped>
</style>
