<template>
  <el-input v-model="inputValue" :disabled="disabled" :placeholder="'请输入' + title" @input="inputChange">
    <el-tooltip slot="append" v-if="!disabled" effect="dark" :content="'自动生成' + title" placement="top">
      <el-button size="mini" :icon="loading ? 'el-icon-loading':'el-icon-refresh'" @click="updateNumber()"></el-button>
    </el-tooltip>
  </el-input>
</template>

<script>
import { getBillNumber } from "@/api/ckbill";
export default {
  name: 'BillNumberInput',
  props: {
    // RK CK ZC HP TY CY JZ
    type: {
      type: String,
      default: 'RK'
    },
    title: {
      type: String,
      default: '单据编号'
    },
    value: {
      type: String | Number,
      default: ''
    },
    disabled: [Boolean],
  },
  data() {
    return {
      inputValue: this.value,
      loading: false,
    }
  },
  watch: {
    value() {
      this.inputValue = this.value;
    }
  },
  methods: {
    updateNumber(type) {
      if (this.loading) {
        return;
      }
      this.loading = true;
      return new Promise((resolve, reject) => {
        getBillNumber(type || this.type).then(res => {
          this.loading = false;
          if (res.data) {
            this.$emit('input', res.data);
            resolve(res.data);
          } else {
            reject();
          }
        }).catch(e => {
          this.loading = false;
          reject();
        });
      });
    },
    inputChange(value) {
      this.$emit('input', value);
    }
  }
}
</script>

<style>

</style>