<template>
	<view class="content">
		<view v-for="(item,index) in listCards" :key="index" class="example-box myContainer myContentBox" style="position: relative;flex: 1;">
			<view style="margin: 20px 0 0 15px;">
				<view style="width: 100%;">
					<text>应付日期：</text>
					<text>{{item.dueDate}}</text>
				</view>
				<view style="width: 100%;">
					<text>应付金额：</text>
					<text>{{item.payableAmount}}</text>
				</view>
				<view style="width: 100%;">
					<text>已付金额：</text>
					<text>{{item.paidAmount}}</text>
				</view>
				<view style="width: 100%;">
					<text>未付金额：</text>
					<text>{{item.unpaidAmount}}</text>
				</view>
			</view>
			<view class="item-this item_line" style="margin: 0 12px 0 12px;">
				<view class="itemname redMi">本次申请付款金额(元):</view>
				<view class="itemtext-this">
					<input class="iteminput" placeholder="请输入申请金额" v-model="item.payAmount" />
				</view>
			</view>
			<view class="item-this item_line item_textarea" style="margin: 0 12px 0 12px;height: 110px;">
				<view class="itemname pl15">备注</view>
				<textarea class="textarea" placeholder="请输入备注信息" v-model="item.remark"></textarea>
			</view>
			<!-- <view class="handle_container">
				<view style="width: 100%;">
					<view>本次申请付款金额(元):
						<input v-model="item.payAmount" placeholder="请输入数字" />
					</view>
				</view>
				<view style="width: 100%;">
					<view>备注:</view>
					<textarea name="" id="" cols="30" rows="10" placeholder="请输入"></textarea>
				</view> -->
			<!-- <view class="handle2">
					<uni-icons class="handle_icon" type="minus" color="#3e84e4" @click="reduce(index)"></uni-icons>
					<input class="num" v-model="item.amount" type="number" />
					<uni-icons class="handle_icon" type="plus-filled" color="#3e84e4" @click="plus(index)"></uni-icons>
				</view>
				<view class="handle1">总价:{{setTotal(item)}}元</view> -->
			<!-- <view class="handle1">总价:
					<input type="number" v-model="item.total" disabled style="width: 50px; margin:0px 5px; height:50upx; border: 1px solid #3382FF;
					display: inline-block; vertical-align: bottom;" />
					元</view> -->

			<!-- </view> -->
			<view class="itemtext" style="color:#007aff;position: absolute; top: 0; right: 20px;"
				@click="deleteItem(index)">
				<uni-icons type="trash" class="myarrowright" style="right:56upx; font-size: 36upx;color:#007aff;">
				</uni-icons>删除
			</view>
		</view>
		<view class="goHome" @click='goMain("formInfo")' style="bottom:250upx;">
			<img src="../../static/img/gohome.png">
		</view>
		<view class="add" @click="add_spare_part()">添加关联单据</view>
		<view class="bottom_btn">
			<view class="previous" @click="toPrevious()">上一步</view>
			<view class="next" @click="tosubmit(0)">暂存</view>
			<view class="next" @click="tosubmit(1)">提交</view>
		</view>
		<view class="mysearchlist" style="z-index: 10000;">
			<chooseCodeMulti ref="mysearchlist" :showsearch.sync="show_search_user" :itemInfo_.sync="search_value_user">
			</chooseCodeMulti>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import devBlock from '@/components/xinsoft-dev-block/xinsoft-dev-block.vue'
	import uniIcons from '@/components/uni-icons/uni-icons.vue'
	import chooseCodeMulti from '@/components/xinsoft-search-list/chooseCodeMulti.vue'
	export default {
		components: {
			devBlock,
			uniIcons,
			chooseCodeMulti
		},
		data() {
			return {
				show_search_user: false,
				search_value_user: [],
				id: '',
				recordId: '',
				listCards: [],
				sparePartsCost: 0,
				formInfo: '',
				// partList:[]
			}
		},
		computed: {
			// ...mapState(['repairRecordList']),
			...mapState(['partList', 'repairRecordList']),
		},
		onLoad(option) {
			this.id = option.id;
			this.recordId = option.recordId;
			this.sparePartsCost = option.sparePartsCost;
			this.formInfo = uni.getStorageSync('formInfo')
			if(option.id){
				this.listCards=this.formInfo.payApplyItemList
			}
			// this.listCards = this.partList;
		},
		watch: {
			search_value_user(newv) {
				console.log(newv)
				if (newv.length > 0) {
					this.listCards = newv
				}

				// this.setpartList(this.listCards);
			},
			// partList: {
			// 	handler(newV) {
			// 		if (newV.length > 0) {
			// 			var sum = 0;
			// 			for (var i = 0; i < this.partList.length; i++) {
			// 				sum += (this.partList[i].amount * this.partList[i].price)
			// 			}
			// 			this.sparePartsCost = sum
			// 			console.log(this.sparePartsCost)
			// 		}
			// 	},
			// 	deep: true
			// }

		},
		methods: {
			...mapMutations(['setpartList', 'setrepairRecordList']),
			//计算总价
			// setTotal(item) {
			// 	return (item.price || 0) * item.amount
			// },
			//减
			// reduce(index) {
			// 	if (this.listCards[index].amount > 0) {
			// 		this.listCards[index].amount = JSON.parse(this.listCards[index].amount) - 1;
			// 	}
			// },
			// 加
			// plus(index) {
			// 	this.listCards[index].amount = JSON.parse(this.listCards[index].amount) + 1;
			// },
			//删除
			deleteItem(index) {
				//删除 备件
				this.partList.splice(index, 1);
			},
			tosubmit(type) {
				var url;
				if (type == 0) { //暂存
					url = "/apis/payApply/tempSave"
				} else if (type == 1) { //保存
					url = "/apis/payApply/submit"
				}
				if (!(this.formInfo && this.formInfo.payApply.applicationDate)) {
					uni.showToast({
						title: '请选择申请日期',
						duration: 2000,
						icon: "none"
					});
					return false;
				}
				if (!(this.formInfo && this.formInfo.payApply.applicantName)) {
					uni.showToast({
						title: '请填写申请人',
						duration: 2000,
						icon: "none"
					});
					return false;
				}
				if (!(this.formInfo && this.formInfo.payApply.paymentDate)) {
					uni.showToast({
						title: '请选择支付日期',
						duration: 2000,
						icon: "none"
					});
					return false;
				}
				if (!(this.formInfo && this.formInfo.payApply.paymentReason)) {
					uni.showToast({
						title: '请填写付款事由',
						duration: 2000,
						icon: "none"
					});
					return false;
				}
				this.formInfo.payApplyItemList = this.listCards
				let totalPayAmount=0//申请付款金额合计
				let totalPaidAmount=0//已付金额合计
				let totalPayableAmount=0//应付付款金额合计
				let totalUnpaidAmount=0//未付金额合计
				this.formInfo.payApplyItemList.forEach(item=>{
					totalPayAmount+=parseFloat(item.payAmount)
					totalPaidAmount+=parseFloat(item.paidAmount)
					totalPayableAmount+=parseFloat(item.payableAmount)
					totalUnpaidAmount+=parseFloat(item.unpaidAmount)
				})
				this.formInfo.payApply.totalPayAmount=totalPayAmount
				this.formInfo.payApply.totalPaidAmount=totalPaidAmount
				this.formInfo.payApply.totalPayableAmount=totalPayableAmount
				this.formInfo.payApply.totalUnpaidAmount=totalUnpaidAmount
				var _this = this;
				this.$http.request({
					url: url,
					data: this.formInfo,
				}).then(res => {
					if (res.data.code == 0) {
						uni.removeStorageSync('formInfo');
						this.setpartList([]);
						uni.showToast({
							title: '操作成功',
							duration: 1000,
							icon: "none"
						});
						setTimeout(function() {
							//提交成功后写死跳转总还是有问题的。跳转到详情，我觉得没有问题，没必要非要跳转到列表。
							setTimeout(function() {
								uni.redirectTo({
									url: 'payApply'
								});
							}, 1000)
						}, 1000)
					} else {
						uni.showToast({
							title: res.data.msg ? res.data.msg : '操作失败',
							duration: 1000,
							icon: "none"
						});
					}
				}).catch(err => {
					uni.showToast({
						title: '提交失败',
						duration: 1000,
						icon: "none"
					});
				});
			},
			// 上一步
			toPrevious() {
				var _this = this;
				console.log(_this.sparePartsCost)
				uni.navigateBack({});
				// uni.navigateTo({
				//     url: 'repairStep1?id='+_this.id+"&from=back"+"&sparePartsCost="+_this.sparePartsCost
				// });
			},
			// 添加关联单据
			add_spare_part() {
				if (!this.formInfo.payApply.supplierId) {
					uni.showToast({
						title: '请先选择付款单位',
						duration: 2000,
						icon: "none"
					});
					return false;
				}
				this.show_search_user = true;
				// var url;
				// if(this.recordId){
				// 	url='addSparePart?id='+this.id+"&recordId="+this.recordId+'&url=repairStep2'
				// }else{
				// 	url='addSparePart?id='+this.id+'&url=repairStep2'
				// }
				//   uni.redirectTo({
				// 	  url: url
				//   });
			},

			//列表数据格式化
			// formaterData(data) {
			// 	var resData = [];
			// 	data.forEach((vals, index) => {
			// 		var combineJSON = {
			// 			id: vals.id,
			// 			title: vals.name,
			// 			amount: 1, //数量
			// 			checked: false,
			// 			contentList: [{
			// 					id: 1,
			// 					isDouble: true,
			// 					labelName: '应付日期',
			// 					labelContent: vals.dueDate
			// 				},
			// 				{
			// 					id: 2,
			// 					isDouble: true,
			// 					labelName: '应付金额(元)',
			// 					labelContent: vals.payAmount
			// 				},
			// 				{
			// 					id: 3,
			// 					isDouble: true,
			// 					labelName: '已付金额(元)',
			// 					labelContent: vals.amountPaid
			// 				},
			// 				{
			// 					id: 4,
			// 					isDouble: true,
			// 					labelName: '未付金额(元)',
			// 					labelContent: vals.unpayAmount
			// 				}
			// 			],
			// 		}
			// 		resData.push(combineJSON);
			// 	});
			// 	return resData;
			// },
		}
	}
</script>

<style>
	page {
		background-color: #f5f7f5;
	}
</style>
<style scoped>
	.content {
		position: relative;
		min-height: 100%;
		overflow: auto;
		padding-bottom: 260upx;
		box-sizing: border-box;
	}

	.step_num {
		display: flex;
		flex-direction: row;
		justify-content: center;
		width: 100%;
		height: 80upx;
		background-color: #fff;
		padding-right: 48upx;
		box-sizing: border-box;
	}

	.step {
		position: relative;
		font-size: 28upx;
		line-height: 80upx;
		padding-left: 30upx;
		float: left;
	}

	.step:before {
		position: absolute;
		left: 0px;
		content: '1';
		border: 1px solid #000;
		width: 28upx;
		height: 28upx;
		box-sizing: border-box;
		line-height: 28upx;
		border-radius: 200upx;
		font-size: 20upx;
		text-align: center;
		top: 26upx;
	}

	.step:after {
		position: absolute;
		content: '';
		width: 60upx;
		height: 2upx;
		background-color: #000;
		top: 40upx;
		left: 200upx;
	}

	.step2 {
		margin-left: 64upx;
	}

	.step2:before {
		content: '2';
	}

	.step2:after {
		background-color: transparent;
	}

	.bottom_btn {
		position: absolute;
		left: 5%;
		bottom: 30upx;
		width: 90%;
		height: 82upx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;

	}

	.next,
	.previous {
		width: 30%;
		border-radius: 5px;
		border: 1px solid #3382FF;
		text-align: center;
		color: #3382FF;
		line-height: 82upx;
		background-color: #fff;
	}

	.next {
		border: none;
		background-color: #3382FF;
		color: #fff;
	}

	.add {
		position: absolute;
		left: 5%;
		bottom: 142upx;
		width: 90%;
		height: 82upx;
		border-radius: 5px;
		border: 1px solid #3382FF;
		text-align: center;
		color: #3382FF;
		line-height: 82upx;
		background-color: #EBF5FF;
	}

	>>>.dev-label {
		white-space: nowrap;
	}

	>>>.dev-block {
		border-radius: 12upx 12upx 0px 0px;
		box-shadow: none;
	}

	.handle_container {
		margin: 0 24upx;
		position: relative;
		display: flex;
		justify-content: space-around;
		align-items: center;
		font-size: 24upx;
		color: #333;
		background-color: #fff;
		height: 121upx;
		line-height: 121upx;
		border-top: 1px solid #EDEDED;
		text-align: center;
		margin-top: -24upx;
		border-radius: 0px 0px 12upx 12upx;
	}

	.handle1 {
		flex: 1;
	}

	.handle2 {
		flex: 0.8;
		display: flex;
		justify-content: center;
		align-items: center;
		padding-right: 20upx;
		box-sizing: border-box;
	}

	.handle_icon {
		font-size: 44upx !important;
	}

	.num {
		font-size: 22upx;
	}

	.itemtext {
		width: 60px !important;
	}

	.item-this {
		position: relative;
		overflow: auto;
		min-height: 46px;
		background-color: #fff;
	}

	.itemtext-this {
		position: relative;
		float: right;
		right: 15px;
		width: 45%;
		min-height: 46px;
		line-height: 20px;
		color: #333;
		padding-top: 13px;
		padding-bottom: 13px;
		box-sizing: border-box;
		text-align: right;
	}

	>>>.doubleList {
		width: 100%;
	}
</style>
