<template>
	<view>
		
		<view class="navbar-tit">{{title}}</view>
		
		<view class="statisticsData"  >
			<view class="statisticsData-item">
					<h3 style="font-size: 20px;">{{info.merchantAccountName}}</h3>
			</view>
			<view class="statisticsDataMain">
				<view class="statisticsData-item">
					
					<view class="statisticsData-head" @click="gotoUrl('pagesFinance/detailed/index')">
						<p>今日充电流水</p>
						<u-icon name="arrow-right-s-line" custom-prefix="custom-icon" color="#637AA2" size="36"></u-icon>
						
					</view>
					<view class="statisticsData-main">
						<h2>{{indexData.todayAmount}}元</h2>
					</view>
					<view class="statisticsData-foot">
						<p>昨日流水</p>
						<h2 style="margin-left: 10px;">{{indexData.yesterdayAmount}}元</h2>
					</view>
				</view>
				<view class="statisticsData-item">
					<view >
						<h3>&nbsp;</h3>
					</view>
					<view class="statisticsData-head">
						<p>今日充电笔数</p>
					</view>
					<view class="statisticsData-main">
						<h2>{{indexData.todayEQNum}}笔</h2>
					</view>
					<view class="statisticsData-foot">
						<p>本月流水</p>
						<h2 style="margin-left: 10px;">{{indexData.thisMonthAmount}}元</h2>
					</view>
				</view>
			</view>
		</view>
		<u-popup v-model="popupShow" mode="top" height="80%">
			<view class="popup-screen">
				<view class="screen">
					<view class="screen-item">
						<view class="screen-head">查询日期</view>
						<view class="screen-main2">
							
							<u-calendar v-model="showdate" mode="range" @change="changedate"></u-calendar>
							
							<u-input :value="startTime?startTime+'至'+endTime:'选择时间筛选'" :type="type" :border="border" @click="showdate = true" />
						<!-- 	
							<u-action-sheet :list="actionSheetList" v-model="show" @click="actionSheetCallback"></u-action-sheet>
						 --></view>
					</view>
					<view class="screen-item">
						<view class="screen-head">充电桩类型</view>
						<view class="screen-main">
							<view 
							:class="{
								active:selecttype==''
							}" @click="selecttype=''"
							class="screen-entry ">全部</view>
							<view 
							:class="{
								active:selecttype=='0'
							}" @click="selecttype='0'"
							
							class="screen-entry type1">自行车充电</view>
							<view 
							:class="{
								active:selecttype=='2'
							}" @click="selecttype='2'"
							class="screen-entry type3">交流慢充</view>
							<view 
							:class="{
								active:selecttype=='1'
							}" @click="selecttype='1'"
							class="screen-entry type2">直流快充</view>
						</view>
					</view>
					<view class="screen-item">
						<view class="screen-head">站点</view>
						<view class="screen-main">
							<view class="screen-entry  "
							:class="{
								active:selectstationId==''
							}"
							 @click="selectstationId=''"
							>全部</view>
							<view 
							v-for="(item,i) in stationList" :key="i"
							:class="{
								active:selectstationId==item.id
							}"
							 @click="selectstationId=item.id,selectdeviceNo=''"
							class="screen-entry" >{{item.name}}</view>
						
						</view>
					</view>
					<view 
				
					class="screen-item">
						<view class="screen-head">桩号</view>
						<view class="screen-main">
							 
							<view class="screen-entry  "
							:class="{
								active:selectdeviceNo==''
							}"
							 @click="selectdeviceNo=''"
							>全部</view>
							<view 
							v-for="(item,i) in stationListSon" :key="i"
							v-show="selectstationId?(selectstationId==item.stationId):true"
							:class="{
								active:selectdeviceNo==item.deviceNo
							}"
							
							 @click="selectdeviceNo=item.deviceNo"
							class="screen-entry" >{{item.name}}</view>
						</view>
					</view>
				</view>
				<view class="screen-foot">
					<view class="screen-btn-l"
					 @click="resetBtn" >重置</view>
					<view class="screen-btn-r" @click="okbtn" >确定</view>
				</view>
			</view>
		</u-popup>
		
		<u-calendar v-model="showdate2" mode="range" @change="changedate2"></u-calendar>
		
		<u-popup v-model="popupShow2" mode="top" height="50%">
			<view class="popup-screen">
				<view class="screen">
					<view class="screen-item">
						<view class="screen-head">查询日期</view>
						<view class="screen-main2">
							
							
							<u-input :value="startTime2?startTime2+'至'+endTime2:'选择时间筛选'" :type="type" :border="border" @click="showdate2 = true,popupShow2=false" />
						<!-- 	
							<u-action-sheet :list="actionSheetList" v-model="show" @click="actionSheetCallback"></u-action-sheet>
						 --></view>
					</view>
				 
					<view class="screen-item">
						<view class="screen-head">站点</view>
						<view class="screen-main">
							<view class="screen-entry  "
							:class="{
								active:selectstationId2==''
							}"
							 @click="selectstationId2=''"
							>全部</view>
							<view 
							v-for="(item,i) in stationList" :key="i"
							:class="{
								active:selectstationId2==item.id
							}"
							 @click="selectstationId2=item.id,selectdeviceNo=''"
							class="screen-entry" >{{item.name}}</view>
						
						</view>
					</view>
					 
				</view>
				<view class="screen-foot">
					<view class="screen-btn-l"
					 @click="resetBtn2" >重置</view>
					<view class="screen-btn-r" @click="okbtn2" >确定</view>
				</view>
			</view>
		</u-popup>
		
		<view class="statisticsChart">
			<view class="statisticsChart-head">
				<h4  >流水占比</h4>
				 <view class="navbar-screen" @click="popupShow = true"><span>筛选</span><u-icon name="filter-2-fill" custom-prefix="custom-icon" color="#b0b8c8" size="32"></u-icon></view>
				 
				
			</view>
			<view class="statisticsChart-main" >
<!-- 				<u-calendar v-model="showdate" mode="range" @change="changedate"></u-calendar>
 -->				
				<view class="statisticsChart-time" style="
    text-align: center; margin-bottom: 5px;
">
					<p >{{startTime}} 至 {{endTime}} </p>
				
				</view>
					<u-subsection   :list="subsection" @change="changesub" :current="current" button-color="#2E7Dff"
						active-color="#fff"></u-subsection>
						
				 
		
				<cover-view id="pieEcharts" style="min-height:250px;">

				</cover-view>
			</view>
			<view class="statisticsChart-head" style="margin-top: 20px;">
				<h4  >收益统计</h4>
				<!-- <view class="navbar-screen" >
					 {{startTime}}至{{endTime}}
				 </view> -->
				 <view class="navbar-screen" @click="popupShow2 = true"><span>筛选</span><u-icon name="filter-2-fill" custom-prefix="custom-icon" color="#b0b8c8" size="32"></u-icon></view>
				 
				
			</view>
		
			<view class="details" v-if="indexData2.incomeMap" >
				<p style="text-align: center; margin-bottom: 5px;" >{{startTime2}}至{{endTime2}} </p>
				<view class="details-row" >
					<p>投资人收入</p>
					<span>{{indexData2.incomeMap.incomeAmount}}元</span>
				</view>
				<view class="details-row" >
					<p>用电度数</p>
					<span>{{indexData2.incomeMap.electricQuantity/10000}}度</span>
				</view>
				<view class="details-row" >
					<p>电费</p>
					<span>{{indexData2.incomeMap.costAmount}}元</span>
				</view>
				<view class="details-row" >
					<p>服务费收益</p>
					<span><b>{{indexData2.incomeMap.serviceAmount}}元</b></span>
				</view>
			</view>
			
		</view>
			<Tabbar :current="0"  ref="tabbarMain" ></Tabbar>
<!-- 		<u-tabbar v-model="current" :list="tabbarList" active-color="#185ac6"></u-tabbar>
 -->	</view>
</template>

<script>
	import Tabbar from '@/components/TabbarFinance.vue'
	import * as API from '@/apis/finance.js'
	 import * as echarts from "echarts";
	//require("@/apis/echarts-5-3.min.js")
	//import * as echarts from "@/apis/echarts-5-3.min.js";
	 import {
		beforeTimeStamp,
		currentTimeStamp,
		parseUnixTime
	} from '@/utils'
	export default {
		data() {
			return {
				isinit:true,
				form:{},
				form2:{},
				selecttype:"",
				selectstationId:"",
				selectdeviceNo:"",
				popupShow2:false,
				popupShow:false,
				
				selectstationId2:"",
				info:{},
				title:"",
				showdate: false,
				showdate2: false,
				
				startTime: "",
				indexData:{},
				endTime: "",
				
				startTime2: "",
				indexData2:{},
				endTime2: "",
				
				myChart: null,
				subsection: [{
						name: '金额'
					},
					{
						name: '笔数'
					}
				],
			stationList:[],
			stationListSon:[],
				tabbarList: [{
						iconPath: "bar-chart-box-fill",
						selectedIconPath: "bar-chart-box-fill",
						text: '统计',
						count: 0,
						isDot: true,
						customIcon: true,
					},
					{
						iconPath: "article-fill",
						selectedIconPath: "article-fill",
						text: '明细',
						midButton: true,
						customIcon: true,
					},

					{
						iconPath: "account-pin-box-fill",
						selectedIconPath: "account-pin-box-fill",
						text: '我的',
						count: 0,
						isDot: false,
						customIcon: true,
					},
				],
				current: 0,
				background: {
					background: 'none'
				},
			 value: '',
			 type: 'select',
			 show: false,
			 border: true,
			}
		},
		onShow(){
			if(this.$refs.tabbarMain){
				this.$refs.tabbarMain.setcount(0);
			}
		},
		onLoad() {
			this.title="收益统计"
			 this.info=this.carhelp.getPersonInfo("merchantUser")
			 
		},
		components: {
			Tabbar
		
		},
		onReady() {
			this.startTime = parseUnixTime(beforeTimeStamp(7), '{y}-{m}-{d}')
			this.endTime = parseUnixTime(currentTimeStamp(), '{y}-{m}-{d}')
			this.startTime2 = parseUnixTime(beforeTimeStamp(7), '{y}-{m}-{d}')
			this.endTime2 = parseUnixTime(currentTimeStamp(), '{y}-{m}-{d}')
			this.getData()
			this.getStation()
		},
		methods: {
			getStation(bl) {
				
				API.stationList().then((res) => {
					
					this.stationList = res.data.stationList
					this.stationListSon=res.data.deviceList	
				}).catch(error => {
					uni.showToast({
					
						title: error
					})
				})
			},
			resetBtn(){
				this.startTime = parseUnixTime(beforeTimeStamp(7), '{y}-{m}-{d}')
				this.endTime = parseUnixTime(currentTimeStamp(), '{y}-{m}-{d}')
				
			 
			 
				this.selecttype=""
				this.selectstationId=""
				this.selectdeviceNo=""
			 
				this.form = {
				 
				 
				 
					type:this.selecttype,
					stationId:this.selectstationId,
					deviceNo:this.selectdeviceNo,
				};
				this.popupShow=false;
				this.list = [];
					this.getData()
				
			},
			okbtn(){
				this.popupShow=false;
				 
				this.form = {
				 
					type:this.selecttype,
					stationId:this.selectstationId,
					deviceNo:this.selectdeviceNo,
				};
				this.list = [];
					this.getData()
			},
			resetBtn2(){
				this.startTime2 = parseUnixTime(beforeTimeStamp(7), '{y}-{m}-{d}')
				this.endTime2 = parseUnixTime(currentTimeStamp(), '{y}-{m}-{d}')
				
			 
			 
 				this.selectstationId2=""
 				 
				this.form2 = {
				 
					stationId:this.selectstationId2,
				
				};
				this.popupShow2=false;
			
					this.getData2()
				
			},
			okbtn2(){
				this.popupShow2=false;
 				
				this.form2 = {
				 
					stationId:this.selectstationId2,
				
				};
			
				this.getData2()
			},
			getData(){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
 				
				this.form.startDate=this.startTime,
				this.form.endDate=this.endTime 
				
				API.spIncomeStatistics(this.form).then((res) => {
					 
					this.indexData = res.data
					if(this.isinit){
						this.isinit=false;
						this.indexData2=res.data
					}
					this.getPie()
				
					
					uni.hideLoading()
					
				}).catch(error => {
					uni.showToast({
					
						title: error
					})
				})
			},
			getData2(){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
 				
				this.form2.startDate=this.startTime2,
				this.form2.endDate=this.endTime2 
				
				API.spIncomeStatistics(this.form2).then((res) => {
					 this.indexData2=res.data
					
					uni.hideLoading()
					
				}).catch(error => {
					uni.showToast({
					
						title: error
					})
				})
			},
			changesub(e){
				console.log(e)
				this.current=e
				this.getPie()
			},
			changedate(e) {
				this.startTime = e.startDate
				this.endTime = e.endDate
			},
			changedate2(e) {
				this.startTime2 = e.startDate
				this.endTime2= e.endDate
				this.popupShow2=true
				 
			},
			getPie() {
				
				var list=this.indexData.pieChart;
				var getData=[];
				var unit=this.current?'笔':'元'
				list.forEach(item=>{
					 var value=this.current?item.eqNum:item.amount
					 if(value){
						 getData.push({
						 	value: value,
						 	name: item.deviceName
						 })
					 }
					
				})
				
				if (!this.myChart) {
					this.myChart = echarts.init(document.getElementById('pieEcharts'));

				}
				// 指定图表的配置项和数据
				var option = {
					tooltip: {
						trigger: 'item',
					  formatter: '{b} <br/> {c}'+unit,
						position:function(point,params,dom,rect,size){
							return ['10%','0%']
						}

					},
					
					series: [{
						//name: 'Access From',
						type: 'pie',
						radius: '40%',
						data: getData,
						emphasis: {
							itemStyle: {
								shadowBlur: 10,
								shadowOffsetX: 0,
								shadowColor: 'rgba(0, 0, 0, 0.5)'
							}
						},
						
						label:{
					
							fontWeight:"bold",
							fontSize:14,
						}
					}]
				};

				// 使用刚指定的配置项和数据显示图表。
				this.myChart.setOption(option);
			}
		}
	}
</script>
<style>
	page {
		background: url(../../assets/img/index_header_bg.png) no-repeat top center #f7f7f7;
		background-size: 100%;
	}
</style>

<style lang="scss" scoped>
	.details{
		margin-top: 16px;
		padding: 20px;
		background-color: #fff;
		border-radius: 8px;
		
	}
	.details-title{
		margin-bottom: 16px;
		h4{
			font-weight: normal;
			font-size: 16px;
			position: relative;
			padding-left:10px;
			&::after{
				content: '';
				position: absolute;
				height: 12px;
				width: 4px;
				background-color: #27B148;
				left: 0;
				top:5px;
			}
		}
	}
	 .details-row{
	 	display: flex;
	 	justify-content: space-between;
	 	align-items: center;
	 	margin-top: 15px;
	 	
	 	p{
	 		color: #37393c;
	 		font-weight: bold;	
	 	}
	 }
	.popup-screen{
		padding: 20px;
		position: relative;
		.screen{
			padding-bottom: 30px;
		}
		.screen-item{
			margin-bottom: 20px;
			.screen-head{
				margin-bottom: 8px;
				font-size: 16px;
			}
			.screen-main{
				display: flex;
				//    display: -webkit-box;
				flex-wrap: wrap;
			
			}
			.screen-entry{
				width: 29%;
				padding:6px 0;
								display: flex;
								align-items: center;
								justify-content: center;
	
	
				background-color: #F2F5FA ;
				text-align: center;
				margin-bottom: 10px;
				border-radius: 3px;
			
				margin-right: 6px;
	
			}
			.screen-entry.active{
				background-color: #185AC6;
				color:#fff;
			}
		}
		.screen-foot{
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			display: flex;
			height:50px;
			border-top: 1px solid #ededed;
			.screen-btn-l{
				 background-color: #fff;
				flex: 0.2;
				text-align: center;
				line-height: 50px;
			}
			.screen-btn-r{
				flex: 0.8;
				text-align: center;
				line-height: 50px;
				background-color: #185AC6;
				color:#fff;
			}
		}
	}
	.navbar-tit {
		color: #fff;
		font-size: 24px;
		padding-left: 15px;
		    padding-top: 10px;
	}

	.statisticsData {
		background-color: #fff;
		margin: 12px;
	
		padding: 12px;
		border-radius: 8px;
	
		margin-top: 60px;
	}

	.statisticsDataMain {
	
		display: flex;
	
	}
	.statisticsData-item {
		flex: 1;

		.statisticsData-head {
			display: flex;
			align-items: center;

			p {
				color: #637AA2;
				font-size: 12px;
			}
		}

		.statisticsData-main {
			margin-top: 4px;
			font-size: 20px;
		}

		.statisticsData-foot {
			display: flex;
			align-items: center;
			margin-top: 16px;

			p {
				color: #637AA2;
			}

			h4 {
				margin-left: 8px;
			}
		}
	}

	.statisticsChart {
		margin: 16px;

		.statisticsChart-head {
			margin-bottom: 5px;
			display: flex;
			justify-content: space-between;
			align-items: center;

			h4 {
				font-weight: normal;
				font-size: 16px;
				position: relative;
				padding-left: 10px;

				&::after {
					content: '';
					position: absolute;
					height: 12px;
					width: 4px;
					background-color: #4E8DF6;
					left: 0;
					top: 5px;
				}
			}

			.statisticsChart-time {
				display: flex;
				align-items: center;

				p {
					color: #666;
				}
			}
		}

		.statisticsChart-main {
			background-color: #fff;
			padding:10px 16px 16px 16px;
			border-radius: 8px;
		}
	}
</style>
