<template>
  <div>
    <Select
      v-if="isLoginToApply"
      ref="selcom"
      v-bind="$attrs"
      :value="value"
      v-on="$listeners"
      @input="handleInput"
      @on-open-change="handleQuery"
      @on-change="handleChange"
    >
      <Option
        v-for="item in dataList"
        :key="item.value"
        :value="item.value"
      >
        {{ item.label }}
      </Option>
    </Select>
    <span v-if="!isLoginToApply">{{ name }}</span>
  </div>
</template>
<script>
/**
 * 选择归属公司--付款计划行查询
 */
import Server from './server.js';
const server = new Server();
export default {
  name: 'SelectCompany',
  components: {},
  directives: {},
  props: {
    value: {
      type: String,
      default() {
        return '';
      }
    },
    name: {
      type: String,
      default() {
        return '';
      }
    },
    // 弃用，改为query 枚举方式
    flag: {
      type: Boolean,
      default() {
        return false;
      }
    },
    isLoginToApply: {
      type: Boolean,
      default() {
        return true;
      }
    },
    query: {
      type: String,
      default: ''
      // default() {
      //   //queryCompanys (all)
      //   //paymentQueryCompanys(com)
      //   //QueryOuListByBusiness(bu)
      //   return "all";
      // }
    }
  },
  data() {
    return {
      dataList: [],
      dataMap: {},
      server: server
    };
  },
  computed: {
    userEmpInfo() {
      return this.$store.state.userEmpInfo;
    }
  },
  watch: {
    dataList: function(newVal, oldVal) {
      this.dataMap = {};
      for (const item of this.dataList) {
        this.dataMap[item.value] = item.label;
      }
      this.loadFinish();
    }
  },
  created: function() {},
  beforeMount: function() {},
  mounted: async function() {
    const self = this;
    // 假设用户传了query 没传递flag
    if (self.query && !self.flag) {
      if (self.query === 'bu') {
        const response = await self.server.QueryOuListByBusiness();
        self.dataList = [];
        for (const item of response || []) {
          self.dataList.push({
            label: item.ouName,
            value: item.companyCode
          });
        }
      } else if (self.query === 'all') {
        // self.server.queryCompanys().then(response => {
        //   console.log(response);
        //   const list = [];
        //   self.dataList = [];
        //   const temp = {};
        //   for (const item of response || []) {
        //     list.push({
        //       label: item.ouName,
        //       value: item.ouCode
        //     });
        //   }
        //   list.map((item, index) => {
        //     if (!temp[item.value]) {
        //       self.dataList.push(item);
        //       temp[item.value] = true;
        //     }
        //   });
        // });
      } else if (self.query === 'com') {
        self.server.paymentQueryCompanys().then(response => {
          self.dataList = [];
          for (const item of response || []) {
            self.dataList.push({
              label: item.ouName,
              value: item.companyCode
            });
          }
        });
      }
    } else if (self.flag) {
      self.server.paymentQueryCompanys().then(response => {
        self.dataList = [];
        for (const item of response || []) {
          self.dataList.push({
            label: item.ouName,
            value: item.companyCode
          });
        }
      });
    } else {
      // 从sessionStorage中取userInfo
      const userInfoes = JSON.parse(sessionStorage.getItem('singleUserInfo'));
      const list = [];
      self.dataList = [];
      self.server.queryCompanys().then(response => {
        console.log('response', response);
        const list1 = [];
        const temp1 = {};
        for (const item of response || []) {
          list1.push({
            label: item.ouName,
            value: item.ouCode
          });
        }
        list1.map((item, index) => {
          if (!temp1[item.value]) {
            list.push(item);
            temp1[item.value] = true;
          }
        });
        const params = {
          empCode: userInfoes.userCode,
          orgId: String(userInfoes.orgId)
        };
        self.server.queryCompanysA(params).then(response => {
          const list2 = [];
          const temp2 = {};
          for (const o of response || []) {
            list2.push({
              label: o.compName,
              value: o.compCode
            });
          }
          list2.map((v, index) => {
            if (!temp2[v.value]) {
              list.push(v);
              temp2[v.value] = true;
            }
          });
          self.dataList = list;
        });
      });
    }
  },
  beforeDestroy: function() {},
  destroyed: function() {},
  methods: {
    handleInput(value) {
      if (value !== undefined) {
        this.$emit('input', value);
      } else {
        this.$emit('input', '');
      }
    },
    loadFinish() {
      this.$emit('loadFinish', {
        valueList: this.dataList,
        valueMap: this.dataMap
      });
    },
    handleQuery(open) {
      if (this.value === '' && !open) {
        this.$refs.selcom.clearSingleSelect();
      }
    },
    handleChange(data) {
      this.$emit('handleSelect', data);
    }
  }
};
</script>
<style lang="less" scoped>
</style>
