<template>
	<view class="container">
		<uni-list class="custom-uni-list">
			<uni-list-item :title="$t('index.order-sum')"
				:rightText="'¥' + orderMarginInfo.totalAmount"></uni-list-item>
			<!-- <uni-list-item title="定金支付总额" :rightText="'¥' + orderMarginInfo.totalAmount"></uni-list-item> -->
			<uni-list-item :title="$t('deposit.order.pay')" :rightText="'¥' + orderMarginInfo.deposit"></uni-list-item>
			<uni-list-item :title="$t('index.order.money.leave')"
				:rightText="'¥' + orderMarginInfo.surplusPay"></uni-list-item>
			<uni-list-item :title="$t('index.this-time-pay')">
				<template v-slot:footer>
					<input class="custom-uni-value" v-model="currentPaymentAmount" type="digit"
						:placeholder="$t('index.enter-pay-sum')" />
				</template>
			</uni-list-item>
			<uni-list-item :title="$t('index.pay-time')" showArrow>
				<template v-slot:footer>
					<uni-datetime-picker style="flex: auto" v-model="datetimesingle">
						<view class="deposit-value">{{paymentDate}}</view>
					</uni-datetime-picker>
				</template>
			</uni-list-item>
		</uni-list>
		<view class="upload-box">
			<view class="upload-title">{{$t('index.annex-upload')}}</view>
			<view class="upload">
				<uni-file-picker limit="3" mode="grid" file-mediatype="image" @select="select" @progress="progress"
					@success="success">
				</uni-file-picker>
			</view>
		</view>

		<view class="btn-box">
			<button class="btn" @click="submit">{{$t('index.sure')}}</button>
		</view>
	</view>
</template>

<script>
	import {
		getFnalPayment,
		uploadFile,
		finalPayment
	} from '@/config/api.js'
	import {
		Moment
	} from '@/utils/moment.js'
	let orderId = null;
	export default {
		name: 'deposit',
		data() {
			return {
				fileIds: [],
				currentPaymentAmount: '',
				datetimesingle: '',
				orderMarginInfo: {}
			}
		},
		computed: {
			paymentDate() {
				return new Moment(this.datetimesingle).format('YYYY-MM-DD HH:mm:ss')
			}
		},
		methods: {
			getFnalPayment() {
				getFnalPayment({
					id: orderId
				}).then(({
					content
				}) => {
					this.orderMarginInfo = content
					if (content.surplusPay > 0) {
						this.currentPaymentAmount = content.surplusPay
					}
				})
			},
			// 获取上传状态
			select(e) {
				let serveList = []
				e.tempFilePaths.forEach(url => {
					serveList.push(this.uploadImage(url))
				})
				let _this = this
				Promise.all(serveList).then(data => {
					setTimeout(() => {
						uni.showToast({
							title: _this.$t('index.operate-success'),
							icon: 'none'
						});
					}, 30)
					this.fileIds = data.map(item => item.id)
				}).catch(err => {
					setTimeout(() => {
						uni.showToast({
							title: _this.$t('index.msg-upload-err'),
							icon: 'none'
						});
					}, 30)
				})
				// uploadFile({
				// 	filePath: e.tempFilePaths[0],
				// 	name: 'file',
				// 	formData: {                                  //其他需要携带的参数
				// 		'modelType': 'default'
				// 	}
				// }).then(({content})=>{
				// 	this.fileIds = content.id
				// })
			},
			uploadImage(path) {
				return new Promise((resolve, reject) => {
					uploadFile({
						filePath: path,
						name: 'file',
						formData: { //其他需要携带的参数
							'modelType': 'default'
						}
					}).then(({
						content
					}) => {
						resolve(content)
					}).catch(err => {
						reject(err)
					})
				})
			},
			submit() {
				if (!this.currentPaymentAmount) {
					uni.showToast({
						title: this.$t('index.enter') + this.$t('index.this-time-pay'),
						icon: 'none'
					});
					return
				}
				if (!this.fileIds.length) {
					uni.showToast({
						title: this.$t('index.enter-annex-upload'),
						icon: 'none'
					});
					return
				}
				finalPayment({
					orderId: orderId,
					fileIds: this.fileIds.join(','),
					currentPaymentAmount: this.currentPaymentAmount,
					paymentDate: this.paymentDate
				}).then(res => {
					uni.redirectTo({
						url: `/pages/paymentStatus/index?type=payment&price=${this.currentPaymentAmount}`
					})
				}).catch(err => {
					uni.redirectTo({
						url: '/pages/paymentStatus/index?type=payment'
					})
				})
			}
		},
		onLoad(options) {
			orderId = options.id
			this.datetimesingle = new Moment().format('YYYY-MM-DD HH:mm:ss')
			this.getFnalPayment()
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		background-color: #FFFFFF;

		/deep/ .file-picker__progress {
			display: none;
		}
	}

	.deposit-value {
		text-align: right;
		font-size: 14px;
		color: #B60001;
	}

	.upload-box {
		padding: 12px 15px 0;

		.upload-title {
			font-size: 14px;
			color: #B60001;
		}

		.upload {
			margin-top: 12px;
		}
	}

	.btn-box {
		margin-top: 100rpx;
		padding: 0 30rpx;

		.btn {
			background-color: #B60001;
			font-size: 14px;
			color: #FFFFFF;

			&::after {
				border-radius: 0;
				border: 0;
			}
		}
	}
</style>