qian<!-- eslint-disable unicorn/prefer-ternary -->
<script setup lang="ts">
import { useUserStore } from '@vben/stores'

import { spService } from '#/api/service/sp/sp'
import {
  mj_form_input,
  mj_form_money,
  mj_form_radio_group,
  mj_form_text,
} from '#/components/mj/mj-table'
import hud from '#/utils/hud'
import num from '#/utils/num'
import obj from '#/utils/obj'

defineOptions({ name: 'TransferToSp' })
const props = withDefaults(
  defineProps<{
    modelValue?: boolean
    providerData: any
    spRender: any
  }>(),
  {
    modelValue: false,
  },
)

const emits = defineEmits(['update:modelValue', 'refresh'])

const targetTypeOptions = [
  {
    label: '服务商',
    value: '1',
  },
  {
    label: '学校机构',
    value: '2',
  },
]

const transTypeOptions = [
  {
    label: '赠送',
    value: '0',
  },
  {
    label: '销售',
    value: '1',
  },
  {
    label: '公益捐赠',
    value: '2',
  },
]

const { userInfo } = storeToRefs(useUserStore())
const show = computed({
  get() {
    return props.modelValue
  },
  set(newVal) {
    emits('update:modelValue', newVal)
    if (newVal) {
      formRef.value?.setData({
        ...props.providerData,
      })
    }
  },
})

watch(
  () => props.providerData,
  (newVal) => {
    // 只是更新，不是覆盖
    formRef.value?.updateData({
      ...newVal,
    })
  },
  {
    deep: true,
  },
)

// 表单
const formRef = ref()
const form = {
  titleWidth: 150,
  items: obj.joinObjs<any>(
    !userInfo.value.publicOrgFlag || {
      required: true,
      field: 'targetType',
      title: '接收方类型',
      span: 24,
      itemRender: {
        name: mj_form_radio_group,
        options: targetTypeOptions,
      },
    },
    {
      required: true,
      field: 'targetName',
      title: '接收方名称',
      span: 24,
      itemRender: {
        name: mj_form_input,
        props: { clearable: true },
      },
    },
    !userInfo.value.publicOrgFlag || {
      required: true,
      field: 'transType',
      title: '转让类型',
      span: 24,
      itemRender: {
        name: mj_form_radio_group,
        options: transTypeOptions,
      },
    },
    {
      required: true,
      field: 'currency',
      title: '乐贝',
      span: 24,
      itemRender: {
        name: mj_form_money,
        props: { clearable: true, showPrefix: false },
      },
    },
    !userInfo.value.isSuperAdmin || {
      required: true,
      field: 'providerName',
      title: '源头服务商',
      span: 24,
      itemRender: {
        name: mj_form_text,
        props: {
          linkedRender: props.spRender,
        },
      },
    },
  ),
} as any

const submit = () => {
  const data = formRef.value?.getData()
  const params = {
    currency: data.currency,
    targetName: data.targetName,
    targetType: data.targetType,
    transType: data.transType,
    providerId: data.providerId,
  }
  if (userInfo.value.isSpStaff) {
    params.providerId = userInfo.value.providerId
  }
  let from = ''
  if (data.providerName) {
    from = `【${data.providerName}】的`
  }
  const coinText = hud.highlight(num.formatMoney(params.currency))
  hud.confirmLoad(
    `确认转让${from}${coinText}乐贝给${hud.highlight(params.targetName)}吗？`,
    {
      task: async () => {
        await spService.transferCoinToSp(params)
        emits('update:modelValue', false)
        emits('refresh')
      },
      option: '转让',
      hideError: true,
    },
  )
}
</script>

<template>
  <mj-modal
    v-model="show"
    :options="{
      title: '乐贝转让',
      width: 600,
    }"
  >
    <mj-form ref="formRef" :options="form" @submit="submit" />
  </mj-modal>
</template>
