/*
 * @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" v-on="$listeners" :value="strValue" filterable @input="handleInput" clearable>
    <Option v-for="(option, index) in categoryList" :value="option.value" :label="option.label" :key="index"></Option>
  </Select> 
</template>
<script>
import Server from "./server.js";
const server = new Server();
export default {
  name: "SelectCategory",
  props: {
    value: {
      type: String,
      default() {
        return "";
      }
    },
    // 收款方式  ，传值为“客户”“供应商”“其他”，*必填
    receivablesClass:{
      type:String,
      default(){
        return "";
      }
    },
  },
  created() {
    if (!sessionStorage.getItem("selectCateGory$"+this.receivablesClass)) {
      sessionStorage.setItem("selectCateGory$"+this.receivablesClass, JSON.stringify({}));
    }
  },
  mounted() {
    this.loadData();
  },
  data() {
    return {
      categoryList: [],
      server: server,
      categoryMap: {}
    };
  },
  computed: {
    strValue() { 
      return this.value;
      // if(this.categoryList.length > 0 && this.value.trim() !== ""){
      //   console.info(this.value,this.categoryMap[this.value].label);
      //   return this.categoryMap[this.value].label || "";
      // }else{
      //   return '';
      // }
    },
    getReceivablesClass(){ 
      return this.receivablesClass;
    }
  },
  methods: {
    handleInput(value) {
      this.$emit("input", value);
      this.$emit("dataSelectChange", this.categoryMap[value]);
    },
    async loadData() { 
      if (this.getLocalData()) return;
      let data = await this.server.queryCategoryList({"receivablesClass":this.getReceivablesClass});
      if (data.statusCode === "-1") {
        this.$Message.error(data.msg);
      } else {
        let arr = [];


        // categoryCode: "5001"
        // categoryName: "销售回款"
        // cfgList: null
        // isAutoClaim: "N"
        // isHasSerchg: "Y"
        // isRefunds: "Y"
        // receivableClass: "客户"
        // receivableRelClaim: "50,5001,5002"
        for (let item of data.data.list || []) {
          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
          });
        }
        this.categoryList = arr;
        for (let item of arr || []) {
          this.categoryMap[item.value] = item;
        }
        sessionStorage.setItem(
          "selectCateGory$"+this.receivablesClass,
          JSON.stringify(this.categoryList)
        );
      }
    },
    loadFinish() {
      let valueSet = JSON.parse(sessionStorage.getItem("selectCateGory$"+this.receivablesClass));
      this.$emit('loadFinish', {
        valueList: this.categoryList,
        valueMap: this.categoryMap,
      }); 
    },
    getLocalData() { 
      let valueSet = JSON.parse(sessionStorage.getItem("selectCateGory$"+this.receivablesClass));
      if (valueSet.length > 0) {
        this.categoryList = valueSet;
        for (let item of this.categoryList) {
          this.categoryMap[item.value] = item;
        }
        return true;
      } else {
        return false;
      }
    }
  },
  watch: { 
    categoryList: function(newVal, oldVal) { 
      this.categoryMap = {};
      for(let item of this.categoryList) {
        this.categoryMap[item.value] = item;
      }
      this.loadFinish();
    } 
  },
};
</script>

