<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" keyName="dictValue" 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.ordNo" @custom="custom" @search="custom"></u-search>
			</view>
		</view>
		<view class="content" style="margin-top: 100px;">
			<view class="contentBox" v-for="(item,index) in dataList" :key="index" @click="choice(item,index)">
				<view style="width: 100%;margin: 0 auto;">
					<u-cell-group :border="false">
						<u-cell :border="false" center :title="item.ordNo" arrow-direction="down">
							<view slot="icon" style="width: 10rpx;height: 35rpx;background-color: #fd4b09;"></view>
							<view slot="value">
								<view
									style="padding: 5rpx 30rpx;border:1rpx solid #FD4B09;border-radius: 100rpx;color: #FD4B09;">
									{{item.status}}
								</view>

							</view>
						</u-cell>
					</u-cell-group>
					<view class="textBox">
						<view style="font-size: 32rpx;">{{item.customerName}}</view>
						<view>{{item.businesDate?item.businesDate.slice(0,10):''}}</view>
					</view>
					<view class="textBox">
						<view class="widthdian">{{item.goodsNameJoin}}</view>
					</view>
					<!-- 				<view class="textBox">
						<view style="font-size: 28rpx;">数量:{{Number(item.goodsTotalNum).toFixed(0)}}</view>
					</view> -->
					<!-- 				<view class="textBox">
						<view>订单金额:￥{{item.totalMoney}}</view>
						<view style="color: #FD4B09;font-size: 32rpx;">
							{{(item.totalMoney - item.paymentAmountTl) == 0?'已结清':'未收款:￥' + (item.totalMoney - item.paymentAmountTl) +'元'}}
						</view>
					</view> -->
					<view class="textBox">
						<view style="font-size: 28rpx;">资金方:</view>
						<view style="font-size: 28rpx;">{{item.fundingPartyName}}</view>
					</view>
					<view class="textBox">
						<view style="font-size: 28rpx;">配资比例:</view>
						<view style="font-size: 28rpx;">{{item.proportion}}</view>
					</view>
					<view class="textBox">
						<view style="font-size: 28rpx;">保证金:</view>
						<view style="font-size: 28rpx;">{{item.bondAmount}}</view>
					</view>
					<view class="textBox">
						<view style="font-size: 28rpx;">融资金额:</view>
						<view style="font-size: 28rpx;">{{item.fundingAmount}}</view>
					</view>
					<view class="textBox">
						<view style="font-size: 28rpx;">采购数量/采购金额:</view><span
							style="font-size: 28rpx;">{{item.goodsTotalNum}}/{{item.totalMoney}}</span>
					</view>
					<view class="textBox">
						<view style="font-size: 28rpx;">到货数量/到货金额:</view><span
							style="font-size: 28rpx;">{{item.outGoodsTotalShipNum}}/{{item.goodsTotalShipAmount}}</span>
					</view>
					<view class="textBox">
						<view style="font-size: 28rpx;">在库数量/在库金额:</view><span
							style="font-size: 28rpx;">{{item.surplusShipNum}}/{{item.surplusShipAmount}}</span>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		queryBrand
	} from '@/api/views/stock/index.js'
	import {
		queryList,
		corpsDescList,
		gainUser
	} from '@/api/views/sale/salesOrderList.js'
	import {
		collectPayment,
		getCorpDesc
	} from '@/api/views/sale/salesOrderDetails.js'
	import {
		dateFormat
	} from '@/common/dateFormat'
	import {
		logo
	} from '../../../common/setting'
	export default {
		data() {
			return {
				timeOpen: false,
				datetime: Number(new Date()),
				dataList: [],
				screen: [],
				columns: [],
				show: false,
				openPopup: false,
				sortOpen: false,
				mask: false,
				current: 0,
				total: 0,
				keyName: 'cname',
				status: 'loadmore',
				form: {
					current: 1,
					size: 20,
					bsType: "RZCG",
				},
				formTwo: {},
				tabsList: [{
					dictKey: null,
					dictValue: '全部'
				}, {
					dictKey: "录入",
					dictValue: "录入"
				}, {
					dictKey: "待确认",
					dictValue: "待确认"
				}, {
					dictKey: "已收货",
					dictValue: "已收货"
				}],
				scrollTop: 0,
				// 会员卡余额
				balanceAmount: 0,
				// 深拷贝的优惠金额数据
				JsonthisUsedProfit: 0
			}
		},
		onLoad() {
			this.dataList = []
			this.query()
		},
		onReachBottom() {
			this.status = 'loading'
			if (this.dataList.length < this.total) {
				this.form.current++
				this.query()
			} else {
				this.status = 'nomore'
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		methods: {
			newCompany() {
				uni.$u.route('/pages/views/product/createCompany', {
					code: "account",
					title: "收款账户"
				});
			},
			custom(e) {
				this.form.current = 1
				this.dataList = []
				this.query()
			},
			//时间确认选择
			confirmTiem(e) {
				this.formTwo[this.screen[1]] = dateFormat(new Date(e.value), "YYYY-MM-DD HH:mm:ss")
				this.timeOpen = false
				this.mask = false
			},
			thisUsedProfitchange() {
				// 订单金额
				this.formTwo.orderAmount = this.formTwo.orderAmount ? Number(this.formTwo.orderAmount) : 0
				// 优惠金额
				this.formTwo.thisUsedProfit = this.formTwo.thisUsedProfit ? Number(this.formTwo.thisUsedProfit) : 0
				// 已收金额
				this.formTwo.settlmentAmount = this.formTwo.settlmentAmount ? Number(this.formTwo.settlmentAmount) : 0
				// 应收金额
				this.formTwo.debitAmount = this.formTwo.debitAmount ? Number(this.formTwo.debitAmount) : 0
				// 会员卡金额
				this.formTwo.memberAmout = this.formTwo.memberAmout ? Number(this.formTwo.memberAmout) : 0
				if (this.formTwo.thisUsedProfit < 0) {
					this.$refs.uToast.show({
						type: 'error',
						icon: false,
						message: "优惠金额不能小于零",
					})
					this.formTwo.thisUsedProfit = 0
				}
				if (this.formTwo.thisUsedProfit > this.formTwo.orderAmount) {
					this.$refs.uToast.show({
						type: 'error',
						icon: false,
						message: "优惠金额不能大于订单金额",
					})
					this.formTwo.thisUsedProfit = 0
				}
				this.formTwo.debitAmount = this.formTwo.orderAmount - this.formTwo.thisUsedProfit
				this.formTwo.memberAmout = this.formTwo.debitAmount - this.formTwo.currentAmount
				this.formTwo.currentAmount = this.formTwo.debitAmount - this.formTwo.memberAmout


			},
			// 会员卡收款监听事件
			memberBalancechange() {
				// 订单金额
				this.formTwo.orderAmount = this.formTwo.orderAmount ? Number(this.formTwo.orderAmount) : 0
				// 优惠金额
				this.formTwo.thisUsedProfit = this.formTwo.thisUsedProfit ? Number(this.formTwo.thisUsedProfit) : 0
				// 已收金额
				this.formTwo.settlmentAmount = this.formTwo.settlmentAmount ? Number(this.formTwo.settlmentAmount) : 0
				// 应收金额
				this.formTwo.debitAmount = this.formTwo.debitAmount ? Number(this.formTwo.debitAmount) : 0
				// 会员卡金额
				this.formTwo.memberAmout = this.formTwo.memberAmout ? Number(this.formTwo.memberAmout) : 0

				if (this.formTwo.memberAmout < 0) {
					this.$delete(this.formTwo, 'memberAmout')
					this.$set(this.formTwo, 'memberAmout', 0)
					this.$refs.uToast.show({
						type: 'error',
						icon: false,
						message: "会员卡金额不能小于零",
					})
					// this.formTwo.memberAmout = 0
				}
				if (this.formTwo.memberAmout >= this.balanceAmount) {
					this.$delete(this.formTwo, 'memberAmout')
					this.$set(this.formTwo, 'memberAmout', this.balanceAmount)
					// this.formTwo.memberAmout = this.balanceAmount
					this.formTwo.currentAmount = 0
					this.$refs.uToast.show({
						type: 'error',
						icon: false,
						message: "会员卡金额不能大于余额",
					})
				}
				const curr = this.formTwo.debitAmount - this.formTwo.settlmentAmount
				if (this.formTwo.memberAmout > curr) {
					// this.formTwo.memberAmout = curr
					this.$delete(this.formTwo, 'memberAmout')
					this.$set(this.formTwo, 'memberAmout', curr)
				}
				this.formTwo.currentAmount = this.formTwo.debitAmount - this.formTwo.settlmentAmount - this.formTwo
					.memberAmout
			},
			discount(type) {
				if (type == 0) {
					if (this.formTwo.thisUsedProfit < 0) {
						return this.formTwo.thisUsedProfit = null
					}
					this.formTwo.debitAmount = (this.formTwo.orderAmount - this.formTwo.thisUsedProfit).toFixed(2)
					if (this.formTwo.debitAmount < 0) {
						this.$set(this.formTwo, "debitAmount", null)
						this.$set(this.formTwo, "thisUsedProfit", null)
						uni.showToast({
							icon: "none",
							title: '优惠金额不能大于订单金额',
							mask: true
						});
					}
					if ((this.formTwo.currentAmount - this.formTwo.debitAmount) > 0) {
						this.formTwo.overPayment = (this.formTwo.currentAmount - this.formTwo.debitAmount).toFixed(2)
					} else {
						this.formTwo.overPayment = '0.00'
					}
				} else if (type == 1) {
					// 已收金额
					this.formTwo.settlmentAmount = this.formTwo.settlmentAmount ? Number(this.formTwo.settlmentAmount) : 0
					// 应收金额
					this.formTwo.debitAmount = this.formTwo.debitAmount ? Number(this.formTwo.debitAmount) : 0
					this.formTwo.memberAmout = this.formTwo.memberAmout ? Number(this.formTwo.memberAmout) : 0
					this.formTwo.currentAmount = this.formTwo.currentAmount ? Number(this.formTwo.currentAmount) : 0
					console.log((this.formTwo.debitAmount - this.formTwo.settlmentAmount - this.formTwo.memberAmout), 353);
					if (this.formTwo.currentAmount > (this.formTwo.debitAmount - this.formTwo.settlmentAmount - this
							.formTwo.memberAmout)) {
						this.$refs.uToast.show({
							type: 'error',
							icon: false,
							message: "收款和会员卡金额不能大于应收金额",
						})
						this.formTwo.currentAmount = 0
					}

					if (this.formTwo.currentAmount < 0) {
						return this.formTwo.currentAmount = 0
					}
					// if ((this.formTwo.currentAmount - this.formTwo.orderAmount) > 0) {
					// 	this.formTwo.overPayment = (this.formTwo.currentAmount - this.formTwo.orderAmount).toFixed(2)
					// } else {
					// 	this.formTwo.overPayment = '0.00'
					// }
				}
			},
			collectionConfirm() {
				if (!this.formTwo.orderAmount) {
					return uni.showToast({
						icon: "none",
						title: '合同金额不能为空',
						mask: true
					});
				}
				// 判断本次收款和会员卡收款不能为空
				if (!this.formTwo.currentAmount || this.formTwo.currentAmount == 0) {
					if (!this.formTwo.memberAmout || this.formTwo.memberAmout == 0) {
						return uni.showToast({
							icon: "none",
							title: '本次收款金额或会员卡金额不能为空或零',
							mask: true
						});
					}
				}
				if (!this.formTwo.account) {
					return uni.showToast({
						icon: "none",
						title: '收款账户不能为空',
						mask: true
					});
				}
				this.formTwo.currentAmount = this.formTwo.currentAmount ? this.formTwo.currentAmount : 0
				let this_ = this
				uni.showModal({
					title: '收款:',
					content: '现金收款:' + this_.formTwo.currentAmount + '元\n会员卡:' + this_.formTwo.memberAmout + '元',
					success: function(rest) {
						if (rest.confirm == true) {
							uni.showLoading({
								title: '加载中',
								mask: true
							});
							collectPayment(this_.formTwo).then(res => {
								uni.showToast({
									icon: "none",
									title: '收款成功',
									mask: true,
									duration: 2000
								});
								this_.openPopup = false
								this_.mask = false
								setTimeout(function() {
									uni.hideLoading();
									this_.dataList = []
									this_.query()
								}, 2000);
							})
						}
					}
				})
			},
			//集合筛选条件
			filterAll(screen) {
				this.screen = screen
				switch (screen[1]) {
					case "account":
						this.$u.api.getWorkDicts('account').then(res => {
							this.columns = [res.data]
							this.keyName = "dictValue"
							this.mask = true
							this.show = true
						})
						break;
					case "advanceCollectionDate":
						this.timeOpen = true
						this.mask = true
						break;
				}
			},
			collection(item, index) {
				this.formTwo = JSON.parse(JSON.stringify(item))
				this.formTwo.advanceCollectionDate = dateFormat(new Date(), "YYYY-MM-DD HH:mm:ss")
				// 深拷贝优惠金额判断是否可以编辑
				this.JsonthisUsedProfit = JSON.parse(JSON.stringify(this.formTwo.thisUsedProfit))

				const curr = this.formTwo.debitAmount - this.formTwo.settlmentAmount
				// const curr = (this.formTwo.orderAmount - this.formTwo.thisUsedProfit - this.formTwo.settlmentAmount).toFixed(2)
				// this.formTwo.currentAmount = (this.formTwo.orderAmount - this.formTwo.settlmentAmount).toFixed(2)
				// 判断剪掉的值是不是大于会员卡余额
				getCorpDesc({
					id: item.corpId
				}).then(res => {
					console.log(res, 478);
					this.balanceAmount = res.data.balanceAmount ? res.data.balanceAmount : 0

					if (curr > this.balanceAmount) {
						this.formTwo.memberAmout = this.balanceAmount
						this.formTwo.currentAmount = curr - this.formTwo.memberAmout
					} else {
						this.formTwo.memberAmout = curr
						this.formTwo.currentAmount = 0
					}

				})
				this.openPopup = true
				this.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/procurement/details', {
					id: item.id,
					corpId: item.corpId
				});
			},
			//查询
			query() {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				queryList({
					notComplete: 1,
					...this.form,
				}).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'
					}
				})
			},
			// tabs切换
			tabsClick(item) {
				this.current = item.index
				this.form.current = 1
				this.form.status = item.dictKey
				this.mask = false
				this.dataList = []
				this.query()

			},
			confirm(e) {
				if (this.screen[0] == "corpId") {
					this.form = {
						current: 1,
						size: 20,
						corpId: e.value[0].id
					}
					this.dataList = []
					this.query()
					this.show = false
					this.mask = false
				} else 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.formTwo.account = e.value[0].dictValue
					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: absolute;
		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%;
	}

	/* 长度过长截取 */
	.widthdian {
		width: 100%;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}
</style>