<template>
	<view style="">
		<view class="userInfo" style="padding: 0 13px">
			<u-row customStyle="padding-bottom: 5px">
				<u-col span="4">
					<view style="text-align: center;font-size: 14px; margin: 0 0 0 10px;border-radius: 10px;">
						<u-avatar shape="square" size="70" :showLoading="true" :src="baseURL+userInfo.avatar"
							width="70px" height="70px" lazyLoad="false"></u-avatar>
						<!-- <image class="avatar" src=""> -->
					</view>
				</u-col>
				<u-col span="5">
					<view>
						<u-col>
							<view style="font: 25px Verdana;">
								{{userInfo.nickName}}
							</view>
						</u-col>
					</view>
				</u-col>
			</u-row>
			<u-row style="font-size: 12px;padding-top: 5px;">
				<u-col span="4">
					<view v-if="type !== 0" style="padding-left: 20px;">
						<span v-if="userInfo.userType === null">
						</span>
						<span v-if="userInfo.userType === '1'">事业编群
						</span>
						<span v-if="userInfo.userType === '2'">非事业编群
						</span>
						<span v-if="userInfo.userType === '3'">第三方外包
						</span>
						<span v-if="userInfo.userType === '4'">护工群
						</span>
					</view>
				</u-col>
				<u-col span="4">
					<view>
						<span>{{"至："+userInfo.effectiveDate}}</span>
					</view>
				</u-col>
				<u-col span="4">
					<view style="text-align: center;">
						<span>
							{{userInfo.userName}}
						</span>
					</view>
				</u-col>
			</u-row>
		</view>
		<view class="gongNengList">
			<u-cell-group>
				<view>
					<!-- style="border: 10rpx #f5f5f5 solid;" -->
					<picker @change="bindPickerChange" :value="index" :range="columns[0]">
						<view
							style="
							padding: 5rpx 0;
							margin-bottom: 20rpx;
		border-bottom-left-radius: 150rpx;
		border-bottom-right-radius: 150rpx;
		background-color: rgb(55,186,189);color:#FFFFFF;text-align: center;font-size: 30rpx;letter-spacing:20rpx;font-weight: bold;">
							<view style="margin: 0 auto;">
								<u--text bold :iconStyle="{color: 'rgb(255, 255, 255)',fontSize:'40rpx'}"
									suffixIcon="arrow-down" :text="orgName" color="rgb(255, 255, 255)" size="18">
								</u--text>
							</view>
						</view>
					</picker>
				</view>
				<u-cell icon="account" title="个人档案" @click="toProFilePage" :isLink="true"></u-cell>
				<u-gap height="10" bgColor="#f5f5f5"></u-gap>
				<view v-if="type === 2">
					<u-cell icon="calendar" title="会积分" :isLink="true" @click="toHZLPage">
					</u-cell>
					<u-cell icon="rmb-circle" title="积分余额" value="">
						<u--text slot="right-icon" :text="acTitle" color="rgb(55,186,189)"></u--text>
						<u--text slot="right-icon" :text="amassScore" color="rgb(55,186,189)"></u--text>
					</u-cell>
					<u-cell icon="calendar" title="积分订单" @click="toExpenseCalendarPage2" :isLink="true">
						<u--text v-if="isNews !== '0'" slot="right-icon" text="新消息" color="rgb(55,186,189)"></u--text>
					</u-cell>
				</view>
				<view v-if="type !== 4">
					<u-cell icon="rmb-circle" title="积分余额" value="">
						<!-- <u-badge count="99" :absolute="false" slot="right-icon"></u-badge> -->
						<u--text slot="right-icon" :text="acTitle" color="rgb(55,186,189)"></u--text>
						<u--text slot="right-icon" :text="userInfo.integral" color="rgb(55,186,189)"></u--text>
					</u-cell>
					<u-cell icon="rmb-circle" title="记账额度" value="">
						<u--text slot="right-icon" :text="acTitle" color="rgb(55,186,189)"></u--text>
						<u--text slot="right-icon" :text="userInfo.quota" color="rgb(55,186,189)"></u--text>
					</u-cell>
					<u-cell icon="rmb-circle" title="押金" value="">
						<u--text slot="right-icon" :text="acTitle" color="rgb(55,186,189)"></u--text>
						<u--text slot="right-icon" :text="userInfo.deposit" color="rgb(55,186,189)"></u--text>
					</u-cell>
					<u-gap height="10" bgColor="#f5f5f5"></u-gap>
					<u-cell icon="calendar" title="消费记录" @click="toExpenseCalendarPage" :isLink="true">
					</u-cell>
					<u-cell icon="calendar" title="兑换记录" @click="toExpenseCalendarPageDh" :isLink="true">
					</u-cell>
					<u-cell icon="attach" title="绑定卡片" :isLink="true" @click="toCardPage" :value="isCardBindingText">
					</u-cell>
					<u-cell icon="clock" title="充值历史记录" @click="toRechargeHistoryPage" :isLink="true">
					</u-cell>
				</view>

				<u-gap height="10" bgColor="#f5f5f5"></u-gap>
				<!-- <u-cell icon="bell" title="消息提醒" @click="toNotificationPage" :isLink="true"></u-cell> -->
				<u-cell icon="setting" title="系统设置" :isLink="true" @click="toSetting"></u-cell>
				<u-cell icon="reload" title="退出登录" @click="isOutLoginShow" :isLink="true"></u-cell>
				<view style="background-color: #f5f5f5;height: 30px;">
					<view style="text-align: center;width: 100%;padding: 0;font-size: 12px;color: #dedede;">
						<text style="margin: 5px;">会山科技 | hzl@willalp.com</text>
					</view>
				</view>
			</u-cell-group>
		</view>
		<u-modal :show="modalContent1.show" :title="modalContent1.title" :showCancelButton="true" confirmColor="#53a591"
			@confirm="outLogin" @cancel="isOutLoginShow">
			{{modalContent1.content}}
		</u-modal>
		<!-- <wp-tabbar pagePath="/pages/user/index" :type="type"></wp-tabbar> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				amassScore: 0,
				index: 0,
				orgName: '',
				baseURL: this.$BASE_URL,
				orglistShow: false,
				avathrSrc: 'http://114.115.142.166/static/img/willalp.c4ec824a.jpg',
				//avathrSrc: 'D:/hs/WQX_HS/wqx_uniapp_demo/hs_uView_demo/static/uview/willalpImg/750x560-.png',
				acTitle: '',
				userInfo: {
					avatar: '',
					nickName: '',
					userName: '',
					userType: '',
					organizationCode: '',
					integral: 0.0,
					date: Number(new Date()),
				},
				modalContent1: {
					show: false,
					title: '提示',
					content: '确认退出登录吗？'
				},
				isCardBindingText: '未绑定',
				from: {

				},
				show: false,
				columns: [],
				orgInfo: {
					organizationName: '',
					organizationCode: '',
				},
				data: {

				},
				type: undefined,
				isNews: '0'
			}
		},
		mounted() {
			this.userInfo = null
			this.isGetUserInfo()
			this.getAppUserInfo()
			uni.setStorageSync('isTabLogin', 1)
			this.type = uni.getStorageSync("tabType")
			this.userInfo = null
			this.isGetUserInfo()
			this.getAppUserInfo()
		},
		onShow() {
			uni.reLaunch({
				url: '/pages/tabbar/wpTabbar'
			})
			this.type = uni.getStorageSync("tabType")
			this.userInfo = null
			this.isGetUserInfo()
			this.getAppUserInfo()
		},
		onPullDownRefresh() {
			this.getAppUserInfo()
			this.isGetUserInfo()
		},
		onHide() {

		},
		methods: {
			bindPickerChange: function(e) {
				this.index = parseInt(e.detail.value)
				this.subOrgLogin()
				this.show = false;
				uni.setStorageSync("isRefresh", 1)
			},
			async subOrgLogin(orgCode) {
				let form = uni.getStorageSync("loginForm")
				form.orgCode = this.columns[1][this.index].organizationCode
				uni.showLoading({
					mask: true,
					title: "正在切换"
				})
				// 发送请求
				const {
					data: res
				} = await this.$httpRequest({
					url: '/api/subOrgLogin',
					method: 'post',
					urlType: 1,
					data: form,
					isNotToken: true
				});
				if (res.code === 200) {
					uni.setStorageSync('refreshStart', 0)
					uni.setStorageSync('isTabLogin', 0)
					this.orgInfo = this.columns[1][this.index]
					uni.setStorageSync('orgInfo', this.orgInfo);
					this.orgName = this.columns[0][this.index]
					let port
					if (this.orgInfo.organizationCode === 'G00000003') {
						uni.setStorageSync("tabType", 2)
						this.type = 2
						port = 3010
					} else if (this.orgInfo.organizationCode === 'G00000007') {
						uni.setStorageSync("tabType", 3)
						this.type = 3
						port = 3005
					} else if (this.orgInfo.organizationCode === 'G00000005') {
						uni.setStorageSync("tabType", 4)
						this.type = 4
						port = 3012
					}
					//写入 【webSocket】 地址
					let ws = this.$BASE_SOCKET_URL + ':' + port
					uni.setStorageSync("wsServerUrl", ws)
					this.$wsConnection.close()
					this.$wsConnection.initRequest()
					const data = res.loginUser.user
					uni.setStorageSync('setUserName', data.userName);
					uni.setStorageSync('token', res.token);
					uni.setStorageSync('QRcodeToken', res.loginUser.token);
					uni.setStorageSync('refreshStart', 1)
					uni.setStorageSync('isTabLogin', 1)
					this.$emit('setType', this.type)
					this.getAppUserInfo()
					this.isGetUserInfo()
					this.$getAllConfigData()
					this.$getAllDicts()
					uni.setNavigationBarTitle({
						title: uni.getStorageSync("orgInfo").organizationName
					})
					setTimeout(() => {
						uni.hideLoading()
					}, 1200)

				} else {
					uni.setStorageSync('refreshStart', 0)
					uni.setStorageSync('isTabLogin', 0)
					setTimeout(() => {
						uni.hideLoading()
					}, 1200)
				}
			},
			async getOrderInfo() {
				const {
					data: res
				} = await this.$httpRequest({
					url: '/app/orderIsNews',
					method: 'get',
				})
				if (res.code == 200) {
					this.isNews = res.msg
				} else {}
			},
			async getDataAmassScore(data) {
				const {
					data: res
				} = await this.$httpRequest({
					url: '/app/ac?uc=' + uni.getStorageSync("setUserName"),
					method: 'get',
					urlType: this.$getUrlType()
				});
				if (res.code === 200) {
					this.amassScore = res.data
				} else {
					uni.showToast({
						duration: 2500,
						title: res.msg,
						icon: 'error',
					});
				}
			},
			async getWillalpAc(data) {
				const {
					data: res
				} = await this.$httpRequest({
					url: '/app/ac?uc=' + uni.getStorageSync("setUserName"),
					method: 'get',
					urlType: 2
				});
				if (res.code === 200) {
					this.$showModal("发放成功")
				} else {
					uni.showToast({
						duration: 2500,
						title: res.msg,
						icon: 'error',
					});
				}
			},
			toHZLPage() {
				this.$goto('../../pageA/card/index')
			},
			takeMeal() {
				var that = this;
				uni.scanCode({
					scanType: ['qrCode'],
					success: function(res) {
						// uni.showModal({
						// 	content: JSON.stringify(res),
						// })
						uni.setStorageSync("rfiCard", res.result)
						that.$goto('../../pageA/card/activetion');
					},
					fail: function(a) {},
					complete: function(b) {}
				})
			},

			//uniPush 2.0
			getPushCid() {
				if (undefined !== plus) {
					let pinf = plus.push.getClientInfo();
					//客户端标识
					//let cid = pinf && pinf.clientid || '';
					const userInfo = uni.getStorageSync('userInfo');
					const orgInfo = uni.getStorageSync('orgInfo');
					this.data.userNo = userInfo.userName
					this.data.userName = userInfo.nickName
					this.data.orgNo = orgInfo.organizationCode
					this.data.orgName = orgInfo.organizationName
					this.data.pushCid = cid //'b588d2a110c79b7591de409c17719ef2'  
					this.savePushCid(this.data)
				}

			},
			async savePushCid(data) {
				const {
					data: res
				} = await this.$httpRequest({
					url: '/api/push/save/cid',
					method: 'post',
					data: data
				})
				if (res.code == 200) {
					console.log("update - push - cid")
				} else {}
			},
			async getAppUserInfo() {
				this.acTitle = this.orgInfo.organizationName.substring(0, 1) + "："
				this.from.userName = uni.getStorageSync('setUserName');
				this.from.organizationCode = uni.getStorageSync("orgInfo").organizationCode;
				// 发送请求
				const {
					data: res
				} = await this.$httpRequest({
					url: '/app/getUser',
					data: this.from,
					urlType: this.$getUrlType()
				});
				if (res.code === 200) {
					const data = res.data
					uni.setStorageSync("userInfo", data)
					this.userInfo = uni.getStorageSync("userInfo")
					this.getDataAmassScore()
				}
				uni.stopPullDownRefresh()
			},
			confirm(e) {
				this.orgInfo = e.value[0];
				let port
				if (this.orgInfo.organizationCode === 'G00000003') {
					uni.setStorageSync("tabType", 2)
					port = 3010
				} else if (this.orgInfo.organizationCode === 'G00000007') {
					uni.setStorageSync("tabType", 3)
					port = 3005
				} else if (this.orgInfo.organizationCode === 'G00000005') {
					uni.setStorageSync("tabType", 4)
					port = 3012
				}
				//写入 【webSocket】 地址
				let ws = this.$BASE_SOCKET_URL + ':' + port
				uni.setStorageSync("wsServerUrl", ws)
				this.$wsConnection.initRequest()
				uni.setStorageSync("orgInfo", this.orgInfo);
				uni.setStorageSync("isRefresh", 1);
				this.getAppUserInfo()
				this.isGetUserInfo()
				this.show = false;
			},
			cancel() {
				this.show = false
			},
			isOutLoginShow() {
				if (this.modalContent1.show) {
					this.modalContent1.show = false
				} else {
					this.modalContent1.show = true
				}
			},
			isShowOrgInfo() {
				if (0 === this.columns[0].length) {
					uni.showModal({
						title: '提示',
						content: '请联系机构管理员激活账户',
						showCancel: false
					});
					return;
				}
				this.show = true
			},
			isGetUserInfo() {
				const value = uni.getStorageSync('userInfo');
				if (value) {
					//有登录信息
					this.userInfo = value
					const {
						userName,
						cardNumber
					} = this.userInfo
					this.from.userName = userName
					this.isCardBindingText = cardNumber ? '已绑定' : '未绑定'
					//console.log("已登录用户：", value);
					const orgInfo = uni.getStorageSync("orgInfo");
					if (orgInfo) {
						let orgList = uni.getStorageSync("orglist")
						this.columns = [
							[],
							[]
						]
						orgList[0].forEach(item => {
							this.columns[0].push(item.organizationName)
						})
						this.columns[1] = orgList[0]
						this.orgName = uni.getStorageSync("orgInfo").organizationName
						this.index = uni.getStorageSync("tabType")
						//获取提交的机构信息
						const {
							organizationName,
							organizationCode
						} = orgInfo
						this.orgInfo.organizationName = organizationName
					} else {
						uni.showToast({
							icon: 'none',
							title: '未选择机构',
							duration: 2000
						});
						uni.redirectTo({
							url: '../../pageA/login/selectOrg'
						})
					}
				} else {
					uni.showToast({
						icon: 'none',
						title: '用户信息过期请重新登录',
						duration: 2000
					});
					uni.redirectTo({
						url: '../../pageA/login/login'
					});
				}
			},
			async getNotRead() {
				const {
					data: res
				} = await this.$httpRequest({
					url: '/api/yq/notRead/?userNo=' + uni.getStorageSync("setUserName") +
						"&orgNo=" + uni.getStorageSync("orgInfo").organizationCode,
					method: 'get',
				})
				if (res.code == 200) {
					var num = res.data
					if (0 < num) {
						uni.setTabBarBadge({
							index: 1,
							text: num + ""
						})
					}
				}
			},
			toSetting() {
				uni.navigateTo({
					url: "../../pages/user/settings/settings"
				})
			},
			//退出登录
			outLogin() {
				//清空所有本地缓存
				uni.clearStorageSync()
				uni.reLaunch({
					url: '../../pageA/login/login'
				})
			},
			toProFilePage() {
				uni.navigateTo({
					url: "../../pages/user/profile"
				})
			},
			toExpenseCalendarPage2() {
				uni.navigateTo({
					url: "../../pageA/card/expense/expenseCalendar"
				})
			},
			toExpenseCalendarPage() {
				uni.navigateTo({
					url: "../../pages/user/expense/expenseCalendar"
				})
			},
			toExpenseCalendarPageDh() {
				uni.navigateTo({
					url: "../../pageA/integral/expenseCalendar"
				})
			},
			toRechargeHistoryPage() {
				uni.navigateTo({
					url: "../../pages/user/rechargeHistory"
				})
			},
			toCardPage() {
				uni.navigateTo({
					url: "../../pages/user/card"
				})
			},
			toNotificationPage() {
				uni.navigateTo({
					url: "../../pages/user/notification"
				})
			},
			toControlPage() {
				uni.navigateTo({
					url: "../control/index"
				})
			}
		}
	}
</script>
<style>
	.userInfo {
		box-shadow: 0px 2px 2px #9e9e9e;
		margin-top: 10rpx;
	}

	.avatar {
		margin: 10px;
		width: 70px;
		height: 70px;
		border-radius: 50px;
	}

	.gongNengList {
		border-radius: 10px;
	}

	.uni-fixed-bottom {
		width: 100%;
		bottom: 0upx;
		padding: 0upx;
		position: fixed;
		background: #FFFFFF;
		transform: translateZ(0);
		bottom: env(safe-area-inset-bottom);
		bottom: constant(safe-area-inset-bottom);
	}
</style>
