import Taro, { useLoad } from '@tarojs/taro'
import { Text, View } from '@tarojs/components'
import { useState } from 'react'
import { Page } from '@/components/ui'

export default function CheckoutSuccessPage() {
  const [expertBookingId, setExpertBookingId] = useState('')
  const [dataServiceRequestId, setDataServiceRequestId] = useState('')
  const [consultingRequestId, setConsultingRequestId] = useState('')

  useLoad((query) => {
    setExpertBookingId(String(query.expertBookingId || query.bookingId || ''))
    setDataServiceRequestId(String(query.dataServiceRequestId || ''))
    setConsultingRequestId(String(query.consultingRequestId || ''))
  })

  const title = expertBookingId ? '专家预约已付款' : dataServiceRequestId ? '数据服务已付款' : consultingRequestId ? '规划咨询已付款' : '购买成功'
  const text = expertBookingId
    ? '预约已提交给专家确认，确认后的会议方式会同步到我的专家预约。'
    : dataServiceRequestId
    ? '服务已进入进行中状态，业务员完成整理后会开放交付文件下载。'
    : consultingRequestId
    ? '咨询服务已进入执行阶段，顾问完成后会反馈最终方案。'
    : '订单已按商品拆分，服务记录已进入个人中心。'

  return (
    <Page>
      <View className="success-shell">
        <View className="success-mark">✓</View>
        <Text className="success-title">{title}</Text>
        <Text className="success-text">{text}</Text>
        <View className="success-action">
          <View
            className="primary-btn success-btn"
            onClick={() => expertBookingId
              ? Taro.redirectTo({ url: '/pages/account/expert-bookings/index' })
              : dataServiceRequestId
              ? Taro.redirectTo({ url: `/pages/account/reports/detail/index?id=${encodeURIComponent(dataServiceRequestId)}` })
              : consultingRequestId
              ? Taro.redirectTo({ url: `/pages/account/consulting/detail/index?id=${encodeURIComponent(consultingRequestId)}` })
              : Taro.switchTab({ url: '/pages/account/index' })}
          >
            {expertBookingId ? '查看专家预约' : dataServiceRequestId || consultingRequestId ? '查看服务进度' : '查看我的服务'}
          </View>
        </View>
      </View>
    </Page>
  )
}
