<template>
	<view class="center">
		<view>
			<view class='wrap'>
				<view class="c-row">
					<view class="title">车牌号</view>
					<view class="con-list">
						<input v-model='detailData.carNo' type="text">

					</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 class="c-row">
					<view class="left">备注</view>
				</view>
				<view style='position:relative;' class="wrap1 no-boder">
					<u-input ref='textarea' class='textarea' v-model="detailData.remark" :type="typeRemark" :border="border"
						placeholder="选填,不超过150字" :height="height" :auto-height="autoHeight" maxlength="150" />
					<view class="remark">
						{{detailData.remark?detailData.remark.length:'0'}}/150个字
					</view>
				</view>
				<!-- <view class="c-row">
					<view class="title">用途</view>
					<view class="con-list">
						<view @click='show1=true'>{{detailData.purpose?detailData.purpose:'选择用途'}}
							<u-icon name="arrow-right" color=""></u-icon>
						</view>
						<u-picker :range="purposeList" range-key="purpose" @confirm='ltCheck($event)' v-model="show1"
							mode="selector">
						</u-picker>
					</view>
				</view>
				<view class="c-row" v-if="detailData.purpose == '库点费用'">
					<view class="title">仓库名称</view>
					<view class="con-list">
						<view @click='selectWare()'>{{detailData.warehouseName?detailData.warehouseName:"请选择仓库名称"}}
							<u-icon name="arrow-right" color=""></u-icon>
						</view>
					</view>
				</view>
				<view class="c-row" v-if="detailData.purpose == '合同费用'">
					<view class="title">合同编号</view>
					<view class="con-list">
						<view @click='selectCon'>{{detailData.contractNo?detailData.contractNo:"请选择合同编号"}}
							<u-icon name="arrow-right" color=""></u-icon>
						</view>
					</view>
				</view> -->
			</view>
			<view class='wrap1' v-for="(item , index) in fillingDetail.fillingDetailInfoList" :Key="index">
				<view class="tit">加油-{{item.serialNumber}}</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 type='digit' 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>
				</view>
				<view class="voucher">
					<u-upload  imageMode='aspectFill' class="upload" :ref="'upload'+index" name='fileName' :file-list="item.fileList" :form-data="{companyId: pcUserInfo.compId,
								modelId: '',
								vesselId: ''}" :action="action"  :max-size="maxSize" :max-count="9"
						:size-type="['compressed']" @on-success="getImgUrl($event,index)" @on-error="onError"
						@on-remove="onRemove($event,index)" :show-progress='false' :limitType ="['png', 'jpg', 'jpeg', 'webp', 'gif']"
						@on-progress="onProgress"></u-upload>
					<!-- <upload class="upload" ref="upload" :action="action" :max-size="maxSize" :max-count="9"
						: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">总油耗(升)</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>
		<u-modal v-model="show" @confirm='submit' content="确定提交申请信息？" :show-cancel-button='true' ></u-modal>
	</view>
</template>

<script>
	import * as config from '../../config'
	import upload from '@/components/upload.vue';
	export default {
		components: {
			upload,
		},
		data() {
			return {
				detailData: {
					carNo: '',
					destinationProvince: "",
					destinationCity: "",
					destinationArea: "",
					carNo: "",
					originProvince: "",
					originCity: "",
					originArea: "",
					purpose: '库点费用',
					remark: "",
					warehouseName: '选择仓库',
				},
				height: 150,
				autoHeight: true,
				typeRemark: 'textarea',
				border: true,
				show:false,
				show1:false,
				purposeList: [{
						purpose: "合同费用"
					},
					{
						purpose: "库点费用"
					},
				],
				pcUserInfo :uni.getStorageSync('pcUserInfo'),
				fillingDetail: {
					fillingDetailInfoList: [{
						serialNumber:1,
						oilConsumption: "",
						currentOilPrice: "",
						currentConsumption: "",
						addressUrl: [],
						compId: uni.getStorageSync('pcUserInfo').compId,
						commonId: uni.getStorageSync('pcUserInfo').userId,
						fillingId: "",
					}],
					tfc: "",
					totalCost: ""
				},
				action: config.def().baseUrlNew+ 'appendix/api/uploadFiles',
				maxSize: 50 * 1024 * 1024, //限制文件大小 50M
				btnLoading: false, //防止重复点击
				isAdd: true,
				tfc: "",
				totalCost: "",
			}
		},
		onLoad(options) {
			this.height1 = uni.getSystemInfoSync().windowHeight
			this.id = options.id
			this.fillingDetail.fillingDetailInfoList[0].fillingId = this.id
			var that = this
			if (this.id) {
				this.seeInfo()
			}
		},
		onShow() {
			
			// this.warehouseInfo = uni.getStorageSync('theWarehouse1');
			// if (this.warehouseInfo && this.detailData.purpose == "库点费用") {
			// 	this.detailData.warehouseName = this.warehouseInfo.warehouseName
			// 	this.$forceUpdate()
			// }
			// this.contractNoInfo = uni.getStorageSync('reContractNo1');
			// if (this.contractNoInfo && this.detailData.purpose == "合同费用") {
			// 	this.detailData.contractId = this.contractNoInfo.id
			// 	this.detailData.contractNo = this.contractNoInfo.contractNo
			// 	this.$api.doRequest('get', 'customerInfo/getPayeeInfo', {
			// 		contractNo: this.detailData.contractNo
			// 	}).then(res => {
			// 		this.detailData.payee=res.customerType=="企业客户"?res.compName:res.customerName
			// 		this.detailData.accountNumber=res.bankCard
			// 		this.detailData.bank=res.bankDeposit
			// 		this.detailData.bankBranch=res.bankDepositBranch
			// 		this.$forceUpdate()
			// 	})
			// }
			
		},
		methods: {
			selectCon() {
				uni.navigateTo({
					url: '/pages/fuelfilling/selectContract'
				})
			},
			selectWare() {
				uni.navigateTo({
					url: '/pages/fuelfilling/selectWarehouse?warehouseType=1'
				})
			},
			ltCheck(e) {
				this.detailData.purpose = this.purposeList[e[0]].purpose
				if (this.detailData.purpose == "合同费用") {
					this.detailData.expensesPurpose = "1"
					delete this.detailData.warehouseName
			
				} else if (this.detailData.purpose == "库点费用") {
					this.detailData.expensesPurpose = "3"
					delete this.detailData.contractNo
			
				}
				console.log(this.detailData)
			},
			async seeInfo() {
				await this.$api.doRequest('get', '/fuelFillingInfo/getFilling', {
					id: this.id
				}).then(res => {
					if (res.data.code == 200) {
						this.detailData = res.data.data
						if(this.detailData.fillingDetailInfoList&&this.detailData.fillingDetailInfoList.length>0){
							for (var i = 0; i < this.detailData.fillingDetailInfoList.length; i++) {
									this.detailData.fillingDetailInfoList[i].serialNumber=i+1
									this.tfc = (Number(this.tfc) + Number(this.detailData.fillingDetailInfoList[i].oilConsumption))
										.toFixed(2)
									this.totalCost = (Number(this.totalCost) + Number(this.detailData.fillingDetailInfoList[i]
										.currentConsumption)).toFixed(2)
								this.detailData.fillingDetailInfoList[i].fileList=[]
								if(this.detailData.fillingDetailInfoList[i].addressUrl){								var arr=this.detailData.fillingDetailInfoList[i].addressUrl.split(',')
									for (var q = 0; q < arr.length; q++) {
										if(arr[q]!=''){
											this.detailData.fillingDetailInfoList[i].fileList.push({url:arr[q]})
										}
									}
									
								}
							}
							this.fillingDetail.fillingDetailInfoList=this.detailData.fillingDetailInfoList
							this.$forceUpdate()
						}
						console.log(this.fillingDetail.fillingDetailInfoList)
						// if(this.detailData.fillingDetailInfoList){
						// 	this.fillingDetail.fillingDetailInfoList[0].serialNumber=this.detailData.fillingDetailInfoList.length+1
						// }
					}
				})
			},
			getImgUrl(res, index) {
				if(typeof(this.fillingDetail.fillingDetailInfoList[index].addressUrl)=='string'){
					this.fillingDetail.fillingDetailInfoList[index].addressUrl=this.fillingDetail.fillingDetailInfoList[index].addressUrl.split(',')
				}
				
				this.fillingDetail.fillingDetailInfoList[index].addressUrl.push(res.data.appendixPath)
				// 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(this.fillingDetail.fillingDetailInfoList[index])
				// console.log('------------res-----------')
			},
			onError(error) {
				alert(error)
				console.log('------------error-----------')
				console.log(error)
			},
			onProgress(e) {
				console.log(e)
			},
			onRemove(index, num) {
				if(typeof(this.fillingDetail.fillingDetailInfoList[num].addressUrl)=='string'){
					this.fillingDetail.fillingDetailInfoList[num].addressUrl=this.fillingDetail.fillingDetailInfoList[num].addressUrl.split(',')
				}
				this.fillingDetail.fillingDetailInfoList[num].addressUrl.splice(index, 1)
				
				
			},
			filterFileType(index, lists) {
				console.log(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) {
				var that = this
				if(this.fillingDetail.fillingDetailInfoList.length>1){
					if(this.fillingDetail.fillingDetailInfoList[index].id){
						that.$api.doRequest('post', '/fillingDetailInfo/deleteInfo',
								{id:this.fillingDetail.fillingDetailInfoList[index].id}
							)
							.then(res => {
								uni.hideLoading()
								if (res.data.code == 200) {
									that.$api.msg('删除成功')
					
								} else {
									that.$api.msg('删除失败')
									return
								}
							})
					}
					console.log(this.$refs,this.$refs[`upload${index}`])
					// this.$refs[`upload${index}`].clear
					this.$refs[`upload${index}`][0].clear()
					this.fillingDetail.fillingDetailInfoList[index].fileList=[]
					this.fillingDetail.fillingDetailInfoList.splice(index, 1)
					console.log(this.fillingDetail.fillingDetailInfoList)
					this.tfc=0
					this.totalCost=0
					for (var i = 0; i < this.fillingDetail.fillingDetailInfoList.length; i++) {
						this.fillingDetail.fillingDetailInfoList[i].serialNumber=i+1
						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()
				}else{
					uni.showToast({
						title: "至少要保留1条不可删除的！",
						icon: "none"
					})
				}
				
			},
			addApply() {
				this.fillingDetail.fillingDetailInfoList.push({
					serialNumber:Number(this.fillingDetail.fillingDetailInfoList[this.fillingDetail.fillingDetailInfoList.length-1].serialNumber)+1,
					oilConsumption: "",
					currentOilPrice: "",
					currentConsumption: "",
					addressUrl: [],
					compId: uni.getStorageSync('pcUserInfo').compId,
					commonId: uni.getStorageSync('pcUserInfo').userId,
					fillingId: this.id
				})
			},
			submitApply() {
				var that = this
				for (let i = 0; i < that.fillingDetail.fillingDetailInfoList.length; i++) {
					let count = i + 1
					if (!that.fillingDetail.fillingDetailInfoList[i].oilConsumption) {
						uni.showToast({
							title: "加油-" + count + " 油耗不能为空！",
							icon: "none"
						})
						return
					}
					if (!that.fillingDetail.fillingDetailInfoList[i].currentOilPrice) {
						uni.showToast({
							title: "加油-" + count + " 当前油价不能为空！",
							icon: "none"
						})
						return
					}
					if (that.fillingDetail.fillingDetailInfoList[i].addressUrl.length == 0) {
						uni.showToast({
							title: "加油-" + count + " 请上传加油凭证！",
							icon: "none"
						})
						return
					}
				}
				this.show=true
			},
			submit(){
				var that = this
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				that.fillingDetail.tfc = that.tfc
				that.fillingDetail.remark=that.detailData.remark
				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 => {
						uni.hideLoading()
						if (res.data.code == 200) {
							that.$api.msg('提交成功')
							uni.navigateBack({
								delta: 2
							});
						} else {
							that.$api.msg(res.data.message)
						}
					})
			}
		}
	}
</script>

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

	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;
		width: 100%;
		padding: 20px 10px;

		.button {
			background: #4089ff;
			width: 90%;
			margin: 0px 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;
	}
	
	.remark {
		position: absolute;
		right: 10px;
		bottom: 20px;
		color: #AFB3BF;
	}
	.voucher{
		margin-left: 20rpx;
	}
	.wrap1 {
		padding-bottom: 10px;
		font-size: 14px;
		background: #fff;
		margin: 10px;
		border-radius: 10px;
	
		input {
			font-size: 14px;
		}
	
		>.title {
			padding: 10px 16px;
		}
	
	}

</style>
