<template>
	<view class="container">
		<view class="vheigh"></view>
		<scroll-view class="content-box" scroll-y="true">
			<view class="user-section">
				<view class="user-info-box ">
					<view class="detail flex" @click="navTo('/pages/set/userinfo')">
						<view class="portrait-box">
							<image class="portrait" :src="shopAction.image || '/static/error/shopErroe.png'"></image>
						</view>
						<view class="info-box">
							<view class="username">{{ shopAction.name?shopAction.name:'载入中'}}</view>
							<view class="authentication success">已认证</view>
						</view>
					</view>
				</view>
			</view>
			<view class="cover-container">
				<view class="tj-sction" v-if="type==1">
					<view class="tj-item" @click="navTo('./money/wallet')">
						<text>支付金额</text>
						<text class="num">{{ orderNum.todayPrice || '0' }}</text>
						<text>昨日：{{orderNum.proPrice}}</text>
					</view>
					<!-- <view class="tj-item" @click="navTo('/pages/user/award/award')">
						<text>会员数</text>
						<text class="num">{{ orderNum.proPrice || '0' }}</text>
						<text>昨日：{{orderNum.proPrice}}</text>
					</view> -->
					<view class="tj-item" @click="navTo('/pages/user/scoreAccumulate')">
						<text>支付订单数</text>
						<text class="num">{{ orderNum.todayCount || '0' }}</text>
						<text>昨日：{{orderNum.proCount}}</text>
					</view>
				</view>
				<!-- 订单 -->
				<view class="item-box">
					<view class="box-title flex">
						<view class="title"><text>店铺订单</text></view>
						<!-- <view class="link" @click="navTo('/pages/shop/shopOrder?state=0')" hover-class="common-hover">
							<text>全部</text>
							<text class="iconfont iconenter"></text>
						</view> -->
					</view>
					<view class="order-section">
						<view class="order-item" @click="navTo('/pages/shop/shopOrder?state=0')"
							hover-class="common-hover" :hover-stay-time="50">
							<view class=" icon position-relative">
								<view class="numOrderItem font-color-gray">
									{{orderNum.unpaid_count}}
								</view>
							</view>
							<text>待付款</text>
						</view>
						<view class="order-item" @click="navTo('/pages/shop/shopOrder?state=1')"
							hover-class="common-hover" :hover-stay-time="50">
							<view class=" icon position-relative">
								<view class="numOrderItem font-color-red">
									{{orderNum.unshipped_count}}
								</view>
							</view>
							<text>待发货</text>
						</view>
						<view class="order-item" @click="navTo('/pages/shop/shopOrder?state=2')"
							hover-class="common-hover" :hover-stay-time="50">
							<view class="icon position-relative">
								<view class="numOrderItem font-color-gray">
									{{orderNum.received_count}}
								</view>
							</view>
							<text>已发货</text>
						</view>
						<!-- <view class="order-item" @click="navTo('/pages/shop/shopOrderAfter?state=3')"
							hover-class="common-hover" :hover-stay-time="50">
							<view class="icon position-relative">
								<view class="numOrderItem font-color-yellow">
									{{orderNum.refund_count}}
								</view>
							</view>
							<text>待售后</text>
						</view> -->
					</view>
				</view>
				<view class="listBox">
					<view class="list">
						<view class="flex listItem" @click="smsh">
							<view class="flex titleBox">
								<image class="listIconImg" src="../../static/icon/mySet.png" mode="widthFix"></image>
								<text class="title">核销</text>
							</view>
							<view class="right flex">
								<text></text>
								<!-- <image class="img" src="../../static/icon/next1.png" mode="widthFix"></image> -->
							</view>
						</view>
						<!-- <view class="flex listItem" @click="navTo('/pages/shop/repair/clientRepair')" v-if="type==1">
							<view class="flex titleBox">
								<image class="listIconImg" src="../../static/icon/myWx.png" mode="widthFix"></image>
								<text class="title">客户报修</text>
							</view>
							<view class="right flex" v-if="question>0">
								<text class="numbox">{{question}}</text>
							</view>
						</view> -->
						<!-- <view class="flex listItem" @click="navTo('/pages/shop/repair/repairHistory')">
							<view class="flex titleBox">
								<image class="listIconImg" src="../../static/icon/myWx.png" mode="widthFix"></image>
								<text class="title">维修历史</text>
							</view>
						</view> -->
						<!-- <view class="flex listItem" @click="navTo('/pages/set/set')">
							<view class="flex titleBox">
								<image class="listIconImg" src="../../static/icon/shopMoney.png" mode="widthFix">
								</image>
								<text class="title">店铺钱包</text>
							</view>
							<view class="right flex">
								<text></text>
							</view>
						</view> -->
						<!-- <view class="flex listItem" @click="navTo('/pages/shop/repair/taskClientRepair')">
							<view class="flex titleBox">
								<image class="listIconImg" src="../../static/icon/myWx.png" mode="widthFix"></image>
								<text class="title">维修任务</text>
							</view>
							<view class="right flex" v-if="tesk>0">
								<text class="numbox">{{tesk}}</text>
							</view>
						</view> -->
						<!-- <view class="flex listItem" @click="navTo('/pages/set/set')">
							<view class="flex titleBox">
								<image class="listIconImg" src="../../static/icon/myServer.png" mode="widthFix"></image>
								<text class="title">联系平台</text>
							</view>
							<view class="right flex">
								<text></text>
							</view>
						</view> -->
						<view class="flex listItem" @click="navTo('/pages/set/set')">
							<view class="flex titleBox">
								<image class="listIconImg" src="../../static/icon/mySet.png" mode="widthFix"></image>
								<text class="title">设置</text>
							</view>
							<view class="right flex">
								<text></text>
								<!-- <image class="img" src="../../static/icon/next1.png" mode="widthFix"></image> -->
							</view>
						</view>
					</view>
				</view>
				<view class="tapBottom" @click="changeTab">
					前往用户界面
				</view>
			</view>
		</scroll-view>
		<!-- 核销弹窗 -->
		<uni-popup ref="popuphx" class="agree-wrapper">
			<view class="hx-wrapper">
				<view class="hx-img">
					<image src="../../static/img/hxbg.png" mode=""></image>
				</view>
				<view class="hx-body">
					<view class="nav-hx">
						<view class="action" style="text-align: center;">
							{{hxtype == 0?'商品核销':(hxtype == 1 ? '电池核销': '升压器核销') }}</view>
					</view>
					<!-- <view class="hx-title">
						核销码
					</view> -->
					<input type="text" v-model="code" placeholder="请输入核销码" placeholder-class="hx-placeholder" />
					<view class="flex dcm" v-if="hxtype == 1 || hxtype == 2">
						<input type="text" v-model="dcode" :placeholder="hxtype == 1? '请输入电池编号': '请输入升压器编号'"
							placeholder-class="hx-placeholder" style="margin-bottom: 0;" />
						<image src="../../static/icon/scend.png" mode="widthFix" style="width: 50rpx;" @click="smdc">
						</image>
					</view>
					<view class="hx-btn" @click="qhx">
						立即核销
					</view>
				</view>
				<view class="hx-close" @click="close">
					<image src="../../static/icon/close.png" mode=""></image>
				</view>
			</view>
		</uni-popup>
	</view>
</template>
<script>
	// #ifdef H5
	import weixinObj from "@/plugin/jweixin-module/index.js";
	// #endif
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		statistics,
		funds,
		question,
		adminStore,
		task
	} from '@/api/shop.js';
	import {rentVerific,orderVerific} from '@/api/index.js'
	import {
		saveUrl,
		interceptor
	} from '@/utils/loginUtils.js';
	export default {
		data() {
			return {
				hxtype: 0,
				code: '',
				dcode: '',
				type: 0, //0为数据未加载,1为门店2为店员
				orderNum: {
					unpaid_count: 0, //待支付
					unshipped_count: 0, //待发货
					received_count: 0, //待收获
					refund_count: 0, //待售后
					todayPrice: 0, //今日成交额
					todayCount: 0, //今日订单数
					proPrice: 0, //昨日成交额
					proCount: 0, //昨日订单数
				},
				question: 0, //维修数量
				tesk: 0, //维修任务数量
			};
		},
		computed: {
			...mapState('user', ['hasLogin']),
			...mapState('shop', ['shopAction'])
		},
		created() {
			this.type = this.shopAction.type
		},
		methods: {
			...mapMutations('shop', ['setShopAction']),
			smdc() {
				let that = this
				// #ifndef H5
				uni.scanCode({
					autoDecodeCharset: true,
					success: (res) => {
						if (that.hxtype == 1) {
							console.log(res);
							let str = res.result.split('编号：')
							this.dcode = str[1]
						} else {
							this.dcode = res.result
						}

					}
				})
				// #endif
			},
			qhx() {
				let that = this
				if (that.hxLoading) {
					return
				}
				if (that.code == '') {
					return that.$api.msg('请输入核销码')
				}
				if (that.hxtype == 0) {
					that.hxLoading = true
					orderVerific({
						verify_code: that.code,
						is_confirm: 1
					}).then(res => {
						uni.showToast({
							title: '核销成功',
							mask: true,
							duration: 1500
						});
						that.close()
						that.hxLoading = false
					}).catch(err => [
						that.hxLoading = false
					])
				} else {
					if (that.dcode == '') {
						return that.$api.msg('请输入编号')
					}
					that.hxLoading = false
					let qdata = {
						verify_code: that.code,
					
						is_confirm: 1
					}
					if(that.hxtype == 1) {
						qdata.battery_number = that.dcode
					}else {
						qdata.booster_number = that.dcode
					}
					rentVerific(qdata).then(res => {
						uni.showToast({
							title: '核销成功',
							mask: true,
							duration: 1500
						});
						that.close()
						that.hxLoading = false
					})
				}
			},
			openHx() {
				this.$refs.popuphx.open()
			},
			close() {
				this.code = ''
				this.dcode = ''
				this.$refs.popuphx.close()
			},
			// 扫码
			smsh() {
				let that = this
				uni.showActionSheet({
					itemList: ['商品核销', '电池核销', '升压器核销'],
					success: function(res) {
						console.log(res, '上传')
						that.hxtype = res.tapIndex
						console.log();
						that.openHx();
						// #ifdef H5
						try {
							weixinObj.scanQRCode({
								needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
								scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
								success: function(res) {
									var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
									let arr = result.split('&');
									if (that.hxtype == 0) {
										that.code = arr[0];
									} else {
										that.code = arr[1]
										that.dcode = arr[2]
									}
									that.openHx();
								},
								false(err) {
									console.log('shib');
									that.openHx();
								}
							});
						} catch (err) {
							that.openHx();
						}

						// #endif

						// #ifndef H5
						uni.scanCode({
							success: (res) => {
								let result = res.result
								let arr = result.split('&')
								if (that.hxtype == 0) {
									that.code = arr[0];
								} else {
									that.code = arr[1]
									that.dcode = arr[2]
								}
								that.openHx();
							}
						})
						// #endif
					},
					fail: function(res) {
						// erro(res)

						console.log(res.errMsg);
					}
				});







			},
			// 重新加载数据
			getInit() {
				const that = this;
				// 订单数
				statistics().then((e) => {
					that.orderNum = e.data
				})
				// 门店资金
				funds().then((e) => {
					that.name = e.data.name;
					that.logo = e.data.image;
					// 保存商店数据
					if (e.data.address) {
						e.data.type = 1;
						that.setShopAction(e.data);
					} else {
						e.data.type = 2;
						adminStore().then((ar) => {
							that.setShopAction(ar.data);
						}).catch((e) => {
							console.log(e);
						})
					}
					that.type = e.data.type;
				})
				// 查询店长客户报修数量
				question().then((e) => {
					that.question = e.data.count
				})
				// 查询维修任务数量
				task().then((e) => {
					that.tesk = e.data.count;
				}).catch((e) => {
					console.log(e);
				})

			},
			// 界面切换
			changeTab() {
				this.$emit('tab', false)
			},
			navTo(url) {
				if (!this.hasLogin) {
					// 保存地址
					saveUrl();
					// 登录拦截
					interceptor();
				} else {
					uni.navigateTo({
						url
					});
				}
			},
		}
	};
</script>
<style lang="scss">
	page {
		height: 100%;
	}

	.tapBottom {
		margin: 0 50rpx;
		text-align: center;
		color: #FFFFFF;
		border: 1px solid $color-green;
		background-color: $color-green ;
		line-height: 1;
		padding: 25rpx;
		border-radius: 100rpx;
		margin-top: 50rpx;
	}

	.listBox {
		margin-top: 20rpx;
		background-color: #FFFFFF;
		border-radius: 20rpx;
		overflow: hidden;
	}

	.list {
		.listItem {
			padding: 35rpx 40rpx;
			border-bottom: 1px solid $page-color-light;
		}

		.listIconImg {
			width: 36rpx;
			height: 36rpx;
		}

		.right {
			.img {
				width: 26rpx;
			}

			.numbox {
				background-color: $color-red;
				color: #FFFFFF;
				font-size: 22rpx;
				border-radius: 100rpx;
				min-width: 36rpx;
				height: 36rpx;
				line-height: 36rpx;
				text-align: center;
				padding: 0 5rpx;
			}
		}

		.titleBox {
			.title {
				padding-left: 20rpx;
				color: $font-color-base;
				font-size: $font-base;
			}
		}
	}

	%flex-center {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	%section {
		display: flex;
		justify-content: space-around;
		align-content: center;
		border-radius: 10rpx;
	}

	.container {
		height: 100%;
		background-color: $page-color-base;
	}

	.content-box {
		height: 100%;
	}

	.vheigh {
		height: var(--status-bar-height);
	}

	.user-section {
		padding: 15rpx 30rpx 0;
		position: relative;
	}

	.user-info-box {
		height: 180rpx;
		color: white;
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: relative;
		z-index: 1;

		.detail {
			height: 130rpx;

			.portrait-box {
				height: 100%;

				.portrait {
					width: 130rpx;
					height: 100%;
					border: 5rpx solid #fff;
					border-radius: 50%;
				}
			}

			.info-box {
				margin-left: 20rpx;
				line-height: 1.5;

				.username {
					font-size: 40rpx;
					color: $font-color-dark;
					font-weight: bold;
					height: 100%;
				}

				.authentication {
					border: 2rpx solid #FF4241;
					border-radius: 20px;
					padding: 0 25rpx;
					font-size: 24rpx;
					color: #FF4241;
					float: left;

					&.success {
						border: 2rpx solid $color-green;
						color: $color-green;
					}
				}
			}
		}
	}

	.cover-container {
		padding: 0 30rpx;
		position: relative;
		padding-bottom: 20rpx;
	}

	.tj-sction {
		@extend %section;

		.tj-item {
			@extend %flex-center;
			flex-direction: column;
			height: 100rpx;
			font-size: $font-sm;
			color: $font-color-light;
		}

		.num {
			font-size: $font-base;
			color: $font-color-dark;
			margin-bottom: 8rpx;
			font-weight: bold;
		}
	}

	.item-box {
		border-radius: 20rpx;
		background-color: white;
		margin-top: 20rpx;

		.box-title {
			line-height: 1;
			padding: 30rpx;

			.title {
				font-size: $font-lg;
				font-weight: bold;
			}

			.link {
				font-size: $font-base - 2rpx;
				color: $font-color-light;
			}
		}

		.order-section {
			@extend %section;
			padding: 28rpx 0;
			padding-top: 0;

			.order-item {
				@extend %flex-center;
				width: 120rpx;
				height: 120rpx;
				border-radius: 10rpx;
				font-size: $font-sm;
				color: $font-color-dark;

				.numOrderItem {
					font-size: 36rpx;
					font-weight: bold;
					text-align: center;
				}
			}

			.iconfont {
				font-size: 48rpx;
				margin-bottom: 18rpx;
				color: #fa436a;
			}

			.icon-shouhoutuikuan {
				font-size: 44rpx;
			}

			.icon {
				height: 50rpx;
				width: 48rpx;
				margin-bottom: 18rpx;
				background-size: 100%;
				background-repeat: no-repeat;
				background-position: center;

				.icon-img {
					width: 100%;
					height: 100%;
				}
			}
		}
	}

	.history-section {
		// padding: 30rpx 0 0;
		margin-top: 20rpx;
		background: #fff;
		border-radius: 10rpx;

		.h-list {
			white-space: nowrap;
			padding: 30rpx 30rpx 0;

			.h-list-image {
				display: inline-block;
				width: 160rpx;
				height: 160rpx;
				margin-right: 20rpx;
				border-radius: 10rpx;
			}
		}
	}

	.hx-wrapper {
		width: 536rpx;
		// height: 630rpx;
		position: relative;
		padding-bottom: 40rpx;
		background-color: #fff;
		border-radius: 20rpx;

		.hx-img {
			width: 536rpx;
			height: 281rpx;

			image {
				width: 536rpx;
				height: 281rpx;
			}
		}

		.hx-close {
			position: absolute;
			left: 243rpx;
			bottom: -80rpx;
			width: 52rpx;
			height: 52rpx;

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

		.hx-body {
			width: 536rpx;
			// min-height: 349rpx;
			background-color: #fff;
			border-radius: 0 0 10rpx 10rpx;
			padding-top: 40rpx;

			.hx-title {
				width: 536rpx;
				font-size: 36rpx;
				font-weight: 500;
				color: #333333;
				line-height: 1;
				padding-top: 42rpx;
				text-align: center;
				padding-bottom: 10rpx;
			}

			input {
				width: 439rpx;
				height: 68rpx;
				background: #f1faf6;
				border-radius: 10rpx;
				margin: 0 auto 40rpx;
				padding-left: 26rpx;

				.hx-placeholder {
					font-size: 26rpx;
					font-weight: 500;
					color: $base-color;
				}
			}

			.hx-btn {
				margin: 44rpx auto 0;
				width: 353rpx;
				height: 71rpx;
				background: $base-color;
				border-radius: 34rpx;
				font-size: 36rpx;
				font-weight: 500;
				color: #F8F9F9;
				line-height: 71rpx;
				text-align: center;
			}


		}
	}

	.dcm {
		width: 439rpx;
		margin: 0 auto;
		align-items: center;
		height: 68rpx;

		input {
			flex-grow: 1;
			height: 68rpx;
			background: #f1faf6;
			border-radius: 10rpx;
			margin: auto 10rpx auto 0;
			padding-left: 26rpx;
		}

		image {
			flex-shrink: 0;
		}
	}
	.nav-hx {
		display: flex;
		justify-content: center;
		margin-bottom: 20rpx;
		text {
			font-size:28rpx;
			padding: 10rpx;
		}
		.action {
			color: $base-color;
			font-weight: bold;
			
		}
	}
</style>