<!-- 会员/商户我的 -->

<template>
	<view>
		<!-- 已登录成功展示头像昵称钱包等 -->
		<view v-if="user_info.id" class="top_bg">
			<!-- 头像昵称等 -->
			<view class="main_width my_top_box">
				<view style="display: flex;align-items: center;">
					<!-- 头像 -->
					<view @click="to_per_info()" style="width: 124upx;height: 124upx;">
						<view style="border-radius: 62upx;width: 124upx;height: 124upx;overflow: hidden;">
							<image style="width: 124upx;height: 124upx;"
								:src="user_info.profile_photo==''?'../../static/user_w.png': (user_info.profile_photo.indexOf('http://')==0||user_info.profile_photo.indexOf('https://')==0)?user_info.profile_photo:(serverUrl+ user_info.profile_photo)">
						</view>
					</view>
					<!-- 非商家昵称年龄性别 -->
					<view v-if="sys_role==0" style="margin-left: 30upx;">
						<view style="font-size: 36upx;margin-left: 4upx;">
							<view class="start_box" @click="to_per_info()">
								{{user_info.nickname?user_info.nickname:'会员昵称'}}
								<view v-if="!loading" @click="to_appraisal(0)" style="margin-left: 10upx;">
									<trailerStars allowTap="0" :innerScore="user_info.worker_appraisal_avg">
									</trailerStars>
								</view>
							</view>
						</view>
						<view style="margin-top: 16upx;display: flex;align-items: center;color: #9B9B9B;">
							{{user_info.age||'--'}}岁
							<image style="width: 34upx;height: 34upx;"
								:src="user_info.gender=='男' ? '../../static/male.png': '../../static/female.png'"
								v-if="user_info.gender"></image>
							{{user_info.edulevel_name}}
						</view>
					</view>
					<!-- 商家昵称-->
					<view v-if="sys_role==1" style="margin-left: 30upx;">
						<view style="font-size: 36upx;margin-left: 4upx;">
							<view style="max-width: 360upx;" class="start_box" @click="to_per_info()">
								{{user_info.company_name?user_info.company_name:(user_info.linker_name?user_info.linker_name:'商家名称')}}
							</view>
							<view v-if="!loading" @click="to_appraisal(0)" style="margin-top: 15upx;">
								<trailerStars allowTap="0" :innerScore="trader_appraisal_avg">
								</trailerStars>
							</view>
						</view>
					</view>
				</view>

				<!-- 实名认证 -->
				<view v-if="sys_role==0" @click="to_verify()" style="margin-top: 30upx;font-size: 24upx;"
					:class="user_info.idcard?'verify':'verify verify0'">
					{{user_info.idcard?'已认证': '未实名认证'}}
				</view>
				<view v-if="sys_role==1" @click="to_verify()" style="margin-top: 30upx;font-size: 24upx;"
					:class="user_info.realname_handled=='1'?'verify':'verify verify0'">
					{{!user_info.lawer_idcard?'未认证' :(user_info.certif_confirmed==0?'认证中':user_info.catalog==0?'个人认证':'企业认证')}}
				</view>
			</view>

			<!-- 钱包 -->
			<view :style="'background-image: url('+(serverUrlImg+'wallet_bg.png')+');'" class="main_width wallet">
				<view class="wallet_top">
					<image style="width: 36upx;height: 36upx;margin-right: 15upx;" src="../../static/my/wallet.png">
					</image>
					我的钱包
				</view>
				<view class="wallet_bot">
					<view class="balance_box">
						<view class="balance_box1">账户余额</view>
						<view class="balance_box2"><text>{{(user_info.finance_balance).toFixed(2)}}</text>元</view>
					</view>
					<view class="balance_box">
						<view class="balance_box1">冻结{{sys_role==1?'金额':'押金'}}</view>
						<view class="balance_box2"><text>{{(user_info.finance_frozenBalance).toFixed(2)}}</text>元</view>
					</view>
				</view>
				<navigator url="wallet" class="wallet_to_wal">查看详情 ></navigator>
			</view>
		</view>
		<!-- 未登录展示 -->
		<view v-else @click="menu_click('quit')" class="no_login">未登录</view>


		<!-- 我的4种工单，只非商户显示，已登录成功展示 -->
		<view v-if="sys_role==0&&user_info.id" class="nav">
			<view @click="href_order(index)" :data-index="index" class="bar_view" :key="index"
				v-for="(item, index) in status_arr">
				<image style="width: 98upx;height: 98upx;" :src="serverUrlImgOrder+'order'+(index+1)+'.png'"></image>
				<view>
					{{item.name}}
				</view>
				<!-- 右上角提示小红点 -->
				<view v-if="status_num_arr[index]!=0" class="bar_view_num">{{status_num_arr[index]}}</view>
			</view>
		</view>
		<view style="background-color: #F4F4F4;height: 20upx;"></view>


		<!-- 竖向菜单 -->
		<view style="margin-bottom: 50upx;margin-top: 16upx;" class="main_width my_bg_box">
			<view style="width: 92%;margin-left: 4%;overflow: hidden;">
				<view
					v-if="(item.need_login&&user_info.id)||!item.need_login||(isTrader2==1&&item.key=='verify'&&biz_trader2id!='')"
					@click="menu_click(item.key)" class="menu_item" :key="item.key" v-for="(item, index) in menu">
					<view style="display: flex;align-items: center;">
						<image style="width: 32upx;height: 32upx;margin-right: 20upx;"
							:src="'../../static/my/'+item.icon+'.png'"></image>
						<view style="color: #666666;">{{sys_role==0?item.name:item.name_mer||item.name}}</view>
					</view>
					<view style="color: #666666;">></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import commonData from '../../commonData.js'
	import commonFun from '../../commonFun.js'
	import trailerStars from '../../components/starts/index.vue'

	export default {
		data() {
			return {
				biz_trader2id: '', //现场经理id
				loading: true, //我的个人信息加载中...
				status_num_arr: [0, 0, 0, 0, 0, 0], //工单tab右上角红底数字
				serverUrlImgOrder: this.serverUrl + 'show/',
				isTrader2: 2,
				FOREND_TOKEN: '', //是否包括登录信息
				user_info: {}, //用户信息
				sys_role: commonData.sys_role, //角色
				// verify_type: 0, //认证状态，0代表未实名认证，1代表个人认证，1代表企业认证
				serverUrlImg: commonFun.serverUrl,
				menu: [], //竖向菜单
				status_arr: commonData.order_status_arr_merchant, //订单类型数组
				role_col: [ //角色数组，【0，1】与role对应。menu表示纵向菜单的key数组
					{
						menu: ['resumeCheck?allow=1', 'appraisal', 'activity', 'makeMoney',
							'toRecruit',
							'about', 'service/index', 'quit'
						],
						name: '用户端',
					},
					{
						menu: ['appraisal',
							'toMember',
							'about', 'service/index', 'quit'
						],
						name: '商户端',
					},
					{
						menu: [
							'verify',
							'toMember',
							'about', 'service/index', 'quit'
						],
						name: '现场经理',
					}
				],
			}
		},
		methods: {
			// 去个人信息页面
			to_per_info() {
				if (this.sys_role == 1) {
					uni.navigateTo({
						url: 'perInfo'
					})
				} else {
					if (this.user_info.nickname || this.user_info.profile_photo) {
						uni.navigateTo({
							url: 'perInfo'
						})
					} else {
						uni.getUserProfile({
							desc: "获取昵称和头像",
							success: (res) => {
								console.log('获取昵称和头像')
								console.log(res.userInfo)
								commonFun.requestUrl('&WsAjaxBiz=Worker&WsAjaxAction=registerInfo', {
									biz_catalog: 1,
									nickname: res.userInfo.nickName,
									profile_photo: res.userInfo.avatarUrl,
								}, res => {
									uni.navigateTo({
										url: 'perInfo'
									})
								})
							},
							fail: (err) => {
								console.log(err)
								uni.showToast({
									icon: "none",
									title: '获取昵称和头像失败'
								})
								uni.navigateTo({
									url: 'perInfo'
								})
							}
						})
					}
				}
			},
			// 去实名认证
			to_verify() {
				uni.navigateTo({
					url: this.sys_role == 0 ? './authentication' : './authentication_merchant'
				})
			},
			// 订单跳转
			href_order(e) {
				uni.navigateTo({
					url: '../order/index?status=' + e
				})
			},
			// 前往评价页面
			to_appraisal(e) { //0查看对我的评价,1查看我的评价
				if (e == 0) {
					uni.navigateTo({
						url: './appraisal?type=' + (this.sys_role == 0 ? 3 : 2) + '&id=' + this.user_info.id
					})
				} else {
					uni.navigateTo({
						url: './appraisal?type='
					})
				}
			},
			// 竖向菜单跳转
			menu_click(e) {
				if (this.FOREND_TOKEN == '' && e == 'verify') {
					uni.showModal({
						content: '请登录后扫码核销',
						success: (res) => {
							if (res.confirm) {
								uni.navigateTo({
									url: '../login/index?isTrader2=1'
								})
							}
						}
					})
					return
				}
				console.log(e)
				if (e == 'quit') { //退出登录
					uni.showModal({
						title: '确认退出？',
						success: (res) => {
							if (res.confirm) {
								uni.setStorageSync('baseUser', '')
								uni.setStorageSync('bizWorker', '')
								uni.setStorageSync('FOREND_TOKEN', '')
								uni.navigateTo({
									url: '../login/index?isTrader2=' + this.isTrader2
								})
								return
							}
							return
						}
					})
					return
				} else if (e == 'toRecruit') { //非商家跳转到商家
					uni.navigateToMiniProgram({
						appId: 'wx8c230ea00ceb8676',
						path: 'pages/merchant/index',
					})
					return
				} else if (e == 'toMember') { //商家跳转到非商家
					uni.navigateToMiniProgram({
						appId: 'wxcd35a60de7b5942a',
						path: 'pages/index/index',
					})
					return
				} else if (e == 'about') {
					uni.navigateTo({
						url: '../login/web?index=3'
					})
					return
				}
				uni.navigateTo({
					url: './' + e
				})
			},
			// 获取个人信息
			get_info() {
				if (this.isTrader2 == 1 || this.FOREND_TOKEN == '') {
					uni.stopPullDownRefresh()
					return
				}
				uni.showLoading({
					title: '加载中...',
					mask: true,
				})
				this.loading = true
				commonFun.get_user_info(res => {
					if (res.status) {
						uni.hideLoading()
						let user_info = res.data.Result
						this.user_info = user_info
						// 右上角红点数字
						this.status_num_arr = [
							this.user_info.ss_waitEnrollTotal,
							this.user_info.ss_workingTotal,
							this.user_info.ss_waitSettleTotal,
							0,
							this.user_info.ss_unreadWKTotal_ALL,
						]
						uni.setStorageSync('USER_INFO', this.user_info)
						console.log('我的页面获取个人信息')
						console.log(this.user_info)
						this.loading = false
					} else {
						console.log('获取个人信息失败')
						this.user_info = {}
						this.FOREND_TOKEN = ''
					}
					uni.stopPullDownRefresh()
				})
			},
			//现场经理抢夺授权
			grant(recruitId) {
				let params = {
					bizCatalog: 'RecruitEntity',
					handleMode: 'grabDelegate',
					recruitId,
				}
				console.log('抢夺授权请求参数')
				console.log(params)
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				commonFun.requestUrl('&WsAjaxBiz=Trader2&WsAjaxAction=entityDataHandle', params,
					res => {
						if (res.status) {
							console.log('核销页面抢夺授权结果返回')
							console.log(res.data.Result)
							this.biz_trader2id = res.data.Result
						}
						uni.hideLoading()
					}, false, true)
			},
		},
		onShow() {
			let isTrader2 = uni.getStorageSync('isTrader2') || 2 //设置isTrader2为是否是现场经理
			let FOREND_TOKEN = uni.getStorageSync('FOREND_TOKEN')
			this.isTrader2 = isTrader2
			if (this.isTrader2 == 1) {
				this.grant(uni.getStorageSync('recruitId'))
			}
			this.FOREND_TOKEN = FOREND_TOKEN
			this.get_info()
			// 渲染竖向菜单
			this.menu = this.role_col[isTrader2 == 1 ? 2 : commonData.sys_role]['menu'].map(item => {
				for (let i in commonData.my_menu_collect) {
					if (commonData.my_menu_collect[i]['key'] === item) {
						return commonData.my_menu_collect[i]
					}
				}
			})
		},
		onPullDownRefresh() {
			this.get_info()
		},
		onLoad() {},
		components: {
			trailerStars
		},
	}
</script>

<style>
	@import url("index.css");
</style>
