<template>
  <div>
    <div v-if="mode === 'DESIGN'">
      <a-input :placeholder="placeholder" disabled />
      <div v-show="showChinese" style="margin-top: 15px">
        <span>大写：</span>
        <span class="chinese">{{ chinese }}</span>
      </div>
    </div>
    <div v-else>
      <a-input-number
        v-if="!readerMode"
        v-model="internalValue"
        style="width: 100%"
        :min="0"
        :precision="precision"
        :placeholder="placeholder"
        :step="1"
      />
      <div v-else>{{ internalValue }}</div>
      <div v-show="showChinese">
        <span>大写：</span>
        <span class="chinese">{{ chinese }}</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AmountInput',
  model: {
    prop: 'value',
    event: 'update:value'
  },
  props: {
    mode: {
      type: String,
      default: 'DESIGN'
    },
    precision: {
      type: Number,
      default: 0
    },
    placeholder: {
      type: String,
      default: '请输入金额'
    },
    readerMode: {
      type: Boolean,
      default: false
    },
    value: {
      type: [String, Number],
      default: ''
    },
    showChinese: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    internalValue: {
      get() {
        return this.value ? parseFloat(this.value) : 0;
      },
      set(val) {
        this.$emit('update:value', val);
      }
    },
    chinese() {
      return this.convertCurrency(this.value);
    }
  },
  methods: {
    convertCurrency(money) {
      const cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
      const cnIntRadice = ['', '拾', '佰', '仟'];
      const cnIntUnits = ['', '万', '亿', '兆'];
      const cnDecUnits = ['角', '分', '毫', '厘'];
      const cnInteger = '整';
      const cnIntLast = '元';
      const maxNum = 999999999.99;

      let integerNum;
      let decimalNum;
      let chineseStr = '';
      let parts;

      if (money === '') money = '0';
      money = parseFloat(money);
      if (money >= maxNum) return '';
      if (money === 0) return cnNums[0] + cnIntLast + cnInteger;

      money = money.toString();
      if (money.indexOf('.') === -1) {
        integerNum = money;
        decimalNum = '';
      } else {
        parts = money.split('.');
        integerNum = parts[0];
        decimalNum = parts[1].substr(0, 4);
      }

      if (parseInt(integerNum, 10) > 0) {
        let zeroCount = 0;
        const IntLen = integerNum.length;
        for (let i = 0; i < IntLen; i++) {
          const n = integerNum.substr(i, 1);
          const p = IntLen - i - 1;
          const q = Math.floor(p / 4);
          const m = p % 4;
          if (n === '0') {
            zeroCount++;
          } else {
            if (zeroCount > 0) {
              chineseStr += cnNums[0];
            }
            zeroCount = 0;
            chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
          }
          if (m === 0 && zeroCount < 4) {
            chineseStr += cnIntUnits[q];
          }
        }
        chineseStr += cnIntLast;
      }

      if (decimalNum !== '') {
        const decLen = decimalNum.length;
        for (let i = 0; i < decLen; i++) {
          const n = decimalNum.substr(i, 1);
          if (n !== '0') {
            chineseStr += cnNums[parseInt(n)] + cnDecUnits[i];
          }
        }
      }

      if (chineseStr === '') {
        chineseStr = cnNums[0] + cnIntLast + cnInteger;
      } else if (decimalNum === '') {
        chineseStr += cnInteger;
      }
      return chineseStr;
    }
  }
};
</script>

<style scoped>
.chinese {
  color: #afadad;
  font-size: smaller;
}
</style>
