<template>
    <view class="skeleton-box" v-if="pageLoading">
        <van-skeleton title row="4" />
        <van-skeleton title avatar avatar-size="50px" row="4" />
        <van-skeleton title row="4" />
        <van-skeleton title avatar avatar-size="50px" row="4" />
    </view>
    <view class="container" v-else>
        <view class="imgbox">
            <image v-if="status == 'success'" :src="imgHost + '/images/icon/result-success.png'"></image>
            <image v-if="status != 'success'" :src="imgHost + '/images/icon/result-fail.png'"></image>
        </view>
        <view class="tips">
            <text :class="status == 'success' ? 'c-blue' : 'c-red'">{{ status == 'success' ? '提交成功' : '提交失败!' }}</text>
        </view>
        <view class="btn-box">
            <button class="btn" :data-status="status" @tap="toBack">返回</button>
            <button class="btn" v-if="status != 'success'" @tap="resubmit">再次提交</button>
            <button class="btn" v-if="status == 'success'" @tap="goRegister">去挂号</button>
        </view>
    </view>
</template>

<script>
import $http from "@/utils/https";
import Util from "@/utils/utils";
import httpConfig from '@/utils/httpConfig';
import VanSkeleton from '@/components/skeleton/index'
export default {
    components: {
        'van-skeleton': VanSkeleton
    },
    data() {
        return {
            imgHost: httpConfig.imgHost,
            pageLoading: true,
            status: ''
        }
    },
    onLoad(options) {
        const self = this;
        if (options.hasOwnProperty('status')) {
            self.setData({
                status: options.status
            }, function () {
                setTimeout(function () {
                    self.pageLoading = false
                    self.setData(self)
                }, 1500)
            })
        }
    },
    methods: {
        resubmit() {
            const { questionnaireResultData } = app.globalData
            this.submitQuestionnaire(questionnaireResultData)
        },
        submitQuestionnaire(questionnaireObj) {
            const self = this
            const questionnaireDetail = app.globalData.questionnaireDetail
            const userData = this.$cache.get('userData')
            return new Promise((resolve, reject) => {
                let data = {
                    params: [{
                        "id": questionnaireDetail.id,
                        "code": questionnaireDetail.questionnaireCode,
                        "hospCode": questionnaireDetail.hospCode,
                        "patNo": "",
                        "ipAddress": userData.ipAddress,
                        "detail": [questionnaireObj]
                    }]
                }
                uni.showLoading({
                    title: '数据提交中...',
                })
                $http.post("urlS", self, {
                    // namespace: "Y",
                    code: "03010007",
                    data: data,
                    success: function (res) {
                        setTimeout(function () {
                            if (uni.hideLoading(), +res.errorCode === 0) {
                                uni.showToast({
                                    icon: 'none',
                                    title: res.errorMessage,
                                    duration: 2e3,
                                    success() {
                                        uni.redirectTo({
                                            url: '/pages/index/pages/questionnaire/result/result?status=success'
                                        })
                                    }
                                })
                            }
                            resolve(true)
                        }, 2000)
                    },
                    fail(err) {
                        uni.hideLoading()
                        reject(err)
                    }
                })
            })
        },
        toBack(e) {
            const status = e.currentTarget.dataset.status
            let pages = getCurrentPages()
            let prevPage = pages[pages.length - 2]
            prevPage.setData({
                refreshStatus: status == 'success' ? true : false
            })
            uni.navigateBack({
                delta: 1,
            })
        },
    }
}
</script>