import { useEffect, useRef, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { formatPersianNumber } from '../utils/formatNumber'
import { createCafeSchedule, MatchEvent } from '../services/campaignApi'
import { useMatchEvents } from '../hooks/useMatchEvents'
import { formatMatchLabel } from '../utils/matchCountdown'
import { getCafeUuid } from '../utils/cafe'
const ENTRY_FEE_STEP = 10000
const DESCRIPTION_MAX_LENGTH = 200
function toEnglishDigits(value: string) {
const persianDigits = '۰۱۲۳۴۵۶۷۸۹'
return value.replace(/[۰-۹]/g, (digit) => String(persianDigits.indexOf(digit)))
}
function ChevronIcon({ className = '' }: { className?: string }) {
return (
)
}
function ChevronDownIcon({ className = '' }: { className?: string }) {
return (
)
}
function MinusIcon({ className = '' }: { className?: string }) {
return (
)
}
function PlusIcon({ className = '' }: { className?: string }) {
return (
)
}
function MatchSelect({
value,
options,
now,
onChange,
}: {
value: string
options: MatchEvent[]
now: Date
onChange: (uuid: string) => void
}) {
const [open, setOpen] = useState(false)
const selectedMatch = options.find((match) => match.uuid === value)
return (
{open && (
<>
setOpen(false)} />
{options.map((match) => (
))}
>
)}
)
}
function Stepper({
value,
step,
min = 0,
suffix,
editable = false,
onChange,
}: {
value: number
step: number
min?: number
suffix: string
editable?: boolean
onChange: (next: number) => void
}) {
const atMin = value <= min
const measureRef = useRef
(null)
const [inputWidth, setInputWidth] = useState(50)
useEffect(() => {
if (editable && measureRef.current) {
setInputWidth(measureRef.current.offsetWidth)
}
}, [editable, value])
const handleInputChange = (raw: string) => {
const digitsOnly = toEnglishDigits(raw).replace(/[^\d]/g, '')
onChange(digitsOnly === '' ? min : Math.max(min, Number(digitsOnly)))
}
return (
{editable ? (
{formatPersianNumber(value)}
handleInputChange(e.target.value)}
style={inputWidth ? { width: `${inputWidth}px` } : undefined}
className="bg-transparent text-left font-medium text-gray-900 focus:outline-none"
/>
{suffix}
) : (
{formatPersianNumber(value)} {suffix}
)}
)
}
function DescriptionField({ value, onChange }: { value: string; onChange: (value: string) => void }) {
return (
)
}
export default function AddEvent() {
const navigate = useNavigate()
const [selectedMatchUuid, setSelectedMatchUuid] = useState('')
const { matches, now } = useMatchEvents()
const [capacity, setCapacity] = useState(1)
const [entryFee, setEntryFee] = useState(100000)
const [description, setDescription] = useState('')
const [isSubmitting, setIsSubmitting] = useState(false)
const [error, setError] = useState('')
const handleSubmit = async () => {
setError('')
setIsSubmitting(true)
try {
await createCafeSchedule(getCafeUuid() ?? '', {
match_event_uuid: selectedMatchUuid,
total_tickets: capacity,
ticket_price: String(entryFee),
description,
})
navigate('/owner/events/success')
} catch {
setError('ثبت رویداد با خطا مواجه شد. لطفاً دوباره تلاش کنید.')
} finally {
setIsSubmitting(false)
}
}
return (
{error &&
{error}
}
)
}