<template>
    <nav-bar :navbar-data='navbarData'></nav-bar>
    <view class="page addAVisitor" :style="{ marginTop: height +'px', marginBottom: '20rpx' }">
        <view class="title">就诊人列表</view>
        <view class="content" v-for="(item, index) in visitorData" :key="index" @tap="onClickDetail" :data-id="item.patID">
            <view class="box">
                <image class="image" :src="item.patImg"></image>
                <text class="patname">{{ item.patName }}</text>
                <text class="patcard">{{ item.NO }}</text>
            </view>
            <view class="box">
                <text class="face" v-if="item.faceFlag === 'Yes'">已采集</text>
            </view>
        </view>
        <view class="button">
            <button class="btn-primary" @tap="onClickEvent">添加就诊人</button>
            <!-- <text bindtap="showCamera">拍照测试</text> -->
        </view>
        <!-- <view class="button">
          <van-button type="default">添加社保卡</van-button>
        </view> -->
    </view>
</template>

<script>
import $http from "@/utils/https";
import Util from "@/utils/utils";
import { getHospList, switchHospital, dealSwitchHospital } from '@/utils/common';
import httpConfig from '@/utils/httpConfig';
import NavBar from '@/components/navBar/index'
const app = getApp();
export default {
    components: {
        'nav-bar': NavBar
    },
    data() {
        return {
            imgHost: httpConfig && httpConfig.imgHost,
            height: app.globalData.height,
            navbarData: {
                isGoHome: true,
                homePath: '../../../../pages/tabBar/index/index',
                isGoBack: true,
                title: "就诊人列表"
            },
            visitorData: []
        }
    },
    onLoad: function (options) {
        console.log("触发了onLoad")
        // this.init(options)
    },
    onShow: function (options) {
        this.init(options)
    },

    methods: {
        init(options) {
            if (options && options.q) {
                console.log(decodeURIComponent(options.q))
                let qrUrl = decodeURIComponent(options.q).split('/');
                console.log(qrUrl)
                let paramsObj = Util.decrypt(qrUrl[qrUrl.length - 1]);
                console.log(paramsObj)
                let jsonParse = paramsObj ? JSON.parse(paramsObj) : {};
                console.log(jsonParse)
                let userData = this.$cache.get('userData');
                if (userData && userData.hisHospID) {
                    this.getVisitorDataList();
                } else {
                    // 获取医院数据
                    this.getHospSelectData(jsonParse?.hospCode);
                }
            } else {
                this.getVisitorDataList();
            }
        },

        onClickEvent: function () {
            uni.navigateTo({
                url: '/pages/mine/addCard/visitorEntry/visitorEntry'
            })
        },

        onClickDetail: function (e) {
            let id = e.currentTarget.dataset.id;
            uni.navigateTo({
                url: '/pages/mine/addCard/cardDetail/cardDetail?id=' + id
            })
        },

        /**请求就诊人列表数据
         * **/
        getVisitorDataList: function () {
            const {
                imgHost
            } = this
            let data = {
                params: []
            }
            $http.post("urlS", this, {
                code: "01030001",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        for (let i = 0; i < res.result.rows.length; i++) {
                            if (res.result.rows[i].idCardNo.length == 18) {
                                var str = res.result.rows[i].idCardNo.substr(6, 8);
                                var simCard = res.result.rows[i].idCardNo.replace(str, '****');
                                res.result.rows[i].NO = simCard;
                            }
                            if (res.result.rows[i].patSexDesc === '男') {
                                res.result.rows[i].patImg = imgHost + "/images/icon/pat-male.png"
                            } else if (res.result.rows[i].patSexDesc === '女') {
                                res.result.rows[i].patImg = imgHost + "/images/icon/pat-female.png"
                            } else {
                                res.result.rows[i].patImg = imgHost + "/images/icon/pat-unknown.png"
                            }
                        }
                        this.setData({
                            visitorData: res.result.rows
                        });
                    } else {
                        this.setData({
                            visitorData: []
                        });
                    }
                }
            });
        },

        // 通过身份证号获取年龄大小
        calculateAge: function (certificateNum) {
            if (certificateNum) {
                var myDate = new Date();
                var month = myDate.getMonth() + 1;
                var day = myDate.getDate();
                var age = myDate.getFullYear() - certificateNum.substring(6, 10) - 1;
                if (certificateNum.substring(10, 12) < month || certificateNum.substring(10, 12) == month && certificateNum.substring(12, 14) <= day) {
                    age++;
                }
                this.setData({
                    ageNum: age
                })
            }
        },

        // 获取医院数据
        getHospSelectData: async function (hospCode) {
			const _this = this
            let userData = _this.$cache.get('userData');
            if (!(userData && userData.openID)) {
                let count = _this.count ? _this.count : 1;
                if (count > 10) {
                    return;
                }
                setTimeout(() => {
                    _this.getHospSelectData(hospCode);
                }, 300)
                _this.count++
                return;
            }
			await getHospList(app, hospCode).then(res => {
				setTimeout(() => {
				    _this.getVisitorDataList();
				}, 300)
			})
        },
    }
}
</script>

<style lang="scss">
.box-sizing {
    box-sizing: border-box;
}

.addAVisitor {
    height: 100%;
    padding: 0 24rpx;
    @extend .box-sizing;

    .title {
        height: 90rpx;
        line-height: 90rpx;
        font-size: 32rpx;
        color: #333;
        padding-left: 20rpx;
        @extend .box-sizing;
    }

    image {
        height: 94rpx;
        width: 94rpx;
        margin-right: 16rpx;
    }

    .content {
        width: 100%;
        background-color: #fff;
        margin-bottom: 20rpx;
        height: 148rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-left: 24rpx;
        font-size: 32rpx;
        border-radius: 10rpx;

        @extend .box-sizing;
    }

    .button {
        width: 100%;
        text-align: center;
        margin-top: 64rpx;

        .van-button {
            width: 100%;
            border-radius: 10rpx;
            height: 100rpx;
            font-size: 32rpx;
        }
    }

    .patname {
        color: #313131;
        margin-right: 16rpx;
    }

    .patcard {
        color: #999;
    }

    .face {
        color: greenyellow;
        border: 2px solid greenyellow;
        padding: 8rpx;
        margin: 0 16rpx;
    }
}


/* icon男士 */
.icon-boy {
    color: #2DB7F5;
}

.icon-girl {
    color: #FFCCC7;
}

/* icon为儿童 */
.icon-child {
    color: #52C41A;
}

.box {
    display: flex;
    align-items: center;
}
</style>