<template>
	<view class="container">
		<view class="page">
			<view class="property">
				<view class="user">
					<view class="name">{{userName}} {{userPhone}}</view>
					<view class="house-box">
						<view class="add">{{platformName}}</view>
						<picker @change="onHouseChange" range-key="label" :range="houseList">
							<view class="uni-input">{{houseList[houseIndex].label}}</view>
						</picker>
					</view>
				</view>
				<view class="screening">
					<picker @change="onYearChange" :range="yearList">
						<view class="uni-input">{{yearList[index]}}<image src="../../static/images/icon_up.png" class="icon"></image></view>
					</picker>
					<view class="content">
						<view class="text">
							<view class="name">合计金额（元）</view>{{costTotalAmount}}
						</view>
						<view class="text">
							<view class="name">缴费单位</view>{{propertyName}}
						</view>
					</view>
					<view class="bottom" v-if="index === 0">近三年{{costName}}累计：{{costTotalAmount}}元</view>
					<view class="bottom" v-else>{{yearList[index]}}度{{costName}}累计：{{costYearAmount}}元</view>
				</view>
				<view class="statistical">
					<qiun-data-charts type="line" :chartData="chartData" :opts="opts" background="#fff" />
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getHouseListApi,
		communityPaymentStatisticsApi
	} from '@/config/api.js'
	import {Moment} from '@/utils/moment.js'
	let chargeTypeCode = ''
	export default {
		data() {
			return {
				userName: '',
				userPhone: '',
				platformName: '',
				costName: '',
				costTotalAmount: 0,
				costYearAmount: 0,
				houseList: [],
				houseIndex: 0,
				propertyName: '',
				index: 0,
				yearList: [],
				chartData: {
					categories: ['2021年', '2022年', '2023年'],
					series: [{
						"name": "费用统计",
						"data": [13, 34, 1200]
					}]
				},
				opts: {
					color: ["#6A59F2", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",	"#ea7ccc"],
					padding: [15, 10, 0, 10],
					enableScroll: false,
					legend: {},
					xAxis: {
						disableGrid: true
					},
					yAxis: {
						gridType: "dash",
						dashLength: 2
					},
					extra: {
						line: {
							type: "curve",
							width: 2,
							activeType: "hollow"
						}
					},
					update: true
				}
			}
		},
		methods: {
			async getHouseList() {
				let {result} = await getHouseListApi({
					pageNo: 1,
					pageSize: 20,
					auditStatus: "auditPass"
				})
				if(result.total > 0) {
					this.propertyName = result.records[0].propertyName
					this.houseList = result.records.map(item=> {
						return {
							label: item.buildingName + item.unitName + item.roomName,
							value: item.roomId
						}
					})
					this.getStatistical()
				}
			},
			async getStatistical() {
				let roomId = this.houseList[this.houseIndex].value
				let year = this.index === 0 ? '0' : this.yearList[this.index].replace('年', '')
				let {result} = await communityPaymentStatisticsApi({
					roomId,
					chargeTypeCode,
					year
				})
				let yearList = []
				let yearData = []
				result.forEach(item=> {
					if(this.index === 0) {
						yearList.push(`${item.YEAR}`)
					} else {
						yearList.push(`${item.MONTH}`)
					}
					yearData.push(item.totalAmount)
				})
				this.costYearAmount = yearData.reduce((pra, cur)=> {
					return pra + cur
				}, 0)
				if(this.index === 0) {
					this.costTotalAmount = this.costYearAmount
				}
				this.chartData.categories = yearList
				this.chartData.series = [
					{
						name: `${this.costName}统计`,
						data: yearData
					}
				]
			},
			onHouseChange(e) {
				this.houseIndex = Number(e.detail.value)
				this.index = 0
				this.getStatistical()
			},
			async onYearChange(e) {
				this.index = Number(e.detail.value)
				this.getStatistical()
			}
		},
		onLoad(option) {
			this.userName = uni.getStorageSync('user_name')
			this.userPhone = uni.getStorageSync('user_phone')
			this.platformName = uni.getStorageSync('platform_name')
			this.costName = option.name || '费用'
			chargeTypeCode = option.code

			let nowYear = new Moment().format('YYYY')
			let lastYear = (Number(nowYear) - 1).toString() + '年'
			let beforeear = (Number(nowYear) - 2).toString() + '年'
			this.yearList = ['全部', beforeear, lastYear, nowYear + '年']
			this.getHouseList()
		}
	}
</script>
<style>
	page {
		background-color: #F8F6F9;
	}
</style>
<style lang="scss" scoped>
	.page {
		padding: 30rpx;
	}

	.property {

		.user {
			background-position: top center;
			background-repeat: no-repeat;
			background-size: cover;
			background-image: url('https://life.cloud.hjxbc.cn/sys/common/static/miniapp/bg_cost.png');
			border-radius: 16rpx;
			padding: 40rpx;

			.name {
				font-size: 32rpx;
				color: #ffffff;
				margin-bottom: 20rpx;
			}

			.house-box {
				display: flex;
				align-items: center;

				.add {
					font-size: 28rpx;
					color: #D5DCFF;
				}

				.uni-input {
					color: #D5DCFF;
					margin-left: 30rpx;
					position: relative;
					&::after {
						position: absolute;
						content: " ";
						width: 0;
						height: 0;
						top: 50%;
						right: -15px;
						border-top: 6px solid #D5DCFF;
						border-right: 6px solid transparent;
						border-left: 6px solid transparent;
						transform-origin: center;
						transform: translateY(-50%);
					}
				}
			}
		}
		.uni-input {
			font-size: 28rpx;
			color: #373737;
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.icon {
			width: 20rpx;
			height: 12rpx;
			margin-left: 15rpx;
		}

		.screening {
			margin: 30rpx 0;
			background-color: #ffffff;
			border-radius: 16rpx;
			padding: 30rpx;

			.title {
				display: flex;

				// .picker {
				// 	display: flex;
				// 	align-items: center;

				// 	.uni-input {
				// 		font-size: 28rpx;
				// 		color: #373737;
				// 	}

				// 	.icon {
				// 		width: 20rpx;
				// 		height: 12rpx;
				// 		margin-left: 15rpx;
				// 	}
				// }
			}

			.uni-input {
				color: #373737;
			}

			.content {
				padding: 20rpx 0;
				margin: 30rpx 0;
				border-top: 1px solid #F0F0F2;
				border-bottom: 1px solid #F0F0F2;

				.text {
					display: flex;
					font-size: 28rpx;
					color: #373737;
					margin: 15rpx 0;

					.name {
						width: 230rpx;
						color: #9D9CA6;
					}
				}
			}

			.bottom {
				font-size: 28rpx;
				color: #6A59F2;
			}

		}

		.statistical {
			background-color: #ffffff;
			border-radius: 16rpx;
			padding: 30rpx 10rpx;
		}

	}
</style>