<template>
    <view>
        <!-- home.wxml -->
        <nav-bar :navbarData="navbarData"></nav-bar>
        <view class="container" :style="'margin-top: ' + (height * 2 + 10) + 'rpx; min-height: calc(100vh - ' + (height * 2 + 10) + 'rpx)'">
            <view class="workbench-top" :style="'background-image: url(' + imgDomain + '/images/icon/doctor/workback.png);'">
                <view class="greeting">
                    <text class="datetime">{{ dateStr }}</text>
                    <text class="regard">{{ userData.userName }}，您好！</text>
                </view>
                <view class="homeContent">
                    <view class="moudle" @tap="tapName" data-url="../../myConsultRoom/page/graphic/graphic">
                        <view class="module-icon">
                            <image class="myloc" :src="imgDomain + '/images/icon/doctor/graphicConsultation.png'"></image>
                        </view>
                        <view class="module-main">
                            <text class="title">图文咨询</text>
                            <text class="sub-title">
                                今日待诊：
                                <text style="color: #2294ff">{{ num1 }}</text>
                                人
                            </text>
                            <text class="note">{{ imageText }}</text>
                        </view>
                        <!-- <view class="col-1">
          <mp-badge wx:if="{{num1!='0'}}" content="{{num1}}" style="margin-left: 5px;" />
        </view> -->
                        <view class="module-arrow">
                            <view class="module-arrow-icon"></view>
                        </view>
                    </view>
                    <view class="moudle" @tap="tapName" data-url="../../myConsultRoom/page/MyLoc/MyLoc">
                        <view class="module-icon">
                            <image class="myloc" :src="imgDomain + '/images/icon/doctor/myLoc.png'"></image>
                        </view>
                        <view class="module-main">
                            <text class="title">我的诊室</text>
                            <text class="sub-title">
                                今日待诊：
                                <text style="color: #2294ff">{{ num2 }}</text>
                                人
                            </text>
                            <text class="note">{{ videoText }}</text>
                        </view>
                        <view class="module-arrow">
                            <view class="module-arrow-icon"></view>
                        </view>
                    </view>
                </view>
            </view>
            <view class="msgTips">未读消息</view>
            <view class="home">
                <messageCenter statusCode="U" :messageTypeShow="false" />
            </view>
        </view>
    </view>
</template>

<script>
import Cache from '../../../utils/cache';
import notice from '@/pages/components/notice/notice';
import messageCenter from '@/pages/components/messageCenter/messageCenter';
import navBar from '@/pages/components/navbar/index';
//home.js
//获取应用实例
const app = getApp();
import { $http } from '../../../config/https';
import { httpconfig } from '../../../config/httpconfig';
import messageUtil from '../../../utils/message.js';
import Util from '../../../utils/util.js';
export default {
    components: {
        notice,
        messageCenter,
        navBar
    },
    data() {
        return {
            imgDomain: httpconfig.imgDomain,
            dateStr: '',

            userData: {
                userName: ''
            },

            num1: 0,
            num2: 0,
            imageText: '',
            videoText: '',

            navbarData: {
                background: '#2B84FF',
                title: '工作台',
                //导航栏 中间的标题
                height: '100',
                bottom: false,
                cleanPat: 'Y'
            },

            height: app.globalData.height,
            ip: '',
            loginStaus: ''
        };
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        console.log('App onLoad');
        this.getLoginInfo();
    },
    onReady: function () {
        // 页面渲染完成
        console.log('App onReady');
    },
    onShow: function () {
        this.onShowClone3389();
    },
    onHide: function () {
        // 页面隐藏
        console.log('App onHide');
    },
    onUnload: function () {
        // 页面关闭
        console.log('App onUnload');
    },
    onTabItemTap: function (e) {
        let userData = Cache.get('userData');
        if (!userData || !userData.userID) {
            uni.showToast({
                title: '请先登录',
                icon: 'none'
            });
            setTimeout(() => {
                uni.switchTab({
                    url: '/pages/tabBar/login/login'
                });
            }, 1500);
        }
    },
    methods: {
        onShowClone3389: function () {
            // 页面显示
            console.log('App onShow');
            const userData = Cache.get('userData');
            if (userData && userData.userID) {
                this.setData({
                    userData
                });
                this.showTime();
                this.getNumber();
            }
        },

        tapName: function (event) {
            console.log(event);
            let { currentTarget } = event;
            let dataset = currentTarget.dataset;
            uni.navigateTo({
                //目的页面地址
                url: dataset.url,
                success: function (res) {},
                fail: function (res) {
                    console.log(res);
                }
            });
        },

        showTime: function () {
            let show_day = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
            let time = new Date();
            let year = time.getFullYear();
            let month = time.getMonth() + 1;
            let date = time.getDate();
            let day = time.getDay();
            let hour = time.getHours();
            let minutes = time.getMinutes();
            let second = time.getSeconds();
            if (month < 10) {
                month = '0' + month;
            } else {
                month;
            }
            if (hour < 10) {
                hour = '0' + hour;
            } else {
                hour;
            }
            minutes < 10 ? (minutes = '0' + minutes) : minutes;
            second < 10 ? (second = '0' + second) : second;
            let now_time = year + '-' + month + '-' + date + ' ' + hour + ':' + minutes + ':' + second + ' ' + show_day[day];
            this.setData({
                dateStr: now_time
            });
        },

        getNumber: function () {
            const userData = Cache.get('userData');
            if (!userData || !userData.userID) {
                return;
            }
            $http.post('urlDeault', this, {
                code: '03100073',
                data: {
                    params: [
                        {
                            userID: userData.userID,
                            locID: userData.locID,
                            hospID: userData.hospID
                        }
                    ]
                },
                success: function (res) {
                    let errorCode = res.errorCode;
                    if (errorCode === '0') {
                        this.setData({
                            num1: res.result.imageNum,
                            num2: res.result.videoNum,
                            imageText: res.result.imageText ? res.result.imageText : '暂无患者咨询',
                            videoText: res.result.videoText ? res.result.videoText : '暂无待诊患者'
                        });
                    }
                }
            });
        },

        /**
         * 获取登录信息
         */
        getLoginInfo: function () {
            var that = this;
            uni.request({
                url: httpconfig.domain + '/localIP',
                success: function (e) {
                    that.setData({
                        ip: e.data.ipv4
                    });
                }
            });
            const cacheUserData = Cache.get('userData');
            if (cacheUserData && cacheUserData.userID) {
                this.setData({
                    loginStaus: 'Y'
                });
                let TIMInfo = Cache.get('TIMInfo') || {};
                if (TIMInfo.loginStatus !== 'Y') {
                    messageUtil.initTim();
                }
                this.onShowClone3389({});
                return;
            }
            // #ifdef H5
            this.goLogin();
            return;
            // #endif
            var accountInfo = typeof uni.getAccountInfoSync === 'function' ? uni.getAccountInfoSync() : { miniProgram: { appId: '' } };
            var appID = accountInfo.miniProgram.appId;
            uni.login({
                success(res) {
                    if (res.code) {
                        Util.getOpenID(res.code, (openid) => {
                            // 登录接口
                            let data = {
                                params: [
                                    {
                                        openID: openid,
                                        appID: appID,
                                        status: 'Y'
                                    }
                                ]
                            };
                            $http.post('urlDeault', this, {
                                code: '03100014',
                                data: data,
                                success: function (res) {
                                    if (res.errorCode == '0') {
                                        if (res.result && res.result.userID) {
                                            uni.setStorage({
                                                key: 'userData',
                                                data: res.result,
                                                success: function () {
                                                    let TIMInfo = Cache.get('TIMInfo');
                                                    if (TIMInfo == '' || TIMInfo.loginStatus == 'N') {
                                                        messageUtil.initTim();
                                                    }
                                                    that.onShowClone3389({});
                                                }
                                            });
                                            let background = "url('" + httpconfig.domain + "/images/icon/myhead.png')";
                                            that.setData({
                                                loginStaus: 'Y',
                                                navbarData: {
                                                    background: background,
                                                    title: '我的',
                                                    //导航栏 中间的标题
                                                    height: '100',
                                                    bottom: false
                                                }
                                            });
                                        } else {
                                            that.goLogin();
                                        }
                                    } else {
                                        that.goLogin();
                                    }
                                },
                                fail: function (res) {
                                    that.goLogin();
                                }
                            });
                        });
                    }
                },
                fail: function () {
                    that.goLogin();
                }
            });
        },

        /**
         * 跳转登录
         */
        goLogin: function () {
            uni.showToast({
                title: '请先登录',
                icon: 'none'
            });
            setTimeout(() => {
                uni.switchTab({
                    url: '/pages/tabBar/login/login'
                });
            }, 1500);
        }
    }
};
</script>
<style>
/**home.wxss**/

.container {
    min-height: 100%;
    background-color: #f5f4f4;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

page {
    height: 100%;
}

.home {
    padding: 0 20rpx 20rpx 20rpx;
    width: 100%;
    flex: 1;
    min-height: 360rpx;
    overflow-y: auto;
    box-sizing: border-box;
}

.homeContent {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 18rpx;
    padding: 0 20rpx 24rpx;
    box-sizing: border-box;
}

.homeBox {
    height: 150rpx;
    line-height: 110rpx;
    border: 1rpx solid #eee;
    border-top: none;
    margin-left: -6rpx;
    font-size: 30rpx;
    color: #666;
    position: relative;
}

.homeBox .iconImg {
    line-height: 150rpx;
}

.homeBox_label {
    position: absolute;
    height: 20rpx;
    line-height: 20rpx;
    left: 0;
    top: 80rpx;
    color: #ccc;
    width: 100%;
    text-align: left;
    font-size: 20rpx;
}

.homeBox_label2 {
    top: 110rpx;
    color: #f93;
}

.messageview {
    height: calc(100% - 3px);
    width: 100%;
    padding-top: 3px;
}

.homeMessageTitle {
    padding: 0 20rpx;
    font-size: 25rpx;
    border: 2rpx solid #eee;
    border-bottom: none;
    height: 60rpx;
    line-height: 60rpx;
}

.homeMessageContent {
    background-color: #f5f4f4;
    overflow: scroll;
}

.homeMessageBox {
    height: 200rpx;
    font-size: 30rpx;
    color: #666;
    position: relative;
    background-color: #fff;
    margin: 1px 0 2rpx 20rpx;
    width: 95%;
    border-radius: 10rpx;
}

.homeMessageBox .iconImg {
    line-height: 120rpx;
}

.homeMessageBox_label1 {
    position: absolute;
    left: 0;
    top: 10rpx;
    font-size: 30rpx;
    color: #666;
    text-align: left;
}

.homeMessageBox_label2 {
    top: 40rpx;
    right: 20rpx;
    text-align: right;
    font-size: 20rpx;
    color: #ccc;
}

.homeMessageBox_label3 {
    top: 70rpx;
    font-size: 25rpx;
    color: #f93;
}

.moudle {
    display: flex;
    align-items: center;
    margin: 0;
    background-color: #fff;
    border-radius: 14rpx;
    width: 100%;
    min-height: 132rpx;
    height: auto;
    box-shadow: 0 8rpx 24rpx rgba(25, 82, 150, 0.08);
    border: 1px solid rgba(232, 238, 248, 0.9);
    box-sizing: border-box;
}

.moudle:active {
    background-color: #f7f7f7;
}

cover-image {
    height: 120rpx;
    width: 120rpx;
    margin: 12rpx 0 0 11rpx;
}

.title {
    display: inline-block;
    font-size: 32rpx;
    font-weight: 700;
    color: #1f2937;
    line-height: 42rpx;
}

.note {
    display: block;
    margin-top: 8rpx;
    font-size: 26rpx;
    color: #999;
    position: static;
    height: auto;
    line-height: 34rpx;
    width: 100%;
}

.sub-title {
    display: inline-block;
    font-size: 26rpx;
    color: #999;
    margin-left: 16rpx;
    line-height: 36rpx;
}

.module-icon {
    height: auto;
    flex: 0 0 136rpx;
    display: flex;
    justify-content: center;
    align-items: center;
}

.col-9 {
    margin-top: 15rpx;
}

.greeting {
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 100%;
    min-height: 140rpx;
    height: auto;
    font-size: 1rem;
    margin: 0;
    padding: 18rpx 28rpx 18rpx;
    color: #fff;
}

.datetime {
    font-size: 24rpx;
    line-height: 34rpx;
}

.regard {
    margin-top: 8rpx;
    font-size: 34rpx;
    line-height: 44rpx;
    font-weight: 700;
}

.title-center {
    height: 55rpx;
    margin: 0rpx 0 10rpx 20rpx;
}

.patientInfo1 {
    float: left;
}

.patientInfo2 {
    float: left;
    margin-top: 5rpx;
}

.patientInfo3 {
    background: #ffbe32;
    color: white;
    font-size: 30rpx;
}

.person {
    margin-left: 7px;
}

.message {
    height: 80rpx;
    border-bottom: 1rpx solid #f5f4f4;
    color: #333;
}

.label {
    margin: 10rpx 0 0 20rpx;
}

.weui-badge {
    margin-top: 46rpx;
}

.workbench-top {
    min-height: 438rpx;
    height: auto;
    width: 100%;
    margin-top: -2.0009rpx;
    background-repeat: no-repeat;
    background-size: 100% 220rpx;
    background-color: #2b84ff;
    padding-bottom: 10rpx;
    box-sizing: border-box;
    position: relative;
    z-index: 0;
}

.msgTips {
    width: calc(100% - 80rpx);
    margin: 22rpx 20rpx 16rpx;
    padding: 0 20rpx;
    height: 40rpx;
    line-height: 40rpx;
    font-size: 32rpx;
    font-family: PingFang SC;
    font-weight: bold;
    color: rgba(51, 51, 51, 1);
    border-left: 2px solid #1890ff;
}
.myloc {
    height: 92rpx;
    width: 92rpx;
    padding: 0;
}

.module-main {
    margin-top: 0;
    flex: 1;
    min-width: 0;
}

.module-arrow {
    flex: 0 0 48rpx;
    display: flex;
    align-items: center;
    justify-content: center;
}

.module-arrow-icon {
    width: 16rpx;
    height: 16rpx;
    border-top: 3rpx solid #c9cdd4;
    border-right: 3rpx solid #c9cdd4;
    transform: rotate(45deg);
}
</style>
