<template>
	<view class="form-box file-form-box bill-record">
		<uni-forms label-position="top" :modelValue="formData">
			<uni-forms-item label="付款名称" name="name">
				<uni-easyinput type="text" v-model="formData.name" placeholder="请输入付款名称" />
			</uni-forms-item>
			<uni-forms-item label="回执单">
				<upload ref="upload" accept="all" type="insert"></upload>
			</uni-forms-item>
		</uni-forms>
		<view class="hui-button-box">
			<view class="hui-button" @click="submit">保存</view>
		</view>
	</view>
</template>

<script>
	import upload from '@/components/common/upload.vue'
	import {
		insertPaymentRecord,
		putPaymentStatus
	} from '@/request/api/contract.js'
	import {
		updatePayment
	} from '@/request/api/bill.js'
	export default {
		data() {
			return {
				formData: {
					name: '',
					paymentId: '',
					paymentOrdinaryId: ''
				},
				type: '',
				billId: ''
			}
		},
		onLoad(body) {
			this.type = body.type;
			this.billId = body.billId;
			let type = this.type == '1' ? 'paymentId' : 'paymentOrdinaryId';
			this.formData[type] = body.billId;
		},
		methods: {
			submit() {
				if (!this.formData.name) return this.$toast('请输入付款名称');
				let attachment = this.$refs.upload.getFile();
				if (attachment.length === 0) return this.$toast('请上传回执单');
				let postData = JSON.parse(JSON.stringify(this.formData));
				postData['attachment'] = JSON.stringify(attachment);
				insertPaymentRecord(postData).then(res => {
					if (res.code === 200) {
						if (this.type === '1') {
							putPaymentStatus(this.billId, 1);
						} else {
							updatePayment({
								id: this.billId,
								status: 1
							})
						}
						this.$toast('操作成功');
						uni.$emit('reloadBill');
						setTimeout(() => {
							this.$navigateBack();
						}, 400)
					}
				})
			}
		},
		components: {
			upload
		},
	}
</script>

<style lang="scss">
	.bill-record {
		padding: 30rpx;

		.file-form-box {
			padding-bottom: 0rpx;
		}
	}
</style>