<template>
	<view class="page">
		<view class="swiper-container">
			<view class="swiper-page" v-show="currentIndex == 0">
				<view class="title">
					<view class="title-sreach">
						<view class="title-name">
							商家中心
						</view>
					</view>
				</view>
				<view class="shop-title">
					<view class="title1">
						<img :src="shop.shopIcon" alt="" />
					</view>
					<view class="title2">
						<view class="title22" @tap="checkArea">
							{{shop.shopName}}
							<uni-icons type="right"></uni-icons>
						</view>
						<view class="title33" @tap="updateStatus">
							{{businessStatus == '1'?'暂停营业':'正常营业'}}
							<uni-icons type="right" size="12"></uni-icons>
						</view>
					</view>
				</view>
				<view class="shop-order">
					<view class="order1">
						待办事项
					</view>
					<view class="order2">
						<view class="order22">
							<view class="order33" @tap="changeIndex(1,13)">
								<view class="order44">
									{{daichucan}}
								</view>
								<view class="order55">
									待出餐
								</view>
							</view>
							<view class="order33" @tap="changeIndex(1,4)">
								<view class="order44">
									{{daiquhuo}}
								</view>
								<view class="order55">
									待取货
								</view>
							</view>
							<view class="order33" @tap="changeIndex(1,5)">
								<view class="order44">
									{{daisongda}}
								</view>
								<view class="order55">
									待送达
								</view>
							</view>
						</view>
						<view class="order22">
							<view class="order33" @tap="changeIndex(1,2)">
								<view class="order44">
									{{daixiaofei}}
								</view>
								<view class="order55">
									待消费
								</view>
							</view>
							<view class="order33" @tap="goDetail('tui',11)">
								<view class="order44">
									{{shopData.refundCount == undefined?0:shopData.refundCount}}
								</view>
								<view class="order55">
									待退款/售后
								</view>
							</view>
							<view class="order33" @tap="goDetail('pj','cha')">
								<view class="order44">
									{{shopData.pendingBadReviewCount == undefined?0:shopData.pendingBadReviewCount}}
								</view>
								<view class="order55">
									待回复差评
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="shop-order">
					<view class="order1" style="display: flex;">
						<view style="flex: 1;">
							今日实时数据
						</view>
						<view @tap="goDetail('tj')">
							更多
							<uni-icons type="right" size="12"></uni-icons>
						</view>
					</view>
					<view class="data2">
						<view class="data22">
							<view class="data222">
								{{shopData.orderRevenueAndCount == undefined ?0:shopData.orderRevenueAndCount.count}}
							</view>
							<view class="data333">
								今日订单
							</view>
						</view>
						<view class="data22">
							<view class="data222">
								{{shopData.orderRevenueAndCount == undefined ?0:shopData.orderRevenueAndCount.amount == undefined ?0:shopData.orderRevenueAndCount.amount}}
							</view>
							<view class="data333">
								营业额
							</view>
						</view>
					</view>
				</view>
				<view class="shop-order">
					<view class="order1">
						必备工具
					</view>
					<view class="menu2">
						<view class="menu22" @tap="goDetail('sp')">
							<view class="menu33">
								<image src="../../static/image/dingdan/dd0.png" alt="" />
							</view>
							<view class="menu44">
								商品管理
							</view>
						</view>
						<!-- <view class="menu22" @tap="goDetail('dp')">
							<view class="menu33">
								<image src="../../static/image/dingdan/dd1.png" alt="" />
							</view>
							<view class="menu44">
								店铺管理
							</view>
						</view> -->
						<view class="menu22" @tap="changeIndex(1)">
							<view class="menu33">
								<image src="../../static/image/dingdan/dd2.png" alt="" />
							</view>
							<view class="menu44">
								订单管理
							</view>
						</view>
						<view class="menu22" @tap="goDetail('zd')">
							<view class="menu33">
								<image src="../../static/image/dingdan/dd3.png" alt="" />
							</view>
							<view class="menu44">
								账单结算
							</view>
						</view>
						<view class="menu22" @tap="goDetail('pj')">
							<view class="menu33">
								<image src="../../static/image/dingdan/dd4.png" alt="" />
							</view>
							<view class="menu44">
								顾客评价
							</view>
						</view>
						<view class="menu22" @tap="goDetail('hx')">
							<view class="menu33" style="width: 100%;height: 56rpx;">
								<uni-icons type="scan" size="25"></uni-icons>
							</view>
							<view class="menu44">
								扫码核销
							</view>
						</view>
						<view class="menu22" @tap="goDetail('dy')">
							<view class="menu33" style="width: 100%;height: 56rpx;">
								<image src="../../static/image/dingdan/dd1.png" alt="" />
							</view>
							<view class="menu44">
								打印设置
							</view>
						</view>
					</view>
					<view style="width: 100%;height: 200rpx;"></view>
				</view>
			</view>
			<view class="swiper-page" v-show="currentIndex == 1">
				<de-livery ref="deLivery"></de-livery>
			</view>
			<view class="swiper-page" v-show="currentIndex == 2">
				<my-center ref="myCenter"></my-center>
			</view>
		</view>
		<!-- 弹出区域选择 -->
		<uni-popup ref="areaPopup" background-color="#fff">
			<view class="popup-area-content">
				<view class="popup-area-title">
					<text>请选择店铺</text>
				</view>
				<view class="popup-area-container" v-for="(item,index) in areaList" :key="index" @tap="onClickArea(item)">
					<text style="display: inline-block">{{item.shop.shopName}}</text>
				</view>
			</view>
		</uni-popup>
		<tab-bar :width="width" @tab-index="changeIndex" :currentIndex="currentIndex"></tab-bar>
	</view>
</template>

<script>
	import tabBar from "@/components/tab-bar/tab-bar.vue";
	import deLivery from "@/components/tab-bar/delivery.vue";
	import myCenter from "@/components/tab-bar/myCenter.vue";
	export default {
		data() {
			return {
				width: 0,
				currentIndex: 0,
				businessStatus:1,
				shop:{},
				areaList:[],
				shopList: [],
				shopData:{},
				daixiaofei:0,
				daichucan:0,
				daiquhuo:0,
				daisongda:0
			}
		},
		components: {
			tabBar,
			deLivery,
			myCenter
		},
		onShow() {
			this.indexInit()
		},
		onLoad() {
			
		},
		methods: {
			indexInit(){
				let that = this
				uni.getSystemInfo({
					success: function(info) {
						that.width = info.screenWidth * 2;
					}
				});
				this.getData()
				this.getBusinessStatus()
				if(!uni.getStorageSync('area')){
					this.checkArea()
				}
			},
			checkArea(){
				this.NB.sendRequest("/auth/getShopByUser", {}, false, 'post', 'application/x-www-form-urlencoded').then((res) => {
					if (res.code == 200) {
						this.areaList = res.result
						this.$refs.areaPopup.open()
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
				
				})
			},
			//获取营业状态
			getBusinessStatus(){
				this.NB.sendRequest("/app/shoptakeaway/getByShopId", {
					shopId: uni.getStorageSync('shopId')
				}, false, 'GET', 'application/x-www-form-urlencoded').then((res) => {
					if (res.code == 200) {
						this.shop = res.result
						this.shop.shopIcon = JSON.parse(uni.getStorageSync('area')).shop.shopIcon
						this.shop.shopName = JSON.parse(uni.getStorageSync('area')).shop.shopName
						this.businessStatus = res.result.status
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
					uni.hideLoading()
				}).catch((res) => {})
			},
			onClickArea(item){
				
				uni.setStorageSync('area',JSON.stringify(item))
				this.getBusinessStatus()
				this.getData()
				
				this.$refs.areaPopup.close()
			},
			//更改营业状态
			updateStatus(){
				let that = this;
				uni.showModal({
					title: '提示',
					content: '要更改营业状态吗？',
					success: function (res1) {
						if (res1.confirm) {
							that.NB.sendRequest("/app/shoptakeaway/update", {
								shopId: uni.getStorageSync('shopId'),
								status:that.businessStatus == "1"?'0':that.businessStatus == "0"?'1':'2'
							}, false, 'post', 'application/x-www-form-urlencoded').then((res) => {
								if (res.code == 200) {
									uni.showToast({
										title: '更改成功',
										icon:'none'
									});
									that.businessStatus = that.businessStatus == "1"?'0':that.businessStatus == "0"?'1':'2'
								} else {
									uni.showToast({
										title: res.message,
										icon:'none'
									});
								}
								uni.hideLoading()
							}).catch((res) => {})
						}
					}
				});
				
			},
			goDetail(type,index) {
				let url = ''
				switch (type) {
					case 'tj':
						url = '/pages/shop/dataStatistics'
						break;
					case 'db':
						url = '/pages/shop/shopOrderList?index=' + index
						break;
					case 'sp':
						url = '/pages/shop/goodsList'
						break;
					case 'dp':
						url = ''
						break;
					case 'dd':
						url = '/pages/shop/shopOrderList'
						break;
					case 'zd':
						url = '/pages/shop/shopBill'
						break;
					case 'pj':
						let item = {
							shopScore:uni.getStorageSync('shopScore'),
							id:uni.getStorageSync('shopId')
						}
						url = '/pages/shop/shopEvaluate?item=' + JSON.stringify(item) + '&isMerchant=1&isCha=' + (index =='cha'?1:0)
						break;
					case 'tui':
						uni.navigateTo({
							url: '/pages/shop/shopReturnOrder?type=shop'
						})
						break;
					case 'hx':
						this.scanCodeAdd()
						break;
					case 'dy':
						url = '/pages/myCenter/setPrint'
						break;
					default:
				}
				uni.navigateTo({
					url: url
				})
			},
			getData(){
				let that = this;
				that.NB.sendRequest("/mall/order/countByShop/"+uni.getStorageSync('shopId'), {}, false, 'GET', 'application/x-www-form-urlencoded').then((res) => {
					if (res.code == 200) {
						this.shopData = res.result
						for(let i=0;i<this.shopData.orderStatusCount.length;i++){
							if(this.shopData.orderStatusCount[i].counttype){
								if(this.shopData.orderStatusCount[i].counttype == 'daisong'){
									this.daisongda = this.shopData.orderStatusCount[i].count
								}else if(this.shopData.orderStatusCount[i].counttype == 'daiqu'){
									this.daiquhuo = this.shopData.orderStatusCount[i].count
								}else if(this.shopData.orderStatusCount[i].counttype == 'daixiaofei'){
									this.daixiaofei = this.shopData.orderStatusCount[i].count
								}else if(this.shopData.orderStatusCount[i].counttype == 'daichucan'){
									this.daichucan = this.shopData.orderStatusCount[i].count
								}
							}
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
					uni.hideLoading()
				}).catch((res) => {})
			},
			//扫一扫功能
			scanCodeAdd() {
				let that = this;
				uni.scanCode({
					success: (res) => {
						if (res.scanType == "QR_CODE") { //二维码
							let shopId = res.result.split('@@')[1]
							if (shopId == uni.getStorageSync('shopId')) {
								let productId = res.result.split('@@')[0]
								uni.showModal({
									title: '提示',
									content: '确定要核销该订单吗？',
									success: function (res1) {
										if (res1.confirm) {
											that.NB.sendRequest("/mall/order/complete", {
												orderId: productId
											}, false, 'post', 'application/x-www-form-urlencoded').then((res) => {
												if (res.code == 200) {
													uni.showToast({
														title: '核销成功',
														icon: 'none'
													})
												} else {
													uni.showToast({
														title: res.message,
														icon: 'none'
													})
												}
												uni.hideLoading()
											}).catch((res) => {})
										}
									}
								});
								
								
							} else {
								uni.showToast({
									title: '未查询到此订单信息，请核实！',
									icon: 'none'
								})
							}
						}
					},
					fail: (res) => {
						uni.showToast({
							title: '扫描失败',
							icon: 'none'
						})
					}
				})
			},
			//底部tab切换组件状态改变
			changeIndex(index,type) {
				let that = this;
				this.lastIndex = this.currentIndex
				this.currentIndex = index
				switch (index) {
					case 0:
						this.indexInit()
						break;
					case 1:
					
						that.$refs.deLivery.init(type)
						break;
					case 2:
						that.$refs.myCenter.init()
						break;
				}

			}
		}
	}
</script>

<style lang="scss">
	@import url("./index.css");
	.title {
		background: url('https://jewel-shop.oss-cn-beijing.aliyuncs.com/8bc15960c2dc40268e295d6dd23aecce.png') no-repeat;
		width: 100%;
		height: 20%;
	}
	
	.title-sreach {
		width: 100%;
		display: flex;
		height: 200rpx;
		position: relative;
	}
	
	.title-name {
		padding-top: 110rpx;
		font-size: 36rpx;
		font-weight: 700;
		flex: 1;
		text-align: center;
	}
	
	.shop-title {
		width: 95%;
		margin: 0 auto;
		padding: 20rpx;
		background: #fff;
		border-radius: 20rpx;
		display: flex;
		font-size: 28rpx;
		font-weight: 700;
	}
	
	.title1 {
		width: 70rpx;
		height: 70rpx;
	
		img {
			width: 100%;
			height: 100%;
			border-radius: 10rpx;
		}
	}
	.title2{
		    flex: 1;
		    line-height: 70rpx;
		    display: flex;
	}
	.title22{
		    flex: 1;
		    padding-left: 20rpx;
	}
	.shop-order{
		width: 95%;
		margin: 20rpx auto 0;
		background: #fff;
		border-radius: 20rpx;
		padding: 0 20rpx 0;
	}
	.order1{
		height: 70rpx;
		line-height: 70rpx;
		font-size: 28rpx;
		font-weight: 700;
		border-bottom: 1px solid #eee;
	}	
	.order22{
		display: flex;
		height: 100rpx;
		padding-top: 20rpx;
	}
	.order33{
		flex: 1;
		text-align: center;
	}
	.order44{
		font-size: 28rpx;
		font-weight: 700;
	}
	.data2{
		    display: flex;
		    height: 140rpx;
		    text-align: center;
		    padding-top: 20rpx;
	}
	.data22{
		flex: 1;
	}
	.data222{
		    font-size: 36rpx;
		    font-weight: 700;
		    padding-bottom: 20rpx;
	}
	.menu2{
		display: flex;
		flex-wrap: wrap;
		text-align: center;
		padding-top: 20rpx;
	}
	.menu22{
		width: 25%;
		    height: 120rpx;
	}
	.menu33{
		image{
			width: 50rpx;
			    height: 50rpx;
			background-size: 100%;
		}
	}
	.menu44{
		height: 40rpx;
		line-height: 40rpx;
	}
	.popup-area-content {
		align-items: center;
		justify-content: center;
		padding: 30rpx;
		width: 500rpx;
		height: auto;
		background-color: #fff;
	}
	
	.popup-area-title {
		font-size: 36rpx;
		font-weight: bold;
		text-align: center;
		position: relative;
	}
	
	.popup-area-container {
		margin-top: 20rpx;
		height: 100rpx;
		border-radius: 20rpx;
		background: #088FEB;
		line-height: 100rpx;
		text-align: center;
		border-bottom: 1px solid #eee;
		border-top: 1px solid #eee;
		color: #fff;
	}
</style>