<template>
	<view class="page">
		<u-navbar title="我的店铺" :border-bottom="false" :background="bgc" title-color='#fff' title-size='36'
			back-icon-color="#fff" height='50' :custom-back="btns"></u-navbar>
		<view class="box">
			<view class="serch">
				<view class="lt">
					<image src="https://api.ccttiot.com/smartmeter/img/static/uaG7R4JLfYOzBhWfDN0j" mode=""></image><input type="text" v-model="keyword" placeholder="搜索门店"/> <view class="sousuo" @click="searchs">搜索</view>
				</view>
			</view>
			<view class="list" @scrolltolower="onReachBottom">
				<view class="listitem" v-for="(item,index) in wateringList" :key="index" @click="btnshopxq(item)">
					<view class="toptit">
						<image src="https://api.ccttiot.com/smartmeter/img/static/uFKbeneDcYbmekpVuWHm" mode="aspectFit"></image>
						<view class="tit">
							{{item.name}}
						</view>
						<view class="yuan" v-if="item.status == 3" style="color: #808080;" @click.stop="btnzsone(item.storeId,item.status)">
							<text style="width: 9rpx;height: 9rpx;border-radius: 50%;margin-right: 5rpx;background-color: #808080;display: inline-block;"></text>申请店铺在地图上展示<u-icon name="arrow-right" color="#808080" size="28"></u-icon>
						</view>
						<view class="yuan" v-if="item.status == 2" style="color: #FFC052;" @click.stop="btnzstwo">
							<text style="width: 9rpx;height: 9rpx;border-radius: 50%;margin-right: 5rpx;background-color: #FFC052;display: inline-block;"></text>申请审核中<u-icon name="arrow-right" color="#FFC052" size="28"></u-icon>
						</view>
						<view class="yuan" v-if="item.status == 4" style="color: #FF5252;" @click.stop="btnbhcx(item.storeId,index,item)">
							<text style="width: 9rpx;height: 9rpx;border-radius: 50%;margin-right: 5rpx;background-color: #FF5252;display: inline-block;"></text>申请已驳回<u-icon name="arrow-right" color="#FF5252" size="28"></u-icon>
						</view>
						<view class="yuan" v-if="item.status == 1" style="color: #8883F0;" @click="btnzsone(item.storeId,item.status)">
							<text style="width: 9rpx;height: 9rpx;border-radius: 50%;margin-right: 5rpx;background-color: #8883F0;display: inline-block;"></text>店铺已在地图展示<u-icon name="arrow-right" color="#8883F0" size="28"></u-icon>
						</view>
					</view>
					<view class="rishou">
						<view class="rishouwz">
							<text> 今日订单 </text><text> 今日收入 </text><text> 上月收入</text><text>设备状态 </text>
						</view>
						<view class="rishouje">
							<text>￥{{item.todayOrderCount == null ? '--' : item.todayOrderCount}}</text>
							<text>￥{{item.todayIncome == null ? '--' : item.todayIncome}}</text>
							<text>￥{{item.lastMonthIncome == undefined ? '--' : item.lastMonthIncome}}</text>
							<text> <text style="color: #8883F0;">{{item.onlineCount == undefined ? '--' : item.onlineCount}}/</text> {{item.offlineCount == undefined ? '--' : item.offlineCount}}</text>
						</view>
					</view>
					<view class="gzlist">
						<view class="icons">
							<image src="https://api.ccttiot.com/smartmeter/img/static/udXAKRzcop7KDOFiWVY3" mode="" @click.stop="btndpsb(item.storeId)"></image>
							<image src="https://api.ccttiot.com/smartmeter/img/static/uVj4z4rdIZGK0sVcMlHi" style="width: 116rpx;" mode="aspectFit"
								@click.stop="nav(item)"></image>
							<image src="https://api.ccttiot.com/smartmeter/img/static/u9IYNLl7G83pOXtQbaYj" mode="aspectFit"
								@click.stop="btnpag(2,item)"></image> <!-- 4 -->
							<image src="https://api.ccttiot.com/smartmeter/img/static/uVYNIcmP5ytOhhfpasrt" style="width: 116rpx;" mode="aspectFit"
								@click.stop="btnpag(5,item)"></image>
							<image src="https://api.ccttiot.com/smartmeter/img/static/uBpiT22obGqJgdhuA9uj" mode="aspectFit"
								@click.stop="btnsm(item.storeId)"></image>
						</view>
					</view>
				</view>
				<view class="" v-if="showflag" style="width: 100%;height: 200rpx;margin: auto;margin-top: 170rpx;text-align: center;">
					<image style="width: 200rpx;height: 200rpx;"
						src="https://api.ccttiot.com/smartmeter/img/static/uZFUpcz0YZZ4f4RjvGg2" mode="aspectFit"></image>
					<view class="" style="font-size: 28rpx;color: #808080;margin-top: 30rpx;">暂无更多店铺...</view>
				</view>
			</view>
		</view>
		<!-- 审核被驳回 -->
		<view class="noticetc" v-if="bohuiflag">
			<image src="https://api.ccttiot.com/smartmeter/img/static/udODVn4zCbsagM2QBoVm" class="bg" mode=""></image>
			<view class="name">
				驳回原因
			</view>
			<u-parse :html="fuwenben" style="width: 100%;height: 620rpx;background-color: #efefef;padding: 10rpx;box-sizing: border-box;margin-top: 20rpx;"></u-parse>
			<view class="jy">
				建议完善后重新申请
			</view>
			<view class="btn" @click="btnzx">
				我已知晓
			</view>
		</view>
		<view class="mask" v-if="bohuiflag"></view>
		<view class="anniu" @click="creadshop" v-if="types != 11">
			<view class="btn">
				新建店铺
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: " #8883F0",
				},
				pagenum: 1,
				wateringList: [],
				pagesize: 10, // 一页多少数据
				isLoading: false, // 是否正在加载数据  
				noMoreData: false, // 是否没有更多数据  
				total: 0,
				keyword: '',
				showflag: false,
				types:null,
				bohuiflag:false,
				bohuiid:'',
				bohuiindex:'',
				fuwenben:''
			}
		},
		onLoad(option) {
           if(option.types){
			   this.types = option.types
		   }
		},
		// 分享到好友（会话）
		onShareAppMessage: function () {  
		   return {  
		     title: '创想物联',  
		     path: '/pages/shouye/index'  
		   }  
		},  
		// 分享到朋友圈  
		onShareTimeline: function () {  
		   return {  
		     title: '创想物联',  
		     query: '',  
		     path: '/pages/shouye/index'  
		   }  
		},
		onShow() {
			this.pagenum = 1
			this.wateringList = []
			this.getlist()
		},
		methods: {
			// 点击我已知晓
			btnzx(){
				this.$u.put(`/app/store/readReject?storeId=${this.bohuiid}`).then((res) => {
					if (res.code == 200) {
						this.bohuiflag = false
						this.wateringList[this.bohuiindex].status = 3
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration:2000
						})
					}
				})
			},
			// 点击查看驳回详情
			btnbhcx(id,index,item){
				this.bohuiflag = true
				this.bohuiid = id
				this.bohuiindex = index
				this.fuwenben = item.apply.verifyRemark
			},
			// 点击扫一扫绑定
			btnsm(storeId){
				console.log(storeId);
				uni.scanCode({
					onlyFromCamera: true,
					scanType: ['qrCode'],
					success: res => {
						function getQueryParam(url, paramName) {
							let regex = new RegExp(`[?&]${paramName}=([^&]*)`)
							let results = regex.exec(url);
							return results ? decodeURIComponent(results[1].replace(/\+/g, ' ')) : null
						}
						let sceneValue = res.result
						let decodedValue = decodeURIComponent(sceneValue)
						let id = getQueryParam(decodedValue, 's')
						let that = this
						let data = {
							deviceNo: id,
							storeId:storeId
						}
						that.$u.get(`/app/device/isBind?deviceNo=${id}`).then(res => {
							if (res.data == 2) {
								that.$u.get(`/app/device/${id}/withSuitList`).then((res) => {
									if (res.code == 200) {
										uni.navigateTo({
											url: '/page_components/fuwu/index?id=' + id
										})
									}
								})
							} else if (res.data == 1) {
								uni.showModal({
									title: '提示',
									content: '该设备未绑定,你需进行绑定吗？',
									success: function(res) {
										if (res.confirm) {
											that.$u.put("/app/device/bind", data).then(
												res => {
													if (res.code == 200) {
														that.$u.get(`/app/device/${id}/bySn`).then((res) => {
															if (res.code == 200) {
																uni.navigateTo({
																	url: '/page_components/bindsz?id=' + res.data.deviceId
																})
															}
														})
													}
												})
										} else if (res.cancel) {
				
										}
									}
								})
							} else if (res.data == 0) {
								uni.showToast({
									title: '该设备未录入,请先录入',
									icon: 'none',
									duration:2000
								})
							}
						})
					},
					fail: err => {
						console.error('扫描失败：', err)
						uni.showToast({
							title: '扫描失败',
							icon: 'none'
						})
					}
				})
			},
			// 点击跳转到我的
			btns(){
				uni.reLaunch({
					url:'/pages/my'
				})
			},
			// 点击审核中提示
			btnzstwo(){
				uni.showToast({
					title: '正在审核中，请等待',
					icon: 'none',
					duration: 2000
				})
			},
			// 点击申请店铺展示在地图
			btnzsone(id,status){
				uni.navigateTo({
					url:'/page_fenbao/xinzeng/dituzs?storeId=' + id + '&status=' + status
				})
			},
			// 点击跳转到店铺设备列表
			btndpsb(id){
				console.log(id);
				uni.navigateTo({
					url:'/page_fenbao/xinzeng/index?storeId=' + id
				})
			},
			btnpag(num,item) {
				if (num == 1) {
					uni.navigateTo({
						url: '/page_fenbao/statulist/myshop/shebeilist/index?storeId=' + item.storeId
					})
				} else if (num == 2) {
					uni.navigateTo({
						url: '/page_fenbao/statulist/myshop/editshop/index?obj=' + JSON.stringify(item)
					})
				} else if (num == 3) {
					let vm = this
					uni.showModal({
						title: '提示',
						content: '你确定要注销这个店铺吗？',
						success: function(res) {
							if (res.confirm) {
								vm.$u.delete("/app/store/" + item.storeId).then(res => {
									if (res.code == 200) {
										uni.showToast({
											title: '删除成功',
											icon: 'success',
											duration: 1000
										})
										setTimeout(() => {
											uni.navigateBack()
										}, 1500)
									} else if (res.code == 500) {
										uni.showToast({
											title: res.msg,
											icon: 'none',
											duration: 1000
										})
									}
								})
							} else if (res.cancel) {
								console.log('用户点击了取消')
							}
						}
					})
				}else if(num == 4){
					uni.navigateTo({
						url:'/page_components/peizhi?obj=' + JSON.stringify(item)
					})
				}else if(num == 5){
					uni.navigateTo({
						url:'/page_geren/buyCard?storeId=' + item.storeId
					})
				}
			},
			// 点击跳转到合伙人
			nav(item){
				uni.navigateTo({
					url:'/page_fenbao/hehuoren/index?storeId=' + item.storeId,
				})
			}, 
			// 请求店铺列表
			getlist() {
				if(this.types == '11'){
					this.isLoading = true;
					this.$u.get("/staff/store/list?pageNum=" + this.pagenum + '&pageSize=' + this.pagesize + '&keyword=' + this.keyword).then(res => {
						if (res.code == 200) {
							this.total = res.total
							if (this.total > 0) {
								this.showflag = false
							} else {
								this.showflag = true
							}
							if (res.rows.length > 0) {
								// 有数据，追加到列表  
								this.wateringList = this.wateringList.concat(res.rows)
								this.pagenum++
								// 计算创建到现在运行了多少天
								this.wateringList.forEach((item, index) => {
									var dateTime = new Date(item.createTime)
									if (isNaN(dateTime.getTime())) {
										console.error(`无法解析日期时间字符串: ${item.createTime}`)
										return
									}
									var now = new Date().getTime()
									var differenceInMilliseconds = now - dateTime.getTime();
									var differenceInDays = Math.floor(differenceInMilliseconds / (1000 * 60 * 60 * 24))
									// 直接修改原数组中的对象，添加daysDifference属性  
									item.daysDifference = differenceInDays
								})
							} else {
								// 没有更多数据  
								this.noMoreData = true
							}
							this.isLoading = false
						}
					}).catch(error => {
						console.error('获取店铺失败:', error)
						this.isLoading = false // 加载失败也标记为完成  
					})
				}else{
					this.isLoading = true;
					this.$u.get("/app/store/list?pageNum=" + this.pagenum + '&pageSize=' + this.pagesize + '&keyword=' + this.keyword).then(res => {
						if (res.code == 200) {
							this.total = res.total
							if (this.total > 0) {
								this.showflag = false
							} else {
								this.showflag = true
							}
							if (res.rows.length > 0) {
								// 有数据，追加到列表  
								this.wateringList = this.wateringList.concat(res.rows)
								this.pagenum++
								// 计算创建到现在运行了多少天
								this.wateringList.forEach((item, index) => {
									var dateTime = new Date(item.createTime)
									if (isNaN(dateTime.getTime())) {
										console.error(`无法解析日期时间字符串: ${item.createTime}`)
										return
									}
									var now = new Date().getTime()
									var differenceInMilliseconds = now - dateTime.getTime();
									var differenceInDays = Math.floor(differenceInMilliseconds / (1000 * 60 * 60 * 24))
									// 直接修改原数组中的对象，添加daysDifference属性  
									item.daysDifference = differenceInDays
								})
							} else {
								// 没有更多数据  
								this.noMoreData = true
							}
							this.isLoading = false
						}
					}).catch(error => {
						console.error('获取店铺失败:', error)
						this.isLoading = false // 加载失败也标记为完成  
					})
				}
			},
            // 点击搜索店铺
			searchs() {
				this.wateringList = []
				this.getlist()
			},
            // 上拉加载更多
			onReachBottom() {
				let sum = this.total / this.pagesize
				if (this.pagenum - 1 < sum) {
					this.getlist() // 上拉加载更多  
				} else {
					uni.showToast({
						title: '没有更多店铺了',
						icon: 'none',
						duration: 1000
					})
				}
			},
            // 点击跳转到新建店铺
			creadshop() {
				uni.navigateTo({
					url: '/page_fenbao/statulist/myshop/shopxx/index'
				})
			},
			// 点击跳转到店铺详情
			btnshopxq(item) {
				console.log(item);
				uni.navigateTo({
					url: '/page_fenbao/statulist/myshop/shopdetail/index?id=' + item.storeId + '&tit=' + item.name + '&obj=' + JSON.stringify(item)
				})
			}
		}
	}
</script>

<style lang="scss">
	/deep/ .u-title {
		padding-bottom: 22rpx;
	}
	/deep/ .uicon-nav-back {
		padding-bottom: 22rpx;
	}
	.mask {
		width: 100%;
		height: 100vh;
		position: fixed;
		top: 0;
		left: 50%;
		transform: translateX(-50%);
		background-color: #000;
		opacity: .6;
		z-index: 98;
	}
	.noticetc{
		width: 680rpx;
		height: 950rpx;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
		position: fixed;
		top: 440rpx;
		left: 50%;
		transform: translateX(-50%);
		z-index: 99;
		.bg{
			width: 680rpx;
			height: 220rpx;
			position: absolute;
			top: -190rpx;
			left: 0;
		}
		.jy{
			font-size: 28rpx;
			color: #FF5252;
			margin-top: 20rpx;
		}
		.btn{
			width: 600rpx;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			border-radius: 50rpx;
			background-color: #FF5252;
			color: #fff;
			font-size: 32rpx;
			font-weight: 600;
			margin: auto;
			margin-top: 30rpx;
		}
		.name{
			width: 100%;
			text-align: center;
			font-size: 36rpx;
			font-weight: 600;
			margin-top: 30rpx;
		}
		.biaoti{
			font-size: 32rpx;
			margin-top: 20rpx;
		}
		.cont{
			width: 100%;
			height: 620rpx;
			overflow: scroll;
			background-color: #ececec;
			margin-top: 20rpx;
			padding: 20rpx;
			box-sizing: border-box;
		}
	}
	.anniu{
		width: 750rpx;
		height: 152rpx;
		padding-top: 24rpx;
		box-sizing: border-box;
		background: #FFFFFF;
		box-shadow: 0rpx 8rpx 20rpx 0rpx rgba(0,0,0,0.3);
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		position: fixed;
		left: 0;
		bottom: 0; 
		.btn{
		   width: 680rpx;
		   height: 104rpx;
		   background: #8883F0;
		   border-radius: 30rpx;
		   font-weight: 600;
		   font-size: 36rpx;
		   color: #FFFFFF;
		   text-align: center;
		   line-height: 104rpx;
		   margin: auto;
		}
	}
	.serch{
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 36rpx;
		box-sizing: border-box;
		padding-top: 40rpx;
		padding-bottom: 22rpx;
		background-color: #fff;
		.lt{
			display: flex;
			align-items: center;
			width: 100%;
			height: 68rpx;
			border: 2rpx solid #8883F0;
			border-radius: 24rpx;
			padding-left: 30rpx;
			box-sizing: border-box;
			.sousuo{
				width: 140rpx;
				height: 66rpx;
				text-align: center;
				line-height: 66rpx;
				font-size: 32rpx;
				color: #FFFFFF;
				background: #8883F0;
				border: 2rpx solid #8883F0;
				border-radius: 0 20rpx 20rpx 0;
			}
			image{
				width: 46rpx;
				height: 46rpx;
			}
			input{
				width: 460rpx;
				margin-left: 30rpx;
				height: 68rpx;
				line-height: 68rpx;
			}
		}
	}
	.page {
		width: 750rpx;
		height: 100vh;
		.box {
			width: 750rpx;
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			// padding-top: 150rpx;
			.list {
				padding-bottom: 400rpx;
				margin-top: 26rpx;
				.listitem {
					margin-bottom: 28rpx !important;
					padding: 24rpx;
					box-sizing: border-box;
					width: 680rpx;
					max-height: 1540rpx;
					background: #FFFFFF;
					border-radius: 38rpx 38rpx 38rpx 38rpx;
					margin: auto;
					.yunxing {
						display: flex;
						justify-content: space-between;
						font-size: 26rpx;
						color: #808080;
						margin-top: 26rpx;
					}
					.phones {
						margin-top: 16rpx;
						font-size: 26rpx;
						color: #1DBE7B;
						border-bottom: 1px solid #ccc;
						padding-bottom: 26rpx;
						box-sizing: border-box;
						text {
							color: #808080;
						}
					}
					.phone {
						margin-top: 16rpx;
						font-size: 26rpx;
						color: #3D3D3D;
						text {
							color: #808080;
						}
					}
					.gzlist {
						margin-top: 20rpx;
						// border-bottom: 1rpx solid #ccc;
						padding-bottom: 16rpx;
						.icons {
							display: flex;
							justify-content: space-between;
							flex-wrap: wrap;
							padding: 0 20rpx;
							box-sizing: border-box;
							image {
								width: 96rpx;
								height: 96rpx;
								margin-top: 20rpx;
							}
						}
						.jfgz {
							font-size: 26rpx;
							color: #3D3D3D;
							text {
								color: #808080;
							}
						}
					}
					.rishou {
						margin-top: 26rpx;
						box-sizing: border-box;
						.rishouwz {
							display: flex;
							justify-content: space-between;
							text {
								font-size: 26rpx;
								color: #808080;
								width: 230rpx;
								text-align: center;
							}
						}
						.rishouje {
							display: flex;
							justify-content: space-between;
							margin-top: 10rpx;
							text {
								font-weight: 500;
								font-size: 32rpx;
								color: #3D3D3D;
								width: 230rpx;
								text-align: center;
							}
						}
					}
					.toptit {
						border-bottom: 1px solid #ccc;
						display: flex;
						justify-content: space-between;
						padding-bottom: 12rpx;
						box-sizing: border-box;
						align-items: center;
						image {
							width: 44rpx;
							height: 44rpx;
						}
						.tit {
							width: 450rpx;
							height: 68rpx;
							line-height: 68rpx;
							padding-left: 10rpx;
							// background: #F4F3FF;
							border-radius: 16rpx 16rpx 16rpx 16rpx;
							font-weight: 600;
							font-size: 30rpx;
							color: #3D3D3D;
							box-sizing: border-box;
						}
						.bd {
							margin-top: 28rpx;
							height: 100%;
							font-size: 20rpx;
							color: #FFFFFF;
							padding: 4rpx 12rpx;
							box-sizing: border-box;
							background: #1DBE7B;
							border-radius: 6rpx 6rpx 6rpx 6rpx;
						}
						.yuan {
							width: 600rpx;
							justify-content: flex-end;
							display: flex;
							align-items: center;
						}
					}
				}
			}
			.top {
				width: 750rpx;
				height: 150rpx;
				background: #FFFFFF;
				padding: 45rpx 36rpx;
				box-sizing: border-box;
				position: fixed;
				top: 170rpx;
				.cjshop {
					width: 200rpx;
					height: 64rpx;
					background-color: #8883F0;
					border-radius: 50rpx;
					text-align: center;
					line-height: 64rpx;
					color: #fff;
					font-size: 32rpx;
					margin-left: 20rpx;
				}
				/deep/ .u-content {
					border: 1px solid #ccc;
					border-radius: 50rpx 0 0 50rpx !important;
				}
				/deep/ .u-action {
					border-radius: 0 50rpx 50rpx 0 !important;
					width: 112rpx;
					height: 65rpx;
					line-height: 65rpx;
					border: 2rpx solid #ccc;
					margin-left: 0;
					color: #3D3D3D;
					background-color: #f2f2f2;
				}
			}
		}
	}
</style>