<template>
	<view>
		<u-navbar title="充值记录" title-color="#101010"></u-navbar>
		<view class="tabs-box" v-if="!popShow" >
			<view class="tabs1" v-show="tenantList.length>1" :style="{width: tenantList.length>4 ? '' : '100%'}" >
				<u-tabs :list="tenantList1"   inactive-color="#777777" active-color="#333333	" 
				 bg-color="#fff" :is-scroll="false" :current="current" @change="change"></u-tabs>
			</view>
			<view class="icon"  @click="popShow=true" v-if="tenantList.length >= 4" >
				<u-icon name="arrow-down" color="#777777" size="28"></u-icon>
			</view>
		</view>
		<!-- 标签弹出层 -->
		<u-popup v-model="popShow"  length="80%"  @close="keyword=''"  duration="10" mode="top" :negative-top="188" border-radius="32">
			<u-navbar title="充值记录" title-color="#101010"></u-navbar>
			
			<view class="head-head">
				<u-search placeholder="请输入关键字" :show-action="false"		 v-model="keyword"></u-search>
				
			</view>
			<view class="head-head-placeholder ">
				
			</view>
			
			
			<view class="tabs-options">
				<view class="item" v-for="(item, index) in tenantList"
				v-show="keyword==''||(item.name&&item.name.indexOf(keyword)!=-1)"
				
				 :class="{
				 	'item-index' : item.id==formData.tenantId
				 }"
				 :key="index"
					@click="merchantChange(item,index)">
					{{item.name}}
				</view>
				<u-divider   nonetext="没有找到相关内容"
					border-color="#CFD2D5">已经到底了</u-divider>
			</view>
		</u-popup>
		
		
		<!-- 折线图 -->
		<view class="chart-box">
			<view class="chart">
				<view id="pieEcharts" >
				
				</view>
			</view>

		</view>
		<!-- 标签 -->
		<view class="tabs">
			<u-picker  v-model="tabsFrom.show1" mode="selector"
			  :default-selector="[tabsFrom.show1Index]"
			 :range="tabsFrom.selector1"  range-key="label" @confirm="selector1confirm" ></u-picker>
			<u-picker-select :noselect="false"
			title="日期选择" v-model="tabsFrom.show2"
			 :defaultTime="tabsFrom.show2Index" :endYear="endYear"
			 mode="time" :params="params"  @confirm="selector2confirm" @reset="selector2reset" ></u-picker-select>
			
			<view class="tabsItem" @click="tabsFrom.show1=!tabsFrom.show1">{{tabsFrom.show1Text}} <u-icon name="arrow-up"
					v-if="tabsFrom.show1"></u-icon><u-icon v-else name="arrow-down"></u-icon></view>
			<view class="tabsItem" @click="tabsFrom.show2=!tabsFrom.show2">{{tabsFrom.show2Text}} <u-icon name="arrow-up"
					v-if="tabsFrom.show2"></u-icon><u-icon v-else name="arrow-down"></u-icon></view>
		</view>
		<!-- 记录 -->
		<view class="records" >
			<view class="time">
				{{queryMonth}} 充值 {{jpAmount(sumAmount)}}元

			</view>
			<view class="details"  v-for="(item,i) in list"
			 @click="gotoUrl('/pages/tenantList/rechargeDetails?id='+item.id)"
			 
			 :key="i">
				<view class="details-title">
					<view class="name" v-if="item.tenantName">
						{{item.tenantName}}    
					</view>
					<view class="record-time" v-if="item.tenantName">
						{{item.remark}}-{{item.methodN}}    
					</view>
					
					<view class="name" v-else>
						{{item.remark}}-{{item.methodN}}    
					</view>
					<view class="record-time" v-if="item.extraInfo" v-html="extraInfoText(item.extraInfo)" >
						 
					</view>
					<view class="record-time">
						{{item.createTime}}  
					</view>
				</view>
				
				
				<view class="sum">
					{{jpAmount(item.amount)}} 
					<u-icon name="arrow-right" style="margin-left: 10rpx;" size="32" color="#999999"></u-icon>
					

				</view>
			</view>
			<u-divider :isnone="list.length==0"  nonetext="没有找到相关内容"
				border-color="#CFD2D5">已经到底了</u-divider>
		</view>
	</view>
</template>

<script>
	//import * as API from '@/apis/pagejs/deduction.js'
	import * as API from '@/apis/pagejs/tenantList.js'
	import {
		nextDay,
		nextMonth,
	
	} from '@/apis/utils'
	import * as echarts from "echarts";
	
	export default {
		data() {
			return {
				myChart:null,
				list:[],
				formData:{
					pageIndex:1,
					pageSize:9999,
					tenantId:"",
				},
				
				popShow:false,
					keyword:'',
					current:0,
					current1: 0,
					current2: 0,
				tenantList:[],
				tenantList1:[],
				endYear:'',
					queryMonth:"",
				params: {
					year: true,
					month: true,
					day: false,
					hour: false,
					minute: false,
					second: false
				},
				tabsFrom: {
					show1: false,
					show1Index:0,
					show2Index:'',
					show2: false,
					show1Text: "全部类型",
					show2Text: "全部时间",
					selector1:[
						{
								label: '全部类型',
								value: '',
							},
						{
								label: '线上充值',
								value: '1',
							},
							{
								label: '线下充值',
								value: '2',
							},
					]
				},
				 
				
			 
			}
		},
		onLoad() {
			this.endYear=new Date().getFullYear()
			
			var date=new Date()
			this.formData.startDate=date.getFullYear()+"-"+(date.getMonth() + 1) +"-1"
			this.formData.endDate=nextMonth(date.getFullYear(),date.getMonth()+ 1, 1,1)
			
			this.queryMonth=date.getFullYear()+"年"+(date.getMonth() + 1) +"月"
			
			this.getTenantList()
		
		},
		computed:{
			
			sumAmount(){
				let totalAmount =0
				if(this.list){
					var amounts=this.list.map(item=>{
						return item.amount
					})
				
					 totalAmount = amounts.reduce((accumulator, currentValue) => {  
					  return accumulator + currentValue;  
					}, 0); 
				}
				 
				
				return   totalAmount
			}
		},
		methods:{
			merchantChangeApi(){
				this.rechargeRecord();
				this.rechargeRecordGraph();
			},
			change(index) {
				
				this.current = index;
				this.formData.tenantId = this.tenantList1[index].id;
				 this.popShow = false;
				this.merchantChangeApi()
			},
			merchantChange(item, index) {
				if(this.current==0){
					this.current=1;
				}
				this.formData.tenantId= item.id;
				
				
				 //this.$forceUpdate()
				
				this.getTenantListSort()
				this.merchantChangeApi()
				this.popShow = false;
			},
			 
			getTenantList(){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				API.tenantList().then((response) => {
					uni.hideLoading();
					
					
					var list=response.data.tenantInfoList;
					//this.formData.tenantId=list[0].id
					///mList1.push();
					this.tenantList=list
					
					this.getTenantListSort()
					this.merchantChangeApi()
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			getTenantListSort( ){
				var list=this.tenantList
				for(var i  in list){
					var obj=list[i]
					if(this.formData.tenantId==obj.id){
						var obj1=list[this.current-1];
						list[this.current-1]=obj;
						list[i]=obj1;
					}
				}
				
				var mList1=[]
				mList1.push({
					id: '',
					name: '全部'
				});
				for (var i = 0; i < list.length; i++) {
					if (i >= 3) {
					
					} else {
						mList1.push(list[i]);
					}
				}
				
				this.tenantList1 = mList1;
			},
			extraInfoText(extraInfo){
				var obj=JSON.parse(extraInfo)
				if(obj.outOrderNo){
					return  `充值订单编号：${obj.outOrderNo}`
				}else{
					return ''
				}
				
				
			},
			selector2confirm(e){
				
				this.tabsFrom.show2Text=e.year+"年"+e.month+"月"
				
				this.tabsFrom.show2Index=e.year+"-"+e.month
				if(e.day){
					this.tabsFrom.show2Text+=e.day+"日"
					this.tabsFrom.show2Index+='-'+e.day	
				}
				//this.tabsFrom.show2Index=this.tabsFrom.show2Text
				
			
				if(e.day){
					this.formData.startDate=e.year+"-"+e.month +"-"+e.day	
				
					this.queryMonth=e.year+"年"+e.month+"月"+e.day+"日"
					this.formData.endDate=this.formData.startDate
					//nextDay(e.year,e.month, e.day)
					
					
				}else{
					this.queryMonth=e.year+"年"+e.month+"月"
					this.formData.startDate=e.year+"-"+e.month +"-1"
					
					this.formData.endDate=nextMonth(e.year,e.month, 1,1)
					
					
				}
				this.rechargeRecord()
			},
			selector2reset(e){
			
				this.tabsFrom.show2Text='全部时间'
				this.tabsFrom.show2Index='';
				
				var date=new Date()
				this.formData.startDate=date.getFullYear()+"-"+(date.getMonth() + 1) +"-1"
				this.queryMonth=date.getFullYear()+"年"+(date.getMonth() + 1)+"月" 
				this.rechargeRecord()
			},
			selector1confirm(e){
			
				var index=e[0]
				this.tabsFrom.show1Index=index
				this.tabsFrom.show1Text=this.tabsFrom.selector1[index].label
				this.formData.type=this.tabsFrom.selector1[index].value
				this.rechargeRecord()
			},
			rechargeRecordGraph(){
				
				API.rechargeRecordGraph({
					tenantId:this.formData.tenantId
				}).then((response) => {
					
					var list=response.data;
					
					  // uni.getSystemInfo({  
					  //     success: (res) => {  
					  //       const screenWidth = res.windowWidth; // 屏幕宽度,单位为px  
					  //       console.log('屏幕宽度:', screenWidth); 
							
					  //     },  
					  //   });  
						 this.getPle(list)
						//uni.upx2px(600)
					
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			getPle(list){
				
				if (!this.myChart) {
					this.myChart = echarts.init(document.getElementById('pieEcharts'),null,{
						width:uni.upx2px(700),height:uni.upx2px(280)
					});
				}
				this.myChart.clear();
				var data1=list.map(item=>{
					return item.month.replace("-", "年")+"月"
				});
				var data2=list.map(item=>{
					return item.monthAmount
				});
				var headitemby=""
				var showkey = "";
				var option = {
					 tooltip: {
					    trigger: 'axis'
					  },
					grid: {
						top: '6%',
						left: '3%',
						right: '8%',
						bottom: '8%',
						containLabel: true
					},
					  xAxis: {
					    type: 'category',
					    data: data1,
						axisLabel: {
							formatter: (value) => {
								if (headitemby == "") {
									headitemby = value
								}
								var i = 0
								
								var showvalue = "";
								var key = value.substring(0, 5);
						
								if (showkey == "" || value == headitemby) {
									showkey = key
						
									showvalue = value
									showvalue = value.substring(2)
								} else {
									if (key != showkey) {
										showkey = key
										showvalue = value
										showvalue = value.substring(2)
									} else {
										showvalue = value.substring(5)
									}
								}
								return showvalue.replace("年", ".").replace("月", "");
						
							},
							textStyle: {
								color: "#333"
							}
						},
					  },
					  yAxis: {
					    type: 'value'
					  },
					  series: [
					    {
						  name: '合计',
					      data: data2,
					      type: 'line'
					    }
					  ]
				}
				console.log(option)
				this.myChart.setOption(option);
				
				
			},
			rechargeRecord(){
				uni.showLoading({
					mask:true,title:'加载中...'
				})
				API.rechargeRecord(this.formData).then((response) => {
					uni.hideLoading();
					this.list=response.data.data;
				}).catch(error => {
					uni.hideLoading();
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	
	// 标签
	.tabs-box{
		  display: flex;
		  align-items: center;
		  justify-content: space-between;
		  padding: 16rpx 32rpx 16rpx 0;
		  .tabs{
			  width: 96%;
		  }
		  /deep/.u-tabs{
			  background-color: inherit !important
		  }
	}
	
	// 标签
	.tabs-box{
		  display: flex;
		  align-items: center;
		  justify-content: space-between;
		  padding: 16rpx 32rpx 16rpx 0;
		  .tabs{
			  width: 96%;
		  }
	}
	.tabs-box{
		background-color: #fff;
		  display: flex;
		  align-items: center;
		  justify-content: space-between;
		  padding: 16rpx 32rpx 16rpx 32rpx;
		  .tabs1{
			  width: 96%;
		  }
	}
	
	
	.popup-tabs {
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-top: 32rpx;
		padding-right: 32rpx;
		color: rgba(51, 51, 51, 1);
		font-size: 32rpx;
	
		.tabs1 {
			width: 88%;
	
		}
	}
	
	/deep/.u-tab-item {
		width: 25% !important;
		flex: none !important;
	}
	
		.tabs-options {
			//display: flex;
			padding: 24rpx 32rpx ;
			flex-wrap: wrap;
		
			.item {
				//width: 25%;
				color:rgb(119, 119, 119);
				margin-bottom: 24rpx;
				font-size: 32rpx;
			}
			.item-index{
				    color: rgb(51, 51, 51);
					font-weight: bold;
			}
		}
		.head-head{
			  box-shadow: 0px 4px 4px 0px rgba(0, 0, 0, 0.15);
			background: #fff;
			padding:24rpx;
			position: fixed;
			    left: 0;
			    right: 0;
			    top: 80rpx;
			    z-index: 991;
		}
		.head-head-placeholder{
			height: 112rpx;
			
		}
	// 折线图
	.chart-box {
		padding: 24rpx 32rpx;
		background: linear-gradient(180deg, rgba(203, 234, 255, 1) 0%, rgba(255, 255, 255, 1) 100%);

		.title {
			color: rgb(16, 16, 16);
		}

		.sum {
			color: rgb(51, 51, 51);
			font-size: 64rpx;
			margin-top: 16rpx;

			.unit {
				color: rgb(16, 16, 16);
				font-size: 28rpx;
				margin-left: 8rpx;
			}
		}

		.chart {
			width: 100%;
			height: 240rpx;
			margin-top: 8rpx;

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

	}

	// 标签
	.tabs {

		height: 96rpx;
		line-height: 96rpx;
		background-color: #fff;
		border-top: 1px solid rgba(241, 241, 241, 1);
		display: flex;
		justify-content: space-around;

	}

	// 记录
	.records {
		.time {
			padding: 24rpx 32rpx;
			color: rgba(119, 119, 119, 1);
			font-size: 32rpx;
		}

		.details {
			background-color: #fff;
			padding: 32rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.details-title {
				.title {
					color: rgba(51, 51, 51, 1);
					font-size: 32rpx;
				}

				.record-time {
					color: rgb(153, 153, 153);
					font-size: 24rpx;

					margin-top: 8rpx;
				}
			}

			.sum {
				color: rgb(16, 16, 16);
				font-size: 40rpx;
			}
		}
	}
</style>