<template>
	<view class="container">
		<u-navbar bgColor="#fff" :placeholder="true" @leftClick="scan">
			<view slot="left">
				<u-icon name="scan" color="#333" size="32"></u-icon>
			</view>
			<view slot="center" style="color:#333;font-size: 36rpx;">
				开门柜管理
			</view>
		</u-navbar>

		<!-- 统计信息 -->
		<view class="total">
			<view class="total-sale flex flex-start ">
				<view class="sale-item">
					<view class="sale-name">
						今日总收益（元）
					</view>
					<view class="sale-num">
						{{$xy.delMoney(total.day.salesPrice)}}
					</view>
				</view>
				<view class="sale-item">
					<view class="sale-name">
						本月总收益（元）
					</view>
					<view class="sale-num">
						{{$xy.delMoney(total.month.salesPrice)}}
					</view>
				</view>

				<view class="sale-more" @click="$tab.navigateTo('/pages/globalPages/moreData')">
					更多数据
					<image src="https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/white-more.png"
						mode="widthFix"></image>
				</view>

				<view @click="update" :class="[load?'update load':'update']">
					<image src="https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/update.png"
						mode="heightFix"></image>
				</view>
			</view>
			<view class="refund-order flex flex-start">
				<view class="refund-item" @click="$tab.navigateTo('/pages/order/refundList')">
					<view class="refund-num">
						￥{{$xy.delMoney(total.day.refundMoney)}}
					</view>
					<view class="refund-name">
						今日退款
					</view>
				</view>
				<view class="refund-item" @click="$tab.navigateTo('/pages/order/orderQuery')">
					<view class="refund-num">
						{{total.day.orderNum}}
					</view>
					<view class="refund-name">
						今日订单数
					</view>
				</view>
			</view>
			<view class="notice" @click="goRisk">
				<view>
					<u-notice-bar :text="notice" color="#CB7506" bgColor="#fff" speed="80" mode="link"
						@close="noticeClose">
					</u-notice-bar>
				</view>
				<!-- <view class="notice-tips flex justify-around align-center" v-if="tipsCount.riskOrderNum>0">
					<view>
						{{tipsCount.riskOrderNum}}
					</view>
				</view> -->

				<view class="notice-tips flex justify-around align-center" v-if="tipsCount.riskOrderNum>0">
					<u-badge numberType="overflow" bgColor="#E60012" max="99" :value="tipsCount.riskOrderNum"></u-badge>
				</view>
			</view>
		</view>

		<!-- 常用菜单 -->
		<view class="useful-menu" v-if="operMenu.length>0">
			<view  class="flex justify-between flex-wrap">
				<view class="useful-menu-item flex justify-between align-center" v-for="(item,index) in operMenu"
					:key="item.id" @click="menuClick(item)">
					<view class="image">
						<u-image width="75rpx" height="75rpx" :src="iconList[item.name]" mode="widthFix"
							:lazy-load="true">
						</u-image>
					</view>
					<view class="menu-name">{{item.name}}</view>
					<!-- <view class="tips flex justify-around align-center" v-if="tips(item.name)>0">
						<view>
							{{tips(item.name)}}
						</view>
					</view> -->
					<u-badge absolute :offset="[-2,-2]" is-dot bgColor="#E60012" v-if="tips(item.name)>0"></u-badge>
					<!-- <u-badge numberType="overflow" :absolute="true" :offset="[-4,-4]" bgColor="#E60012" max="99" :value="tips(item.name)"></u-badge> -->
				</view>
			</view>
		</view>

		<!--便捷功能 -->
		<view class="menu"  v-if="usefMenu.length>0">
			<view class="menu-title">
				便捷功能
			</view>
			<view class="menu-content flex justify-start">
				<view class="menu-item" v-for="(item,index) in usefMenu" :key="item.id" @click="menuClick(item)">
					<view class="image">
						<u-image width="57rpx" height="57rpx" :src="iconList[item.name]" mode="widthFix"
							:lazy-load="true">
						</u-image>
					</view>
					<view>{{item.name}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		allCount
	} from "@/api/device/device.js"

	import {
		tipsCount
	} from "@/api/order/order.js"
	tipsCount

	export default {
		data() {
			return {
				tabArr: [{
						name: '今日',
						id: 0
					},
					{
						name: '本月',
						id: 1
					}
				],
				current: 0,

				//菜单对应图片路径
				iconList: {
					'退款审核': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/refund.png',
					'异常订单': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/risk.png',
					'客诉处理': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/cc.png',
					'补货管理': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/replenishment.png',
					'增加设备': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/menu-icon (9).png',
					'增加点位': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/menu-icon (8).png',
					'团队管理': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/menu-icon (4).png',
					'加盟商': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/menu-icon (2).png',
					'流量卡管理': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/menu-icon (5).png',
					'会员管理': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/menu-icon (1).png',
					'运营仓': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/menu-icon (7).png',
					'黑名单管理': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/menu-icon (3).png',
					'营销管理': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/menu-icon (6).png',
					'费用账单': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/menu-icon (10).png',
					'商品建模': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/goods-model.png',
					'订单管理': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/order-man.png',
					// '商品统计': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/goods-total.png',
					'销售统计': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/device-total.png',
					
					//暂时使用
					'激活管理': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/menu-icon (6).png',
					'商品清单': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/menu-icon (7).png',
					'常见错误码': 'https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/menu-icon (3).png',
				},

				tempTotal: {},

				total: {
					day: {
						orderNum: 0,
						refundMoney: 0,
						salesPrice: 0
					},
					month: {
						orderNum: 0,
						refundMoney: 0,
						salesPrice: 0
					}
				},

				riskNum: 0,
				noticeShow: false,
				load: false,
				timer: null,
				tipsCount: {
					refundNum: 0,
					complaintsNum: 0,
					outOfStockNum: 0,
					riskOrderNum: 0
				}
			}
		},

		computed: {
			//运营任务菜单
			operMenu() {
				return this.getMenu('首页', '运营任务')
			},
			//便捷菜单
			usefMenu() {
				return this.getMenu('首页', '便捷功能')
			},
			//消息
			notice() {
				let msg = '您有新的消息~'
				if (this.tipsCount.riskOrderNum > 0) {
					msg = `您有${this.tipsCount.riskOrderNum}个待办订单需处理!`
				} else {
					msg = '无待办事项~'
				}
				return msg
			}
		},

		created() {
			this.getTotalData() //顶部统计数据
			this.getNum() //常用菜单角标
		},

		methods: {
			//更新统计数据
			update() {
				if (this.timer) {
					clearTimeout(this.timer)
				}
				this.load = true;
				this.timer = setTimeout(() => {
					this.load = false;
				}, 1000)
				this.getTotalData()
				this.getNum() //常用菜单角标
			},

			onshow() {
				this.getTotalData()
				this.getNum()
			},
			//统计数据
			getTotalData() {
				allCount().then(res => {
					this.total = res.data
				})
			},

			tips(name) {
				let num = 0;
				switch (name) {
					case '退款审核':
						num = this.tipsCount.refundNum
						break;
					case '异常订单':
						num = this.tipsCount.riskOrderNum
						break;
					case '客诉处理':
						num = this.tipsCount.complaintsNum
						break;
					case '补货管理':
						num = this.tipsCount.outOfStockNum
						break;
					default:
						break;
				}
				return num
			},

			//常用功能角标数据
			getNum() {
				tipsCount().then(res => {
					this.tipsCount = res.data
				}).catch(err => {
					this.tipsCount = {
						refundNum: 0,
						complaintsNum: 0,
						outOfStockNum: 0,
						riskOrderNum: 0
					}
				})
			},

			getMenu(menu1, menu2) {
				let menu = []
				if (this.$store.state.permission.permissions_menu && this.$store.state.permission.permissions_menu != '[]') {
					let allMenu = JSON.parse(this.$store.state.permission.permissions_menu);
					if (allMenu.find(i => i.name == menu1) && allMenu.find(i => i.name == menu1).children) {
						let homeMenu = allMenu.find(i => i.name == menu1).children;
						if (homeMenu.find(i => i.name == menu2) && homeMenu.find(i => i.name == menu2).children) {
							menu = homeMenu.find(i => i.name == menu2).children
						}
					}
				}
				return menu
			},
			scan() {
				uni.scanCode({
					success: (res) => {
						let deviceId = res.result.split('=')[1]
						if (deviceId) {
							this.$tab.navigateTo(`/pages/activeDevice/bindDevice?id=${deviceId}`)
						} else {
							this.$modal.msg('该二维码无效~')
						}
					}
				});
			},
			tabChange(e) {
				this.current = e.id
				this.switchTotal(this.current)
			},
			menuClick(e) {
				if (e.name == '补货管理') {
					this.$tab.navigateTo('/pages/replenish/replenishmentManagement')
					return
				}
				if (e.name == '退款审核') {
					this.$tab.navigateTo('/pages/order/refundList')
					return
				}
				if (e.name == '异常订单') {
					this.$tab.navigateTo('/pages/order/riskOrder')
					return
				}
				if (e.name == '增加点位') {
					this.$tab.navigateTo('/pages/point/point')
					return
				}
				if (e.name == '商品建模') {
					this.$tab.navigateTo('/pages/commodity/auditList')
					return
				}
				if (e.name == '订单管理') {
					this.$tab.navigateTo('/pages/order/orderQuery')
					return
				}
				if (e.name == '销售统计') {
					this.$tab.navigateTo('/pages/globalPages/statistics')
					return
				}
				if (e.name == '设备统计') {
					this.$tab.navigateTo('/pages/globalPages/deviceStatistics')
					return
				}
				if (e.name == '激活管理') {
					this.$tab.navigateTo('/pages/activeDevice/deviceManage')
					return
				}
				if (e.name == '商品清单') {
					this.$tab.navigateTo('/pages/commodity/commoditylist')
					return
				}
				if (e.name == '常见错误码') {
					this.$tab.navigateTo('/pages/globalPages/errCode')
					return
				}
				this.$modal.msg('功能开发中，尽请期待~')
			},

			clearStor() {
				try {
					uni.clearStorageSync();
				} catch (e) {
					console.log(e)
				}
			},

			noticeClose() {
				this.noticeShow = false
			},

			goRisk() {
				if (this.tipsCount.riskOrderNum > 0) {
					this.$tab.navigateTo('/pages/order/riskOrder')
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	@keyframes rotate {
		from {
			transform: rotate(0deg);
		}

		to {
			transform: rotate(359deg);
		}
	}

	/deep/.u-notice-bar {
		padding: 9px 12rpx !important;
	}

	.container {
		padding-bottom: 88rpx;
		position: relative;

		.nav-style {
			font-size: 32rpx;
			font-weight: bold;
			color: #fff;
		}

		// 统计信息
		.total {
			width: 724rpx;
			margin-left: 14rpx;
			overflow: hidden;
			border-radius: 14rpx;
			background-color: #fff;
			margin-top: 10rpx;

			.total-sale {
				width: 724rpx;
				height: 205rpx;
				background: url('https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/home/home-bg.png') no-repeat left top;
				background-size: 100% 100%;
				position: relative;

				.sale-item {
					margin-top: 46rpx;
					padding-left: 30rpx;

					.sale-name {
						font-size: 26rpx;
						font-weight: 500;
						color: #EEEDFF;
						line-height: 26rpx;
					}

					.sale-num {
						line-height: 50rpx;
						font-size: 50rpx;
						font-weight: 800;
						color: #FFFFFF;
						margin-top: 55rpx;
					}
				}

				.sale-more {
					font-size: 26rpx;
					font-weight: 500;
					color: #FFFFFF;
					padding-right: 18rpx;
					position: absolute;
					right: 12rpx;
					top: 22rpx;
					line-height: 26rpx;

					image {
						width: 24rpx;
						height: 24rpx;
						position: absolute;
						right: 0;
						top: 0;
					}
				}

				.update {
					width: 50rpx;
					height: 50rpx;
					position: absolute;
					right: 16rpx;
					bottom: 16rpx;

					&.load {
						animation: rotate 1s linear;
						animation-iteration-count: 3;
					}

					image {
						width: 50rpx;
						height: 50rpx;
					}
				}
			}

			.refund-order {
				padding-left: 34rpx;
				overflow: hidden;
				padding-bottom: 26rpx;

				.refund-item {
					text-align: center;
					color: #2E2E2E;
					margin-top: 68rpx;

					&:nth-child(1) {
						margin-right: 70rpx;
					}

					.refund-num {
						font-size: 36rpx;
						font-weight: 800;
						line-height: 32rpx;
					}

					.refund-name {
						font-size: 28rpx;
						font-weight: 500;
						line-height: 28rpx;
						margin-top: 27rpx;
					}
				}
			}

			.notice {
				width: 100%;
				position: relative;

				// .notice-tips {
				// 	padding: 10rpx;
				// 	color: #fff;
				// 	font-size: 24rpx;
				// 	text-align: center;
				// 	position: absolute;
				// 	right: 56rpx;
				// 	top: 50%;
				// 	transform: translateY(-50%);
				// 	z-index: 999;
				// 	background-color: #fff;

				// 	>view {
				// 		width: 36rpx;
				// 		height: 36rpx;
				// 		background-color: #E60012;
				// 		border-radius: 36rpx;
				// 		line-height: 36rpx;
				// 	}
				// }

				.notice-tips {
					padding: 10rpx;
					text-align: center;
					position: absolute;
					right: 40rpx;
					top: 50%;
					transform: translateY(-54%);
					z-index: 999;
					background-color: #fff;
				}
			}
		}

		//常用菜单
		.useful-menu {
			width: 724rpx;
			margin-left: 14rpx;

			.useful-menu-item {
				margin-top: 20rpx;
				width: 352rpx;
				height: 168rpx;
				background: #FFFFFF;
				box-shadow: 0px 0px 10px 0px rgba(174, 201, 255, 0.2);
				border-radius: 14rpx;
				padding: 0 66rpx 0 46rpx;
				position: relative;

				>.image {
					width: 75rpx;
					height: 75rpx;
				}

				>.menu-name {
					font-size: 32rpx;
					font-weight: 500;
					color: #333333;
				}

				.tips {
					// width: 40rpx;
					// height: 40rpx;
					// background-color: #E60012;
					// color: #fff;
					// font-size: 24rpx;
					// text-align: center;
					// line-height: 28rpx;
					// border-radius: 40rpx;
					// position: absolute;
					// right: -8rpx;
					// top: -8rpx;
					// padding: 6rpx;

					width: 40rpx;
					height: 40rpx;
					position: absolute;
					right: -8rpx;
					top: -8rpx;
					padding: 6rpx;
				}
			}
		}

		// 便捷功能
		.menu {
			width: 724rpx;
			margin-left: 14rpx;
			background: #FFFFFF;
			box-shadow: 0px 0px 10rpx 0px rgba(174, 201, 255, 0.2);
			border-radius: 14rpx;
			display: flex;
			flex-flow: row nowrap;
			justify-content: space-around;
			margin-top: 20rpx;
			position: relative;
			padding: 101rpx 36rpx 0rpx;

			.menu-title {
				font-size: 28rpx;
				font-weight: 800;
				color: #000;
				position: absolute;
				left: 28rpx;
				top: 28rpx;
			}

			.menu-item {
				margin-bottom: 48rpx;
			}

			.menu-content {
				width: 100%;
				flex-flow: row wrap;
			}

			.menu-item {
				text-align: center;
				width: 25%;
				display: flex;
				flex-direction: column;
				align-items: center;

				>.image {
					width: 57rpx;
					height: 57rpx;
				}

				>view {
					color: #333;
					font-size: 24rpx;
					line-height: 24rpx;
					font-size: 28rpx;
					line-height: 28rpx;
					margin-top: 16rpx;
				}
			}
		}
	}
</style>