<template>
	<view class="content flex flex_column" v-if="billInfo">
		<view class="b_head">
			<view class="head_list">
				<text>分享时间</text>
				<text>{{ billInfo.createDate }}</text>
			</view>
			<view class="head_list" v-if="billInfo.customer">
				<text>客户名称</text>
				<text>{{ billInfo.customer.customerName }}</text>
			</view>
			<!-- <view class="head_list">
				<text>对账周期</text>
				<text>{{ timeInfo || '--' }}</text>
			</view> -->
			<view class="head_list">
				<text>付款状态</text>
				<text>{{ billInfo.billStatus == 'UNSETTLE' ? '未付款' : billInfo.settleStyleSnDictValue ? '已结清（' + billInfo.settleStyleSnDictValue + '）' : '已结清' }}</text>
			</view>
		</view>
		<view class="b_detail">
			<view class="detail_list u-flex">
				<view class="detail_box">
					<text>总单数：</text>
					<text>{{ billInfo.detailNum }}</text>
				</view>
				<view class="detail_box">
					<text>待收金额合计：</text>
					<text>￥{{ billInfo.totalAmount ||billInfo.totalAmount == 0 ? toThousands(billInfo.totalAmount,2):'--' }}</text>
				</view>
			</view>
			<view class="detail_list u-flex">
				<view class="detail_box">
					<text>折让金额：</text>
					<text>￥{{ billInfo.discountAmount||billInfo.discountAmount==0? toThousands(billInfo.discountAmount,2):'--'}}</text>
				</view>
				<view class="detail_box">
					<text>折让后待收金额：</text>
					<text>￥{{ billInfo.settleAmount||billInfo.settleAmount ==0? toThousands(billInfo.settleAmount,2):'--' }}</text>
				</view>
			</view>
			<view class="detail_list u-flex" v-if="billInfo.billStatus != 'UNSETTLE'">
				<view class="detail_box">
					<text>实际折让金额：</text>
					<text>￥{{ billInfo.discountAmount||billInfo.discountAmount==0? toThousands(billInfo.discountAmount,2):'--'}}</text>
				</view>
				<view class="detail_box">
					<text>实际收款金额：</text>
					<text>￥{{ billInfo.settleAmount||billInfo.settleAmount ==0? toThousands(billInfo.settleAmount,2):'--' }}</text>
				</view>
			</view>
		</view>
		<view class="detail_con"><billList ref="billBox" :list="billArr" :showCheck="isShowEdit" @allChecked="chooseBillList"></billList></view>
		<view class="footer flex align_center" v-if="billInfo.billStatus == 'UNSETTLE' && billArr&&billArr.length!=0">
			<u-checkbox-group style="flex:1;">
				<u-checkbox v-model="checkedAll" shape="circle" @change="chooseAll">{{checkedAll?'取消全选':'全选'}}</u-checkbox>
			</u-checkbox-group>
			<view class="u-flex u-row-right">
				<u-button type="error" shape="circle" :loading="btnLoading" @click="handleEvents('del')" :custom-style="customStyle">删除</u-button>
				<u-button type="warning" shape="circle" style="margin-left: 30rpx;" @click="handleEvents('update')">一键刷新</u-button>	
			</view>
		</view>
		<u-modal v-model="modalShow" :show-title="false" :content="modalCon" :show-cancel-button="true" @confirm="handleConfirm"></u-modal>
	</view>
</template>

<script>
import billList from './billListComponent.vue';
import { findBySn, insert,flushBill,deleteDetails } from '@/api/verify.js';
import getDate from '@/libs/getDate.js';
import { toThousands } from '@/libs/tools.js'
export default {
	components: { billList },
	data() {
		return {
			customStyle: {
				width:'200rpx',
				color: '#fff'
			},
			verifySn: '',
			billInfo: null,
			billArr: [],
			timeInfo: '',
			btnLoading: false,
			shareShow: false,
			isShowEdit:true,//删除
			shareContent: `是否同意发送对账单给微信好友？`,
			toThousands,
			form:{
				money:''
			},
			checkedAll:false,
			rules: {
				money: [
					{ 
						required: true, 
						message: '请输入实际收款金额', 
						trigger: ['change','blur'],
					}
				]
			},
			orderList:[],
			modalShow:false,
			modalCon:'刷新后，将删除对账单中已收款的订单，更新待收款单据。确定刷新吗？',
			eventsType:'' //打开弹窗类型 del删除  update更新
		};
	},
	onReady() {
		uni.setNavigationBarColor({
			frontColor: '#ffffff',
			backgroundColor: this.$config('primaryColor')
		});
	},
	onLoad(options) {
		if (options && options.billId) {
			this.verifySn = options.billId;
			this.loadData({ verifySn: options.billId });
		}
	},
	onBackPress(e){
		uni.redirectTo({
			url: '/pages/sales/billHistoryDetail?billId=' + this.verifySn
		})
		return true;
	},
	computed: {
		//经销商信息
		userData() {
			return this.$store.state.vuex_userData;
		}
	},
	methods: {
		//一键刷新
		handleUpdate(){
			flushBill({verifySn:this.verifySn}).then(res => {
				if (res.status == 200) {
					this.loadData({ verifySn: this.verifySn })
				}
			});
		},
		//删除
		deleteOrder(){
			let that=this
			deleteDetails({verifySn:that.verifySn,detailSnList:that.orderList}).then(res => {
				if (res.status == 200) {
					uni.showToast({
						title:res.message,
						duration: 2000
					})
					that.loadData({ verifySn: that.verifySn });
				}
			});
		},
		handleEvents(type){
			this.eventsType=type
			if(type == 'del'){
				let allList=this.$refs.billBox.getAllData();
				let newChooseList = []
				let num = 0
				allList.forEach(item=>{
					if(item.checked){
						newChooseList.push(item.detailSn)
						num+=item.unsettleAmount
					}
				})
				if(newChooseList.length == 0){
					uni.showToast({
						title:'请先选择要删除的对账单',
						icon:'none'
					})
					return
				}
				this.orderList=newChooseList
				let newNum=toThousands(num,2)
				
				this.modalCon = '选中'+newChooseList.length+'项，待收金额￥'+newNum+'，确认删除吗？'
			}
			let that=this
			that.$nextTick(()=>{
				that.modalShow = true
			})
		},
		//确定提交删除、刷新
		handleConfirm(){
			if(this.eventsType=='del'){
				this.deleteOrder();
			}else{
				this.handleUpdate();
			}
		},
		chooseBillList(flag){
			this.checkedAll = flag
		},
		chooseAll(){
			this.$refs.billBox.allSelect(!this.checkedAll);
		},
		loadData(ajaxData) {
			let that = this;
			findBySn(ajaxData).then(res => {
				if (res.status == 200) {
					that.billInfo = res.data;
					that.billArr = res.data.detailList;
					let timeobj = getDate.editDay(res.data.bizBeginDate, res.data.bizEndDate);
					that.timeInfo = timeobj.starttime + '~' + timeobj.endtime;
					that.$nextTick(() => {
						that.$refs.billBox.setData(res.data.detailList);
					});
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.content {
	height: 100vh;
	width: 100%;
	box-sizing: border-box;
	.b_head {
		background-color: #fff;
		.head_list {
			padding: 14rpx 20rpx;
			border-bottom: 1rpx solid #f8f8f8;
			
			text {
				&:first-child {
					color: $uni-text-color-grey;
					display: inline-block;
					width: 160rpx;
				}
			}
		}
	}
	.b_detail {
		margin: 6rpx 0;
		background-color: #fff;
		padding: 20rpx;
		box-sizing: border-box;
		.detail_list {
			margin-bottom: 10rpx;
			&:last-child {
				margin-bottom: 0rpx;
			}
			.detail_box::after {
				content: '；';
				color: $uni-text-color-grey;
			}
			.detail_box {
				text {
					&:first-child {
						color: $uni-text-color-grey;
					}
					&:last-child {
						color: $uni-color-warning;
						font-weight: 600;
					}
				}
			}
		}
	}
	.detail_con {
		flex-grow: 1;
		overflow: auto;
		background-color: #fff;
	}
	.footer {
		width: 100%;
		box-sizing: border-box;
		padding: 10rpx 20rpx;
		background: #fff;
	}
}
// 确认收款弹窗
.slot-content{
	.u-form-item{
			line-height:0 !important;
		}
		.paymentFooter{
			border-top:1rpx solid #eee;
			view{
				width: 50%;
				text-align:center;
				padding:26rpx 0;
			}
		}
		.paymentFooter view:last-child{
			color:#007aff;
			border-left:1rpx solid #eee;
		}
}
</style>
