<template>
  <div class="eibs-tab">
    <c-col :span="12">
      <c-col :span="12">
        <el-form-item :label="message.title" :prop="message.cur">
          <c-select
            v-model="model[message.cur]"
            maxlength="3"
            :code="code"
            placeholder="请选择币种"
          ></c-select>
        </el-form-item>
      </c-col>

      <c-col :span="12">
        <el-form-item
          label=""
          style="margin-left: -230px !important"
          :prop="message.amt"
        >
          <c-input
            v-model="model[message.amt]"
            @change="amtChanged"
            @input="amtInput"
            :placeholder="'请输入' + message.title"
          ></c-input>
        </el-form-item>
      </c-col>
    </c-col>
  </div>
</template>
<script>
import CommonProcess from "~/mixin/CommonProcess";
import CodeTable from "~/config/CodeTable";
export default {
  inject: ["root"],
  mixins: [CommonProcess],
  props: ["model", "message"],
  data() {
    return {
      code: [...CodeTable.cur],
      data: [],
    };
  },
  methods: {
    amtChanged(newamt) {
      if (newamt === "" || newamt === undefined || newamt === null) {
        this.model[this.message.amt] = "";
        return;
      }
      newamt = newamt.replace(/^0+\./, "0.");
      if (newamt === "" || newamt === undefined || newamt === null) {
        this.model[this.message.amt] = "";
        return;
      }
      newamt = newamt.replace(/^0+([0-9])/, "$1");
      let fix = this.message.fix || 2;
      let idx = newamt.indexOf(".");
      let segs = undefined;
      if (idx > -1) segs = newamt.split(".");
      else segs = [newamt || "0", "0".repeat(fix)];
      if (!segs[0]) segs[0] = "0";
      if (segs[1].length > fix) segs[1] = segs[1].substring(0, fix);
      else if (segs[1].length < fix)
        segs[1] = segs[1] + "0".repeat(fix - segs[1].length);
      let npreseg = "";
      segs[0] = segs[0].replace(/\D+/g, "");
      let preseg = segs[0];
      for (let i = 0; i < preseg.length; i++) {
        npreseg = preseg.charAt(preseg.length - 1 - i) + npreseg;
        if (i && (1 + i) % 3 == 0 && i != preseg.length - 1)
          npreseg = "," + npreseg;
      }
      this.model[this.message.amt] = `${segs[0] ? segs[0] : 0}.${segs[1]}`;
    },
    amtInput(newamt) {
      if (newamt === "" || newamt === undefined || newamt === null) {
        this.model[this.message.amt] = "";
        return;
      }
      this.model[this.message.amt] = newamt.replace(/[^\d.]/g, "");
    },
  },
  watch: {},
  // methods: { ...Event },
  created: function () {},
};
</script>
<style>
</style>
