<template>
	<view>
		<z-navbar title="确认收款" back></z-navbar>
		<w-picker mode="date" startYear="2000" endYear="2100" v-model="info.operateTime" :current="true" fields="day"
			@confirm="onConfirmWantDate($event)" @cancel="onCancel" :disabled-after="false" ref="operateTime">
		</w-picker>
		<z-card class="mt-30 ptb-10">
			<z-form :model="info" :rules="rules" ref="info">
				<z-form-item label="收款时间" prop="operateTime" showline>
					<date-select v-model="info.operateTime" placeholder="选择收款时间"></date-select>
				</z-form-item>
				</z-form-item>
				<z-form-item height="100rpx" label="支付方式" prop="payment" showline>
					<z-text :value="paymentText" arrow placeholder="请选择" @click="showPayWay=true"></z-text>
					<z-select-popup class="payway_area" :visible.sync="showPayWay" doText="确定" @cancel="toPayment">
						<view v-for="(item,index) in paywayList" :key="index">
							<view class="flex-n fac fjb plr-40">
								<view class="payway_area_text">{{item.dicName}}</view>
								<z-input class="payway_area_input" v-model="item.price" type="number" placeholder="¥">
								</z-input>
							</view>
							<view class="line" v-if="index<paywayList.length-1"></view>
						</view>
					</z-select-popup>
				</z-form-item>
				<z-form-item label="备注信息" prop="remark" labelPos="top">
					<textarea class="textarea" v-model="info.remark" placeholder="请输入备注信息" maxlength="512"
						auto-height></textarea>
				</z-form-item>
			</z-form>
		</z-card>
		<view class="common_btn" @click="submit">
			确认收款
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import dateSelect from '../supplement/date-select.vue';
	const app = getApp({
		allowDefault: true
	});
	export default {
		components: {
			dateSelect
		},
		data() {
			var validateConfirmMoney = (rule, value, callback) => {
				if (this.info.confirmMoney <= 0) {
					return {
						state: false,
						message: '确认收款金额必须大于0'
					};
				} else
					return true;
			};
			return {
				id: '',
				type: 0,
				info: {
					payment: [],
					operateTime: '',
					remark: ''
				},
				rules: {
					operateTime: [{
						required: true,
						message: ''
					}],

				},
				switchAlert: false,

				showPayWay: false,
				paywayList: [],
				paymentText: ''
			};
		},
		onLoad(option) {
			this.id = option.id;
			this.type = option.type;
			this.getPayMent();
			//获取当前时间的信息
			var date = this.$Date.getDayDate().date;
			var nowTime = new Date();
			this.$set(this.info, 'operateTime', date + ' ' + nowTime.getHours() + ':' + nowTime.getMinutes());
		},
		methods: {
			getPayMent() {
				this.$axios.post('app/dic/paymentCode', {
					"parentId": "1001",
					"dicType": " paymentCode"
				}).then(res => {
					console.log(res.data);
					for (var i in res.data.list) {
						if (res.data.list[i].dicCode != '06') {
							this.paywayList.push(res.data.list[i]);
						}
					}
				}).catch(err => {})
			},
			toPayment() {
				var paymentText = '';
				for (var i in this.paywayList) {
					if (this.paywayList[i].price) {
						paymentText += this.paywayList[i].dicName + '、';
					}
				}
				this.paymentText = paymentText.substring(0, paymentText.length - 1);
			},
			submit() {
				this.$refs.info.validate(val => {
					console.log(val)
					if (val) {
						var info = this.info,
							confirmMoney = 0;
						for (var i in this.paywayList) {
							// if (this.paywayList[i].dicCode == '06') {
							// 	info.consumeMoney = this.paywayList[i].price || 0;
							// }
							if (this.paywayList[i].price)
								info.payment.push({
									"payMoney": this.paywayList[i].price,
									"paymentCode": this.paywayList[i].dicCode,
									"paymentName": this.paywayList[i].dicName,
								})
							confirmMoney += Number(this.paywayList[i].price || 0)
						}
						this.$set(this.info, 'confirmMoney', confirmMoney);
						this.info = info;
						if (this.type == 0) {
							this.submitData();
						} else {
							this.submitWedData();
						}

					} else {
						if (this.info.confirmMoney <= 0) {
							this.$refs.uToast.error('确认收款金额必须大于0');
						} else
							this.$refs.uToast.error('表单填写有误');
					}
				})
			},
			submitData() {
				this.$axios.post('app/orderFinanceConfirm/saveReceivable', {
					"confirmMoney": this.info.confirmMoney,
					"storeId": uni.getStorageSync('storeId'),
					"operateTime": this.info.operateTime, //操作时间
					"isSupplement": 0,
					"orderId": this.id,
					"payment": this.info.payment,
					"remark": this.info.remark
				}).then(res => {
					this.$refs.uToast.success('订单确认收款成功！');
					uni.navigateBack({
						delta: 1
					});
				}).catch(err => {
					this.$refs.uToast.error(err.msg);
				})
			},

			submitWedData() {
				this.$axios.post('app/weddingFinanceConfirm/saveWeddingReceivable', {
					"operateTime": this.info.operateTime, //操作时间
					"payment": this.info.payment,
					"isSupplement": 0,
					"weddingOrderId": this.id,
					"remark": this.info.remark
				}).then(res => {
					this.$refs.uToast.success('婚庆订单确认收款成功！');
					uni.navigateBack({
						delta: 1
					});
				}).catch(err => {
					this.$refs.uToast.error(err.msg);
				})
			},
			choosewantDate() {
				this.$refs.operateTime.show();
			},
			onConfirmWantDate(e) {
				this.$set(this.info, 'operateTime', e.value)
			},
			onCancel() {
				console.log(this.$refs.date.value, this.$refs.date.pickVal)
			},
		},

	}
</script>

<style scoped lang="scss">
	.textarea {
		font-size: 28rpx;
		min-height: 180rpx;
		padding-bottom: 20rpx;
		width: 100%;
	}

	.common_btn {
		margin: 0 auto;
		width: 690rpx;
		line-height: 88rpx;
		background: #2481EE;
		border-radius: 6rpx;
		margin-top: 50rpx;
		color: white;
		font-size: 32rpx;
		text-align: center;
	}
</style>
