<script setup lang="ts">
import { spService } from '#/api/service/sp/sp'
import { mj_form_money } from '#/components/mj/mj-table'
import { pattern_pos_int } from '#/components/mj/mj-table/fmt'
import hud from '#/utils/hud'
import num from '#/utils/num'

defineOptions({ name: 'TransferCoin' })
const props = withDefaults(
  defineProps<{
    modelValue?: boolean
    school: any
  }>(),
  {
    modelValue: false,
  },
)
const emits = defineEmits(['update:modelValue', 'success'])
const show = computed({
  get() {
    return props.modelValue
  },
  async set(newVal) {
    emits('update:modelValue', newVal)
    if (!newVal) {
      formRef.value?.setData({})
    }
  },
})

const title = computed(() => {
  return `给【${props.school?.name}】赠送乐贝`
})

const formRef = ref()
const form = reactive({
  items: [
    {
      required: true,
      pattern: pattern_pos_int,
      span: 24,
      field: 'currency',
      title: '乐贝数额',
      itemRender: {
        name: mj_form_money,
        props: {
          clearable: true,
          showPrefix: false,
        },
      },
    },
  ],
  data: {},
} as any)

// 编辑表单事件
const submit = () => {
  const data = formRef.value?.getData()
  const coinText = hud.highlight(num.formatMoney(data.currency))
  hud.confirmLoad(
    `确认赠送${coinText}乐贝给${hud.highlight(props.school.name)}吗？`,
    {
      task: async () => {
        await spService.transferCoinToSchool({
          schoolId: props.school.id,
          currency: data.currency,
          providerId: props.school.providerId,
        })
        emits('update:modelValue', false)
        emits('success')
      },
      option: '赠送',
      hideSuccess: false,
    },
  )
}
</script>

<template>
  <mj-modal
    v-model="show"
    :options="{
      title,
    }"
  >
    <mj-form ref="formRef" :options="form" @submit="submit" />
  </mj-modal>
</template>

<style lang="scss" scoped></style>
