<template>
	<view class="cu-modal" :class="show?'show':''">
		<view class="cu-dialog">
			<view class="cu-bar bg-white justify-end solid-bottom">
				<view class="content">申请订单加价/预付款</view>
				<view class="action" data-modal="applyExtraChargeModal" @click="hideModal">
					<text class="cuIcon-close text-red"></text>
				</view>
			</view>
			<view class="bg-white padding text-left">
				<view class="text-lg padding-top flex justify-start align-center">
					<text class="title-input">加价金额：</text>
					<input type="digit" class="radius-input inline-input" placeholder="请输入金额（订单完成或取消后原路退回此款）" v-model="moneyAmount"></input>
				</view>
				<view class="text-lg padding-top flex justify-start align-center">
					<text class="title-input">加价原因：</text>
					<picker style="width: 75%;" @change="bindPickerChange" :value="chooseIndex" :range="array">
						<view class="radius-input inline-input custom-input">{{array[chooseIndex]}}</view>
					</picker>
				</view>
				<view class="text-lg padding-top flex justify-start align-center">
					<text class="title-input">{{chooseIndex < 6 ? '配件费' : '施工费'}}：</text>
					<input type="digit" class="radius-input inline-input" placeholder="请输入该费用实际金额" v-model="extraAmount"></input>
				</view>
				<view>
					<view class="padding-tb-sm">凭证上传</view>
					<view class="grid col-3 grid-square">
						<view class="bg-img" v-for="(item,index) in imgList" :key="index"
							@tap="viewImage($event, imgList)" :data-url="item">
							<image :src="item" mode="aspectFill"></image>
							<view class="cu-tag bg-red" @tap.stop="delImg($event, imgList)" :data-index="index">
								<text class='cuIcon-close'></text>
							</view>
						</view>
						<view class="solids" @tap="chooseImage" v-if="imgList.length < 1">
							<text class='cuIcon-cameraadd'></text>
						</view>
					</view>
				</view>
				<view>
					<view class="padding-tb-sm">申请原因</view>
					<view class="solid">
						<textarea style="width: 100%; height: 200rpx;" fixed="true" class="radius text-left padding-sm"
							v-model="remark" maxlength="-1"></textarea>
					</view>
				</view>
			</view>
			<view class="cu-bar bg-white solid-top">
				<view class="action margin-0 flex-sub text-black" data-modal="applyChargeModal"
					@click="hideModal">取消</view>
				<view class="action margin-0 flex-sub text-main-color solid-left" data-modal="applyChargeModal"
					@click="submit">确认</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'applyCharge',
		props: {
			show: {
				type: Boolean,
				default: false
			},
			data: {
				type: Object,
				default: () => {}
			}
		},
		data() {
			return {
				array: ['加单加价','加急费','拆机/拆换费','搬运费','清理费','远程费','施工费','垫付金','奖励金','质保金','客户押金','工具押金','物品押金'],
				chooseIndex: 0,
				imgList: [],
				moneyAmount: '',
				reason: '1',
				extraAmount: '',
				remark: ''
			}
		},
		methods: {
			bindPickerChange(e) {
				this.chooseIndex = parseInt(e.detail.value)
			},
			hideModal(e) {
				this.$emit('close', e);
			},
			chooseImage(e) {
				uni.chooseMedia({
					count: 1, //默认9
					mediaType: ['image'],
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						let tempFilePaths = [];
						res.tempFiles.forEach((fileObj) => {
							tempFilePaths.push(fileObj.tempFilePath)
						})
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(tempFilePaths)
						} else {
							this.imgList = tempFilePaths
						}
					}
				});
			},
			viewImage(e, imgList) {
				uni.previewImage({
					urls: imgList,
					current: e.currentTarget.dataset.url
				});
			},
			delImg(e, imgList) {
				uni.showModal({
					title: '',
					content: '确定要删除这张图片吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			async resetPriceChangedInfo() {
				let res = await this.$request.getChangeOrderPrice(this.data);
				if (res && res.code === 0) {
					return res.data
				}
			},
			resetData() {
				this.chooseIndex = 0
				this.imgList = []
				this.moneyAmount = ''
				this.extraAmount = ''
				this.remark = ''
			},
			async submit(e) {
				if (!this.moneyAmount) {
					uni.showToast({
						title: '请输入本次加价总金额',
						icon: 'none'
					});
					return;
				}
				if (!this.extraAmount) {
					uni.showToast({
						title: '请输入该费用实际金额',
						icon: 'none'
					});
					return;
				}
				if(this.chooseIndex === 6 && this.imgList.length === 0) {
					uni.showToast({
						title: '请上传凭证，至少一张',
						icon: 'none'
					});
					return;
				}
				const num_moneyAmount = parseInt(this.moneyAmount)
				const num_extraAmount = parseInt(this.extraAmount)
				if(num_extraAmount > num_moneyAmount) {
					uni.showToast({
						title: '费用实际金额不能大于本次加价总金额',
						icon: 'none'
					});
					return;
				}
				if(this.chooseIndex < 6 && num_moneyAmount == num_extraAmount) {
					// 直接修改配件费
					console.log('调用详情接口',{
						orderDetailId: this.data.orderDetailId,
						attachMoney: this.moneyAmount,
						type: '01'
					});
					// 调用后端接口，添加附加费
					let res = await this.$request.addOrderAttach({
						orderDetailId: this.data.orderDetailId,
						attachMoney: this.moneyAmount,
						type: '01'
					});
					if (res.code === 0) {
						uni.showToast({
							title: '操作成功',
							icon: 'success'
						});
						this.hideModal(e);
						this.resetData()
					}
				} else if(this.chooseIndex >= 6 && num_moneyAmount == num_extraAmount) {
					// 直接修改施工费
					const res = await this.resetPriceChangedInfo()
					if (res && res.type != 1) {
						uni.showToast({
							icon: 'none',
							title: '你有加价未付款，请客户支付后再加或在原加价上增加金额！',
							duration: 3500
						})
						return;
					}
					
					console.log('调用列表接口',{
						orderDetailId: this.data.orderDetailId,
						changeMoney: num_moneyAmount,
						type: 1,
						remark: this.remark
					});
					let res1 = await this.$request.changeOrderPrice({
						orderDetailId: this.data.orderDetailId,
						changeMoney: newPrice,
						type: this.payAction,
						remark: this.remark
					});
					if (res1 && res1.code === 0) {
						uni.showToast({
							icon: 'success',
							title: '操作成功'
						})
						this.hideModal(e);
						this.resetData()
					}
				} else if(num_moneyAmount > num_extraAmount) {
					const res = await this.resetPriceChangedInfo()
					if (res && res.type != 1) {
						uni.showToast({
							icon: 'none',
							title: '你有加价未付款，请客户支付后再加或在原加价上增加金额！',
							duration: 3500
						})
						return;
					}
					let type1_price, type2_price;
					if(this.chooseIndex < 6) {
						type1_price = num_extraAmount
						type2_price = num_moneyAmount - num_extraAmount
					} else {
						type2_price = num_extraAmount
						type1_price = num_moneyAmount - num_extraAmount
					}
					console.log('调用详情接口',{
						orderDetailId: this.data.orderDetailId,
						attachMoney: type1_price,
						type: '01'
					});
					let res1 = await this.$request.addOrderAttach({
						orderDetailId: this.data.orderDetailId,
						attachMoney: type1_price,
						type: '01'
					});
					if (res1.code != 0) return
					console.log('调用列表接口',{
						orderDetailId: this.data.orderDetailId,
						changeMoney: type2_price,
						type: 1,
						remark: this.remark
					});
					let res2 = await this.$request.changeOrderPrice({
						orderDetailId: this.data.orderDetailId,
						changeMoney: type2_price,
						type: this.payAction,
						remark: this.remark
					});
					if (res2.code != 0) return
					uni.showToast({
						icon: 'success',
						title: '操作成功'
					})
					this.hideModal(e);
					this.resetData()
				}
			}
		}
	}
</script>

<style scoped>
	.grid.col-3.grid-square>view {
		padding-bottom: calc((100% - 40rpx)/3);
		height: 0;
		width: calc((100% - 40rpx)/3);
	}
	
	.inline-input {
		flex-basis: 70%;
	}
	.title-input {
		display: inline-block;
		width: 165rpx;
	}
	.custom-input {
		height: 1.4rem;
		min-height: 1.4rem;
	}
</style>
