<template>
	<view>
		<!-- 购买商品列表 -->
		<view class="buy-list">
			<view class="list_top">
				<view class="tip">请选择需要退货的商品</view>
				<view class="checkbox-box" @tap="allSelect">
					<view class="checkbox">
						<view :class="[isAllselected?'on':'']"></view>
					</view>
					<view class="text">全选</view>
				</view>
			</view>
			
			<view class="row" v-for="(row,index) in buylist" :key="index">
				<view class="goods-info">
					<view class="img">
						<image :src="row.img"></image>
					</view>
					<view class="info">
						<view class="title">{{row.mc}}{{row.dw}}</view>
						<view>
							<view class="spec">{{row.gg}} <text style="color: red;">单价:￥{{row.price}}</text></view>
							<view class="foodnum">可退数量:{{row.dinghl}}</view>
						</view>	
						<view class="price-number">
							<view class="price">￥{{row.price*row.dinghl|toFixeds}}</view>
							<view class="number">
								退款数
								<view class="input">
									<input type="digit" :disabled="!row.selected" v-model="row.selected==false?'':row.tuiksl" @input="inputchange($event,row.id,index,row.tuiksl,row.price)" />
								</view>
							</view>
						</view>
					</view>
					<view class="checkbox-box" @tap="selected(index)" >
						<view class="checkbox">
							<view :class="[row.selected?'on':'']"></view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 提示-备注 -->
		<view class="order">
			<view class="row">
				<view class="left">
					退货说明 :
				</view>
				<view class="right">
					<input placeholder="选填,退货备注" v-model="note" />
				</view>
			</view>
			<view class="row">
				<view class="left">
					申请金额 :
				</view>
				<view class="right">
					<input placeholder="申请退货金额" v-model="sqmoney" />
				</view>
			</view>
		</view>
		<!-- 明细 -->
		<view class="detail">
			<view class="row">
				<view class="nominal">
					商品金额
				</view>
				<view class="content">
					￥{{goodsPrice|toFixeds}}
				</view>
			</view>
			
		</view>
		<view class="blck"></view>
		<view class="footer">
			<view class="settlement">
				<view class="btn" @tap="tosq">提交申请</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				buylist:[],		//订单列表
				goodsPrice:0,	//商品合计价格
				note:'',		//备注
				cpids:'',
				dhid:0,
				sqmoney:0,
				index:0,
				isAllselected:false,
				selectedList:[],
				gids:'',
				tktype:0,
				tuiksls:'',//退款产品的数量
			};
		},
		onLoad(option) {
			//option为object类型，会序列化上个页面传递的参数
			this.dhid = parseInt(option.dhid);
			this.tktype=option.tktype
			//this.dhid = 32882;
			this.index=parseInt(option.index);//当前订单状态
			//this.goodsPrice=option.allmoney;
			//this.orderdetail(this.dhid)
		},
		onShow() {
			//页面显示时，加载订单信息
			this.orderdetail(this.dhid)
		},
		onHide() {
			
		},
		filters: {
			toFixeds:function(x) {
				return parseFloat(x).toFixed(2);
			}
		},
		methods: {
			//修改退款数量
			inputchange(even,id,index,tuiksl,dj)
			{
				let inputnum=parseFloat(even.detail.value);
				if(inputnum>this.buylist[index].dinghl)
				{
					inputnum=this.buylist[index].dinghl;
				}else if(inputnum>0){
					inputnum=inputnum;
				}else{
					inputnum='';
				}
				this.buylist[index].tuiksl=inputnum;	
				this.sum();
			},
			async orderdetail(id){
				let redata=await this.$api.orderdetail({action:'orderdetail',dhid:id});
				if(redata.code==1){
					this.buylist = redata.data;
					let len = redata.data.length;
					let allmoney=0;
					for(let i=0;i<len;i++){
						this.buylist[i].selected=false;
						allmoney=(Number(allmoney) + Number(this.buylist[i].je))
					}
					this.goodsPrice=allmoney
				}else{
					uni.showToast({
						title:'暂无数据',icon:'none'
					})
				}
			},
			async sqtuik(dhid) {
				let userinfo=uni.getStorageSync('userinfo');
				let reinfo=await this.$api.sqtuik({action:'sqtuih',uid:userinfo.uid,aid:userinfo.aid,
				dhid:dhid,bz:this.note,sqmoney:this.sqmoney,gids:this.gids,isAllselected:this.isAllselected,tktype:this.tktype,tuiksls:this.tuiksls});
				if(reinfo.code==1){
					uni.showToast({
						title:reinfo.msg,icon:'none'
					})
					uni.redirectTo({
						url:"/pages/orderlist/orderlist?state="+this.index
					})
				}else{
					uni.showToast({
						title:reinfo.msg,icon:'none'
					})
				}
				uni.hideLoading();
				
			},
			tosq(){
				let t=this;
				// console.log(Number((parseFloat(this.sqmoney*100)/100).toFixed(2))+'---'+Number((parseFloat(this.goodsPrice*100)/100).toFixed(2)))
				//this.sqmoney=parseInt(this.sqmoney).toFixed(2)
				if(Number((parseFloat(this.sqmoney*100)/100).toFixed(2))>Number((parseFloat(this.goodsPrice*100)/100).toFixed(2))){
					uni.showToast({
						title:'申请金额:'+Number((parseFloat(this.sqmoney*100)/100).toFixed(2))+'，最多申请'+Number((parseFloat(this.goodsPrice*100)/100).toFixed(2))+'元',icon:'none'
					})
					return false;
				}else if(this.sqmoney<=0){
					uni.showToast({
						title:'请填申请金额',icon:'none'
					})
					return false;
				}else if(this.gids==''){
					uni.showToast({
						title:'请选择商品',icon:'none'
					})
					return false;
				}else if(this.tuiksls==''){
					uni.showToast({
						title:'请选择退货数量',icon:'none'
					})
					return false;
				}
				uni.showModal({
				    title: '温馨提示',
				    content: '是否申请退货'+t.sqmoney+'元?',
				    success: function (res) {
				        if (res.confirm) {
							uni.showLoading({
								title:'正在申请...'
							})
				           t.sqtuik(t.dhid)
				        } else if (res.cancel) {
				            console.log(t.gids,'用户点击取消');
				        }
				    }
				});
				
			},
			// 选中商品
			selected(index){
				this.buylist[index].selected = this.buylist[index].selected?false:true;
				let i = this.selectedList.indexOf(this.buylist[index].id);
				i>-1?this.selectedList.splice(i, 1):this.selectedList.push(this.buylist[index].id);
				this.isAllselected = this.selectedList.length == this.buylist.length;
				this.sum();
			},
			//全选
			allSelect(){
				let len = this.buylist.length;
				let arr = [];
				for(let i=0;i<len;i++){
					this.buylist[i].selected = this.isAllselected? false : true;
					arr.push(this.buylist[i].id);
				}
				this.selectedList = this.isAllselected?[]:arr;
				this.isAllselected = this.isAllselected||this.buylist.length==0?false : true;
				this.sum();
			},
			// 合计
			sum(){
				this.sqmoney=0;
				let len = this.buylist.length;
				let gids='';let cpsl='';
				for(let i=0;i<len;i++){
					let tuiksl=this.buylist[i].tuiksl==''?0:this.buylist[i].tuiksl;
					tuiksl=tuiksl==NaN?0:tuiksl;
					if(this.buylist[i].selected && tuiksl>0) {
						
						this.sqmoney=parseFloat(this.sqmoney*100)/100+parseFloat((this.buylist[i].price*tuiksl)*100)/100;
						gids+=','+this.buylist[i].id;
						cpsl+=','+tuiksl;
					}
				}
				this.sqmoney=parseFloat(this.sqmoney).toFixed(2)
				this.gids=gids.substr(1);
				this.tuiksls=cpsl.substr(1);
			},
			
		}
	}
</script>

<style lang="scss">
.buy-list{
	width: 86%;
	padding: 10upx 3%;
	margin: 30upx auto 20upx auto;
	box-shadow: 0upx 5upx 20upx rgba(0,0,0,0.1);
	border-radius: 20upx;
	.list_top{
		display: flex;
		justify-content: space-between;
		padding: 10upx;
		.tip{
			color: #A2A2A2;
			font-size: 30upx;
		}
	}
	// 选择框样式
	.checkbox-box{
		display: flex;
		align-items: center;
		.checkbox{
			width: 35upx;
			height: 35upx;
			border-radius: 100%;
			border: solid 2upx #f06c7a;
			display: flex;
			justify-content: center;
			align-items: center;
			.on{
				width: 25upx;
				height: 25upx;
				border-radius: 100%;
				background-color: #f06c7a;
			}
		}
		.text{
			font-size: 28upx;
			margin-left: 10upx;
		}
	}
	.row{
		margin: 30upx 0;
		
		.goods-info{
			width: 100%;
			display: flex;
			border-bottom: 1upx solid #E0E0E0;
			padding-bottom: 15upx;
			.img{
				width: 22vw;
				height: 22vw;
				border-radius: 10upx;
				overflow: hidden;
				flex-shrink: 0;
				margin-right: 10upx;
				image{
					width: 22vw;
					height: 22vw;
				}
			}
			.info{
				width: 100%;
				height: 22vw;
				overflow: hidden;
				display: flex;
				flex-wrap: wrap;
				position: relative;
				.title{
					width: 100%;
					font-size: 28upx;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					// text-align: justify;
					overflow: hidden;
				}
				.spec{
					font-size: 22upx;
					background-color: #f3f3f3;
					color: #a7a7a7;
					height: 40upx;
					display: flex;
					align-items: center;
					padding: 0 10upx;
					border-radius: 20upx;
					// margin-bottom: 20vw;
					margin-top: 10vm;
				}
				.foodnum{
					font-size: 26upx;
					height: 40upx;
					display: flex;
					align-items: center;
					padding: 0 10upx;
					margin-bottom: 10vw;
					display: block;
					color: #999;
					padding-top: 10rpx;
				}
				.price-number{
					position: absolute;
					width: 100%;
					bottom: 0upx;
					display: flex;
					justify-content: space-between;
					align-items: flex-end;
					font-size: 28upx;
					height: 40upx;
					.price{
						color: #f06c7a;
					}
					.number{
						display: flex;
						justify-content: center;
						align-items: center;
						
					}
				}
				.input {
					width: 100upx;
					height: 45upx;
					margin: 0 10upx;
					// background-color: #f3f3f3;
					border-bottom: 1upx #ADADAD solid;
				
					input {
						width: 80upx;
						height: 40upx;
						display: flex;
						justify-content: center;
						align-items: center;
						text-align: center;
						font-size: 26upx;
					}
				}
			}
		}
	}
}
.order{
	width: 86%;
	padding: 10upx 3%;
	margin: 30upx auto 20upx auto;
	box-shadow: 0upx 5upx 20upx rgba(0,0,0,0.1);
	border-radius: 20upx;
	.row{
		margin: 20upx 0;
		height: 40upx;
		display: flex;
		.left{
			font-size: 28upx;
		}
		.right{
			margin-left: 40upx;
			font-size: 26upx;
			color: #999;
			input{
				font-size: 26upx;
				color: #999;
			}
		}
	}
}
.blck{
	width: 100%;
	height: 100upx;
}
.footer{
		width: 100%;
		padding: 0 4%;
		background-color: #fbfbfb;
		height: 100upx;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		font-size: 28upx;
		position: fixed;
		bottom: 0upx;
		z-index: 5;
		
		.settlement{
			width: 80%;
			display: flex;
			justify-content: flex-end;
			align-items: center;
			.sum{
				width: 50%;
				font-size: 28upx;
				margin-right: 10upx;
				display: flex;
				justify-content: flex-end;
				.money{
					font-weight: 600;
				}
			}
			.btn{
				padding: 0 30upx;
				height: 60upx;
				background-color: #f06c7a;
				color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30upx;
				border-radius: 40upx;
			}
		}
	}
.detail{
	width: 86%;
	padding: 10upx 3%;
	margin: 30upx auto 20upx auto;
	box-shadow: 0upx 5upx 20upx rgba(0,0,0,0.1);
	border-radius: 20upx;
	.row{
		height: 60upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.nominal{
			font-size: 28upx;
		}
		.content{
			font-size: 26upx;
			color: #f06c7a;
		}
	}
}
</style>
