<template>
    <view class="userCard-box" >
        <view v-if="currentErpUser" class="index-header-userCard flex" @click="showChange=true">
            <view class="user-avatar" :style="{background:currentErpUser.bgColor}">
				{{currentErpUser.AccountName.substring(0,1)}}
			</view>
			<view>
				<view class="user-name">
					{{currentErpUser.AccountName}}
				</view>
				<view class="user-change-text">
					点击切换学员
				</view>
			</view>
        </view>
		<view v-else class="index-header-userCard flex">
		    <view class="user-avatar" style="background-color: gray;"></view>
			<view>
				<view class="user-change-text">
					点击进行关联账号绑定
				</view>
			</view>
		</view>
		<van-popup
		  :show="showChange"
		  round
		  v-if="currentErpUser"
		  custom-style="height: 650rpx;width:650rpx"
		>
			<view class="user-change-box">
				<view class="title-box">
					<view class="icon">
						<van-icon class-prefix="iconfont" name="icon-qiehuan1"></van-icon>
					</view>
					<view class="title">
						切换学员
					</view>
					<view class="close">
						<van-icon name='cross' @click="closeChangeUserHandler"></van-icon>
					</view>
				</view>
				<scroll-view style="overflow-y: auto; height: 1px; flex:1;margin-top:50rpx">
					<view class="user-item" @click="changeErpUserHandler(x)" v-for="(x,i) in erpUsers">
						<view class="avatar" :style="{background:x.bgColor}">{{x.AccountName.substring(0,1)}}</view>
						<view class="uname">
							{{x.AccountName}}
						</view>
						<view class="check" v-if="x.AccountId==currentErpUser.AccountId">
							<van-icon name="success" />
						</view>
					</view>
				</scroll-view>
			</view>
		</van-popup>
    </view>
  </template>
  <script>
  import { reactive, toRefs, onMounted } from "vue";
  export default {
    components: {},
    setup() {
      let data = reactive({
        colors:['#9ADBC5','#87DDEA','#FE8D6F','#F986AA','#FDC453'],
		currentErpUser:null,
		erpUsers:[],
		showChange:false,
      });
	  
	  data.erpUsers=uni.getStorageSync('ErpStus')
	  if(data.erpUsers && data.erpUsers.length>0){
		  data.currentErpUser=uni.getStorageSync('erpUserInfo')
		  let userInfo=uni.getStorageSync('userInfo')
		  if(!data.currentErpUser){
			data.erpUsers.forEach(x=>{
				if(x.ClassInAccount==userInfo.Account){
					data.currentErpUser=x
					uni.setStorageSync('erpUserInfo',x)
				}
			})
			
		  }
	  }
      let methods = {
		closeChangeUserHandler(){
			console.log('in....')
			data.showChange=false
		},
		changeErpUserHandler(x){
			uni.setStorageSync("erpUserInfo",x)
			let users=uni.getStorageSync('users')
			uni.removeStorageSync('userInfo')
			if(users && users.length>0){
				users.forEach(item=>{
					if(item.Account==x.ClassInAccount){
						uni.setStorageSync('userInfo',item)
					}
				})
			}
			uni.reLaunch({
				url:'/pages/index/index'
			})
		},
        initColor(){
			if(data.erpUsers){
				let i=0
				data.erpUsers.forEach(x=>{
					x.bgColor=data.colors[i]
					i++;
					if(i==data.colors.length){
						i=0
					}
					if(x.AccountId==data.currentErpUser.AccountId){
						data.currentErpUser.bgColor=x.bgColor
					}
				})
			}
		}
      };
  
      onMounted(() => {
        //data.changePasswordShow = props.AccountType
		methods.initColor()
      });
      return {
        ...toRefs(data),
        ...methods,
      };
    },
  };
  </script>
  <style>
    .index-header-userCard text{
        font-size: 26rpx;
        font-weight: bold;
        color: #282828;
        margin-left: 20rpx;
        letter-spacing: 2rpx;
    }
    .index-header-userCard{
        align-items: center;
    }
    .userCard-box{
        padding: 0 30rpx 0 38rpx;
    }
    .userCard-box .user-avatar{
		width:60rpx;
		height: 60rpx;
		border-radius: 60rpx;
		font-size: 38rpx;
		font-weight: 500;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-right: 25rpx;
    }
	.userCard-box .user-name{
		font-size: 28rpx;
		font-weight: bold;
		color: #282828;
	}
	.userCard-box .user-change-text{
		font-size: 18rpx;
		font-weight: 500;
		color: #B8B8B8;
	}
	.userCard-box .user-change-box {
		padding: 50rpx;
		box-sizing: border-box;
		height: 100%;
		display: flex;
		flex-direction: column;
	}
	.userCard-box .user-change-box .title-box {
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	.userCard-box .user-change-box .title-box .icon{
		font-size: 22rpx;
		color:#282828;
		margin-right: 8rpx;
	}
	.userCard-box .user-change-box .title-box .title{
		font-size: 36rpx;
		font-weight: bold;
		color: #000000;
		width: 1px;
		flex:1;
	}
	.userCard-box .user-change-box .title-box .close{
		width: 26rpx;
		height: 26rpx;
		color:#282828;
	}
	.userCard-box .user-item{
		display: flex;
		flex-direction: row;
		align-items: center;
		margin-bottom: 36rpx;
	}
	.userCard-box .user-item .avatar{
		width: 60rpx;
		height: 60rpx;
		background: #9ADBC5;
		border-radius: 50%;
		
		font-size: 38rpx;
		font-weight: 500;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.userCard-box .user-item .uname{
		font-size: 28rpx;
		margin-left: 32rpx;
		font-weight: 500;
		color: #000000;
		width:1px;
		flex: 1;
	}
	.userCard-box .user-item .check{
		color: #000000;
		font-size: 36rpx;
	}
  </style>