/*
 * @Author: Anmin
 * @Email:changanmin1@crc.com.hk
 * @Date: 2018-10-10 21:59:44
 * @Last Modified by: Anmin
 * @Last Modified time: 2018-12-11 11:48:55
 * @Desc 选择业务类型 组件
 */

<template>
  <Select
    size="small"
    transfer
    v-bind="$attrs"
    :label="strValue"
    :value="strValue"
    clearable
    v-on="$listeners"
    @input="handleInput"
    @on-clear="clearSelect"
    @on-change="changeSelect"
  >
    <Option
      v-for="(option, index) in categoryList"
      :key="index"
      :value="option.value"
      :label="option.label"
      :disabled="isBatch?option.disabled:false"
    ></Option>
  </Select>
</template>
<script>
import Server from './server.js';
import { mapActions } from 'vuex';
const server = new Server();
export default {
  name: 'SelectCategory',
  props: {
    value: {
      type: String,
      default() {
        return '';
      }
    },
    // 收款方式  ，传值为“客户”“供应商”“其他”，*必填
    receivablesClass: {
      type: String,
      default() {
        return '';
      }
    },
    businessTypeCode: {
      type: String,
      default() {
        return '';
      }
    },
    isBatch: {
      type: Boolean,
      default() {
        return false;
      }
    }
  },
  data() {
    return {
      categoryList: [],
      server: server,
      categoryMap: {},
      noSelectCategory: []
    };
  },
  computed: {
    strValue() {
      return this.value;
    },
    getReceivablesClass() {
      return this.receivablesClass;
    },

    getBusinessTypeCode() {
      return this.businessTypeCode;
    }
  },
  watch: {
    categoryList: function(newVal, oldVal) {
      this.categoryMap = {};
      for (const item of this.categoryList) {
        this.categoryMap[item.value] = item;
      }
      this.loadFinish();
    }
  },
  created() {
    if (!sessionStorage.getItem('selectCateGory$' + this.receivablesClass)) {
      sessionStorage.setItem(
        'selectCateGory$' + this.receivablesClass,
        JSON.stringify({})
      );
    }
    $store.dispatch('paymentWorktable/queryValueSetByCode', { code: 'ReceivablesBatchClaimNoSelect' }).then(response => {
      console.log('response', response);
      if (response) {
        this.noSelectCategory = response;
        console.log('noSelectCategory', this.noSelectCategory);
        this.loadData();
      }
    });
  },
  mounted() {},
  methods: {
    ...mapActions('paymentWorktable', ['queryValueSetByCode']),
    clearSelect() {
      this.$emit('clearSelect');
    },
    changeSelect(value) {
      console.log('dataSelectChange');
      this.$emit('changeSelect', value ? this.categoryMap[value] : '');
    },
    handleInput(value) {
      this.$emit('input', value);
      this.$emit('dataSelectChange', this.categoryMap[value]);
    },
    async loadData() {
      // if (this.getLocalData()) return;
      const data = await this.server.queryCategoryList({
        receivablesClass: this.getReceivablesClass,
        categoryCode: this.getBusinessTypeCode

      });
      if (data.statusCode === '-1') {
        this.$Message.error(data.msg);
      } else {
        const arr = [];
        for (const item of data.data.list || []) {
          this.noSelectCategory.forEach(v => {
            item.categoryName == v.vsiValue
              ? (item.disabled = true)
              : (item.disabled = false);
          });
          arr.push({
            label: item.categoryName,
            value: item.categoryCode,
            isRefunds: item.isRefunds,
            isHasSerchg: item.isHasSerchg,
            isAutoClaim: item.isAutoClaim,
            receivableRelClaim: item.receivableRelClaim,
            receivableClass: item.receivableClass,
            isThirdPayty: item.isThirdPayty,
            disabled: item.disabled
          });
        }
        this.categoryList = arr;
        for (const item of arr || []) {
          this.categoryMap[item.value] = item;
        }
        if (this.categoryList.length === 1) {
          this.$emit('onlyOne', this.categoryList[0]);
        }
        sessionStorage.setItem(
          'selectCateGory$' + this.receivablesClass,
          JSON.stringify(this.categoryList)
        );
      }
    },
    loadFinish() {
      // const valueSet = JSON.parse(
      //   sessionStorage.getItem('selectCateGory$' + this.receivablesClass)
      // );
      this.$emit('loadFinish', {
        valueList: this.categoryList,
        valueMap: this.categoryMap
      });
    },
    getLocalData() {
      const valueSet = JSON.parse(
        sessionStorage.getItem('selectCateGory$' + this.receivablesClass)
      );
      if (valueSet.length > 0) {
        this.categoryList = valueSet;
        for (const item of this.categoryList) {
          this.categoryMap[item.value] = item;
        }
        return true;
      } else {
        return false;
      }
    }
  }
};
</script>
