<template>
    <nav-bar :navbar-data="navbarData" />
    <view :style="{ marginTop: height + 'px', height: 'calc(100% - ' + height + 'px)' }">
        <view class="wc-content">
            <van-cell-group>
                <van-cell title="科室医生" :value="visitRelatedInformation.doctorName" />
                <van-cell title="就诊人" :value="visitRelatedInformation.patName" />
                <van-cell title="排队人数" :value="visitRelatedInformation.waitListNum || 0" />
                <van-cell value-class="status" title="状态" :value="visitRelatedInformation.curStatus" />
                <van-cell title="就诊时段" :value="visitRelatedInformation.timeInterval" />
            </van-cell-group>
            <!-- <view class="wc-permission" v-if="{{!(scopeCamera&&scopeRecord)}}">
                <view class="wc-permission-title">视频问诊需要一下权限</view>
                <view class="wc-permission-content">{{(!scopeCamera&&!scopeRecord) ? "摄像头/麦克风" : (!scopeCamera ? "摄像头" : "麦克风")}}</view>
                <view class="wc-permission-desc">让医生更好的进行诊疗，满足政策监管要求。</view>
            </view> -->
        </view>
        <view class="wc-btn">
            <view v-if="!isLink && visitRelatedInformation && visitRelatedInformation.curStatusCode">
                <view style="margin-bottom: 24rpx;">
                    <van-button v-if="visitRelatedInformation.curStatusCode === '01'" type="info" block
                        @tap="buttonOnClick" :disabled="buttonClicked">{{ buttonTitle }}</van-button>
                    <van-button v-if="visitRelatedInformation.curStatusCode === '02'" type="info" block
                        @tap="allowToJoin">进入诊室</van-button>
                    <van-button v-if="visitRelatedInformation.curStatusCode === '03'" type="info" block
                        @tap="handleGoMyConsultation">我的问诊</van-button>
                </view>
                <!-- <van-button
                    v-if="visitRelatedInformation.curStatusCode === '01' || visitRelatedInformation.curStatusCode === '02'"
                    block @tap="inviteRelation">邀请亲属视频陪诊</van-button> -->
                <van-button v-if="visitRelatedInformation.curStatusCode === '03'" block
                    @tap="handleGoHome">首页</van-button>
                <van-button style="margin-top: 20rpx; background-color: #1890FF; color: #fff; border: 0;"
                    v-if="visitRelatedInformation.curStatusCode === '01' || visitRelatedInformation.curStatusCode === '02'"
                    block @tap="handleRemindDoctors">提醒医生接诊</van-button>
            </view>
            <!-- <view v-if="{{!(scopeCamera&&scopeRecord)}}">
            <van-button type="info" block @tap="handleOpenPermission">开启权限</van-button>
            </view> -->
            <view v-if="isLink">
                <van-button type="info" block @tap="allowToJoin">进入诊室</van-button>
            </view>
        </view>
        <van-popup class="wc-modal" :show="inviteRelationShow" @close="inviteRelationOnClose" closeable
            close-icon-position="top-right" custom-style="width: 80%; background: transparent;"
            overlay-style="background: rgba(0,0,0,.4)">
            <view class="wc-modal-main"
                :style="{ background: 'url(' + wcModalBg + ') no-repeat', backgroundSize: '100% 100%' }">
                <view class="wc-modal-content">
                    <view class="wc-modal-head">邀请亲属视频陪诊</view>
                    <view class="wc-modal-text">只能邀请1位亲属陪诊</view>
                    <view class="wc-modal-text">请勿将邀请群发给多人</view>
                </view>
                <view class="wc-modal-btn">
                    <button type="default" open-type="share" block>发送邀请</button>
                </view>
            </view>
        </van-popup>
        <van-popup class="wc-permission-modal" :show="!(scopeCamera && scopeRecord)" close-icon-position="top-right"
            custom-style="width: 80%;" overlay-style="background: rgba(0,0,0,.4)">
            <view class="wc-permission-modal-context">
                <view class="wc-permission-modal-head border-bottom-after">视频问诊需要以下权限</view>
                <view class="wc-permission-modal-content">
                    <view class="wc-permission-modal-title"><text class="dian"></text>{{ (!scopeCamera && !scopeRecord)
                        ?
                        "摄像头/麦克风" : (!scopeCamera ? "摄像头" : "麦克风") }}</view>
                    <view class="wc-permission-modal-desc">让医生更好的进行诊疗，满足政策监管要求。</view>
                </view>
                <view class="wc-permission-modal-btn border-top-before">
                    <text @tap="handleGoBack">取消</text>
                    <text class="border-left-before" @tap="handleOpenPermission">确认</text>
                </view>
            </view>
        </van-popup>
    </view>
</template>

<script>
import $http from "@/utils/https";
import Util from '@/utils/utils';
import httpConfig from '@/utils/httpConfig'
import navBar from '@/components/navBar/index'
import VanButton from '@/components/button/index'
import VanPopup from '@/components/popup/index'
import VanCellGroup from '@/components/cell-group/index'
import VanCell from '@/components/cell/index'
const app = getApp()
export default {
    components: {
        'nav-bar': navBar,
        'van-popup': VanPopup,
        'van-button': VanButton,
        'van-cell-group': VanCellGroup,
        'van-cell': VanCell,
    },
    data() {
        return {
            imgHost: httpConfig && httpConfig.imgHost,
            buttonTitle: "等待医生接诊",
            buttonClicked: true,
            inviteRelationShow: false,
            scopeCamera: true,
            scopeRecord: true,
            height: app.globalData.height,
            navbarData: {
                isGoHome: true,
                isGoBack: true,
                title: "候诊室"
            },
            isLink: false,
            wcModalBg: httpConfig.imgHost + "/images/icon/jurisdiction-modal.png",
            externalParams: {},
            visitRelatedInformation: {}
        }
    },
    onLoad: function (options) {
        let isLink = options && options.isLink ? true : false;
        this.setData({
            isLink,
            externalParams: options
        }, () => {
            this.getVisitRelatedInformation();
        })
        this.queryPermissionStatus();
    },
    onShow: function () {
        let pages = getCurrentPages();
        let currentPage = pages[pages.length - 1];
        let homeMsgData = this.$cache.get('homeMsgData');
        let mesIndex = '';
        console.log("homeMsgData", homeMsgData, "currentPage", currentPage)
        homeMsgData && homeMsgData.forEach(function (item, index) {
            if (homeMsgData && (homeMsgData.type === 'E' || !homeMsgData.type) && item.timetamp == currentPage.options) {
                mesIndex = index
            }
        })
        if (mesIndex != '') {
            homeMsgData.remove(mesIndex)
            this.$cache.set('homeMsgData', homeMsgData);
        }
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function (res) {
        let {
            imgHost,
            externalParams,
            visitRelatedInformation
        } = this;
        if (res.from === 'button') {
            // 来自页面内转发按钮
            this.inviteRelationOnClose();
        }
        return {
            title: visitRelatedInformation.patName + '正在看诊，邀请您加入陪诊',
            path: 'pages/index/pages/waitingConsulation/waitingConsulation?isLink=true&admID=' + externalParams.admID,
            imageUrl: imgHost + '/images/icon/share-invitation.jpeg'
        }
    },
    methods: {
        inviteRelation() {
            this.setData({
                inviteRelationShow: true
            })
        },

        inviteRelationOnClose() {
            this.setData({
                inviteRelationShow: false
            })
        },

        // 查询权限状态
        queryPermissionStatus() {
            uni.getSetting({
                success: (res) => {
                    if (!res.authSetting['scope.camera']) { //获取摄像头权限
                        this.scopeCamera = false
                    } else {
                        this.scopeCamera = true
                    }
                    if (!res.authSetting['scope.record']) { //获取录音权限
                        this.scopeRecord = false
                    } else {
                        this.scopeRecord = true
                    }
                },
                fail(res) {
                    console.log(res)
                }
            })
        },

        // 根据就诊ID获取挂号就诊相关信息
        getVisitRelatedInformation() {
            let {
                externalParams,
                isLink
            } = this;
            let data = {
                params: [{
                    admID: externalParams.admID || ''
                }]
            }
            $http.post("urlS", this, {
                code: "03100075",
                data: data,
                namespace: "Y",
                success: function (res) {
                    if (+res.errorCode === 0) {
                        res.result.isTextChat = isLink ? 'yes' : 'no';
                        this.visitRelatedInformation = res.result
                    } else {
                        this.visitRelatedInformation = {}
                    }
                }
            });
        },

        // 检查人员是否允许加入房间
        allowToJoin() {
            let {
                visitRelatedInformation
            } = this;
            let data = {
                params: [{
                    roomID: visitRelatedInformation.roomID || '', // 房间ID
                    userType: "P", // U-医生,P-患者
                    userID: visitRelatedInformation.patID || '', // 人员ID
                    frUser: visitRelatedInformation.frUserID || '', // 
                }]
            }
            $http.post("urlS", this, {
                code: "02090003",
                data: data,
                namespace: "Y",
                success: function (res) {
                    if (+res.errorCode === 0) {
                        let path = '../../../message/pages/im/trtcRoom/trtcRoom';
                        console.log('房间及人员信息')
                        console.log(visitRelatedInformation)
                        uni.navigateTo({
                            url: path,
                            success: (result) => {
                                result.eventChannel.emit('visitRelatedInformation', visitRelatedInformation)
                            },
                        })
                    } else {
                        Util.showToast(res.errorMessage)
                    }
                },
                error: function (error) {
                    Util.showToast(error)
                }
            })
        },

        buttonOnClick: function () {
            this.setData({
                buttonClicked: true,
                buttonTitle: "等待医生接诊"
            })
        },

        // 开启权限
        handleOpenPermission() {
            uni.openSetting({
                success: (res) => {
                    if (!res.authSetting['scope.record']) {
                        uni.authorize({
                            scope: 'scope.record',
                            success: () => {
                                this.scopeRecord = true
                            },
                            fail() {
                                console.log('用户点击取消')
                            }
                        })
                    } else {
                        this.scopeRecord = true
                    }
                    if (!res.authSetting['scope.camera']) {
                        uni.authorize({
                            scope: 'scope.camera',
                            success: () => {
                                this.scopeCamera = true
                            },
                            fail() {
                                console.log('用户点击取消')
                            }
                        })
                    } else {
                        this.scopeCamera = true
                    }
                },
                fail: function () {
                    console.log("授权设置失败");
                }
            })
        },

        // 返回上一页
        handleGoBack() {
            uni.navigateBack({
                delta: 1
            })
        },

        // 返回首页
        handleGoHome() {
            uni.switchTab({
                url: '/pages/tabBar/index/index',
            })
        },

        // 返回我的问诊
        handleGoMyConsultation() {
            uni.navigateTo({
                url: '../../../mine/myConsultation/myConsultation',
            })
        },

        handleRemindDoctors() {
            let {
                externalParams
            } = this;
            let data = {
                params: [{
                    admID: externalParams.admID || '',
                    // roomID: visitRelatedInformation.roomID || '', // 房间ID
                    // userID: visitRelatedInformation.patID || '', // 人员ID
                    // frUser: visitRelatedInformation.frUserID || '', // 
                }]
            }
            $http.post("urlS", this, {
                code: "04180012",
                data: data,
                namespace: "Y",
                success: function (res) {
                    if (+res.errorCode === 0) {
                        Util.showToast('提醒成功')
                    } else {
                        Util.showToast(res.errorMessage)
                    }
                },
                error: function (error) {
                    Util.showToast(error)
                }
            })
        }
    }
}
</script>

<style>
.wc-content {
    margin-bottom: 56rpx;
}

.wc-content .van-cell .van-cell__title,
.wc-content .van-cell .van-cell__value {
    color: #666;
    font-size: 30rpx;
}

.wc-content .van-cell .van-cell__value {
    flex: 2;
}

.wc-content .van-cell .van-cell__value.status {
    color: #1890FF;
}

.wc-btn {
    padding: 0 24rpx;
}

.wc-btn .van-button {
    height: 100rpx;
    line-height: 100rpx;
    border-radius: 10rpx;
    font-size: 32rpx;
}

.wc-btn .van-button.van-button--default {
    color: #1890FF;
}

.wc-permission {
    margin-top: 12rpx;
    background: #0099ff;
    color: #fff;
    width: 100%;
    padding: 32rpx 80rpx;
    box-sizing: border-box;
}

.wc-permission-title {
    width: 100%;
    text-align: center;
    font-size: 36rpx;
    line-height: 72rpx
}

.wc-permission-content {
    font-size: 32rpx;
    line-height: 48rpx;
}

.wc-permission-desc {
    font-size: 24rpx;
    line-height: 48rpx;
}

.wc-modal .wc-modal-main {
    width: 100%;
    height: 657rpx;
    position: relative;
    background: #fff;
}

.wc-modal .wc-modal-content {
    width: 100%;
    padding: 50rpx 70rpx;
    color: white;
    box-sizing: border-box;
    text-align: center;
}

.wc-modal .wc-modal-head {
    font-size: 40rpx;
    margin-bottom: 24rpx;
    font-weight: 600;
}

.wc-modal .wc-modal-text {
    font-size: 28rpx;
    line-height: 48rpx
}

.wc-modal .van-icon {
    color: #fff;
    font-size: 32rpx;
}

.wc-modal-btn {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 120rpx;
}

.wc-modal-btn .van-button {
    height: 100%;
    line-height: 100%;
    color: #3478F7;
}

/* 权限弹窗 */
.wc-permission-modal-head {
    height: 110rpx;
    line-height: 110rpx;
    width: 100%;
    text-align: center;
    font-size: 36rpx;
    color: #333;
    border-radius: 10rpx 10rpx 0 0;
}

.wc-permission-modal-btn {
    height: 120rpx;
    width: 100%;
    display: flex;
    justify-items: center;
    border-radius: 0 0 10rpx 10rpx;
}

.wc-permission-modal-content {
    font-size: 36rpx;
    color: #333;
    width: 100%;
    padding: 31rpx;
    padding-right: 90rpx;
    box-sizing: border-box;
    background: #fff;
}

.wc-permission-modal-title {
    display: flex;
    align-items: center;
    margin-bottom: 24rpx;
}

.wc-permission-modal-title .dian {
    display: inline-block;
    width: 10rpx;
    height: 10rpx;
    background: #666;
    border-radius: 50%;
    margin-right: 24rpx;
}

.wc-permission-modal-desc {
    color: #666;
    font-size: 28rpx;
}

.wc-permission-modal-btn text {
    font-size: 36rpx;
    flex: 1;
    height: 100%;
    display: flex;
    color: #666;
    justify-content: center;
    align-items: center;
}

.wc-permission-modal-btn text:nth-child(2) {
    color: #1890FF;
}
</style>