<template>
	<view class="center">
		<view>
			<view class='wrap'>
				<view class="c-row">
					<view class="title">车牌号</view>
					<view class="con-list">
						{{detailData.carNo}}
					</view>
				</view>
				<view class="c-row">
					<view class="title">行程轨迹</view>
					<view class="con-list">
						<view class="flex align-center">
							<view class="item1">
								<view class="ssx">{{$helper.getProvinceAbbreviation(detailData.originProvince)}}</view>
								<view class="level2-title">
									{{$helper.filterUrban(detailData.originCity)}}
									{{$helper.filterArea(detailData.originArea)}}
								</view>
							</view>
							<image class="jt-icon item2" src="@/static/images/fuel/jt.png" mode='widthFix'>
							</image>
							<view class="item3">
								<view class="ssx">{{$helper.getProvinceAbbreviation(detailData.destinationProvince)}}
								</view>
								<view class="level2-title">{{$helper.filterUrban(detailData.destinationCity)}}
									{{$helper.filterArea(detailData.destinationArea)}}
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class='wrap1' v-for="(item , index) in fillingDetail.fillingDetailInfoList" :Key="index">
				<view class="tit">加油-{{index+1}}</view>
				<image src="../../static/img/sign/jian@2x.png" class="eliminate1" @click="delApply(index)">
				</image>
				<image src="../../static/img/sign/jia@2x.png" class="eliminate2" @click="addApply(index)">
				</image>
				<view class="c-row">
					<view class="title">油耗(升)</view>
					<view class="con-list">
						<input v-model='item.oilConsumption' placeholder="请输入油耗" @input='calculate(index)'></input>
					</view>
				</view>
				<view class="c-row">
					<view class="title">当前油价(元/升)</view>
					<view class="con-list">
						<input type="digit" v-model='item.currentOilPrice' placeholder="请输入当前油价"
							@input='calculate(index)'></input>
					</view>
				</view>
				<view class="c-row">
					<view class="title">计算金额(元)</view>
					<view class="con-list">
						<input type="digit" v-model='item.currentConsumption' placeholder="不可编辑,自动计算"
							:disabled="true"></input>
					</view>
				</view>
				<view class="c-row">
					<view class="title">加油凭证</view>
					<upload class="upload" ref="upload" :action="action" :max-size="maxSize" :max-count="1"
						:size-type="['compressed']" @on-success="getImgUrl($event,index)" @on-error="onError"
						@on-remove="onRemove($event,index)" @on-uploaded="isAdd = true" :before-upload="filterFileType"
						@on-progress="onProgress"></upload>
				</view>
			</view>
			<view class="footer">
				<view class="c-row">
					<view class="title">总油耗(L)</view>
					<view class="con-list">
						<input :disabled="true" v-model='tfc' placeholder="不可编辑,自动计算" type="digit"></input>
					</view>
				</view>
				<view class="c-row">
					<view class="title">合计金额(元)</view>
					<view class="con-list">
						<input :disabled="true" v-model='totalCost' placeholder="不可编辑,自动计算" type="digit"></input>
					</view>
				</view>
				<view @click='submitApply' class="button">提交申请</view>
			</view>
		</view>
	</view>
</template>

<script>
	import upload from '@/components/upload.vue';
	export default {
		components: {
			upload,
		},
		data() {
			return {
				detailData: {
					carNo: '辽A11111',
					destinationProvince: "",
					destinationCity: "",
					destinationArea: "",
					carNo: "",
					originProvince: "",
					originCity: "",
					originArea: "",
				},
				fillingDetail: {
					fillingDetailInfoList: [{
						oilConsumption: "",
						currentOilPrice: "",
						currentConsumption: "",
						addressUrl: [],
						compId: uni.getStorageSync('pcUserInfo').compId,
						commonId: uni.getStorageSync('pcUserInfo').userId,
						fillingId: "",
					}],
					tfc: "",
					totalCost: ""
				},
				action: this.$uploadUrl,
				maxSize: 50 * 1024 * 1024, //限制文件大小 50M
				btnLoading: false, //防止重复点击
				isAdd: true,
				tfc: "",
				totalCost: ""
			}
		},
		onLoad(options) {
			this.id = options.id
			this.fillingDetail.fillingDetailInfoList[0].fillingId = this.id
		},
		onShow() {
			var that = this
			if (this.id) {
				this.seeInfo()
			}
		},
		methods: {
			seeInfo() {
				this.$api.doRequest('get', '/fuelFillingInfo/getFilling', {
					id: this.id
				}).then(res => {
					if (res.data.code == 200) {
						this.detailData = res.data.data
					}
				})
			},
			getImgUrl(res, index) {
				this.fillingDetail.fillingDetailInfoList[index].addressUrl.push(res)
				// if(this.fillingDetail.fillingDetailInfoList[index].addressUrl){
				// 	this.fillingDetail.fillingDetailInfoList[index].addressUrl = this.fillingDetail.fillingDetailInfoList[index].addressUrl +","+res
				// }else{
				// 	this.fillingDetail.fillingDetailInfoList[index].addressUrl = res
				// }

				// console.log(res)
				// console.log('------------res-----------')
			},
			onError(error) {
				alert(error)
				console.log('------------error-----------')
				console.log(error)
			},
			onProgress(e) {
				console.log(e)
			},
			onRemove(index, num) {
				this.fillingDetail.fillingDetailInfoList[num].addressUrl.splice(index, 1)
			},
			filterFileType(index, lists) {
				if (lists[index].fileType != 'jpg' && lists[index].fileType != 'png' && lists[index].fileType != 'gif') {
					lists.splice(index, 1);
					// 当前文件不支持
					uni.showModal({
						title: '暂不支持当前图片类型',
						showCancel: false
					});
				} else {
					this.isAdd = false;
				}
			},
			calculate(index) {
				this.fillingDetail.fillingDetailInfoList[index].currentConsumption = (this.fillingDetail
						.fillingDetailInfoList[index].oilConsumption * this.fillingDetail.fillingDetailInfoList[index]
						.currentOilPrice)
					.toFixed(2)
				this.tfc = ""
				this.totalCost = ""
				for (let i = 0; i < this.fillingDetail.fillingDetailInfoList.length; i++) {
					this.tfc = (Number(this.tfc) + Number(this.fillingDetail.fillingDetailInfoList[i].oilConsumption))
						.toFixed(2)
					this.totalCost = (Number(this.totalCost) + Number(this.fillingDetail.fillingDetailInfoList[i]
						.currentConsumption)).toFixed(2)
					this.$forceUpdate()
				}
			},
			delApply(index) {
				this.fillingDetail.fillingDetailInfoList.splice(index, 1)
			},
			addApply() {
				this.fillingDetail.fillingDetailInfoList.push({
					oilConsumption: "",
					currentOilPrice: "",
					currentConsumption: "",
					addressUrl: [],
					compId: uni.getStorageSync('pcUserInfo').compId,
					commonId: uni.getStorageSync('pcUserInfo').userId,
					fillingId: this.id
				})
			},
			submitApply() {
				var that = this
				uni.showModal({
					content: "确定提交申请信息？",
					showCancel: true,
					confirmText: '提交',
					success: function(res) {
						if (res.confirm) {
							that.fillingDetail.tfc = that.tfc
							that.fillingDetail.totalCost = that.totalCost
							for (let i = 0; i < that.fillingDetail.fillingDetailInfoList.length; i++) {
								that.fillingDetail.fillingDetailInfoList[i].addressUrl = that.fillingDetail
									.fillingDetailInfoList[i].addressUrl.toString()
							}
							that.$api.doRequest('post', '/fillingDetailInfo/api/addFillingDetail',
									that.fillingDetail
								)
								.then(res => {
									if (res.data.code == 200) {
										that.$api.msg('提交成功')
										uni.navigateTo({
											url: '/pages/fuelfilling/the_fuel_filling'
										})
									} else {
										that.$api.msg('提交失败')
									}
								})
						}
					}
				})
			},
		}
	}
</script>

<style lang='scss' scoped>
	.center {
		padding-bottom: 330rpx;
	}

	page {
		background: #F5F6FA;
	}

	.title_b {
		margin: 20rpx 20rpx 0rpx 20rpx;
		padding: 20rpx 10rpx 20rpx 10rpx;
		font-weight: 550;
	}

	.c-row {
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
		padding: 20rpx 30rpx;
		position: relative;
	}

	.level2-title {}

	.level2-title {
		font-weight: 700;
		color: #000000;
	}

	.jt-icon {
		position: relative;
		top: 6rpx;
		width: 60rpx;
		margin: 0 20rpx;

	}

	.xf-iamge {
		width: 74rpx;
		height: 43rpx;
		position: absolute;
		top: -20rpx;
		right: 0;
	}

	.ssx {
		width: 20px;
		height: 20px;
		background: linear-gradient(180deg, #C8D7E5 0%, #AFC1D6 100%);
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		border-radius: 50%;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-right: 5px;
	}

	.con-list {
		-webkit-box-flex: 1;
		-webkit-flex: 1;
		flex: 1;
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		-webkit-box-orient: vertical;
		-webkit-box-direction: normal;
		-webkit-flex-direction: column;
		flex-direction: column;
		color: #303133;
		line-height: 40rpx;
		text-align: right;

		.item1,
		.item3 {
			// width: 40%;
			display: flex;

			.text {
				text-overflow: ellipsis;
				overflow: hidden;
				white-space: nowrap;
			}

		}

		.item1 .text {
			text-align: left;
		}

		.item3 .text {
			text-align: right;
		}
	}

	.align-center {
		align-items: center;
		justify-content: flex-end;
	}

	.wrap1 {
		background: #fff;
		margin: 10px;
		border-radius: 15px 10px;
		margin-top: 18px;

		input {}

		.tit {
			margin-left: 14px;
			width: 47px;
			padding-top: 10px;
		}

		.eliminate1 {
			max-width: 50%;
			width: 20px;
			height: 20px;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			border-radius: 50%;
			display: flex;
			justify-content: center;
			align-items: center;
			margin-right: 5px;
			margin-top: -20px;
			margin-left: 73px;
		}

		.eliminate2 {
			max-width: 50%;
			width: 20px;
			height: 20px;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			border-radius: 50%;
			display: flex;
			justify-content: center;
			align-items: center;
			margin-right: 5px;
			margin-top: -20px;
			margin-left: 100px;
		}

		.upload {
			margin-left: 157px;
		}
	}

	.wrap {
		background: #fff;
		margin: 10px;
		border-radius: 15px 10px;
		margin-top: 18px;

		input {}

		>.title {
			padding: 10px 16px;
		}

	}

	.footer {
		background: #fff;
		position: fixed;
		bottom: 0;
		width: 100%;
		padding: 20px 10px;
		z-index: 10;
		height: 25%;

		.button {
			background: #4089ff;
			width: 90%;
			margin: 250px auto;
			padding: 10px;
			color: #fff;
			text-align: center;
			border-radius: 30px;
			margin-top: 10px;
		}
	}


	.buns_item {
		display: flex;
		padding: 80rpx 0 50rpx 0;
		justify-content: space-around;
	}

	.but_css {
		background: #22C572;
		width: 40%;
		padding: 20rpx;
		color: #fff;
		text-align: center;
		border-radius: 20rpx;
	}

	/deep/.u-radio-group {
		flex-direction: row-reverse;
	}

	.no-boder {
		border: 0;
	}

	.textarea {
		background: #F9F9FA;
		border: 1px solid #EEEEEE;
	}
</style>
