<template>
    <view class="container">
        <!-- 加载H5认证页面 -->
        <web-view
            :src="userSignUrl"
            @message="handleMessage"
            @error="onWebviewError"
        ></web-view>

        <!-- 加载状态 -->
        <view v-if="loading" class="loading-mask">
            <text>正在加载认证页面...</text>
        </view>
    </view>
</template>

<script>
import {signApi} from "@/api/sign";

export default {
    data() {
        return {
            contractId:'',
            userSignUrl: '',
            loading: true
        }
    },
    onLoad(options) {

        if (options.contractId) {
            this.contractId = options.contractId
            this.loading = false
        } else {
            uni.showToast({ title: '协议链接错误', icon: 'none' })
            uni.navigateBack()
        }

        if (options.userSignUrl) {
            this.userSignUrl = decodeURIComponent(options.userSignUrl)
            this.loading = false
        } else {
            uni.showToast({ title: '协议链接错误', icon: 'none' })
            uni.navigateBack()
        }
    },
    methods: {
        // 接收H5页面消息
        async handleMessage(e) {
            const [message] = e.detail.data

            console.log('收到H5消息:', JSON.parse(JSON.stringify(message)))

            try {
                //查询签约状态？
                setTimeout(() => {
                    uni.navigateBack({ delta: 2 }); // 返回两级页面
                }, 1500);


                if (message.code === 100000) {
                    message['contract_id'] = this.contractId;
                    // 验证结果（需调用后端接口）
                    const res = await signApi.signCheckApi(message)
                    if (res.code === 100000) {
                        setTimeout(() => {
                            uni.navigateBack({ delta: 2 }); // 返回两级页面
                        }, 1500);


                    } else {
                        uni.showModal({
                            title: '认证失败',
                            content: res.msg || '请重新进行认证',
                            showCancel: false
                        })
                    }
                }else{
                    uni.showModal({
                        title: '认证失败',
                        content: message.msg,
                        showCancel: false
                    })
                }



            } catch (error) {
                uni.showToast({ title: '协议签署失败', icon: 'none' })
            }
        },

        // 处理加载失败
        onWebviewError() {
            this.loading = false
            uni.showModal({
                title: '提示',
                content: '协议签署页面加载失败，请检查网络后重试',
                showCancel: false,
                success: () => {
                    uni.navigateBack()
                }
            })
        }
    }
}
</script>

<style scoped>
.loading-mask {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255,255,255,0.9);
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>
