<template>
  <el-table-column :label="label" :prop="prop" :min-width="minWidth" :align="align">
    <template v-slot="scope" >
      <el-input
        ref="input"
        v-model="scope.row[prop]"
        @blur="inputMoney(scope.row, prop)"
        @focus="delPoint(scope.row, prop)"
        :disabled="disabled"
        maxlength="16"
        @input="oninput(scope.row[prop])"
        :placeholder="$t('common.pleaseInput')"
        clearable
      >
      </el-input>
    </template>
  </el-table-column>
</template>

<script>
import {comdify, delcommafy, delInput} from "@/utils/money";

export default {
  name: "MoneyInputColumn",
  data() {
    return {
      value: this.inputValue,
    };
  },
  created() {
    console.log()
  },
  props: {
    inputValue:{
      type: String,
      default: "",
    },
    label: {
      type: String,
      default: "",
    },
    prop: {
      type: String,
    },
    minWidth: {
      type: Number,
      default: 150,
    },
    disabled:{
      type:Boolean,
      default:false,
    },
    align:{
      type:String,
      default:'right',
    },
    customHeight:{
      type:String,
      default:'',
    },
    zeroLength:{
      type: Number,
      default: 2,
    }
  },
  methods:{
    oninput(num) {
      var str = num;
      if (!/^([0-9]|\.)*$/.test(str)) {
        str = str.substring(0, str.length - 1);
        num = str;
        return str;
      }
      if (str.indexOf(".") != -1) {
        str = str.split(".")[0] + "." + str.split(".")[1].substring(0, this.zeroLength); //保留小数
        num = str;
      }
    },
    async delPoint(row, prop) {
      let money = row[prop];
      money = delcommafy(money)
      row[prop] = money; // 更新行的值
      this.$emit("change", money); // 通知父组件
    },
    // handleClear(row, prop){
    //   let money = row[prop];
    //   money = 0.00
    //   row[prop] = money; // 更新行的值
    //   this.$emit("change", money); // 通知父组件
    // },
    async inputMoney(row, prop) {
      let money = row[prop];
      money = await comdify(delcommafy(money), this.zeroLength);
      row[prop] = money; // 更新行的值
      this.$emit("change", money); // 通知父组件
    },

  }

};
</script>


<style lang="scss" scoped>
::v-deep  input{
  text-align: right !important;
}
</style>
