<template>
    <view>
        <!-- pages/tabBar/personalcenter/locList/locList.wxml -->
        <nav-bar :navbarData="navbarData"></nav-bar>
        <view class="locList" :style="'margin-top: ' + (height * 2 + 10) + 'rpx; height: calc(100vh - ' + (height * 2 + 10) + 'rpx)'">
            <view class="row locItem" :data-item="item" @tap="changeLoc" v-for="(item, index) in locList" :key="index">
                <view class="col-11">
                    <view class="locInfo">{{ item.logonLocDesc }}</view>
                    <view class="row locInfo">
                        <view class="hospdesc">{{ item.logonHospDesc }}</view>
                        <view class="hospdesc groupdesc">{{ item.logonGroupDesc }}</view>
                    </view>
                </view>

                <view class="col-1" v-if="curLoc == index">
                    <mp-icon type="outline" icon="done" color="#48D1CC" :size="30"></mp-icon>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
import Cache from '../../../../utils/cache';
import navBar from '@/pages/components/navbar/index';
// pages/tabBar/personalcenter/locList/locList.js
import { $http } from '../../../../config/https.js';
const app = getApp();
export default {
    components: {
        navBar
    },
    data() {
        return {
            locList: [],
            curLoc: '',
            navbarData: {
                background: '',
                title: '科室角色切换',
                //导航栏 中间的标题
                height: '100',
                bottom: true,
                isGoBack: true,
                cleanPat: 'Y'
            },
            height: app.globalData.height
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getLocList();
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        getLocList: function () {
            let that = this;
            let userData = Cache.get('userData');
            let data = {
                params: [
                    {
                        currentLocFlag: 'Y',
                        language: 'CN',
                        userCode: userData.userCode
                    }
                ]
            };
            $http.post('urlDeault', that, {
                code: '01040043',
                data: data,
                success: function (res) {
                    let errorCode = res.errorCode;
                    if (errorCode == '0') {
                        let currentIndex = -1;
                        let logonLocAry = res.result.logonLocAry;
                        for (var i = 0; i < logonLocAry.length; i++) {
                            let locInfo = logonLocAry[i];
                            if (locInfo.logonLocID == userData.locID && locInfo.logonHospID == userData.hospID && locInfo.logonGroupID == userData.groupID) {
                                currentIndex = i;
                            }
                        }
                        this.setData({
                            locList: logonLocAry,
                            curLoc: currentIndex
                        });
                    }
                }
            });
        },

        /**
         * 切换科室
         */
        changeLoc: function (event) {
            let dataset = event.currentTarget.dataset;
            let userData = Cache.get('userData');
            userData.locID = dataset.item.logonLocID;
            userData.locDesc = dataset.item.logonLocDesc;
            userData.groupID = dataset.item.logonGroupID;
            userData.groupDesc = dataset.item.logonGroupDesc;
            userData.hospID = dataset.item.logonHospID;
            userData.hospDesc = dataset.item.logonHospDesc;
            userData.hospCode = dataset.item.logonHospCode;
            Cache.set('userData', userData);
            let currentIndex = this.curLoc;
            for (var i = 0; i < this.locList.length; i++) {
                let locInfo = this.locList[i];
                if (locInfo.logonLocID == userData.locID && locInfo.logonHospID == userData.hospID && locInfo.logonGroupID == userData.groupID) {
                    currentIndex = i;
                }
            }
            this.setData({
                curLoc: currentIndex
            });
        }
    }
};
</script>
<style>
/* pages/tabBar/personalcenter/locList/locList.wxss */
page {
    background: #fff;
}
.locList {
    box-sizing: border-box;
    height: 100%;
    width: 100%;
    overflow-y: auto;
    padding-bottom: env(safe-area-inset-bottom);
}

.locItem {
    height: 80rpx;
    padding: 10rpx 30rpx;
    border-bottom: 2rpx solid #f2f2f2;
}

.locInfo {
    height: 40rpx;
    line-height: 40rpx;
    font-size: 32rpx;
}

.hospdesc {
    float: left;
    font-size: 24rpx;
    color: #a1a1a2;
}

.groupdesc {
    padding-left: 20rpx;
}
</style>
