<template>
    <nav-bar id="navBar" ref="navBar" :navbar-data="navbarData"></nav-bar>
    <view :style="{backgroundColor: '#FFF', height: 'calc(100vh - '+ height +'px)', marginTop: height +'px' }">
        <view class="ith-head">
            <image :src="imgHost +'/images/icon/paySucc.png'" style="width: 148rpx;height: 148rpx;"></image>
            <view style="color:#1890FFFF;" class="font-36">支付成功</view>
            <view style="margin-top: 24rpx; margin-bottom: 18rpx;">
                <text class="font-48">￥</text>
                <text class="font-64">{{ payAmtInt }}</text>
            </view>
            <view class="font-36">订单号：{{ payTradeNum }}</view>
        </view>
        <view class="pay-success-btn">
            <!--暂时隐藏-后期启用视频问诊再开启-->
            <van-button v-if="buttonDisabled" type="info" color="#1890FFFF" @tap="afterPay" custom-class="button-css" :disabled="buttonDisabled">{{buttonMessage}}</van-button>
            <van-button type="info" color="#1890FFFF" plain @tap="gotoIndex" custom-class="button-css">首页</van-button>
            <van-button type="info" color="#1890FFFF" plain @tap="gotoOrder('Y')" custom-class="button-css">账单</van-button>
        </view>
        <!-- <van-toast id="van-toast" /> -->
    </view>
</template>

<script>
import $http from "@/utils/https";
import Utils from '@/utils/utils';
import httpConfig from '@/utils/httpConfig'
import NavBar from '@/components/navBar/index'
import VanButton from '@/components/button/index'
// #ifdef MP
import TIM from 'tim-wx-sdk';
// #endif
// #ifdef H5
import TIM from 'tim-js-sdk';
// #endif
const app = getApp();
export default {
    components: {
        'nav-bar': NavBar,
        'van-button': VanButton
    },
    data() {
        return {
            height: app.globalData.height,
            navbarData: {
                isGoHome: false,
                isGoBack: false,
                title: "支付成功"
            },
            imgHost: httpConfig && httpConfig.imgHost ? httpConfig.imgHost : "",
            entrance: '',
            payAmtInt: '',
            payTradeNum: '',
            prescribeTradeNum: '',
            trandeInfo: {},
            buttonUrl: '',
            buttonMessage: '',
            flag: false,
            buttonDisabled: false
        }
    },
    onLoad: function (options) {
        console.log(options)
        this.setData({
            payAmtInt: options.payAmtInt,
            flag: options.flag === 'false' ? false : true,
            payTradeNum: options.payTradeNum,
            prescribeTradeNum: options.prescribeTradeNum,
            buttonMessage: options.flag === 'true' ? "查看咨询内容" : "进入候诊室",
            entrance: app.globalData.reservationEntrance,
        }, () => {
        // 暂时隐藏-后期启用视频问诊再开启
        // setTimeout(() => {
        //   this.getInfoByTrandNo();
        // }, 500)
        })
    },
    methods: {
        //跳转首页
        gotoIndex: Utils.debounce(function () {
            uni.switchTab({
                url: '../../../tabBar/index/index'
            })
        }),

        // 跳转账单页
        gotoOrder: Utils.debounce(function (flag) {
            let url = '../../../tabBar/order/order'
            if (flag == 'Y') {
                url + '?flag=Y'
            }
            uni.switchTab({
                url: url,
            })
        }),

        //支付成功
        afterPay: Utils.debounce(function () {
            let info = this.trandeInfo
            console.log(info)
            let param = ""
            if (this.trandeInfo.admID) {
                param = '?admID=' + info.admID + '&userID=' + info.frUserID + '&toUser=' + info.toUserID + '&toUserName=' + info.doctorName + '&docImg=' + info.doctorImageUrl + '&patName=' + info.patName;
                uni.navigateTo({
                    url: this.flag ? ("../../../message/pages/im/room/room" + param) : ("../../pages/waitingConsulation/waitingConsulation" + param),
                })
            }
        }),

        //根据订单号查询信息
        getInfoByTrandNo: function () {
            let data = {
                params: [{
                    payTradeNum: this.payTradeNum,
                    prescribeTradeNum: this.prescribeTradeNum
                }]
            }
            $http.post("urlS", this, {
                code: "01050004",
                data: data,
                success: function (res) {
                    console.log(res)
                    if (+res.errorCode === 0) {
                        this.setData({
                            trandeInfo: res.result
                        }, () => {
                            this.handleSendMessage();
                            this.newMessageRecord('user');
                        });
                    } else {
                        Utils.showToast(res.errorMessage)
                        this.setData({
                            trandeInfo: {},
                            buttonDisabled: true
                        });
                    }
                }
            });
        },

        // 发送预约成功消息
        handleSendMessage() {
            let {
                trandeInfo,
                flag,
                entrance
            } = this;
            let messageContent = {
                userID: trandeInfo.doctorID ? trandeInfo.doctorID : '',
                admID: trandeInfo.admID || "",
                patID: trandeInfo.patID ? trandeInfo.patID : '',
                patName: trandeInfo.patName || "",
                userName: trandeInfo.doctorName || "",
                type: 'A',
                text: flag ? '图文预约成功。' : '视频预约成功。',
            };
            let message = app.globalData.tim.createTextMessage({
                to: trandeInfo.toUserID,
                conversationType: TIM.TYPES.CONV_C2C,
                payload: {
                    text: JSON.stringify(messageContent)
                }
            });
            // 2. 发送消息
            let promise = app.globalData.tim.sendMessage(message);
            if (promise) {
                console.log("消息发送成功")
                promise.then((imResponse) => {
                    // 发送成功
                    if (imResponse && imResponse.code === 0) {
                        this.newMessageRecord('patient');
                    }
                }).catch((imError) => {
                    this.newMessageRecord('patient');

                    // 发送失败
                    // Toast({
                    //   context: this,
                    //   message: '网络异常，请稍后再试。'
                    // });
                });
            } else {
                this.newMessageRecord('patient');

                // Toast({
                //   context: this,
                //   message: '网络异常，请稍后再试。'
                // });
            }
        },

        // 新增消息记录
        newMessageRecord(receiveType) {
            let {
                trandeInfo,
                flag
            } = this;
            let data = {
                params: [{
                    sendUserName: trandeInfo.patName,
                    receiveUserName: receiveType === 'user' ? trandeInfo.doctorName : trandeInfo.patName,
                    eventCode: flag ? '2001' : '2002',
                    sendTypeCode: 'P',
                    receiveTypeCode: receiveType === 'patient' ? 'P' : 'U',
                    sendUser: trandeInfo.patID,
                    receiveUser: receiveType === 'user' ? trandeInfo.doctorID : trandeInfo.patID,
                    content: flag ? '图文预约成功。' : '视频预约成功。',
                    userID: '',
                    jumpPages: receiveType === 'patient' ? (flag ? 'pages/message/pages/im/room/room' : 'pages/index/pages/waitingConsulation/waitingConsulation') : '',
                    admID: trandeInfo.admID
                }]
            }
            $http.post("urlS", this, {
                code: "03100096",
                data: data,
                namespace: 'Y',
                success: function (res) {
                    if (+res.errorCode === 0) {
                        console.log(res)
                    }
                }
            });
        },
    }
}
</script>

<style lang="scss">
/* pages/index//pages/paySuccess/paySuccess.wxss */
.ith-head {
  padding-top: 55rpx;
  text-align: center;
}

.font-36 {
  font-size: 36rpx;
  font-family: PingFang SC;
  color: #666666FF;
}

.font-48 {
  font-size: 48rpx;
  font-family: PingFang SC;
  color: #666666FF;
}

.font-58 {
  font-size: 58rpx;
  font-family: PingFang SC;
  color: #333333FF;
}

.font-64 {
  font-size: 64rpx;
  font-family: PingFang SC;
  color: #333333FF;
}

.title {
  font-size: 40rpx;
  font-weight: 550;
  padding-bottom: 20rpx;
}

.pay-success-btn {
  display: flex;
  padding: 0 36rpx;
  box-sizing: border-box;
  justify-content: space-around;
  margin-top: 80rpx;
  text-align: center;
}

.pay-success-btn .button-css.van-button {
  width: 260rpx;
  border-radius: 10rpx;
  font-size: 28rpx;
  margin: 0 10rpx;
}
</style>