<template>
	<view style="background-color: #f2f2f6;padding-bottom: 0.01rpx;" :class="mask ? 'tl-show': ''">
		<!-- <u-navbar title="代收款" bgColor="#FD4B09" :autoBack="true" :placeholder="true" leftIconColor="#fff" :titleStyle="{color: '#fff'}"> -->
		<!-- <view slot="right">
				<u-icon name="clock" size="40rpx" color="#fff" @click="historical0rders()"></u-icon>
			</view> -->
		<!-- </u-navbar> -->
		<view style="background-color: #fff;position: fixed;top: var(--window-top);width: 100%;z-index: 8;">
			<u-tabs :list="tabsList" itemStyle="height:88rpx;" lineColor="#FD4B09" @click="tabsClick"
				:current="current">
				<view slot="right" style="font-size: 15px;" @click="sortOpen = !sortOpen,mask = !mask">
					<text style="margin-left: 10rpx;">|<text style="margin:0rpx 15rpx">排序</text></text>
				</view>
			</u-tabs>
			<view style="background-color: #fff;padding: 10rpx 20rpx;">
				<u-search placeholder="请输入客户名称" v-model="form.corpsName" @custom="custom"></u-search>
			</view>
		</view>
		<view class="dropdown" v-if="sortOpen" @click.stop="dropdownOpen()">
			<view style="background-color: #fff;">
				<u-cell-group>
					<u-cell title="客户名称" @click="sortQuery(1)">
						<view style="display: flex;" slot="value" @click.stop="sortQuery(1,form.sort == 0?'1':'0')">
							{{form.typeSort == 1?form.sort==0?'正序':'倒序':''}}
							<u-icon :name="form.typeSort == 1?form.sort==0?'arrow-up-fill':'arrow-down-fill':''">
							</u-icon>
						</view>
					</u-cell>
					<u-cell title="销售单号" @click="sortQuery(2)">
						<view style="display: flex;" slot="value" @click.stop="sortQuery(2,form.sort == 0?'1':'0')">
							{{form.typeSort == 2?form.sort==0?'正序':'倒序':''}}
							<u-icon :name="form.typeSort == 2?form.sort==0?'arrow-up-fill':'arrow-down-fill':''">
							</u-icon>
						</view>
					</u-cell>
					<u-cell title="金额" @click="sortQuery(3)">
						<view style="display: flex;" slot="value" @click.stop="sortQuery(3,form.sort == 0?'1':'0')">
							{{form.typeSort == 3?form.sort==0?'正序':'倒序':''}}
							<u-icon :name="form.typeSort == 3?form.sort==0?'arrow-up-fill':'arrow-down-fill':''">
							</u-icon>
						</view>
					</u-cell>
					<u-cell title="日期" @click="sortQuery(4)">
						<view style="display: flex;" slot="value" @click.stop="sortQuery(4,form.sort == 0?'1':'0')">
							{{form.typeSort == 4?form.sort==0?'正序':'倒序':''}}
							<u-icon :name="form.typeSort == 4?form.sort==0?'arrow-up-fill':'arrow-down-fill':''">
							</u-icon>
						</view>
					</u-cell>
					<u-cell title="提交" @click="sortQuery(5)">
						<view style="display: flex;" slot="value" @click.stop="sortQuery(5,form.sort == 0?'1':'0')">
							{{form.typeSort == 5?form.sort==0?'正序':'倒序':''}}
							<u-icon :name="form.typeSort == 5?form.sort==0?'arrow-up-fill':'arrow-down-fill':''">
							</u-icon>
						</view>
					</u-cell>
				</u-cell-group>
			</view>
		</view>
		<view class="contentBox" style="margin-top: 100px;">
			<view class="textBox">
				<view>待收款总金额</view>
				<view>本月增加</view>
			</view>
			<view class="textBox">
				<view>￥
					<text style="font-size: 40rpx;">{{statistics.total}}</text>
					元
				</view>
				<view>￥
					<text style="font-size: 44rpx;">{{statistics.month}}</text>
					元
				</view>
			</view>
			<view class="textBox">
				<view>流动资金增长率：{{statistics.flowAmount}}%</view>
			</view>
		</view>
		<view class="content">
			<view class="contentBox" v-for="(item,index) in dataList" :key="index">
				<view style="width: 100%;margin: 0 auto;">
					<u-cell-group :border="false">
						<u-cell :border="false" center :titleStyle="{
							'fontSize': '36rpx',
							'color':'#000'
						}" :title="item.corpsName" arrow-direction="down">
							<view slot="icon" style="width: 10rpx;height: 35rpx;background-color: #fd4b09;"></view>
							<view slot="value" @click="choice(item,index)" style="padding: 5rpx 30rpx;border:1rpx solid #FD4B09;border-radius: 100rpx;color: #FD4B09;">收款</view>
						</u-cell>
					</u-cell-group>
					<view class="textBox">
						<view @click="makePhoneCall(item.tel)">{{item.tel}}</view>
						<view style="color: #FD4B09;font-size: 32rpx;">￥{{item.balanceAmount}}元</view>
					</view>
				</view>
			</view>
		</view>
		<u-empty v-if="total == 0" style="position: absolute;top: 45%;left: 50%;transform:translate(-50%,-50%)"
			mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png" />
		<uni-calendar ref="calendar" :insert="false" :range="true" @confirm="confirmCalendar" />
		<u-picker :show="show" :columns="columns" :keyName="keyName" @cancel="show = false" @confirm="confirm"></u-picker>
		<u-loadmore v-if="total !== 0 && dataList.length != 0" :status="status" />
	</view>
</template>

<script>
	import {
		queryList,
		corpsDescList,
		profitStatistics,
		gainUser
	} from '@/api/views/sale/collectionManagement.js'
	export default {
		data() {
			return {
				dataList: [],
				columns: [],
				show: false,
				sortOpen: false,
				mask: false,
				current: 0,
				total: 0,
				status: 'loadmore',
				keyName: 'cname',
				statistics: {
					flowAmount: "0.00",
					month: "0.00",
					total: "0.00"
				},
				form: {
					current: 1,
					size: 20,
				},
				tabsList: [{
					name: '全部',
					badge: {
						value: 0,
					}
				}, {
					name: '订单区间',
					badge: {
						value: 0,
					}
				}, {
					name: '客户名称',
					badge: {
						value: 0,
					}
				}, {
					name: '业务员',
					badge: {
						value: 0,
					}
				}]
			}
		},
		onShow() {
			this.dataList = []
			this.query()
		},
		onReachBottom() {
			this.status = 'loading'
			if (this.dataList.length < this.total) {
				this.form.current++
				this.query()
			} else {
				this.status = 'nomore'
			}
		},
		methods: {
			custom(e){
				this.form = {
					current: 1,
					size: 20,
					corpsName:e
				}
				this.dataList = []
				this.query()
			},
			makePhoneCall(tel) {
				uni.makePhoneCall({
					phoneNumber: tel,
					success: function() {
						console.log('拨打电话成功');
					},
					fail() {
						uni.showToast({
							title: '拨打电话失败',
							icon: 'none',
							mask: true
						});
					}
				})
			},
			sortQuery(val, sort = 0) {
				this.form = {
					current: 1,
					size: 10,
					sort: sort,
					typeSort: val
				}
				this.dataList = []
				this.sortOpen = false
				this.mask = false
				this.current = 0
				this.query()
			},
			dropdownOpen() {
				this.sortOpen = false
				this.mask = false
			},
			choice(item, index) {
				uni.$u.route('/pages/views/salesSlip/aiiReceivables', item);
			},
			//查询
			query() {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				queryList({
					...this.form,
					billType: "XS",
					tradeType: "YPJ",
					modular: 1
				}).then(res => {
					if (res.data.records) {
						this.dataList = this.dataList.concat(res.data.records)
					}
					this.total = res.data.total || 0
					uni.hideLoading();
					if (this.total < 10) {
						this.status = 'nomore'
					}
				})
				profitStatistics(this.form).then(res => {
					this.statistics = res.data
				})
			},
			tabsClick(item) {
				this.current = item.index
				if (item.name == "订单区间") {
					this.$refs.calendar.open()
				} else if (item.name == "客户名称") {
					uni.showLoading({
						title: '加载中',
						mask: true
					});
					corpsDescList({
						corpType: "KH"
					}).then(res => {
						this.columns = [res.data]
						this.show = true
						this.keyName = "cname"
						uni.hideLoading();
					})
				} else if (item.name == "业务员") {
					this.keyName = "name"
					this.screen = ['sales', 'salesName']
					uni.showLoading({
						title: '加载中',
						mask: true
					});
					gainUser().then(res => {
						this.columns = [res.data]
						this.show = true
						this.mask = true
						uni.hideLoading();
					})
				} else if (item.name == "全部") {
					this.form = {
						current: 1,
						size: 20
					}
					this.dataList = []
					this.query()
				}
			},
			confirm(e) {
				if (this.screen[1] == "salesName") {
					this.form = {
						current: 1,
						size: 20,
						salesName: e.value[0].id
					}
					this.dataList = []
					this.query()
					this.show = false
					this.mask = false
				}else{
					this.form = {
						current: 1,
						size: 20,
						corpId: e.value[0].id
					}
					this.dataList = []
					this.query()
					this.show = false
					this.mask = false
				}
			},
			//确认日期
			confirmCalendar(e) {
				this.form = {
					current: 1,
					size: 20,
					orderStartDate: e.range.before + ' 00:00:00',
					orderEndDate: e.range.after + ' 23:59:59'
				}
				this.dataList = []
				this.query()
			},
		}
	}
</script>

<style scoped>
	.contentBox {
		width: 96%;
		margin: 20rpx auto;
		background-color: #FFFFFF;
		border-radius: 20rpx;
		/* box-shadow: 0 5rpx 14rpx 0 rgba(101, 176, 249, 0.42); */
		padding-top: 15rpx;
		padding-bottom: 10rpx;
	}

	.textBox {
		padding: 0 15px;
		display: flex;
		justify-content: space-between;
		font-size: 24rpx;
		margin-bottom: 10rpx;
		align-items: flex-end;
	}

	.dropdown {
		position: fixed;
		z-index: 9;
		background: rgba(0, 0, 0, .3);
		width: 100%;
		left: 0;
		top: calc(var(--status-bar-height) + 90px);
		bottom: 0;
	}

	.tl-show {
		overflow: hidden;
		position: fixed;
		height: 100%;
		width: 100%;
	}
</style>
