<!-- 支付来源分析 -->
<template>
	<view class="revenue">
		<view class="revenue-line">
			<LineChart v-if="chartData.series[0].data.length" :chartData="chartData" :title="title" :opts="opts"/>
			<view class="empty" v-else>
				<u-empty></u-empty>
			</view>
		</view>
	</view>
</template>

<script>
	import LineChart from '@/components/lineChart.vue'
	export default {
		components: {
			LineChart
		},
		props: {
			title: {
				type: String,
				default: ''
			},
			opts: {
				type: Object,
				default: () => {
					return {
						xAxis: {
							rotateLabel: true
						},
						yAxis: {
							showTitle: true,
							splitNumber: 5,
							data: [{
								title: '元',
								titleOffsetY: -5
							}]
						},
						legend: {
							show: false
						},
						dataLabel: false,
						extra: {
							column: {
								width: 20
							}
						}
					}
				}
			}
		},
		data() {
			return {
				paySourceList: [],
				chartData: {
					categories: [],
					series: [{
						name: '',
						data: []
					}]
				},
				reportType: '',
				queryDate: ''
			}
		},
		created() {
			this.getDict()
		},
		methods: {
			getData({ reportType, queryDate }) {
				this.reportType = reportType
				this.queryDate = queryDate
				this.getPaySourceData()
			},
			getDict() {
				uni.$u.api.getDictApi({ type: 'pay_platform' }).then(res => {
					if (res.code === 200) {
						this.paySourceList = res.data
					}
				})
			},
			getPaySourceData() {
				uni.$u.api.operationalAnalysisApi.getPaySourceDataApi({
					reportType: this.reportType,
					queryDate: this.queryDate
				}).then(res => {
					console.log(res)
					if (res.code === 200) {
						if (res.data.itemList && res.data.itemList.length) {
							let cateList = []
							res.data.itemList.forEach(item => {
								cateList.push(this.getDictLabel(item.payPlatform))
							})
							this.chartData.categories = cateList
							this.chartData.series[0].data = res.data.itemList.map(item => {
								return item.amt
							})
						} else {
							this.chartData.categories = []
							this.chartData.series[0].data = []
						}
					}
				})
			},
			getDictLabel(value) {
				let name
				this.paySourceList.forEach(item => {
					if (item.dictValue == value) {
						name = item.dictLabel
					}
				})
				if (!name) name = '其他'
				return name
			}
		}
	}
</script>

<style lang="scss" scoped>
	.revenue {
		background-color: #fff;
		border-radius: 5px;
		.empty {
			padding: 15px;
		}
	}
</style>
