2025-05-22 14:59:22 +08:00
|
|
|
'use client'
|
2026-03-13 14:13:06 +08:00
|
|
|
import {useForm} from 'react-hook-form'
|
2025-05-22 14:59:22 +08:00
|
|
|
import Center from '@/components/composites/purchase/long/center'
|
|
|
|
|
import Right from '@/components/composites/purchase/long/right'
|
|
|
|
|
import {Form} from '@/components/ui/form'
|
|
|
|
|
import * as z from 'zod'
|
|
|
|
|
import {zodResolver} from '@hookform/resolvers/zod'
|
2026-04-16 14:41:42 +08:00
|
|
|
import {ProductItem} from '@/actions/product'
|
2025-05-22 14:59:22 +08:00
|
|
|
|
|
|
|
|
// 定义表单验证架构
|
|
|
|
|
const schema = z.object({
|
|
|
|
|
type: z.enum(['1', '2']).default('2'),
|
2026-04-16 14:41:42 +08:00
|
|
|
live: z.string(),
|
2025-05-22 14:59:22 +08:00
|
|
|
quota: z.number().min(500, '购买数量不能少于 500 个'),
|
2026-04-16 14:41:42 +08:00
|
|
|
expire: z.string(),
|
2025-05-22 14:59:22 +08:00
|
|
|
daily_limit: z.number().min(100, '每日限额不能少于 100 个'),
|
|
|
|
|
pay_type: z.enum(['wechat', 'alipay', 'balance']),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 从架构中推断类型
|
|
|
|
|
export type Schema = z.infer<typeof schema>
|
|
|
|
|
|
2026-04-16 14:41:42 +08:00
|
|
|
export default function LongForm({skuList}: {skuList: ProductItem['skus']}) {
|
|
|
|
|
if (!skuList) throw new Error('没有套餐数据')
|
|
|
|
|
|
|
|
|
|
const map = new Map<string, string>()
|
|
|
|
|
// const _modeList = new Set<string>()
|
|
|
|
|
const _liveList = new Set<number>()
|
|
|
|
|
const _expireList = new Set<number>()
|
|
|
|
|
for (const sku of skuList) {
|
|
|
|
|
const params = new URLSearchParams(sku.code)
|
|
|
|
|
// _modeList.add(params.get('mode') || '')
|
|
|
|
|
_liveList.add(Number(params.get('live')))
|
|
|
|
|
_expireList.add(Number(params.get('expire')))
|
|
|
|
|
map.set(sku.code, sku.price)
|
|
|
|
|
}
|
|
|
|
|
// const modeList = Array.from(_modeList).filter(Boolean)
|
|
|
|
|
const liveList = Array.from(_liveList).filter(Boolean).map(String)
|
|
|
|
|
const expireList = Array.from(_expireList).filter(Boolean).map(String)
|
|
|
|
|
|
2025-05-22 14:59:22 +08:00
|
|
|
const form = useForm<Schema>({
|
|
|
|
|
resolver: zodResolver(schema),
|
|
|
|
|
defaultValues: {
|
|
|
|
|
type: '2', // 默认为包量套餐
|
2026-04-16 14:41:42 +08:00
|
|
|
live: liveList[0], // 分钟
|
|
|
|
|
expire: '0', // 天
|
2025-05-22 14:59:22 +08:00
|
|
|
quota: 500,
|
|
|
|
|
daily_limit: 100,
|
2026-03-31 16:11:36 +08:00
|
|
|
pay_type: 'balance', // 余额支付
|
2025-05-22 14:59:22 +08:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return (
|
2025-06-09 19:04:25 +08:00
|
|
|
<Form form={form} className="flex flex-col lg:flex-row gap-4">
|
2026-04-16 14:41:42 +08:00
|
|
|
<Center {...{liveList, map, expireList}}/>
|
2026-03-13 14:13:06 +08:00
|
|
|
<Right/>
|
2025-05-22 14:59:22 +08:00
|
|
|
</Form>
|
|
|
|
|
)
|
|
|
|
|
}
|