<template>
	<view class="my_commission_wrap">
		<view class="queryParams_wrap">
			<view class="search">
				<u--input clearable prefixIcon="search" v-model="queryParams.phone" shape="circle" clearable
					@blur="handleKeyword" @clear="handleKeywordClear" placeholder="请输入电话"></u--input>
			</view>
			<view class="query">
				<view style="margin-right: 10rpx;" @click="handleshowFilter">筛选</view>
				<u-icon name="arrow-down-fill" color="#aaa" size="10"></u-icon>
			</view>
		</view>

		<view class="commission_item_wrap">
			<!-- 统计信息 -->
			<view class="commission_item" v-for="item in listData" :key="item.id">
				<view class="commission_top">
					<view class="top_left">{{item.item}}</view>
					<view class="top_right">
						<text class="account_type">{{ item.accountType === '1' ? '前端' : '后端' }}</text>
					</view>
				</view>
				<view class="commission_info">
					<view class="info_row">
						<view class="info_item">
							<text class="label">电话: </text>
							<show-real-text :real="item.phone" :type='type'></show-real-text>
						</view>
					</view>
					<view class="info_row">
						<view class="info_item">
							<text class="label">收单时间: </text>
							<text class="value">{{item.receiptDate || '-'}}</text>
						</view>
					</view>
					<view class="info_row">
						<view class="info_item">
							<text class="label">收单人: </text>
							<text class="value">{{item.receiptNickName || '-'}}</text>
						</view>
						<view class="info_item">
							<text class="label">公司: </text>
							<text class="value">{{item.orgName || '-'}}</text>
						</view>
					</view>
					<view class="info_row">
						<view class="info_item">
							<text class="label">分成所属人: </text>
							<text class="value">{{item.userName || '-'}}</text>
						</view>
						<view class="info_item">
							<text class="label">分成比例: </text>
							<text class="value">{{item.commissionRate || '-'}}%</text>
						</view>
					</view>
				</view>
				<view class="commission_amount">
					<view class="amount_item">
						<view class="amount_label">业绩</view>
						<view class="amount_value primary">{{formatAmount(item.commissionAmount)}}</view>
					</view>
					<view class="amount_item">
						<view class="amount_label">毛业绩</view>
						<view class="amount_value">{{formatAmount(item.grossAmount)}}</view>
					</view>
				</view>
			</view>
		</view>

		<filter-query ref="filter" v-model="queryParams" @getList="resetData" :mapHeight="mapHeight"></filter-query>

		<!-- 加载更多 -->
		<u-loadmore :status="loadStatus" v-if="listData.length > 0" />
		<!-- 空状态 -->
		<show-emtry v-if="listData.length === 0"></show-emtry>
	</view>
</template>

<script>
	import pullUpRefresh from "@/utils/pullUpRefresh";
	import filterQuery from "./filterQuery.vue";
	import dayjs from "dayjs";
	export default {
		mixins: [pullUpRefresh],
		components: {
			filterQuery,
		},
		props: {
			type: {
				type: String,
				default: '2'
			},
			showStats: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {
				queryParams: {
					receiptDateEnd: dayjs().format("YYYY-MM-DD"),
					receiptDateStart: dayjs().startOf('month').format("YYYY-MM-DD"),
					item: undefined,
					phone: undefined,
					receiptUserId: undefined,
					pageNum: 1,
					pageSize: 10,
				},

				mapHeight: "0px"
			}
		},
		methods: {
			handleKeyword() {
				this.resetData();
			},
			handleKeywordClear() {
				// 组件有bug 清空后的值还是存在
				this.queryParams.phone = "";
				this.resetData();
			},
			handleshowFilter() {
				this.$refs.filter.show();
			},
			formatAmount(amount) {
				if (!amount) return '-';
				return parseFloat(amount).toLocaleString();
			},
			async getList() {
				const {
					pageNum,
					pageSize,
				} = this.queryParams;
				this.queryParams.type = this.type;
				// 调用分成接口
				const {
					rows,
					total
				} = await uni.$u.api.selectCommissionList({
					pageSize,
					pageNum,
				}, this.queryParams);
				return rows;
			},
			handleOnReachBottom() {
				this.handleReachBottom();
			}
		},
		mounted() {
			this.resetData();
			uni.getSystemInfo({
				success: (e) => {
					const {
						windowTop,
						windowBottom,
						windowHeight
					} = e;
					this.mapHeight = windowHeight + 'px';
				}
			});
		}
	}
</script>

<style lang="scss" scoped>
	.my_commission_wrap {
		.queryParams_wrap {
			display: flex;
			background: #fff;
			padding: 14px 0;


			.query,
			.search {
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 16px;
				font-weight: 700;
				color: #202020;
			}

			.query {
				flex: 1;
			}

			.search {
				flex: 2;
				padding-left: 20px;
			}
		}
	}

	.stats_info_wrap {
		margin-bottom: 30px;
	}

	.stats_info_top {
		display: flex;
		margin-bottom: 20px;

		.info_item {
			box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
			width: 23%;
			margin-right: 2.6%;
			height: 120px;
			padding: 15px;
			background: #fff;
			border-radius: 15px;

			&:last-child {
				margin-right: 0;
			}

			.info_title {
				font-size: 14px;
				color: #6b7280;
				margin-bottom: 15px;
			}

			.info_value {
				font-size: 24px;
				font-weight: bold;
				color: #202020;
			}
		}
	}

	.commission_item_wrap {
		padding: 20px 20px;
	}

	.commission_item {
		background: #fff;
		border-radius: 20px;
		padding: 20px;
		margin-bottom: 20px;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);

		.commission_top {
			display: flex;
			justify-content: space-between;
			margin-bottom: 15px;

			.top_left {
				font-size: 18px;
				font-weight: bold;
				color: #202020;
			}

			.top_right {
				.account_type {
					background: #108cff;
					color: #fff;
					padding: 4px 8px;
					border-radius: 10px;
					font-size: 12px;
				}
			}
		}

		.commission_info {
			margin-bottom: 15px;

			.info_row {
				display: flex;
				margin-bottom: 8px;

				&:last-child {
					margin-bottom: 0;
				}

				.info_item {
					flex: 1;
					display: flex;
					align-items: center;
					font-size: 14px;

					.label {
						color: #9b9aa2;
						margin-right: 8px;
						min-width: 60px;
					}

					.value {
						color: #202020;
					}
				}
			}
		}

		.commission_amount {
			display: flex;
			justify-content: space-around;
			padding: 15px 0;
			border-top: 1px solid #f0f0f0;
			background: #f8f9fb;
			border-radius: 10px;

			.amount_item {
				text-align: center;

				.amount_label {
					font-size: 12px;
					color: #9b9aa2;
					margin-bottom: 5px;
				}

				.amount_value {
					font-size: 16px;
					font-weight: bold;
					color: #202020;

					&.primary {
						color: #108cff;
					}
				}
			}
		}
	}

	.empty_wrap {
		margin-top: 100px;
	}
</style>