<template>
	<view>
		<image mode="widthFix" class="width100 headimg" src="../../../static/img/head.png"></image>
		<view class="headcont fotct" @click="jumpuserdes('../../tabBar/user/user')">
			<view class="width94 fcorfff font18 alijus" v-if="userInfo.secUser.objectType ==  3">
				{{userInfo.salesman.name}}
				<image mode="widthFix" class="margle iconw" src="../../../static/img/jt.png"></image>
			</view>
			<view class="width94 fcorfff font18 alijus" v-if="userInfo.secUser.objectType ==  4">
				{{userInfo.mer.merAbbreviate}}
				<image mode="widthFix" class="margle iconw" src="../../../static/img/jt.png"></image>
			</view>
			<view class="width94 fcorfff font18 alijus" v-if="userInfo.secUser.objectType ==  2">
				{{userInfo.agent.name}}
				<image mode="widthFix" class="margle iconw" src="../../../static/img/jt.png"></image>
			</view>
			<view class="width94 fcorfff font18 alijus" v-if="userInfo.secUser.objectType ==  5">{{userInfo.store.name}}
				<image mode="widthFix" class="margle iconw" src="../../../static/img/jt.png"></image>
			</view>
			<!-- 业务员 -->
			<view class="font28 fcorfff mart20 width94" v-if="userInfo.secUser.objectType ==  3"></view>
			<view class="font28 fcorfff mart15 width94" v-if="userInfo.secUser.objectType ==  3"></view>
			<!-- 非业务员 -->
			<view class="font15 height40 fcorfff mart20 width94" v-if="userInfo.secUser.objectType !=  3">今日总收入</view>
			<view class="font28 fcorfff mart15 width94" v-if="userInfo.secUser.objectType !=  3">¥<text
					class="margle font40">{{auditnum.tradePrice}}</text></view>
			<!-- 业务员 -->
			<view class="width94 mart80" v-if="userInfo.secUser.objectType ==  3" @click.stop="jumpaudt">
				<view class="width50 fcorfff font14 flleft">
					<view class="width100">进件成功数量</view>
					<view class="width100 mart15 font22">{{auditnum.contractSuccess}}</view>
				</view>
				<view class="width48 fcorfff font14 flright">
					<view class="width100">待审核数量</view>
					<view class="width100 mart15 font22">{{auditnum.contractAudit}}</view>
				</view>
			</view>
			<!-- 非业务员 -->
			<view class="width94 mart15" v-if="userInfo.secUser.objectType != 3" @click.stop="jumpaudt">
				<view class="width50 fcorfff font14 flleft">
					<view class="width100">收款笔数</view>
					<view class="width100 mart15 font22">{{auditnum.tradeSum}}</view>
				</view>
				<view class="width48 fcorfff font14 flright">
					<view class="width100">退款笔数</view>
					<view class="width100 mart15 font22">{{auditnum.refundTradeSum}}</view>
				</view>
			</view>
		</view>
		<!-- 消息中心 -->
		<view class="width100  backcorfff " style="position: absolute;top: 300px;" @click="jumpmescon">
			<view class="width94 alijusstart height50">
				<image src="../../../static/img/mer6.png" mode="widthFix" class="iconw25 marglerig"></image>
				<text class="width90p">消息中心</text>
				<image class="iconw alicntend" mode="widthFix" src="../../../static/img/jtg.png"></image>
			</view>
		</view>
		<!-- 菜单功能 -->
		<view class="width100" style="position: absolute;top:360px;">
			<view class="funcss" v-for="(item,index) in funcList" :key="index" @click="jumpdesfun(item)">
				<image :src="item.img" mode="widthFix" class="funicon"></image>
				<view class="width100 font14 mart5">{{item.title}}</view>
			</view>
		</view>
		<view class="width100 height40p"></view>

		<!-- <downloadup></downloadup> -->
	</view>
</template>

<script>
	import {
		moveHomeCount,
		mermoveHomeCount,
		storemoveHomeCount,
		getDictionaries,
		connectStoreCid,
		voiceSucceed
	} from '../../../Utils/Api.js'
	import downloadup from '../../../components/downloadUp.vue'
	let app = getApp();
	export default {
		components: {
			downloadup
		},
		data() {
			return {
				funcList: [],
				userInfo: app.globalData.userInfo, //登录信息
				auditnum: '', //统计数据
				//语音播报
				playurl: "",
				voicePlayer: null, // 语音播放器
				watchProximity: null, // 设备距离监听器
				sessionPlayMode: 0, // 语音播放模式 0扬声器 1听筒
				wakeLock: null, // Android端唤醒锁
				audioManager: null, // Android音频管理器
				audioFocus: false, // 音频聚焦状态
				storeCid: '', //cid编号
				taskNo: '' //推送编号
			};
		},

		onLoad() {
			if (this.userInfo.secUser.objectType == 5) {
				let status = 1;
				let that = this;
				uni.getPushClientId({
					success: res => {
						that.storeCid = res.cid;
						uni.getStorage({
							key: "clientId",
							success: (res) => {
								if (res.data && res.data != undefined && res.data != that
									.storeCid) {
									status = 2;
								}
							},
							fail(err) {
								status = 3;
								uni.setStorage({
									key: "clientId",
									data: that.storeCid
								})
							}
						})
					},
					fail(err) {
						console.log(err);
					}
				})
				setTimeout(function() {
					if (status == 1 && that.storeCid) {
						that.setTimer();
					}
					if ((status == 2 || status == 3) && that.storeCid) {
						that.connectStoreCid(that.storeCid)
					}
				}, 1500);
			}


			uni.onPushMessage((res) => {
				// console.log("收到推送消息：", res.data.content) //监听推送消息
				let conts = res.data.content.split('!@#PUSH#@!');
				this.playurl = conts[0];
				this.taskNo = conts[1];
				if (this.playurl) {
					this.starts();
					this.voicePlayer.play();
				}
			})

			//监听系统知栏消息点击事件
			plus.push.addEventListener('click', (msg) => {
				uni.navigateTo({
					url: '/pages/index/collectNotes/collectNotes'
				})
			}, false)

			//监听接收透传消息事件  
			plus.push.addEventListener('receive', (msg) => {
				// 通知栏消息
				let payload = msg
				let contsplist = payload.content.split('!@#PUSH#@!');;
				let messageContent = contsplist[2]
				// this.playurl = contsplist[0];
				// this.taskNo = contsplist[1];
				// if (this.playurl) {
				// 	this.starts();
				// 	this.voicePlayer.play();
				// }
				plus.push.createMessage(messageContent, JSON.stringify(payload), {
					title: '惠支付',
					cover: false
				})
			}, false)


			//业务员
			if (this.userInfo.secUser.objectType == 3) {
				this.funcList = [{
						title: '新增商户',
						url: '../../index/merchant-incoming-parts/merchant-incoming-parts',
						img: '../../../static/img/home3.png'
					},
					{
						title: '进件管理',
						url: '../../index/merchant-management/merchant-management',
						img: '../../../static/img/home4.png'
					}, {
						title: '商户管理',
						url: '../../index/normal-Merchant/normal-Merchant',
						img: '../../../static/img/home4.png'
					}, {
						title: '我的设备',
						url: '../../index/mineService/mineService?id=3',
						img: '../../../static/img/casihr.png'
					}, {
						title: '申请订单',
						url: '../../index/salesOrderList/salesOrderList',
						img: '../../../static/img/apply.png'
					}
				]
				return;
			}
			//商户
			if (this.userInfo.secUser.objectType == 4) {
				this.funcList = [{
						title: '收款记录',
						url: '../../index/collectNotes/collectNotes',
						img: '../../../static/img/home1.png'
					},
					{
						title: '门店管理',
						url: '../../index/doorManage/doorManage',
						img: '../../../static/img/home3.png'
					}
				]
				return;
			}
			//门店
			if (this.userInfo.secUser.objectType == 5) {
				this.funcList = [{
						title: '收款记录',
						url: '../../index/collectNotes/collectNotes',
						img: '../../../static/img/home1.png'
					}, {
						title: '二维码列表',
						url: '../../index/scanList/scanList',
						img: '../../../static/img/home2.png'
					},
					{
						title: '我的设备',
						url: '../../index/mineService/mineService?id=1',
						img: '../../../static/img/casihr.png'
					},
					{
						title: '设备申请',
						url: '../../index/serviceApplicationList/serviceApplicationList',
						img: '../../../static/img/apply.png'
					}
				]
				return;
			}
			//代理商
			if (this.userInfo.secUser.objectType == 2) {
				this.funcList = [{
					title: '结算审核',
					url: '../../index/review-Settlement/review-Settlement',
					img: '../../../static/img/home1.png'
				}]
				// , {
				// 	title: '权益活动',
				// 	url: '../../index/equity-activity-review/equity-activity-review',
				// 	img: '../../../static/img/home5.png'
				// }
				return;
			}

		},
		onShow() {

			if (this.userInfo.secUser.objectType == 3) {
				this.moveHomeCount();
			}
			if (this.userInfo.secUser.objectType == 4) {
				this.mermoveHomeCount();
			}

			if (this.userInfo.secUser.objectType == 5) {
				this.storemoveHomeCount();
			}

			this.getDictionaries();
		},
		methods: {
			Timersockt() {},
			//绑定商户cid
			connectStoreCid(item) {
				let datas = {
					"cid": item,
					"storeId": this.userInfo.store.id
				}
				connectStoreCid(datas).then(res => {
					this.setTimer();
				})
			},

			setTimer() {
				let holdTime = 10;
				this.Timersockt = setInterval(() => {
					if (holdTime <= 0) {
						holdTime = 60;
						clearInterval(this.Timersockt);
						let status = 1;
						let that = this;
						uni.getPushClientId({
							success: res => {
								that.storeCid = res.cid;
								uni.getStorage({
									key: "clientId",
									success: (res) => {
										if (res.data && res.data != undefined && res
											.data != that
											.storeCid) {
											status = 2;
										}
									},
									fail(err) {
										status = 3;
										uni.setStorage({
											key: "clientId",
											data: that.storeCid
										})
									}
								})
							},
							fail(err) {
								console.log(err);
							}
						})

						setTimeout(function() {
							if (status == 1 && that.storeCid) {
								that.setTimer();
							}

							if ((status == 2 || status == 3) && that.storeCid) {
								that.connectStoreCid(that.storeCid)
							}
						}, 1500);
						return;
					}
					holdTime--;
				}, 1000)
			},
			//播报结束上传
			voiceSucceed(item) {
				let datas = {
					storeId: this.userInfo.store.id,
					taskNo: this.taskNo
				}
				voiceSucceed(datas).then(res => {

				});
			},
			//语音播报
			starts() {
				this.watchProximity = plus.proximity.watchProximity((distance) => {
					/**
					 * iOS端接近为0，远离为Infinity
					 * Android端接近为0，远离为5
					 */
					if (this.playMode === 0) {
						// 扬声器模式下，需要对声道进行实时修改
						this.voicePlayer.pause();
						if (distance !== 0) {
							// 扬声器
							this.voicePlayer.setRoute(0);
						} else {
							// 听筒
							this.voicePlayer.setRoute(1);
						}
						this.voicePlayer.resume();
					}
				});
				this.voicePlayer = plus.audio.createPlayer({
					src: this.playurl
				});
				this.voicePlayer.setRoute(this.playMode);
				// 监听自然播放完成
				this.voicePlayer.addEventListener('ended', () => {
					// 销毁正在监听设备距离的监听器
					if (this.watchProximity) {
						plus.proximity.clearWatch(this.watchProximity);
						this.watchProximity = null;
					}
					// 释放唤醒锁
					if (this.wakeLock) {
						this.wakeLock.release();
						this.wakeLock = null;
					}
					this.voiceSucceed();
					this.storemoveHomeCount();
				});

				// 监听音频可以开始播放事件
				this.voicePlayer.addEventListener('play', () => {
					// 首次播放时会执行两次回调
					if (this.playState !== 2) {
						// plus.audio.ROUTE_SPEAKER：扬声器 0
						// plus.audio.ROUTE_EARPIECE：听筒 1
						this.voicePlayer.setRoute(this.playMode);
						this.playState++;
					}
				});
				// 监听音频播放错误事件
				this.voicePlayer.addEventListener('error', (err) => {
					console.log('报错err', err);
					// 释放唤醒锁
					if (this.wakeLock) {
						this.wakeLock.release();
						this.wakeLock = null;
					}
					// 销毁正在监听设备距离的监听器
					if (this.watchProximity) {
						plus.proximity.clearWatch(this.watchProximity);
						this.watchProximity = null;
					}
				});

			},

			//查询数据字典
			getDictionaries() {
				getDictionaries().then(res => {
					if (res.return_code == '000000') {
						app.globalData.Dictionaries = res.return_data;
					}
				})
			},
			// 业务员查询统计
			moveHomeCount() {
				moveHomeCount().then(res => {
					if (res.return_code == '000000') {
						this.auditnum = res.return_data;
					}
				})
			},
			// 商户查询统计
			mermoveHomeCount() {
				mermoveHomeCount().then(res => {
					if (res.return_code == '000000') {
						this.auditnum = res.return_data.today;
					}
				})
			},
			// 门店查询统计
			storemoveHomeCount() {
				storemoveHomeCount().then(res => {
					if (res.return_code == '000000') {
						this.auditnum = res.return_data.today;
					}
				})
			},
			/**
			 * 跳转功能
			 */
			jumpdesfun(item) {
				if (item.title == '绑定设备') {
					uni.navigateTo({
						url: '../../index/serviceBuild/serviceBuild?id=1&storeid='
					})
				} else {
					uni.navigateTo({
						url: item.url
					})
				}
			},
			//跳转我的资料
			jumpuserdes(item) {
				uni.navigateTo({
					url: item
				})
			},
			//跳转记录
			jumpaudt() {
				uni.navigateTo({
					url: '../../index/collectNotes/collectNotes'
				})
			},
			//跳转消息中心
			jumpmescon() {
				uni.navigateTo({
					url: '../../index/message-center/message-center'
				})
			}
		}

	};
</script>
<style lang="scss">
	page {
		background-color: #F8F8F8;
	}

	.headimg {
		position: absolute;
		max-height: 300px;
	}

	.headcont {
		position: relative;
		top: 50px;
	}

	.width48 {
		width: 49%;
		border-left: 1px solid #4f65cb;
		height: 65px;
	}

	.mart145 {
		margin-top: 145px;
	}

	// 菜单
	.funcss {
		width: 33%;
		height: 100px;
		border-right: 1px solid #e8e8e8;
		border-bottom: 1px solid #e8e8e8;
		background-color: #FFFFFF;
		text-align: center;
		display: inline-block;

		.funicon {
			width: 35px;
			padding-top: 15px;
		}
	}
</style>