"use client"

import { useState } from "react"
import { X, Wallet, CheckCircle } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { useStore } from "@/lib/store"

interface WithdrawalModalProps {
  isOpen: boolean
  onClose: () => void
  availableAmount: number
}

export function WithdrawalModal({ isOpen, onClose, availableAmount }: WithdrawalModalProps) {
  const { requestWithdrawal } = useStore()
  const [amount, setAmount] = useState<string>("")
  const [method, setMethod] = useState<"wechat" | "alipay">("wechat")
  const [account, setAccount] = useState("")
  const [name, setName] = useState("")
  const [isSubmitting, setIsSubmitting] = useState(false)
  const [isSuccess, setIsSuccess] = useState(false)

  if (!isOpen) return null

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault()
    
    const amountNum = parseFloat(amount)
    if (isNaN(amountNum) || amountNum <= 0 || amountNum > availableAmount) {
      alert("请输入有效的提现金额")
      return
    }

    if (!account || !name) {
      alert("请填写完整的提现信息")
      return
    }

    setIsSubmitting(true)
    
    // Simulate API delay
    await new Promise(resolve => setTimeout(resolve, 1000))
    
    requestWithdrawal(amountNum, method, account, name)
    
    setIsSubmitting(false)
    setIsSuccess(true)
  }

  const handleClose = () => {
    setIsSuccess(false)
    setAmount("")
    setAccount("")
    setName("")
    onClose()
  }

  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center p-4">
      <div className="absolute inset-0 bg-black/80 backdrop-blur-sm" onClick={handleClose} />
      
      <div className="relative w-full max-w-sm bg-white rounded-xl overflow-hidden shadow-2xl animate-in fade-in zoom-in duration-200">
        <button 
          onClick={handleClose}
          className="absolute top-2 right-2 p-1.5 bg-black/10 rounded-full text-gray-500 hover:bg-black/20 z-10"
        >
          <X className="w-5 h-5" />
        </button>

        {isSuccess ? (
          <div className="p-8 flex flex-col items-center text-center">
            <div className="w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mb-4">
              <CheckCircle className="w-8 h-8 text-green-600" />
            </div>
            <h3 className="text-xl font-bold text-gray-900 mb-2">申请提交成功</h3>
            <p className="text-sm text-gray-500 mb-6">
              您的提现申请已提交，预计1-3个工作日内到账。
            </p>
            <Button onClick={handleClose} className="w-full bg-green-600 hover:bg-green-700 text-white">
              完成
            </Button>
          </div>
        ) : (
          <form onSubmit={handleSubmit} className="p-6">
            <div className="flex items-center gap-2 mb-6">
              <Wallet className="w-5 h-5 text-indigo-600" />
              <h3 className="text-lg font-bold text-gray-900">申请提现</h3>
            </div>

            <div className="space-y-4 mb-6">
              <div className="space-y-2">
                <Label htmlFor="amount">提现金额 (可提现: ¥{availableAmount.toFixed(2)})</Label>
                <div className="relative">
                  <span className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-500">¥</span>
                  <Input
                    id="amount"
                    type="number"
                    min="10"
                    max={availableAmount}
                    step="0.01"
                    value={amount}
                    onChange={(e) => setAmount(e.target.value)}
                    className="pl-7"
                    placeholder="最低10元"
                  />
                </div>
              </div>

              <div className="space-y-2">
                <Label>提现方式</Label>
                <div className="flex gap-4">
                  <button
                    type="button"
                    onClick={() => setMethod("wechat")}
                    className={`flex-1 py-2 px-4 rounded-lg border text-sm font-medium transition-colors ${
                      method === "wechat"
                        ? "border-green-600 bg-green-50 text-green-700"
                        : "border-gray-200 hover:bg-gray-50 text-gray-600"
                    }`}
                  >
                    微信支付
                  </button>
                  <button
                    type="button"
                    onClick={() => setMethod("alipay")}
                    className={`flex-1 py-2 px-4 rounded-lg border text-sm font-medium transition-colors ${
                      method === "alipay"
                        ? "border-blue-600 bg-blue-50 text-blue-700"
                        : "border-gray-200 hover:bg-gray-50 text-gray-600"
                    }`}
                  >
                    支付宝
                  </button>
                </div>
              </div>

              <div className="space-y-2">
                <Label htmlFor="account">{method === "wechat" ? "微信号" : "支付宝账号"}</Label>
                <Input
                  id="account"
                  value={account}
                  onChange={(e) => setAccount(e.target.value)}
                  placeholder={method === "wechat" ? "请输入微信号" : "请输入支付宝账号"}
                />
              </div>

              <div className="space-y-2">
                <Label htmlFor="name">真实姓名</Label>
                <Input
                  id="name"
                  value={name}
                  onChange={(e) => setName(e.target.value)}
                  placeholder="请输入收款人真实姓名"
                />
              </div>
            </div>

            <Button 
              type="submit" 
              className="w-full bg-indigo-600 hover:bg-indigo-700 text-white"
              disabled={isSubmitting || !amount || !account || !name}
            >
              {isSubmitting ? "提交中..." : "确认提现"}
            </Button>
          </form>
        )}
      </div>
    </div>
  )
}
