import React, { useState } from 'react'
import { View, Text, ScrollView } from '@tarojs/components'
import Taro from '@tarojs/taro'

definePageConfig({
  navigationBarTitleText: '售后进度',
})

const AfterSaleProgressPage: React.FC = () => {
  const [saleInfo, setSaleInfo] = useState({
    id: 'AS202605120001',
    type: 2, // 1:退款, 2:退货退款, 3:换货
    status: 2, // 1:待审核, 2:审核通过, 3:审核拒绝, 4:已完成, 5:已取消
    reason: '商品质量问题',
    amount: 89.00,
    createTime: '2026-05-10 14:30:00',
    orderId: '123456',
    goodsName: '高品质保温杯',
  })

  // 进度步骤
  const steps = [
    { time: '2026-05-10 14:30', title: '申请提交', desc: '您的售后申请已提交', done: true },
    { time: '2026-05-10 15:00', title: '商家审核', desc: '商家已审核通过，请退货', done: true },
    { time: '2026-05-11 09:00', title: '寄回商品', desc: '您已寄回商品，等待商家收货', done: true },
    { time: '2026-05-12 10:00', title: '商家收货', desc: '商家已收到商品，正在处理', done: false },
    { time: '', title: '退款完成', desc: '退款将返回您的原支付账户', done: false },
  ]

  // 获取售后类型名称
  const getTypeLabel = (type: number) => {
    const map: Record<number, string> = { 1: '仅退款', 2: '退货退款', 3: '换货' }
    return map[type] || '未知'
  }

  // 获取状态标签
  const getStatusLabel = (status: number) => {
    const map: Record<number, { label: string; color: string }> = {
      1: { label: '待审核', color: 'text-orange-500' },
      2: { label: '审核通过', color: 'text-green-500' },
      3: { label: '审核拒绝', color: 'text-red-500' },
      4: { label: '已完成', color: 'text-blue-500' },
      5: { label: '已取消', color: 'text-gray-400' },
    }
    return map[status] || { label: '未知', color: 'text-gray-400' }
  }

  const statusInfo = getStatusLabel(saleInfo.status)

  // 取消售后
  const handleCancel = () => {
    Taro.showModal({
      title: '提示',
      content: '确定取消售后申请吗？',
      confirmColor: '#0e932e',
      success: (res) => {
        if (res.confirm) {
          Taro.showToast({ title: '已取消', icon: 'success' })
        }
      }
    })
  }

  return (
    <View className='min-h-screen bg-gray-50 flex flex-col'>
      <ScrollView scrollY className='flex-1'>
        {/* 状态卡片 */}
        <View className='p-4 text-white' style={{ background: 'linear-gradient(to right, #60a5fa, #22d3ee)' }}>
          <Text className='text-2xl font-bold block mb-2'>
            {statusInfo.label}
          </Text>
          <Text className='text-sm opacity-80 block mb-1'>
            售后编号：{saleInfo.id}
          </Text>
          <Text className='text-sm opacity-80 block'>
            申请时间：{saleInfo.createTime}
          </Text>
        </View>

        {/* 售后信息 */}
        <View className='bg-white mx-3 rounded-xl p-4 relative z-10 shadow-sm' style={{ marginTop: '-12px' }}>
          <Text className='text-sm font-medium text-gray-800 mb-3 block'>售后信息</Text>
          
          <View className='flex justify-between py-2 border-b border-gray-50'>
            <Text className='text-sm text-gray-500'>售后类型</Text>
            <Text className='text-sm text-gray-800'>{getTypeLabel(saleInfo.type)}</Text>
          </View>
          <View className='flex justify-between py-2 border-b border-gray-50'>
            <Text className='text-sm text-gray-500'>售后原因</Text>
            <Text className='text-sm text-gray-800'>{saleInfo.reason}</Text>
          </View>
          {saleInfo.type === 1 && (
            <View className='flex justify-between py-2 border-b border-gray-50'>
              <Text className='text-sm text-gray-500'>退款金额</Text>
              <Text className='text-sm text-red-500 font-bold'>¥{saleInfo.amount}</Text>
            </View>
          )}
          <View className='flex justify-between py-2'>
            <Text className='text-sm text-gray-500'>关联订单</Text>
            <View className='flex items-center gap-1' onClick={() => Taro.navigateTo({ url: `/pages/order/detail?id=${saleInfo.orderId}` })}>
              <Text className='text-sm text-blue-500'>{saleInfo.orderId}</Text>
              <Text className='text-gray-400 text-xs'>→</Text>
            </View>
          </View>
        </View>

        {/* 进度步骤 */}
        <View className='bg-white mx-3 mt-3 rounded-xl p-4'>
          <Text className='text-sm font-medium text-gray-800 mb-3 block'>处理进度</Text>
          
          {steps.map((step, index) => (
            <View key={index} className={`flex mb-3 ${index === steps.length - 1 ? 'mb-0' : ''}`}>
              {/* 时间线 */}
              <View className='flex flex-col items-center mr-3'>
                <View className={`w-3 h-3 rounded-full mt-1 ${
                  step.done ? 'bg-green-500' : 'bg-gray-300'
                }`} />
                {index < steps.length - 1 && (
                  <View className={`w-px flex-1 ${step.done ? 'bg-green-500' : 'bg-gray-300'}`} />
                )}
              </View>

              {/* 内容 */}
              <View className='flex-1 pb-3'>
                <Text className={`text-sm font-medium block ${
                  step.done ? 'text-gray-800' : 'text-gray-400'
                }`}>
                  {step.title}
                </Text>
                <Text className='text-xs text-gray-500 mt-0 block'>
                  {step.desc}
                </Text>
                {step.time && (
                  <Text className='text-xs text-gray-400 mt-1 block'>{step.time}</Text>
                )}
              </View>
            </View>
          ))}
        </View>

        {/* 商品信息 */}
        <View className='bg-white mx-3 mt-3 rounded-xl p-4'>
          <Text className='text-sm font-medium text-gray-800 mb-3 block'>商品信息</Text>
          <View className='flex items-center gap-2'>
            <View className='w-12 h-12 bg-gray-100 rounded-lg flex items-center justify-center flex-shrink-0'>
              <Text className='text-2xl'>🛍️</Text>
            </View>
            <Text className='text-sm text-gray-700 flex-1'>{saleInfo.goodsName}</Text>
          </View>
        </View>

        {/* 操作按钮 */}
        {saleInfo.status === 1 && (
          <View className='p-3'>
            <View
              className='text-center py-3 rounded-full border border-red-500'
              onClick={handleCancel}
            >
              <Text className='text-sm text-red-500'>取消申请</Text>
            </View>
          </View>
        )}

        <View className='h-6' />
      </ScrollView>
    </View>
  )
}

export default AfterSaleProgressPage
