<!-- eslint-disable vue/no-mutating-props -->
<script setup lang="ts">
import { numberToChineseUppercaseCurrency } from 'pixiu-number-toolkit'

import num from '#/utils/num'

defineOptions({ name: 'MjFormMoney' })

const props = withDefaults(
  defineProps<{
    clearable?: boolean
    disabled?: boolean
    modelValue: any
    placeholder?: any
    showPrefix?: boolean
  }>(),
  {
    placeholder: undefined,
    disabled: false,
    clearable: true,
    showPrefix: true,
  },
)

const emit = defineEmits(['update:modelValue'])

const text = computed({
  get() {
    return num.formatMoney(props.modelValue)
  },
  set(val) {
    val = num.formatMoney(val)
    emit('update:modelValue', num.deFormatMoney(val))
  },
})

const cnText = computed(() => {
  let v = Number(num.deFormatMoney(text.value)) as any
  v = v ? numberToChineseUppercaseCurrency(v) : ''
  if (!props.showPrefix) {
    v = v.replace('元', '')
    v = v.replace('整', '')
  }
  return v
})
</script>

<template>
  <div class="mj-form-money-box">
    <el-input
      :disabled="disabled"
      class="mj-form-money"
      v-model="text"
      :placeholder="placeholder"
      :clearable="clearable"
    >
      <template v-if="showPrefix" #prepend>¥</template>
    </el-input>
    <div v-if="showPrefix" class="cn-text">
      <span>{{ cnText }}</span>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.mj-form-money-box {
  position: relative;

  .mj-form-money {
    display: flex;
    align-items: center;
    cursor: pointer;
    border-radius: 4px;
    font-size: 16px;
    :deep(.el-input__inner) {
      color: #000 !important;
    }
  }

  .cn-text {
    position: absolute;
    top: 100%;
    left: 60px;
    right: 0;
    font-size: 12px;
    color: #999;
    display: flex;
    align-items: center;
    justify-content: right;
  }
}
</style>
