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

defineOptions({ name: 'PricePlanList' })
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)
    const defaultData = {
      enableExistingVip: false,
    }
    if (newVal) {
      formRef.value?.setData(
        obj.toNestedToFlattenJSON(props.platform.pricePlanList) || defaultData,
      )
    } else {
      formRef.value?.setData(defaultData)
    }
  },
})

const title = computed(() => {
  return `【${props.platform?.name}】价格体系`
})

// 会员卡的类型
const type_month = 3
const type_quarter = 4
const type_half_year = 5
const type_year = 6
const type_options = [
  {
    label: '月卡',
    value: type_month,
  },
  {
    label: '季卡',
    value: type_quarter,
  },
  {
    label: '半年卡',
    value: type_half_year,
  },
  {
    label: '年卡',
    value: type_year,
  },
]

const formRef = ref()
const form = {
  titleWidth: 130,
  items: [
    {
      required: true,
      title: '会员卡类型',
      field: 'enabledTypes',
      itemRender: {
        name: mj_form_checkbox_group,
        props: {
          options: type_options,
        },
      },
    },
    {
      titleBold: true,
      vertical: true,
      title: '原价',
      field: 'origin',
      children: [
        {
          required: true,
          span: 12,
          field: 'origin.monthPrice',
          title: '月卡',
          itemRender: {
            name: mj_form_money,
          },
        },
        {
          required: true,
          span: 12,
          field: 'origin.quarterPrice',
          title: '季卡',
          itemRender: {
            name: mj_form_money,
          },
        },
        {
          required: true,
          span: 12,
          field: 'origin.halfYearPrice',
          title: '半年卡',
          itemRender: {
            name: mj_form_money,
          },
        },
        {
          required: true,
          span: 12,
          field: 'origin.yearPrice',
          title: '年卡',
          itemRender: {
            name: mj_form_money,
          },
        },
      ],
    },
    {
      titleBold: true,
      vertical: true,
      title: '现价',
      field: 'current',
      children: [
        {
          required: true,
          span: 12,
          field: 'current.monthPrice',
          title: '月卡',
          itemRender: {
            name: mj_form_money,
          },
        },
        {
          required: true,
          span: 12,
          field: 'current.quarterPrice',
          title: '季卡',
          itemRender: {
            name: mj_form_money,
          },
        },
        {
          required: true,
          span: 12,
          field: 'current.halfYearPrice',
          title: '半年卡',
          itemRender: {
            name: mj_form_money,
          },
        },
        {
          required: true,
          span: 12,
          field: 'current.yearPrice',
          title: '年卡',
          itemRender: {
            name: mj_form_money,
          },
        },
      ],
    },
    {
      titleBold: true,
      vertical: true,
      title: '老会员价',
      field: 'existingVip',
      children: [
        {
          required: true,
          span: 24,
          field: 'enableExistingVip',
          title: '是否开启',
          itemRender: {
            name: mj_form_radio_group,
            options: [
              {
                label: '是',
                value: true,
              },
              {
                label: '否',
                value: false,
              },
            ],
          },
        },
        {
          required: true,
          span: 12,
          field: 'existingVip.monthPrice',
          title: '月卡',
          itemRender: {
            name: mj_form_money,
          },
        },
        {
          required: true,
          span: 12,
          field: 'existingVip.quarterPrice',
          title: '季卡',
          itemRender: {
            name: mj_form_money,
          },
        },
        {
          required: true,
          span: 12,
          field: 'existingVip.halfYearPrice',
          title: '半年卡',
          itemRender: {
            name: mj_form_money,
          },
        },
        {
          required: true,
          span: 12,
          field: 'existingVip.yearPrice',
          title: '年卡',
          itemRender: {
            name: mj_form_money,
          },
        },
      ],
    },
  ],
} as any

// 编辑表单事件
const submit = () => {
  hud.load({
    task: async () => {
      await platformService.saveOrUpdate({
        id: props.platform.id,
        pricePlanList: obj.toFlattenToNestedJSONString(
          formRef.value?.getData(),
        ),
      })
      emits('success')
      emits('update:modelValue', false)
    },
    option: '保存',
    hideSuccess: false,
  })
}

// 调整item
const adjustItems = () => {
  const items = [
    'existingVip.monthPrice',
    'existingVip.quarterPrice',
    'existingVip.halfYearPrice',
    'existingVip.yearPrice',
  ]
  const data = formRef.value?.getData()
  if (!data) return
  if (data.enableExistingVip) {
    formRef.value?.showItems(items)
  } else {
    formRef.value?.hideItems(items)
  }
}

watch(
  () => formRef.value?.getData().enableExistingVip,
  () => {
    adjustItems()
  },
  {
    deep: true,
  },
)
</script>

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

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