<template>
	<view class="page">
		<u-navbar :is-back="false" title="首页" :border-bottom="false" :background="bgc" title-color='#2E4975'
			title-size='36' height='45'></u-navbar>
		<view class="info_box">
			<view class="tit">
				运营概览
			</view>
			<!-- 	<view class="data_box">
				<view class="data1">
					近7天
				</view>
				<view class="data1">
					2024-09-05 <view class="iconfont icon-xiangxia1"></view>
				</view>
				<view class="data1">
					近30天
				</view>

			</view> -->
			<view class="top_box">
				<view class="left_text">
					时间范围
				</view>
				<view class="timebox">
					<view class="left_time" @click="time1=true">
						{{firsTime}}
					</view>
					-
					<view class="right_time" @click="time2=true">
						{{lasTime}}
					</view>
				</view>
			</view>
			<view class="tit_box">
				<view class="tit_line"></view>
				经营概况
			</view>
			<view class="echarts">
				<uni-ec-canvas class="uni-ec-canvas" id="uni-ec-canvas" ref="canvas1" canvas-id="uni-ec-canvas"
					:ec="ec">
				</uni-ec-canvas>
			</view>
			<view class="log_box">
				<view class="log_li">
					<image src="https://lxnapi.ccttiot.com/bike/img/static/uLJmp6DVyyoeGsIIh7IJ" mode=""></image>
					订单营收
				</view>
				<view class="log_li">
					<image src="https://lxnapi.ccttiot.com/bike/img/static/u5m5JeBXwVdNB4LWB3Nr" mode=""></image>
					订单数量
				</view>

			</view>
			<view class="info_boxs">
				<view class="info_li">
					<view class="tit">
						{{info.todayIncome}}
					</view>
					<view class="txt">
						今日订单营收（元）
					</view>
					<view class="txt">
						较昨日
						<span>{{ formatIncome(info.comparedYesterdayIncome) }}</span>
						<!-- 如果是负数显示对应的图片 -->
						<image v-if="info.comparedYesterdayIncome < 0"
							src="https://lxnapi.ccttiot.com/bike/img/static/uL4sBdN3Pbvn5tQvhwtk" mode=""></image>
						<!-- 如果是正数显示对应的图片 -->
						<image v-else src="https://lxnapi.ccttiot.com/bike/img/static/ugghyGkhTlc3xr0nle2a" mode="">
						</image>
					</view>
				</view>
				<view class="info_li">
					<view class="tit">
						{{info.todayIncome}}
					</view>
					<view class="txt">
						今日订单数（单）
					</view>
					<view class="txt">
						较昨日
						<span>{{ formatIncome(info.comparedYesterdayOrderNum) }}</span>
						<!-- 如果是负数显示对应的图片 -->
						<image v-if="info.comparedYesterdayOrderNum < 0"
							src="https://lxnapi.ccttiot.com/bike/img/static/uL4sBdN3Pbvn5tQvhwtk" mode=""></image>
						<!-- 如果是正数显示对应的图片 -->
						<image v-else src="https://lxnapi.ccttiot.com/bike/img/static/ugghyGkhTlc3xr0nle2a" mode="">
						</image>
					</view>
				</view>
			</view>
		</view>
		<view class="tits" >
			车辆统计
		</view>
		<view class="info_cont">
			<view class="li">
				<view class="top">
					车辆总数
				</view>
				<view class="bot">
					{{info.deviceVO.total}}
				</view>
			</view>
			<view class="line"></view>
			<view class="li">
				<view class="top">
					空闲中
				</view>
				<view class="bot">
					{{info.deviceVO.idle}}
				</view>
			</view>
			<view class="line"></view>
			<view class="li">
				<view class="top">
					出租中
				</view>
				<view class="bot">
					{{info.deviceVO.rent}}
				</view>
			</view>
		</view>
		<!-- <view class="tits">
			更多功能
		</view>
		<view class="feature" v-if="AccountInfo.userType==03">
			<image src="https://lxnapi.ccttiot.com/bike/img/static/u3J6GbXvEvdTtebETjfH" mode="" @click="topage(0)">
			</image>
			<image src="https://lxnapi.ccttiot.com/bike/img/static/uahBs1ruuAha5PHiiTp9" mode="" @click="topage(1)">
			</image>
			<image src="https://lxnapi.ccttiot.com/bike/img/static/u5yEuqhkS7785kZRiQqC" mode="" @click="topage(2)">
			</image>
		</view>
		<view class="feature" v-if="AccountInfo.userType==02">
			<image src="https://lxnapi.ccttiot.com/bike/img/static/u3J6GbXvEvdTtebETjfH" mode="" @click="topages(0)">
			</image>
			<image src="https://lxnapi.ccttiot.com/bike/img/static/ufE1T9RDOm7OmhLgTzXi" mode="" @click="topages(1)">
			</image>
			<image src="https://lxnapi.ccttiot.com/bike/img/static/u5yEuqhkS7785kZRiQqC" mode="" @click="topages(2)">
			</image>
		</view> -->
		<u-picker mode="time" v-model="time1" :params="params" @confirm="confirm1"
			:default-time='pickertime'></u-picker>
		<u-picker mode="time" v-model="time2" :params="params" @confirm="confirm2"
			:default-time='pickertime'></u-picker>
		<tab-bar :indexs='1' style=""></tab-bar>
	</view>
</template>

<script>
	// const app = getApp();
	// var xBlufi = require("@/utils/blufi/xBlufi.js");
	let _this = null;
	import TabBar from '@/pages_store/components/tab-bar/tab-bar.vue';
	import uniEcCanvas from '@/pages_store/components/uni-ec-canvas/uni-ec-canvas.vue'
	import * as echarts from '@/pages_store/components/uni-ec-canvas/echarts.js'
	let chart = null
	export default {
		components: {
			uniEcCanvas,TabBar
		},
		data() {
			return {
				bgc: {
					backgroundColor: "#F7FAFE",
				},
				ec: {
					lazyLoad: true
				},
				chartday: [], // 保存日期的数组
				chartData1: [], // 保存温度数据的数组
				chartData2: [120, 90, 150, 80, 130, 110, 160],
				info: {},
				AccountInfo: {},
				time1: false,
				time2: false,
				lasTime: '',
				firsTime: '',
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},

			}
		},
		onLoad() {
			// this.generateMockData()
			// this.generateMockData();

			// 获取当前日期
			const currentDate = new Date();

			// 获取一周前的日期
			const oneWeekAgoDate = new Date();
			oneWeekAgoDate.setDate(currentDate.getDate() - 7);

			// 格式化日期为 'YYYY-MM-DD'
			const formatDate = (date) => {
				const year = date.getFullYear();
				const month = (date.getMonth() + 1).toString().padStart(2, '0');
				const day = date.getDate().toString().padStart(2, '0');
				return `${year}-${month}-${day}`;
			};

			// 设置 this.firstTime 和 this.lastTime
			this.firsTime = formatDate(oneWeekAgoDate);
			this.lasTime = formatDate(currentDate);

			console.log(this.firstTime); // 输出一周前的日期
			console.log(this.lastTime); // 输出当前日期
		},
		computed: {

		},
		onShow() {
			
			this.getIndexInfo()
			this.getAccountInfo()
		},
		watch: {

		},

		methods: {
			formatIncome(value) {
				if (value > 0) {
					// 如果是正数，返回带加号的数值
					return `+${value}`;
				} else {
					// 如果是负数，直接返回数值
					return value;
				}
			},
			topages(num) {
				if (this.AccountInfo.userId) {
					if (num == 0) {
						uni.navigateTo({
							url: '/page_Operator/deviceMgmt'
						})
					} else if (num == 1) {
						uni.navigateTo({
							url: '/page_Operator/CarModel'
						})
					} else if (num == 2) {
						uni.navigateTo({
							url: '/page_Operator/order'
						})
					}
				} else {
					uni.navigateTo({
						url: '/pages/login/login'
					})
				}

			},
			callPhone(phone) {
				uni.makePhoneCall({
					phoneNumber: phone
				})

			},
			getAccountInfo() {
				this.$u.get(`/appVerify/myAccountInfo`).then((res) => {
					if (res.code === 200) {
						this.AccountInfo = res.data
						if (this.AccountInfo.userType == '04') {
							uni.redirectTo({
								url: '/page_Delivery/orerList'
							})
						}
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
			topage(num) {
				if (num == 0) {
					uni.navigateTo({
						url: '/page_Merchant/deviceMgmt'
					})
				} else if (num == 1) {
					uni.navigateTo({
						url: '/page_Merchant/StoreManagement'
					})
				} else if (num == 2) {
					uni.navigateTo({
						url: '/page_Merchant/order'
					})
				}
			},
			confirm1(e) {
				console.log(e, 'eeeeeeee');
				this.firsTime = e.year + '-' + e.month + '-' + e.day
				if (this.firsTime != '' && this.lasTime != '') {
					this.getIndexInfo()
				}

			},
			confirm2(e) {
				this.lasTime = e.year + '-' + e.month + '-' + e.day
				// this.pageNum=1
				if (this.firsTime != '' && this.lasTime != '') {
					this.getIndexInfo()
				}



			},
			getIndexInfo() {
				let data = {
					startTime: this.firsTime,
					endTime: this.lasTime
				}
				this.$u.get(`/appVerify/index?`, data).then((res) => {
					if (res.code === 200) {
						this.info = res.data
						this.generateMockData()
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
			generateMockData() {
				// 生成 7 天的日期
				this.chartday = [];
				this.chartData1 = [];
				this.chartData2 = [];

				// 遍历真实数据 incomeVoList 并填充 chartday, chartData1, chartData2
				if (this.info && this.info.incomeVoList) {
					this.info.incomeVoList.forEach(item => {
						// 取出 day, orderFee, orderNum
						const day = new Date(item.day);
						const month = day.getMonth() + 1;
						const date = day.getDate();
						// 格式化日期为 MM-DD
						this.chartday.push(`${month}-${date}`);

						// 填充 orderFee (营收)
						this.chartData1.push(item.orderFee);

						// 填充 orderNum (订单单数)
						this.chartData2.push(item.orderNum);
					});
				}
				this.$refs.canvas1.init(this.initChart)
			},
			initChart(canvas, width, height, canvasDpr) {
				let that = this

				const option = {
					grid: {
						left: '4%',
						right: '4%',
						bottom: '3%',
						top: '4%',
						containLabel: true
					},
					xAxis: {
						show: true,
						type: 'category',
						boundaryGap: false,
						axisLine: {
							show: false // 隐藏X轴线
						},
						axisTick: {
							show: false // 隐藏X轴刻度
						},
						axisLabel: {
							show: true,
							color: '#999', // 调整为浅灰色
							fontSize: 12,
							rotate: 0,
						},
						splitLine: {
							show: false,
						},
						data: that.chartday,
					},
					yAxis: [{
							show: true,
							type: 'value',
							axisLine: {
								show: false, // 隐藏Y轴线
							},
							axisTick: {
								show: false, // 隐藏Y轴刻度
							},
							axisLabel: {
								show: true,
								color: '#999', // 调整为浅灰色
								fontSize: 12,
							},
							splitLine: {
								show: true, // 显示网格线
								lineStyle: {
									type: 'dashed', // 设置为虚线
									color: '#ccc', // 虚线颜色
									width: 1
								}
							}
						},
						{
							show: true,
							type: 'value',
							position: 'right', // 右侧Y轴
							axisLine: {
								show: false, // 隐藏Y轴线
							},
							axisTick: {
								show: false, // 隐藏Y轴刻度
							},
							axisLabel: {
								show: true,
								color: '#999', // 调整为浅灰色
								fontSize: 12,
							},
							splitLine: {
								show: false, // 不显示网格线
							}
						}
					],
					series: [{
							name: '近7日订单营收',
							type: 'line',
							smooth: true,
							symbol: 'none', // 取消圆形标志
							symbolSize: 8, // 圆形标志大小
							itemStyle: {
								color: '#8E6BF9', // 圆形标志颜色
								borderColor: '#fff',
								borderWidth: 2
							},
							lineStyle: {
								color: '#8E6BF9', // 线条颜色
								width: 3, // 线条宽度
							},
							areaStyle: {
								normal: {
									color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
											offset: 0,
											color: 'rgba(142, 107, 249, 0.3)'
										}, // 顶部颜色
										{
											offset: 1,
											color: 'rgba(142, 107, 249, 0)'
										} // 底部颜色
									])
								}
							},
							data: that.chartData1, // 订单营收数据
						},
						{
							name: '近7日订单单数',
							type: 'line',
							smooth: true,
							yAxisIndex: 1, // 使用右侧Y轴
							symbol: 'none', // 取消圆形标志
							symbolSize: 8, // 圆形标志大小
							itemStyle: {
								color: '#67B8FF', // 圆形标志颜色
								borderColor: '#fff',
								borderWidth: 2
							},
							lineStyle: {
								color: '#67B8FF', // 线条颜色
								width: 3, // 线条宽度
							},
							areaStyle: {
								normal: {
									color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
											offset: 0,
											color: 'rgba(103, 184, 255, 0.3)'
										}, // 顶部颜色
										{
											offset: 1,
											color: 'rgba(103, 184, 255, 0)'
										} // 底部颜色
									])
								}
							},
							data: that.chartData2, // 订单单数数据
						}
					],

				};

				chart = echarts.init(canvas, null, {
					width: width,
					height: height,
					devicePixelRatio: canvasDpr
				});
				canvas.setChart(chart);
				chart.setOption(option);
				return chart;
			}






		}
	}
</script>

<style lang="scss">
	page {
		overflow-y: auto;
		background-image: url('https://lxnapi.ccttiot.com/bike/img/static/uYRs7Cv2Pbp95w3KjGO3');
		background-size: cover;
		/* 背景图片等比缩放以覆盖整个容器 */
		background-position: center;
		/* 背景图片居中显示 */
		background-repeat: no-repeat;
		/* 防止背景图片重复 */
		min-height: 100vh;
		/* 确保页面至少有 100% 的视窗高度，避免高度不足导致无法滚动 */
	
	}
	

	.page {
		width: 100%;
		padding-bottom: 200rpx;

		.info_cont {
			display: flex;
			flex-wrap: nowrap;
			align-items: center;
			justify-content: space-around;
			margin: 0 auto;
			margin-top: 18rpx;
			width: 680rpx;
			// height: 172rpx;
			padding: 40rpx 0rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
			border-radius: 24rpx 24rpx 24rpx 24rpx;

			.li {
				width: 110rpx;
				display: flex;
				flex-wrap: wrap;
				justify-content: center;

				.top {
					width: 100%;
					text-align: center;
					font-weight: 400;
					font-size: 24rpx;
					color: #3D3D3D;
				}

				.bot {
					width: 100%;
					text-align: center;
					font-weight: 600;
					font-size: 40rpx;
					color: #3D3D3D;
				}
			}

			.line {
				width: 2rpx;
				height: 74rpx;
				background: #C7C7C7;
				border-radius: 0rpx 0rpx 0rpx 0rpx;
			}
		}

		.feature {
			margin: 0 auto;
			margin-top: 24rpx;
			width: 658rpx;
			padding: 30rpx 40rpx;
			background: #FFFFFF;
			border-radius: 24rpx 24rpx 24rpx 24rpx;

			image {
				margin-right: 66rpx;
				width: 96rpx;
				height: 98rpx;
			}
		}

		.tits {
			margin-top: 40rpx;
			margin-left: 44rpx;
			font-weight: 600;
			font-size: 36rpx;
			color: #3D3D3D;
		}

		.info_box {
			margin: 0 auto;
			padding: 30rpx 22rpx;
			width: 680rpx;
			// height: 966rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 16rpx 40rpx 0rpx rgba(42, 130, 228, 0.1);
			border-radius: 24rpx 24rpx 24rpx 24rpx;

			.top_box {

				display: flex;
				flex-wrap: nowrap;
				align-items: center;
				// padding: 28rpx 30rpx;
				// padding: 20rpx 0;

				margin: 0 auto;
				// width: 672rpx;
				height: 100rpx;
				background: #FFFFFF;
				// box-shadow: 0rpx 4rpx 22rpx 0rpx rgba(0, 0, 0, 0.07);
				border-radius: 10rpx 10rpx 10rpx 10rpx;

				.left_text {
					display: flex;
					flex-wrap: nowrap;
					align-items: center;
					width: 25%;
					font-weight: 400;
					font-size: 32rpx;
					color: #3D3D3D;

					.icon-xiangxia1 {
						font-size: 26rpx;
					}
				}

				.timebox {
					width: 75%;
					display: flex;
					flex-wrap: nowrap;
					align-items: center;
					margin-left: 34rpx;

					.left_time {
						text-align: center;
						margin-right: 6rpx;
						height: 50rpx;
						width: 45%;
						border: 2rpx solid #ccc;
						border-radius: 12rpx;
						font-weight: 400;
						font-size: 32rpx;
						color: #979797;
					}

					.right_time {
						text-align: center;
						margin-left: 6rpx;
						height: 50rpx;
						width: 45%;
						border: 2rpx solid #ccc;
						border-radius: 12rpx;
						font-weight: 400;
						font-size: 32rpx;
						color: #979797;
					}
				}
			}


			.info_boxs {
				margin-top: 26rpx;
				width: 636rpx;
				padding: 24rpx 0;
				background: #F4FBFF;
				border-radius: 24rpx 24rpx 24rpx 24rpx;
				display: flex;
				flex-wrap: nowrap;

				.info_li:first-child {
					border-right: 1rpx solid #D8D8D8;
				}

				.info_li {
					padding: 0 40rpx;
					display: flex;
					flex-wrap: wrap;
					width: 50%;

					.tit {
						text-align: left;
						width: 100%;
						font-weight: 600;
						font-size: 24rpx;
						color: #3D3D3D;
					}

					.txt {
						margin-top: 10rpx;
						width: 100%;
						display: flex;
						align-items: center;
						font-weight: 400;
						font-size: 24rpx;
						color: #3D3D3D;

						span {
							font-size: 600;
						}

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

			.log_box {
				display: flex;
				flex-wrap: nowrap;
				justify-content: space-around;

				.log_li {
					font-weight: 400;
					font-size: 24rpx;
					color: #3D3D3D;

					image {
						margin-right: 8rpx;
						width: 76rpx;
						height: 18rpx;
					}
				}
			}

			.echarts {
				width: 650rpx;
				height: 500rpx;
			}

			.tit_box {
				margin-top: 40rpx;
				margin-bottom: 30rpx;
				display: flex;
				align-items: center;

				font-weight: 600;
				font-size: 32rpx;
				color: #3D3D3D;

				.tit_line {
					margin-right: 6rpx;
					width: 10rpx;
					height: 46rpx;
					background: #4297F3;
					border-radius: 5rpx 5rpx 5rpx 5rpx;
				}
			}

			.data_box {
				margin-top: 32rpx;
				width: 100%;
				display: flex;
				flex-wrap: nowrap;
				justify-content: space-around;

				.act {
					background: #4297F3;
					color: #FFFFFF;
				}

				.data1 {
					display: flex;
					flex-wrap: nowrap;
					padding: 8rpx 32rpx;

					border-radius: 26rpx 26rpx 26rpx 26rpx;
					background: #FFFFFF;
					border: 2rpx solid #4297F3;
					font-weight: 400;
					font-size: 28rpx;
					color: #4297F3;
				}

				// .data2{

				// }
			}

			.tit {
				margin-top: 40rpx;
				width: 100%;
				text-align: center;
				font-weight: 600;
				font-size: 36rpx;
				color: #3D3D3D;
			}
		}

	}
</style>