<template>
    <div class="p-4 bg-gradient-to-b from-blue-50/30 to-gray-50 min-h-screen">
        <!-- 实名认证提示 -->
        <div v-if="!agentStore.isRealName" class="mb-4">
            <div class="rounded-xl shadow-lg p-6" style="background-color: var(--van-theme-primary-light);">
                <div class="flex items-center mb-3">
                    <van-icon name="warning-o" class="text-xl mr-2" style="color: var(--van-theme-primary);" />
                    <h2 class="text-lg font-bold" style="color: var(--van-text-color);">
                        未完成实名认证
                    </h2>
                </div>
                <p class="text-sm mb-4" style="color: var(--van-text-color-2);">
                    根据相关规定，提现功能需要完成实名认证后才能使用，提现金额将转入您实名认证的账户中。
                </p>
                <van-button type="primary" block class="text-white rounded-xl h-10"
                    style="background-color: var(--van-theme-primary);"
                    @click="openRealNameAuth">
                    立即实名认证
                </van-button>
            </div>
        </div>
        <div>
            <!-- 提现卡片 -->
            <div class="rounded-xl shadow-lg p-6 mb-4" style="background: linear-gradient(135deg, var(--van-theme-primary-light), rgba(255,255,255,0.8));">
                <div class="flex items-center mb-6">
                    <van-icon name="alipay" class="text-xl mr-2" style="color: #1677FF;" />
                    <h1 class="text-xl font-bold" style="color: var(--van-text-color);">支付宝提现</h1>
                </div>

                <!-- 支付宝账号 -->
                <div class="mb-6">
                    <label class="text-sm mb-2 block" style="color: var(--van-text-color);">支付宝账号</label>
                    <van-field v-model="alipayAccount" placeholder="请输入支付宝账号"
                        class="flex items-center rounded-lg bg-white/90 backdrop-blur-sm shadow-sm"
                        :rules="[{ required: true, message: ' ' }]">
                        <template #left-icon>
                            <van-icon name="phone-o" style="color: var(--van-text-color-2);" />
                        </template>
                    </van-field>
                    <small class="text-xs mt-1 block" style="color: var(--van-text-color-2);">可填写支付宝账户绑定的手机号</small>
                </div>
                <!-- 支付宝实名姓名 -->
                <div class="mb-6">
                    <label class="text-sm mb-2 block" style="color: var(--van-text-color);">实名姓名</label>
                    <van-field v-model="realName" placeholder="请输入支付宝认证姓名"
                        class="flex items-center rounded-lg bg-white/90 backdrop-blur-sm shadow-sm" :rules="[
                            {
                                required: true,
                                message: ' ',
                                validator: (val) =>
                                    /^[\u4e00-\u9fa5]{2,4}$/.test(val),
                            },
                        ]">
                        <template #left-icon>
                            <van-icon name="contact-o" style="color: var(--van-text-color-2);" />
                        </template>
                    </van-field>
                    <small class="text-xs mt-1 block" style="color: var(--van-text-color-2);">请填写支付宝账户认证的真实姓名</small>
                </div>
                <!-- 提现金额 -->
                <div class="mb-4">
                    <label class="text-sm mb-2 block" style="color: var(--van-text-color);">提现金额</label>
                    <van-field v-model.number="amount" type="number" placeholder="请输入提现金额"
                        class="flex items-center rounded-lg bg-white/90 backdrop-blur-sm shadow-sm" :rules="[
                            { required: true, message: ' ' },
                            { validator: validateAmount, message: ' ' },
                        ]">
                        <template #left-icon>
                            <van-icon name="gold-coin-o" style="color: var(--van-text-color-2);" />
                        </template>
                        <template #right-icon> 元 </template>
                        <template #button>
                            <van-button size="small" type="primary"
                                class="rounded-full px-3 shadow-sm"
                                style="background-color: var(--van-theme-primary); color: white;"
                                @click="fillMaxAmount">
                                全部提现
                            </van-button>
                        </template>
                    </van-field>
                </div>

                <!-- 金额提示 -->
                <div class="text-sm mb-2" style="color: var(--van-text-color);">
                    可提现金额：<span class="font-semibold" style="color: var(--van-theme-primary);">¥{{ availableAmount }}</span>
                </div>


                <!-- 提现规则 -->
                <div class="p-4 rounded-xl backdrop-blur-sm" style="background-color: var(--van-theme-primary-light);">
                    <div class="flex items-center text-sm mb-2" style="color: var(--van-theme-primary);">
                        <van-icon name="warning" class="mr-1" />提现须知
                    </div>
                    <ul class="text-xs space-y-1" style="color: var(--van-text-color);">
                        <li>· 每日限提现1次，最低50元</li>
                        <li>· 提现收取6%税收</li>
                        <li>· 到账时间：24小时内</li>
                    </ul>
                </div>

                <!-- 税收说明 -->
                <div class="p-4 rounded-xl backdrop-blur-sm mt-4" style="background-color: rgba(251, 191, 36, 0.1);">
                    <div class="flex items-center text-sm mb-2" style="color: #f59e0b;">
                        <van-icon name="info-o" class="mr-1" />税收说明
                    </div>
                    <div class="text-xs leading-relaxed" style="color: var(--van-text-color);">
                        <p>根据相关规定，提现时将统一收取6%的税收，该税收用于相关税费支出。</p>
                        <p class="mt-2" style="color: #f59e0b;">• 税率标准：统一按6%收取</p>
                        <p style="color: #f59e0b;">• 适用范围：所有提现金额</p>
                    </div>
                </div>
            </div>

            <!-- 提交按钮 -->
            <van-button type="primary" block :loading="isSubmitting"
                class="text-white rounded-xl shadow-lg h-12 font-bold text-base"
                style="background: linear-gradient(135deg, var(--van-theme-primary), var(--van-theme-primary-dark));"
                @click="handleSubmit">
                立即提现
            </van-button>
        </div>

        <!-- 税务确认弹窗 -->
        <van-popup v-model:show="showTaxConfirmPopup" round position="center"
            :style="{ width: '85%', borderRadius: '20px' }" :overlay-style="{ backgroundColor: 'rgba(0,0,0,0.4)' }">
            <div class="p-8 bg-gradient-to-b from-white to-blue-50/30 relative">
                <div class="text-center space-y-5">
                    <!-- 标题 -->
                    <div>
                        <h2 class="text-xl font-semibold text-gray-800 mb-2">提现确认</h2>
                        <p class="text-sm text-gray-500">请确认以下提现信息</p>
                    </div>

                    <!-- 金额详情 -->
                    <div class="rounded-xl p-4 space-y-3" style="background-color: var(--van-background-color-light);">
                        <div class="flex justify-between items-center">
                            <span style="color: var(--van-text-color-2);">提现金额：</span>
                            <span class="font-semibold" style="color: var(--van-text-color);">¥{{ amount }}</span>
                        </div>
                        <div class="flex justify-between items-center">
                            <span style="color: var(--van-text-color-2);">税收：</span>
                            <span class="font-semibold text-red-500">
                                -¥{{ taxAmount.toFixed(2) }}
                            </span>
                        </div>
                        <div class="border-t pt-2" style="border-color: var(--van-border-color);">
                            <div class="flex justify-between items-center">
                                <span class="font-medium" style="color: var(--van-text-color);">实际到账：</span>
                                <span class="font-bold text-lg" style="color: var(--van-theme-primary);">¥{{ actualAmount.toFixed(2) }}</span>
                            </div>
                        </div>
                    </div>

                    <!-- 提示信息 -->
                    <div class="bg-blue-50 rounded-lg p-3 text-xs">
                        <p class="text-blue-600 font-medium mb-2">
                            税收说明
                        </p>
                        <div class="text-gray-600 space-y-1">
                            <p>• 提现金额：¥{{ amount }}</p>
                            <p>• 税率：6%</p>
                            <p class="text-blue-600">• 税收计算：¥{{ amount }} × 6% = ¥{{ taxAmount.toFixed(2) }}</p>
                        </div>
                    </div>

                    <!-- 操作按钮 -->
                    <div class="flex space-x-3 mt-6">
                        <van-button block round class="flex-1 h-11"
                            style="background-color: var(--van-background-color-light); color: var(--van-text-color-2);"
                            @click="showTaxConfirmPopup = false">
                            取消
                        </van-button>
                        <van-button block round class="flex-1 h-11 font-medium shadow-sm"
                            style="background-color: var(--van-theme-primary); color: white;"
                            :loading="isSubmitting" @click="confirmWithdraw">
                            确认提现
                        </van-button>
                    </div>
                </div>
            </div>
        </van-popup>

        <!-- 提现结果弹窗 -->
        <van-popup v-model:show="showStatusPopup" round position="center"
            :style="{ width: '85%', borderRadius: '20px' }" :overlay-style="{ backgroundColor: 'rgba(0,0,0,0.4)' }">
            <div class="p-8 bg-gradient-to-b from-white to-blue-50/30 relative">
                <!-- 状态内容 -->
                <div class="text-center space-y-5">
                    <!-- 状态图标 -->
                    <div class="relative inline-block">
                        <div class="absolute inset-0 bg-gradient-to-r opacity-20 rounded-full animate-pulse blur-sm"
                            :class="statusBg[status]"></div>
                        <van-icon :name="statusIcon[status]" size="56" class="p-1 rounded-full border-[3px]"
                            :class="statusIconClass[status]" />
                    </div>

                    <!-- 状态文案 -->
                    <div>
                        <h2 class="text-xl font-semibold mb-1" :class="statusTextColors[status]">
                            {{ statusMessages[status] }}
                        </h2>
                        <template v-if="status === 2">
                            <p class="text-sm" style="color: var(--van-text-color-2);">
                                已向
                                <span style="color: var(--van-theme-primary);">{{
                                    alipayAccount
                                    }}</span>
                                转账
                            </p>
                            <p class="text-2xl font-bold mt-2" style="color: #10b981;">
                                ¥{{ amount }}
                            </p>
                        </template>
                        <template v-if="status === 3">
                            <p class="text-sm px-4" style="color: #ef4444;">
                                {{ failMsg }}
                            </p>
                        </template>
                    </div>

                    <!-- 进度条（处理中状态） -->
                    <van-progress v-if="status === 1" :percentage="60" stroke-width="8"
                        :color="`linear-gradient(to right, var(--van-theme-primary), var(--van-theme-primary-light))`" 
                        :track-color="`var(--van-theme-primary-light)`"
                        class="!rounded-full" />

                    <!-- 辅助文案 -->
                    <div class="text-xs space-y-1.5" style="color: var(--van-text-color-2);">
                        <template v-if="status === 2">
                            <p>预计24小时内到账</p>
                            <p>可在支付宝账单中查看详情</p>
                        </template>
                        <template v-if="status === 1">
                            <p>您的申请已进入处理队列</p>
                            <p>5分钟后结果在提现记录种查看</p>
                        </template>
                    </div>

                    <!-- 操作按钮 -->
                    <van-button block round size="small"
                        class="mt-4 h-11 font-medium shadow-sm"
                        :style="{ backgroundColor: statusButtonColor[status], color: status === 1 ? 'var(--van-theme-primary)' : 'white' }"
                        @click="handlePopupAction">
                        {{
                            status === 1
                                ? "知道了"
                                : status === 2
                                    ? "完成"
                                    : "重新提现"
                        }}
                    </van-button>
                </div>
            </div>
        </van-popup>
    </div>
    <!-- 引入实名认证对话框 -->
    <RealNameAuthDialog />
</template>

<script setup>
import { ref, computed } from "vue";
import { showToast } from "vant";
import RealNameAuthDialog from "@/components/RealNameAuthDialog.vue";

const agentStore = useAgentStore();
const dialogStore = useDialogStore();
// 状态管理
const status = ref(null);
const failMsg = ref("");
const isSubmitting = ref(false);
const showStatusPopup = ref(false);
const showTaxConfirmPopup = ref(false);

// 税务
const taxFreeAmount = ref(0); // 免税额度
const usedExemptionAmount = ref(0); // 已使用免税额度
const remainingExemptionAmount = ref(0); // 剩余免税额度
const taxRate = ref(0); // 税率

// 计算扣税金额和实际到账金额
const taxAmount = computed(() => {
    if (!amount.value) return 0;
    const withdrawAmount = Number(amount.value);

    // 统一收取6%手续费，不再有免税额度
    return withdrawAmount * 0.06;
});

const actualAmount = computed(() => {
    if (!amount.value) return 0;
    return Number(amount.value) - taxAmount.value;
});

// 样式配置
const statusIcon = {
    1: "clock",
    2: "checked",
    3: "close",
};

const statusIconClass = {
    1: "text-blue-400 border-blue-100 bg-blue-50",
    2: "text-green-500 border-green-100 bg-green-50",
    3: "text-red-500 border-red-100 bg-red-50",
};

const statusBg = {
    1: "from-blue-100 to-blue-50",
    2: "from-green-100 to-green-50",
    3: "from-red-100 to-red-50",
};

const statusTextColors = {
    1: "text-blue-600",
    2: "text-green-600",
    3: "text-red-600",
};

const statusButtonColor = {
    1: "var(--van-theme-primary-light)",
    2: "#10b981",
    3: "#ef4444",
};
const statusMessages = {
    1: "提现申请处理中，请稍后再查询结果",
    2: "提现成功",
    3: "提现失败",
};

// 表单数据
const alipayAccount = ref("");
const amount = ref(0);
const availableAmount = ref(null);
const realName = ref("");

const getData = async () => {
    const { data: res, error } = await useApiFetch("/agent/revenue")
        .get()
        .json();

    if (res.value?.code === 200 && !error.value) {
        availableAmount.value = res.value.data.balance;
    }
};

onBeforeMount(() => {
    getData();
    getTax();
});

// 表单验证
const validateAmount = (val) => {
    const num = Number(val);
    return num >= 50 && num <= availableAmount.value;
};

const validateForm = () => {
    if (!realName.value.trim()) {
        showToast("请输入账户实名姓名");
        return false;
    }
    if (!/^[\u4e00-\u9fa5]{2,4}$/.test(realName.value)) {
        showToast("请输入2-4位中文姓名");
        return false;
    }
    if (!alipayAccount.value.trim()) {
        showToast("请输入支付宝账号");
        return false;
    }

    const amountNum = Number(amount.value);
    if (!amount.value || isNaN(amountNum)) {
        showToast("请输入有效金额");
        return false;
    }

    if (amountNum < 50) {
        showToast("提现金额不能低于50元");
        return false;
    }

    if (amountNum > availableAmount.value) {
        showToast("超过可提现金额");
        return false;
    }

    return true;
};

// 打开实名认证对话框
const openRealNameAuth = () => {
    dialogStore.openRealNameAuth();
};

// 获取税务
const getTax = async () => {
    const { data, error } = await useApiFetch("/agent/withdrawal/tax/exemption")
        .get()
        .json();
    if (data.value?.code === 200 && !error.value) {
        taxFreeAmount.value = data.value.data.total_exemption_amount;
        usedExemptionAmount.value = data.value.data.used_exemption_amount;
        remainingExemptionAmount.value = data.value.data.remaining_exemption_amount;
        taxRate.value = data.value.data.tax_rate;
    }
};

const handleSubmit = async () => {
    // 检查实名认证状态
    if (!agentStore.isRealName) {
        showToast("请先完成实名认证");
        openRealNameAuth();
        return;
    }

    // 先进行表单验证
    if (!validateForm()) return;

    // 显示税务确认弹窗
    showTaxConfirmPopup.value = true;
};

// 确认提现
const confirmWithdraw = async () => {
    isSubmitting.value = true;
    try {
        const { data, error } = await useApiFetch("/agent/withdrawal")
            .post({
                payee_account: alipayAccount.value,
                amount: amount.value,
                payee_name: realName.value,
            })
            .json();
        if (data.value?.code === 200) {
            status.value = data.value.data.status;
            showTaxConfirmPopup.value = false;
            showStatusPopup.value = true;
            if (status.value === 3) {
                failMsg.value = data.value.data.fail_msg;
            }
        }
    } catch {
    } finally {
        isSubmitting.value = false;
    }
};

// 弹窗操作
const handlePopupAction = () => {
    if (status.value === 3) {
        showStatusPopup.value = false;
        resetForm();
    } else {
        showStatusPopup.value = false;
        if (status.value === 2) resetPage();
    }
};
// 填充最大金额
const fillMaxAmount = () => {
    amount.value = availableAmount.value;
};

// 重置页面
const resetForm = () => {
    status.value = null;
    alipayAccount.value = "";
    amount.value = "";
    realName.value = "";
};
</script>

<style>
/* 自定义表单样式 */
.van-field__control {
    @apply py-1 px-4 text-gray-800;
}

.van-field__error-message {
    @apply mt-1;
}

.van-button--disabled {
    @apply opacity-60 cursor-not-allowed;
}

/* 弹窗入场动画 */
.van-popup {
    transition: transform 0.4s cubic-bezier(0.22, 0.61, 0.36, 1),
        opacity 0.3s ease;
}

.van-popup-enter-active,
.van-popup-leave-active {
    transition: opacity 0.3s;
}

.van-popup-enter-from,
.van-popup-leave-to {
    opacity: 0;
}

.van-popup-enter-active {
    transform: scale(0.95);
}

.van-popup-enter-to {
    transform: scale(1);
}

/* 状态图标动画 */
.animate-pulse {
    animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}

@keyframes pulse {
    50% {
        opacity: 0.5;
    }
}

/* 实名认证提示样式 */
.pointer-events-none {
    pointer-events: none;
}
</style>
