<template>
	<view class="index-container">
		<view class="ic-content">
			<view class="ic-header">
				<view class="ih-address" v-if="loginState" @click="toStoreList">
					<view>{{indexData.brand_name || '-'}}(共{{indexData.stadium_num || '0'}}家)</view>
					<image src="/static/images/icon/arrow_ff.png" mode="aspectFit"></image>
				</view>
				<view v-else class="ih-btn" hover-class="hover-active" @click="showAuthor">点击登陆</view>
				<block v-if="isPermissionShowTab({ serverKey: 1001 })">
					<view class="ih-tip">今日总收入</view>
					<view class="ih-price"><text>{{loginState?'¥':''}}</text>{{loginState?(indexData.amount || '0'):'***'}}</view>
					<view class="ih-amount">
						<view>
							<view>收款笔数</view>
							<view>{{loginState?(indexData.in_count || '0'):'**'}}</view>
						</view>
						<view>
							<view>退款笔数</view>
							<view>{{loginState?(indexData.out_count || '0'):'**'}}</view>
						</view>
					</view>
				</block>
				<image class="ih-bg-icon" style="left: 0;" mode="aspectFit" src="/static/images/icon/index/header_bg_a.png"></image>
				<image class="ih-bg-icon" mode="aspectFit" src="/static/images/icon/index/header_bg_b.png"></image>
			</view>
			<view class="ic-notice" @click="toNoticeList">
				<view class="in-left">
					<image src="/static/images/icon/index/notice.png" mode="aspectFit"></image>
					<text>消息中心</text>
				</view>
				<view class="in-right">
					<view v-if="indexData.message_count>0">{{indexData.message_count || '0'}}</view>
					<image src="/static/images/icon/arrow_b2.png" mode="aspectFit"></image>
				</view>
			</view>
		</view>
		<view class="ic-tabs" v-if="indexData&&indexData.permission">
			<block v-for="(e, i) in tabList" :key="i">
				<!-- tid1807 去掉"核销查询“菜单(已有悬浮按钮) -->
				<view class="it-item" @click="toPageInfo(e)" v-if="isPermissionShowTab(e)&&e.id!=4">
					<view>
						<image mode="aspectFit" :src="'/static/images/icon/index/tab_'+ e.id + '.png'"></image>
						<view>{{e.name}}</view>
					</view>
				</view>
			</block>
			<!-- <view class="hold-place-blank">
				<navigator url="/subpackage/party/pages/index/index">party/pages/index/index</navigator>
				<navigator url="/subpackage/party/pages/login/login">party/pages/login/login</navigator>
			</view> -->
		</view>
		
		<bottom-logo></bottom-logo>

		<authorization-login ref="authorizationLogin"></authorization-login>

		<view class="fly_btn"  @click="toPageInfo(tabList[4])" v-if="loginState&&isPermissionShowTab(tabList[4])">
			<image class="f_bg" src="/static/images/icon/index/green_bg_circle.png" mode="scaleToFill"/>
			<image class="f_icon" src="/static/images/icon/index/scan_icon_white.png" mode="scaleToFill"/>
			<text>核销</text>
		</view>

	</view>
</template>
<script>

	import util from '../../utils/util';
	import { servers } from '../../js/server';
	import { API } from '../../js/api';
	import { mapGetters } from 'vuex';
	import bottomLogo from "@/subpackage/menu/components/bottom_logo.vue";
	import authorizationLogin from "@/subpackage/authorization/components/login.vue";
	/**
	 * tid1807
	 * 1、去掉"核销查询“菜单(已有悬浮按钮) *
	 * 2、去掉“收款记录”(在收入统计页面已有入口进入收款记录) *
	 * 3、去掉“进场人数异常”(该入口计划放入新改版的“核销查询”页面) *
	 * 4、原“系统工具”文案改为“小程序管理”;原“钱包提现”文案改为“钱包&提现”: 原“设备管理”文案改为“智能设备”; *
	 * 5、增加底部 (工作台、推广营销、商家服务、我的) *
	 * 6、增加“工单”功能
	 * 
	 * */ 
	const tabList = [
		{
			id: 0,
			name: '收入统计',
			path: '/pages/turnover/turnover',
			serverKey: 1001  // 后端对应权限编号
		},
		// {
		// 	id: 1,
		// 	name: '收款记录',
		// 	path: '/subpackage/income/pages/details_record/details_record',
		// 	serverKey: 1002  // 后端对应权限编号
		// },
		{
			id: 2,
			name: '订单管理',
			path: '/subpackage/order/pages/order_manage/order_manage',
			serverKey: 1012  // 后端对应权限编号
		},
		{
			id: 3,
			name: '员工管理',
			path: '/pages/employee/manage/manage',
			serverKey: 1007  // 后端对应权限编号
		},
		{
			id: 4,
			name: '核销查询',
			path: '/subpackage/verification/pages/index',
			serverKey: 1008  // 后端对应权限编号
		},
		{
			id: 5,
			name: '场地看板',
			// path: '/pages/site/order_list/order_list', // 20210804 不要占用列表
			path: '/pages/site/manage/manage',
			serverKey: 1009  // 后端对应权限编号
		},
		{
			id: 6,
			name: '智能设备',
			path: '/subpackage/device/pages/index/index',
			serverKey: 1010  // 后端对应权限编号
		},
		{
			id: 7,
			name: '商品零售',
			path: '/subpackage/retail/pages/index/index',
			serverKey: 1011  // 后端对应权限编号
		},
		// {
		// 	id: 8,
		// 	name: '课程管理',
		// 	path: '/subpackage/course/pages/index/index',
		// 	serverKey: 1013  // 后端对应权限编号 1013
		// },
		{
			id: 9,
			name: '储值卡管理',
			path: '/pages/order_list/order_list?order_type=18',
			serverKey: 1014  // 后端对应权限编号 1014
		},
		// {
		// 	id: 10,
		// 	name: '进场人数异常',
		// 	path: '/subpackage/blacklist/pages/abnormal_list/abnormal_list',
		// 	serverKey: 1015  // 后端对应权限编号 1015
		// },
		{
			id: 11,
			name: '钱包&提现',
			path: '/subpackage/wallet/pages/index/index',
			serverKey: 1017  // 后端对应权限编号 1017
		},
		
		{//需求要求更改ICON位置
			id: 12,
			name: '小程序管理',
			path: '/subpackage/party/pages/index/index',
			serverKey: 1016  // 后端对应权限编号 1016
		},
		{
			id: 13,
			name: '工单',
			path: '/subpackage/message/pages/work_order/list',
			serverKey: 1022  // 后端对应权限编号 1022
		},
	];
	
	const app = getApp();
	const APPID = uni.getAccountInfoSync().miniProgram.appId;
	
	export default {
		components: { bottomLogo, authorizationLogin },
		computed: {
			...mapGetters([ 'loginState' ]),
		},
		data() {
			return {
				tabList,
				indexData: {},
			}
		},
		async onLoad(options) {	
			try{
				let _loginRes = {};
				// 20230829 新增平台账户不绑定，不检验，每次进来走重新登陆绑定流程
				if(options.role !== 'ADMIN-PLATFORM')_loginRes = await this.$store.dispatch('checkUserAuthor');
				// 20240325 新增openid绑定账号检测
				if(_loginRes?.data === '')this.webBrandUserCheck();
				if(_loginRes?.data)this.getIndexInfo();
			}catch(err){
				console.warn('pages index onLoad err--->', err);
			}
		},
		onShow(){
			let { indexData, loginState, getIndexInfo } = this; 
			if(JSON.stringify(indexData)!='{}'&&loginState)getIndexInfo();
		},
		methods: {
			// 账号有开启了权限的才显示菜单，否则不显示【ID1000840】
			isPermissionShowTab(e){
				let { indexData } = this;
				if(indexData&&Object.prototype.toString.call(indexData.permission) === '[object Object]'){
					return indexData.permission[e.serverKey]
				}
				return false;
			},
			toNoticeList(){
				if(!this.loginState)return this.showAuthor();
				util.routeTo(`/pages/message/list/list`,'nT');
			},
			toPageInfo(tabInfo){
				let { indexData, loginState } = this;
				let _permission = indexData.permission || {};
				if(!loginState)return this.showAuthor();
				if(!tabInfo.path)return util.showNone('暂未开放！');
				//if(tabInfo.id == 2)return util.routeTo(tabInfo.path,'nT'); //测试
				if(!_permission[tabInfo.serverKey])return util.showNone('暂无权限，请联系管理员开启！')
				
				if(tabInfo.id === 1){
					util.$_emit('turnoverToDetailsRecord', {
						stadiumInfo: null,
						brandInfo: indexData&&indexData.brand ? indexData.brand : null,
					});
				}

				if([ 10, 11, 4, 13 ].includes(tabInfo.id))return util.routeTo(tabInfo.path + `?brand_id=${indexData.brand.id}`,'nT');

				if(tabInfo.id === 12){
					let url = tabInfo.path + `?appid=${indexData.brand.mini_wechat_appid}`
					console.log(222,url)
					return util.routeTo(url,'nT');
				}
				
				util.routeTo(tabInfo.path,'nT');
			},
			
			getIndexInfo(){	
				servers.get({
					url: API.calc,
					data: {},
					failMsg: '加载数据失败！'
				})
				.then(res=>{
					this.indexData = res;

					this.$store.commit('setBrandInfo',res);
				})
			},	
			showAuthor(){
				this.$refs?.authorizationLogin?.alert?.({
					success: ()=>{
						setTimeout(this.getIndexInfo, 1000);
					},
					fail: err =>{
						// console.warn('pages index showAuthor authorizationLogin Err ->', err)
					}
				})
			},
			webBrandUserCheck(){

				// return this.webUserGet();

				console.warn('webBrandUserCheck')
				let token = uni.getStorageSync('token');
				let token_web = uni.getStorageSync('token_web');
				let _data = {
					token: token||"",
					token_web: token_web||"",
				}
				return servers.get({
					url: API['party'].webBrandUser,
					data: _data,
					failMsg: '加载数据失败！'
				})
				.then(res=>{
					console.log('webBrandUserCheck',res)
					// if(!res.had_binding){
					// 	console.warn('webBrandUser 验证绑定账号失败！');
					// 	return util.routeTo(`/pages/login/login`,'rL');
					// }else{
						 //status: 1创建品牌 2实名认证 3审核成功 4审核失败 5完成 6修改审核中???待定
						console.warn(res,'webBrandUserCheck success',res.status);
						if(res.status==0&&token_web){
							return util.routeTo(`/subpackage/party/pages/login/regist`,'rL');
						}else if(res.status==1){
							return util.routeTo(`/subpackage/party/pages/login/reviewing`,'rL');
						}else if(res.status==2){
							return util.routeTo(`/subpackage/party/pages/login/reviewing`,'rL');
						}else if(res.status==5){
							//完成 小程序端执行绑定
							this.bindUserOpenid();
						}
						
						// return res;
					// }
					
				}).catch(err=>{
					console.warn('webBrandUserCheck err',err)
					util.routeTo(`/pages/login/login`,'rL');
				})
			},
			toStoreList(){
				let { indexData, loginState } = this;
				if(!loginState)return this.showAuthor();
				util.routeTo(`/pages/store_list/store_list?brand_id=${indexData.brand.id}`,'nT');
			},
			bindUserOpenid(){

				let token_web = uni.getStorageSync('token_web');
				let _data = {
					token_web: token_web||"",
				}
				return servers.post({
					url: API['party'].bindingOpenId,
					data: _data,
					failMsg: '绑定失败！'
				})
				.then(res=>{
					console.log('小程序端绑定bindUserOpenid',res)
					if(res.msg=="success"){
						util.showNone('绑定成功！');
						util.routeTo(`/pages/index/index`,'rL');
					}else{
						util.showNone(res.message || '绑定失败！');
					}
				}).catch(err=>{
					console.warn('bindUserOpenid err',err)
					util.showNone(err.message || '绑定失败！');
					// util.routeTo(`/subpackage/party/pages/login/reviewing`,'rL');
					util.routeTo(`/pages/login/login`,'rL');
				})
			},
			webUserGet(){
				let token_web = uni.getStorageSync('token_web');
				let _data = {
					token_web: token_web||"",
				}
				return servers.post({
					url: API['party'].webUserGet,
					data: _data,
					failMsg: '获取信息失败！'
				})
				.then(res=>{
					console.log('webUserGet:',res)
					if(res.code==0){
					}else{
						util.showNone(res.message || '失败！');
					}
				}).catch(err=>{
					console.warn('bindUserOpenid err',err)
					util.showNone(err.message || '获取信息失败！');
				})
			},


		}
	}
</script>

<style lang="scss" scoped>
	@import "~style/public.scss";
	.index-container{
		position: relative;
	}
	.ic-content{
		padding: 24upx;
	}
	.ic-header{
		position: relative;
		height: auto;
		margin-bottom: 24upx;
		padding: 38upx 24upx 30upx;
		border-radius: 14upx;
		background-color: $themeColor;
		overflow: hidden;
		.ih-address{
			margin-bottom: 26upx;
			@include centerFlex(center);
			>view{
				max-width: 90%;
				line-height: 50upx;
				text-align: center;
				font-size: 36upx;
				color: #fff;
				@include textHide(1);
			}
			>image{
				flex-shrink: 0;
				margin-left: 20upx;
				width: 28upx;
				height: 28upx;
			}
		}
		.ih-btn{
			margin: 0 auto 28upx;
			width: 160upx;
			height: 48upx;
			text-align: center;
			line-height: 48upx;
			font-size: 28upx;
			color: $themeColor;
			background-color: #fff;
			border-radius: 24upx;
		}
		.ih-tip{
			margin-bottom: 8upx;
			line-height: 40upx;
			text-align: center;
			font-size: 28upx;
			color: rgba($color: #fff, $alpha: .7);
		}
		.ih-price{
			margin-bottom: 50upx;
			line-height: 96upx;
			text-align: center;
			font-size: 84upx;
			font-weight: 500;
			color: #fff;
			@include textHide(1);
			>text{
				line-height: 74upx;
				font-size: 64upx;
			}
		}
		.ih-amount{
			@include centerFlex(space-around);
			>view{
				>view{
					&:first-child{
						margin-bottom: 16upx;
						line-height: 40upx;
						text-align: center;
						font-size: 28upx;
						color: rgba($color: #fff, $alpha: .7);
					}
					&+view{
						line-height: 56upx;
						text-align: center;
						font-size: 40upx;
						font-weight: 500;
						color: #fff;
					}
				}
			}
		}
		.ih-bg-icon{
			position: absolute;
			top: 0;
			width: 120upx;
			height: 142upx;
			&+.ih-bg-icon{
				right: 0;
				top: 38upx;
				width: 240upx;
				height: 400upx;
			}
		}
	}
	.ic-notice{
		padding: 0 24upx;
		height: 108upx;
		border-radius: 10upx;
		background-color: #fff;
		@include centerFlex(space-between);
		.in-left{
			>image{
				vertical-align: middle;
				margin-right: 10upx;
				width: 52upx;
				height: 50upx;

			}
			>text{
				font-size: 28upx;
				line-height: 40upx;
				color: #1a1a1a;
			}
		}
		.in-right{
			@include centerFlex(flex-end);
			>view{
				margin-right: 14upx;
				line-height: 50upx;
				text-align: center;
				width: 50upx;
				height: 50upx;
				line-height: 50upx;
				font-size: 24upx;
				border-radius: 50%;
				color: #fff;
				background-color: #ea5061;
				@include textHide(1);

			}
			>image{
				width: 28upx;
				height: 28upx;

			}
		}
	}
	.ic-tabs{
		display: flex;
		flex-wrap: wrap;
		padding: 0upx 14upx 12upx;
		margin-top: -12upx;
		.it-item{
			flex-shrink: 0;
			width: 33.33%;
			padding: 12upx;
			>view{
			    padding: 40upx 10upx 0;
			    height: 218upx;
			    border-radius: 10upx;
			    background-color: #fff;
				>image{
					display: block;
					margin: 0 auto 20upx;
					width: 52upx;
					height: 52upx;
				}
				>view{
					text-align: center;
					line-height: 40upx;
					font-size: 28upx;
					color: #1a1a1a;
					@include textHide(1);
				}
				
			}
			
		}
	}

	// fly_btn
	.fly_btn{
		position: fixed;
		bottom: 100rpx;
		right: 80rpx;
		@include centerFlex(center);
		flex-direction: column;
		.f_bg{
			position: absolute;
			width: 148upx;
			height: 148upx;
		}
		.f_icon{
			z-index: 3;
			width: 40rpx;
			height: 40rpx;
		}
		>text{
			z-index: 3;
			font-size: 20rpx;
			color: #fff;
		}
	}
	
	.hold-place-blank{
		height:250rpx;
		width: 1rpx;
	}
</style>
