<template>
  <div>
    <Select
      v-if="isLoginToApply"
      ref="selcom"
      v-bind="$attrs"
      :value="value"
      :size="size"
      :placeholder="placeholder"
      transfer
	  :multiple="isMultiple"
      v-on="$listeners"
      @input="handleInput"
      @on-open-change="handleQuery"
    >
      <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 { QueryCompanys, PaymentQueryCompanys, QueryOuListByBusiness } from '../server.js';
export default {
  name: 'SelectCompany',
  props: {
    value: {
      type: String,
      default() {
        return '';
      }
    },
	isMultiple: {
      type: Boolean,
      default() {
        return false;
      },
    },
    name: {
      type: String,
      default() {
        return '';
      }
    },
    // 弃用，改为query 枚举方式
    flag: {
      type: Boolean,
      default() {
        return false;
      }
    },
    isLoginToApply: {
      type: Boolean,
      default() {
        return true;
      }
    },
    query: {
      type: String,
      default: ''
    },
    size: {
      type: String,
      default: 'default'
    },
    placeholder: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dataList: [],
      dataMap: {}
    };
  },
  computed: {},
  watch: {
    dataList: function(newVal, oldVal) {
      this.dataMap = {};
      for (const item of this.dataList) {
        this.dataMap[item.value] = item.label;
      }
      this.loadFinish();
    }
  },
  mounted: async function() {
    const self = this;
    // 假设用户传了query 没传递flag
    if (self.query && !self.flag) {
      if (self.query === 'bu') {
        QueryOuListByBusiness().then(data => {
          self.dataList = [];
          for (const item of data || []) {
            self.dataList.push({
              label: item.ouName,
              value: item.companyCode
            });
          }
        });
      } else if (self.query === 'all') {
        QueryCompanys().then(data => {
          const jobList = data.dataResult.jobList;
          const list = [];
          self.dataList = [];
          const temp = {};
          for (const item of jobList || []) {
            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') {
        PaymentQueryCompanys().then(data => {
          self.dataList = [];
          for (const item of data || []) {
            self.dataList.push({
              label: item.ouName,
              value: item.companyCode
            });
          }
        });
      }
    } else if (self.flag) {
      PaymentQueryCompanys().then(data => {
        self.dataList = [];
        for (const item of data || []) {
          self.dataList.push({
            label: item.ouName,
            value: item.companyCode
          });
        }
      });
    } else {
      QueryCompanys().then(data => {
        const jobList = data.dataResult.jobList;
        const list = [];
        self.dataList = [];
        const temp = {};
        for (const item of jobList || []) {
          list.push({
            label: item.ouName,
            value: item.ouCode
          });
        }
        list.map((item, index) => {
          if (!temp[item.value]) {
            self.dataList.push(item);
            temp[item.value] = true;
          }
        });
      });
    }
  },
  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();
      }
    }
  }
};
</script>
