<!--申请退款-->
<template>
    <nav-bar :navbar-data="navbarData"></nav-bar>
    <view :style="{ marginTop: height +'px' }">
        <view class="card">
            <view v-if="detailObj.invType == 'O'">
                <view class="card_type">
                    <text style="font-size:30rpx;color:rgba(51,51,51,1);">{{ hospDesc }}</text>
                </view>
                <view v-for="(item, index) in detailObj.detail" :key="index">
                    <view class="border-top-before"></view>
                    <view style="display: flex;flex-direction: row;">
                        <view class="top"></view>
                        <view class="card_drup">
                            <text style="font-size:28rpx;color:#333333">{{ item.medItemDesc }}</text>
                        </view>
                        <view style="margin:16rpx 23rpx 0 0;width:158rpx">
                            <view style="float:right">
                                <text style="font-size:24rpx;color:#999999">￥</text>
                                <text style="font-size:28rpx;">{{ item.medPrice }}</text>
                            </view>
                        </view>
                    </view>
                    <view style="margin:12rpx 23rpx 16rpx 50rpx">
                        <text style="font-size:26rpx;">{{ item.spec }}</text>
                        <text style="font-size:26rpx;float:right">x{{ item.qty }}{{ item.unitDesc }}</text>
                    </view>
                </view>
                <!-- <view class="border-top-before"></view>
                <view style="margin:20rpx 23rpx 20rpx 23rpx">
                    <text >配送费</text>
                    <text style="float:right">¥ 6</text>
                </view> -->
                <view class="border-top-before"></view>
                <view style="padding:20rpx 23rpx 20rpx 23rpx">
                    <text>小计</text>
                    <text style="float:right">¥ <text style="color:#FA541C">{{ detailObj.ordReasonPatAmt }}</text></text>
                </view>
                <view class="border-top-before"></view>
                <view style="padding:20rpx 23rpx 20rpx 23rpx">
                    <view>
                        <text>联系商家</text>
                        <text style="float:right">{{ telephone }}</text>
                    </view>
                </view>
            </view>

            <view v-if="detailObj.invType == 'R'">
                <view class="table">
                    <text>下单时间</text>
                    <text style="float:right">{{ detailObj.admDateTime }}</text>
                </view>
                <view class="table">
                    <text>订单类型</text>
                    <text style="float:right">{{ detailObj.businessDesc }}</text>
                </view>
                <view class="table">
                    <text>订单金额</text>
                    <view style="float:right">
                        <text>￥</text>
                        <text style="color:rgba(250,84,28,1);">{{ detailObj.ordReasonPatAmt }}</text>
                    </view>
                </view>
                <view class="table">
                    <text>订单状态</text>
                    <text style="float:right">{{ detailObj.orderType == "P" ? "已付款" : "未付款" }}</text>
                </view>

                <view class="table">
                    <text>医生</text>
                    <text style="float:right">{{ detailObj.admDocDesc }}</text>
                </view>
                <!-- <view class="table">
                    <text>就诊时间</text>
                    <text style="float:right">{{detailObj.admDateTime}}</text>
                </view> -->
                <view class="table">
                    <text>订单号</text>
                    <text style="float:right">{{ detailObj.payTradeNum }}</text>
                </view>
            </view>
            <view class="border-top-before"></view>
            <view style="margin:20rpx 23rpx 20rpx 23rpx">
                <view>
                    <view>
                        <text><text style="color:#F5222D">*</text>退款原因</text>
                    </view>
                    <view style="padding-top:16rpx">
                        <!-- <textarea size="large" :value="value" placeholder="请输入退款原因（必填）" border="true" @input="getInputValue" /> -->
						<textarea placeholder-style="color:#d1d1d1" placeholder="请输入退款原因（必填）" border="true" @input="getInputValue"/>
                    </view>
                </view>
            </view>
        </view>
        <view class="footbutton">
            <van-button type="default" customClass="btn" @tap="turnOrder">取消</van-button>
            <van-button type="info" customClass="btn" @tap="refund">提交</van-button>
        </view>
    </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'
const app = getApp();
export default {
    components: {
        'nav-bar': navBar,
        'van-button': VanButton
    },
    data() {
        return {
            height: app.globalData.height,
            navbarData: {
                isGoHome: true,
                isGoBack: true,
                title: "申请退款"
            },
            hospDesc: String,
            hospAddress: String,
            telephone: String,
            value: String,
            detailObj: {},
            userData: null
        }
    },
    onLoad: function (options) {
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.on("detailObj", (data) => {
            this.setData({
                ...data
            });
        })
        const userData = this.$cache.get('userData')
        if (userData) {
            this.setData({
                userData: userData,
                hospDesc: userData.hospDesc,
                hospAddress: userData.hospAddress,
                telephone: userData.telephone,
            })
        }
    },
    methods: {
        getInputValue(event) {
            this.setData({
                value: event.detail.value
            })
        },

        // 推送退号成功消息
        handleSendMessage(receiveType) {
            let {
                detailObj,
                userData
            } = this;
            let messageContent = {
                userID: detailObj.admDocID,
                admID: detailObj.admID || "",
                patID: userData.hisPatID || "",
                patName: detailObj.patName || "",
                userName: detailObj.admDocDesc || "",
                type: 'C',
                text: '退费成功。',
            };
            let message = app.globalData.tim.createTextMessage({
                to: receiveType === 'user' ? receiveType + detailObj.admDocID : userData.imUserID,
                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(receiveType);
                    }
                }).catch(function (imError) {
                    // 发送失败
                    uni.showToast({
                        title: imError,
                        icon: 'none',
                        duration: 4000
                    })
                });
            }
        },

        // 新增消息记录
        newMessageRecord(receiveType) {
            let {
                detailObj,
                userData
            } = this;
            let data = {
                params: [{
                    sendUserName: detailObj.patName,
                    receiveUserName: receiveType === 'user' ? detailObj.admDocDesc : detailObj.patName,
                    eventCode: '2004',
                    sendTypeCode: 'P',
                    receiveTypeCode: receiveType === 'patient' ? 'P' : 'U',
                    sendUser: userData.hisPatID,
                    receiveUser: receiveType === 'user' ? detailObj.admDocID : userData.hisPatID,
                    content: "退费成功。",
                    userID: '',
                    jumpPages: receiveType === 'user' ? '' : '/pages/order/order',
                    admID: detailObj.admID
                }]
            }
            $http.post("urlS", this, {
                code: "03100096",
                data: data,
                namespace: 'Y',
                success: function (res) {
                    if (+res.errorCode === 0) {
                        console.log(res)
                    }
                }
            });
        },

        refund: Utils.debounce(function () {
            var {
                authCode,
                detailObj,
                value
            } = this;
            const userData = this.$cache.get('userData')
            if (!value) {
                uni.showToast({
                    title: '退款原因不能为空',
                    icon: 'none',
                    duration: 3000
                })
                return;
            }
            uni.showLoading({
                mask: true,
                title: '处理中,请稍后...'
            })
            let data = {
                params: [{
                    ...detailObj,
                    reasonDesc: value
                }]
            }
            if (authCode) {
                data.params[0]['authCode'] = authCode
                data.params[0]['openID'] = userData.openID
            }
            $http.post("urlS", this, {
                code: "01050008",
                data: data,
                success: function (res) {
                    console.log(res)
                    uni.hideLoading();
                    if (+res.errorCode === 0) {
                        // 临时注释
                        // this.handleSendMessage('patient');
                        // this.handleSendMessage('user');
                        // this.newMessageRecord('user');
                        uni.showToast({
                            title: '退款成功',
                            duration: 3000,
                            success() {
								setTimeout(() => {
									uni.switchTab({
									    url: '../../../tabBar/order/order?flag=Y',
									})
								}, 1000)
                            }
                        })
                    } else {
                        uni.showToast({
                            title: res.errorMessage,
                            icon: 'none',
                            duration: 4000
                        })
                    }
                },
                error: function () {
                    uni.hideLoading();
                    uni.showToast({
                        title: "网络错误，请稍后再试！",
                        icon: 'none',
                        duration: 4000
                    })
                }
            });
        }),

        turnOrder() {
            uni.navigateBack()
        },
    }
}
</script>

<style>
text {
  font-size: 32rpx;
  font-family: PingFang SC;
  color: #666666;
  line-height: 39rpx;
}

.card_address {
  background: rgba(255, 255, 255, 1);
  border-radius: 10rpx;
  margin: 0 24rpx 10rpx 24rpx;

}

.card_drup {
  margin: 16rpx 0 0 10rpx;
  width: 483rpx;
}

.foot {
  height: 92rpx;
  background: white;
  line-height: 92rpx;
  bottom: 0rpx;
  position: fixed;
  width: 100%;
}

.paybutton {
  width: 240rpx;
}

.card {
  background: rgba(255, 255, 255, 1);
  border-radius: 10rpx;
  margin: 10rpx 25rpx 0 25rpx;
}

.top {
  width: 10rpx;
  height: 10rpx;
  background: rgba(204, 204, 204, 1);
  border-radius: 50%;
  margin: 30rpx 0 0 32rpx
}

.card_type {
  margin: 20rpx 0 20rpx 25rpx;
}

.bottom_line {
  width: 750rpx;
  height: 1rpx;
  background: rgba(245, 244, 244, 1);
}

.input {
  font-size: 32rpx;
}

.van-cell__title {
  font-size: 32rpx;
  margin-left: -10rpx;

}

.van-button--info {
  border: 0 !important;
  margin-top: -5rpx !important;
}

.time_show {
  color: #FA8C16 !important;
}

textarea {
  width: 615rpx !important;
  height: 160rpx !important;
  border: 1rpx solid rgba(225, 225, 225, 1) !important;
  border-radius: 4rpx !important;
  padding: 20rpx !important;
}

button {
  width: 50% !important;
}

.btn {
    height: 44px
}

.table {
  background: rgba(255, 255, 255, 1);
  margin-top: 1rpx;
  padding: 20rpx 23rpx 20rpx 23rpx;
}

.footbutton {
  position:fixed; 
  bottom: env(safe-area-inset-bottom);
  width:100%
}
</style>