import { settleAccountList } from '@/api/settleAcount'
// 结算账户
const SettleAccount = {
  template: `
        <a-select
          :id="id"
          :placeholder="placeholder"
          allowClear
          :value="defaultVal"
          :showSearch="true"
          @change="handleChange"
          :filter-option="filterOption">
          <a-select-option v-for="item in list" :key="item[defValKey]" :value="item[defValKey]">{{ item.accountName }}-{{item.accountNo}}</a-select-option>
        </a-select>
    `,
  props: {
    value: {
      type: String,
      default: ''
    },
    id: {
      type: String,
      default: ''
    },
    defValKey: {
      type: String,
      default: 'settleAccountSn'
    },
    placeholder: {
      type: String,
      default: '请选择结算账户'
    },
    settleStyleSn:{
      type: String,
      default: ''
    }
  },
  data() {
    return {
      defaultVal: this.value||undefined,
      list: []
    };
  },
  watch: {
    value(newValue, oldValue) {
      console.log(newValue)
      this.defaultVal = newValue||undefined
    },
    settleStyleSn(a,b){
      console.log(a,b)
      if(a){
        this.getList()
      }else{
        this.handleChange(undefined)
      }
    }
  },
  mounted() {
    if(this.settleStyleSn){
      this.getList()
    }
  },
  methods: {
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    handleChange(value) {
      this.defaultVal = value;
      const row = this.list.find(item => item[this.defValKey] == value)
      this.$emit('input', value);
      this.$emit('change', this.defaultVal, row?row.accountName:'');
    },
    // 列表数据
    getList () {
      const _this = this
      settleAccountList({settleStyleSn: this.settleStyleSn,pageNo:1,pageSize:500,state:'ENABLE'}).then(res => {
        if (res.status == 200) {
          _this.list = res.data.list || []
        } else {
          _this.list = []
        }
      })
    },
  },
};

export default SettleAccount