import React, { useState, useCallback, forwardRef, useImperativeHandle } from 'react'
import Taro from '@tarojs/taro'
import { View, Text, Image } from '@tarojs/components'
import images from '@/config/images'
import TextareaTag from '@/components/TextareaTag'
import CoverImageUpload, { type CoverImage } from '@/components/ImageUpload'
import { useDictionaryActions } from '@/store/dictionaryStore'
import './StadiumDetail.scss'
export interface Stadium {
id?: string
name: string
address?: string
longitude?: number
latitude?: number
istance?: string
court_type?: string
court_surface?: string
description?: string
description_tag?: string[]
venue_image_list?: CoverImage[]
}
interface StadiumDetailProps {
stadium: Stadium
}
// 定义暴露给父组件的方法接口
export interface StadiumDetailRef {
getFormData: () => any
setFormData: (data: any) => void
}
// 公共的标题组件
const SectionTitle: React.FC<{ title: string,prop: string }> = ({ title, prop }) => {
if (prop === 'venue_image_list') {
return (
{title}
添加截图,平台会优先推荐
)
}
return (
{title}
)
}
// 公共的容器组件
const SectionContainer: React.FC<{ title: string; children: React.ReactNode, prop: string }> = ({ title, children, prop }) => (
{children}
)
const StadiumDetail = forwardRef(({
stadium,
}, ref) => {
const { getDictionaryValue } = useDictionaryActions()
const court_type = getDictionaryValue('court_type') || []
const court_surface = getDictionaryValue('court_surface') || []
const supplementary_information = getDictionaryValue('supplementary_information') || []
const stadiumInfo = [
{
label: '场地类型',
options: court_type,
prop: 'court_type',
type: 'tags'
},
{
label: '地面材质',
options: court_surface,
prop: 'court_surface',
type: 'tags'
},
{
label: '场地信息补充',
options: supplementary_information,
prop: 'description',
type: 'textareaTag'
},
{
label: '场地预定截图',
options: ['有其他场地信息可备注'],
prop: 'venue_image_list',
type: 'image'
}
]
const [formData, setFormData] = useState({
name: stadium.name,
address: stadium.address,
latitude: stadium.longitude,
longitude: stadium.latitude,
istance: stadium.istance,
court_type: court_type[0] || '',
court_surface: court_surface[0] || '',
additionalInfo: '',
venue_image_list: [] as CoverImage[],
description:{
description: '',
description_tag: []
}
})
// 暴露方法给父组件
useImperativeHandle(ref, () => ({
getFormData: () => formData,
setFormData: (data: any) => setFormData(data)
}), [formData, stadium])
const handleMapLocation = () => {
Taro.chooseLocation({
success: (res) => {
console.log(res,'resres');
setFormData({
...formData,
name: res.name,
address: res.address,
latitude: res.longitude,
longitude: res.latitude
})
},
fail: (err) => {
console.error('选择位置失败:', err)
Taro.showToast({
title: '位置选择失败',
icon: 'error'
})
}
})
}
const updateFormData = useCallback((prop: string, value: any) => {
setFormData(prev => ({ ...prev, [prop]: value }))
}, [])
const getSelectedByLabel = useCallback((label: string) => {
if (label === '场地类型') return formData.court_type
if (label === '地面材质') return formData.court_surface
return ''
}, [formData.court_type, formData.court_surface])
console.log(stadium,'stadiumstadium');
return (
{/* 已选球场 */}
handleMapLocation()}
>
{formData.name}
{formData.istance} ·
{formData.address}
{stadiumInfo.map((item) => {
if (item.type === 'tags') {
const selected = getSelectedByLabel(item.label)
return (
{item.options.map((opt) => (
updateFormData(item.prop, opt)}
>
{opt}
))}
)
}
if (item.type === 'textareaTag') {
return (
updateFormData(item.prop, value)}
placeholder='有其他场地信息可备注'
options={(item.options || []).map((o) => ({ label: o, value: o }))}
/>
)
}
if (item.type === 'image') {
return (
updateFormData(item.prop, images)}
/>
)
}
return null
})}
)
})
export default StadiumDetail