<template>
  <div>
    <Input
      v-if="!showEdit"
      :size="size"
      :value="value"
      icon="log-in"
      :compCode="compCode"
      readonly
      :disabled="disabled"
      v-bind="$attrs"
      @input="handleInput"
      @click.native="showPaymentCompanyTable"
      v-on="$listeners"
    ></Input>
    <Input
      v-if="showEdit"
      :size="size"
      :value="value"
      :compCode="compCode"
      readonly
      :disabled="disabled"
      v-bind="$attrs"
      @input="handleInput"
      @click.native="showPaymentCompanyTable"
      v-on="$listeners"
    ></Input>
  </div>
</template>
<script>
/**
 * 根据付款方公司信息查询银行信息组件
 */
import BankInfoList from './PaymentCompanyList';
import Server from './server';

const server = new Server();
export default {
  name: 'SelectRealPaymentCompany',
  components: {},
  directives: {},
  props: {
    size: {
      type: String,
      default() {
        return '';
      }
    },
    title: {
      type: String,
      default() {
        return '请选择实际付款公司';
      }
    },
    columns: {
      type: Array,
      default() {
        return [];
      }
    },
    value: {
      type: String,
      default() {
        return '';
      }
    },
    paymentmode: {
      type: String,
      default() {
        return '';
      }
    },
    compCode: {
      type: String,
      default() {
        return '';
      }
    },
    compName: {
      type: String,
      default() {
        return '';
      }
    },
    showSave: {
      type: Number,
      default() {
        return 0;
      }
    },
    defaultPayBack: {
      type: String,
      default() {
        return 'N';
      }
    },
    // 是否自动查询 N / Y
    isAutoQuery: {
      type: String,
      default() {
        return 'Y';
      }
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      bankInfo: {
        bankAccountName: '',
        bankAccountNum: ''
      }
    };
  },
  computed: {
    showEdit() {
      if (this.showSave == 0) {
        return true;
      } else {
        return false;
      }
    }
  },
  watch: {
    compCode: function(newVal, oldVal) {
      if (newVal) {
        this.queryDefaultBank();
      }
    }
  },
  created: function() {
    if (this.isAutoQuery === 'Y') {
      this.queryDefaultBank();
    }
  },
  beforeMount: function() {},
  mounted: function() {},
  beforeDestroy: function() {},
  destroyed: function() {},
  methods: {
    queryDefaultBank() {
      if (this.compCode && this.defaultPayBack == 'Y') {
        server.queryDefaultBank({
          compCode: this.compCode,
          defaultPayBack: this.defaultPayBack
        })
          .then((response) => {
            if (response.statusCode != -1) {
              if (response.data && response.data.length == 1) {
                this.bankInfo = response.data[0];
                this.value = response.data[0];
                this.$emit('handleSelect', this.bankInfo);
              }
            }
          });
      }
    },
    handleInput(value) {
      this.$emit('input', value);
    },
    showPaymentCompanyTable() {
      if (this.disabled) {
        return;
      }
      if (this.columns.length != 0) {
        new this.$pageModal(
          BankInfoList,
          {
            props: {
              title: this.title,
              compCode: this.compCode,
              columns: this.columns,
              compName: this.compName,
              paymentmode: this.paymentmode
            }
          },
          data => {
            if (data) {
              this.$emit('handleSelect', data);
            }
          }
        );
      } else {
        new this.$pageModal(
          BankInfoList,
          {
            props: {
              title: this.title,
              compCode: this.compCode,
              paymentmode: this.paymentmode
            }
          },
          data => {
            if (data) {
              this.$emit('handleSelect', data);
            }
          }
        );
      }
    }
  }
};
</script>
<style lang="less" scoped>
</style>
