<!-- eslint-disable unicorn/prefer-ternary -->
<script setup lang="ts">
import { platformService } from '#/api/service/auth/platform'
import { mj_form_input } from '#/components/mj/mj-table'
import hud from '#/utils/hud'
import obj from '#/utils/obj'

defineOptions({ name: 'WxMpCfg' })
const props = withDefaults(
  defineProps<{
    modelValue?: boolean
    platform: 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(obj.toJSON(props.platform.wxMpCfg) || {})
    } else {
      formRef.value?.setData({})
    }
  },
})

const formRef = ref()

const title = computed(() => {
  return `【${props.platform?.name}】微信公众号配置`
})

const form = {
  titleWidth: 110,
  items: [
    {
      required: true,
      span: 12,
      field: 'name',
      title: '名称',
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      required: true,
      span: 12,
      field: 'appId',
      title: 'AppID',
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      required: true,
      span: 12,
      field: 'secret',
      title: 'Secret',
      itemRender: {
        name: mj_form_input,
        props: {
          type: 'password',
        },
      },
    },
    {
      required: true,
      span: 12,
      field: 'homeUrl',
      title: '首页URL',
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      required: true,
      span: 12,
      field: 'authRedirectUrl',
      title: '授权回调URL',
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      required: true,
      span: 12,
      field: 'bindAuditUrl',
      title: '绑定审核URL',
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      span: 12,
      field: 'deviceBindApplyTemplateId',
      title: '设备绑定申请模板',
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      span: 12,
      field: 'deviceUnbindTemplateId',
      title: '设备解绑模板',
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      span: 12,
      field: 'deviceAuditResultTemplateId',
      title: '设备审核结果模板',
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      span: 12,
      field: 'childBindApplyTemplateId',
      title: '孩子绑定申请模板',
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      span: 12,
      field: 'childUnbindTemplateId',
      title: '孩子解绑模板',
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      span: 12,
      field: 'childAuditSuccessTemplateId',
      title: '孩子审核通过模板',
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      span: 12,
      field: 'childAuditFailTemplateId',
      title: '孩子审核拒绝模板',
      itemRender: {
        name: mj_form_input,
      },
    },
    {
      span: 12,
      field: 'adminTransferTemplateId',
      title: '管理员转让模板',
      itemRender: {
        name: mj_form_input,
      },
    },
  ],
} as any

const submit = () => {
  hud.load({
    task: async () => {
      await platformService.saveOrUpdate({
        id: props.platform.id,
        wxMpCfg: obj.toJSONString(formRef.value?.getData()),
      })
      emits('success')
      emits('update:modelValue', false)
    },
    option: '保存',
    hideSuccess: false,
  })
}
</script>

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

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