<template>
	<view class="content">
		<view class="inner">
			<!-- 药品列表 -->
			<view class="drug-list">
				<view  class="item">
					<!-- 药品信息 -->
					<view v-if="order!=null" class="drug-info" v-for="(item,index) in items" :key="index">
						<view class="img-box">
							<image :src="JSON.parse(item.jsonInfo).image==''?'https://cdn.his.cdwjyyh.com/images/drug.svg':JSON.parse(item.jsonInfo).image" mode="aspectFit"></image>
						</view>
						<view class="info">
							<view class="top">
								<view class="title ellipsis2">{{ JSON.parse(item.jsonInfo).productName}}</view>
								<view class="spec">{{JSON.parse(item.jsonInfo).sku}}</view>
							</view>
							<view class="price-num">
								<view class="price-box">
									<text class="unit">¥</text>
									<text class="price">{{JSON.parse(item.jsonInfo).price.toFixed(2)}}</text>
								</view>
								<view class="num">x{{JSON.parse(item.jsonInfo).num}}</view>
							</view>
						</view>
					</view>
					 
				</view>
			</view>
			<!-- 申请原因 -->
			<view class="reason-apply">
				<view class="title-box">
					<text class="label">退款金额</text>
					 <input class="money" type="text" disabled v-model="refundAmount" placeholder="退款金额" placeholder-class="form-input" />
				</view>
				<view class="title-box" >
					<text class="label">申请原因</text>
					<picker @change="handleReasons" :value="reasons"  range-key="dictLabel" :range="reasonsOptions">
						<view class="chose-box"  >
							<text class="text">{{reasons}}</text>
							<image src="https://cdn.his.cdwjyyh.com/images/arrow_gray.png" mode=""></image>
						</view>
					</picker>
				</view>
				<view class="textarea-box">
					<u--textarea :height="100" v-model="explains" placeholder="请描述申请售后服务的具体原因"  :count="100"></u--textarea>
				</view>
			</view>
			 <!-- 底部按钮 -->
			 <view class="btn-box">
			 	<view class="sub-btn" @click="submit()">申请售后</view>
			 </view>
		</view>
		
	 
	</view>
</template>

<script>
	// import {getDictByKey} from '@/api/common.js'
	
	// import {applyAfterSales,getStoreOrderItems} from '@/api/storeAfterSales'
	export default {
		data() {
			return {
				orderId:null,
				order:null,
				orderCode:null,
				items:[],
				type:null,
				reasonsOptions:[],
				reasons:"请选择",
				explains:"",
				refundAmount:0.00,
				 
			}
		},
		onLoad(option) {
			this.type=option.type;
			this.orderId=option.orderId;
			this.getStoreOrderItems()
			this.getDictByKey("sys_sales_reasons")
		},
		methods: {
			getDictByKey(key){
				var data={key:key}
				getDictByKey(data).then(
					res => {
						if(res.code==200){
							if(key=="sys_sales_reasons"){
								this.reasonsOptions=res.data;
							}
						}
					},
					err => {
					}
				);
				
			},
			getStoreOrderItems(){
				var data={orderId:this.orderId};
				getStoreOrderItems(data).then(res => {
					if(res.code==200){
						this.order=res.order;
						this.items=res.items;
						this.refundAmount=this.order.payMoney.toFixed(2)
					}else{
						uni.showToast({
							icon:'none',
							title: "请求失败",
						});
						 
					}
				});
			},
			handleReasons(e) {
				console.log(e.detail.value)
				this.reasons = this.reasonsOptions[e.detail.value].dictLabel
			},
			submit(){
				if(this.reasons=="请选择"){
					uni.showToast({
						icon:'none',
						title: '请选择原因'
					});
					return;
				}
				if(this.refundAmount<0){
					uni.showToast({
						icon:'none',
						title: '请输入退款金额'
					});
					return;
				}
				 
				var productIds=this.items.map(item=>item.productId);
				var products=[];
				for(var i=0;i<productIds.length;i++){
					var item={productId:productIds[i]};
					products.push(item);
				}
				var data={
					refundAmount:this.refundAmount,
					orderId:this.orderId,
					refundType:this.type,
					reasons:this.reasons,
					explains:this.explains,
					productList:products,
				};
				applyAfterSales(data).then(res => {
					if(res.code==200){
						 uni.showToast({
						 	icon:'success',
						 	title:'提交成功'
						 });
						 setTimeout(function() {
							 uni.$emit('refreshOrder');
							 uni.navigateBack({
								 delta: 1
							 })
						 }, 500);
						 
					}else{
						uni.showToast({
							icon:'none',
							title: res.msg
						});
						 
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.content{
		margin-bottom: 170upx;
		.inner{
			padding: 20upx;
			.drug-list{
				.item{
					background: #FFFFFF;
					border-radius: 16upx;
					margin-bottom: 20upx;
					padding: 0 30upx;
					.drug-info{
						display: flex;
						align-items: center;
						padding: 30upx 0;
						.img-box{
							width: 160upx;
							height: 160upx;
							margin-right: 30upx;
							image{
								width: 100%;
								height: 100%;
							}
						}
						.info{
							width: calc(100% - 160upx);
							height: 160upx;
							display: flex;
							flex-direction: column;
							justify-content: space-between;
							.top{
								.title{
									font-size: 28upx;
									font-family: PingFang SC;
									font-weight: 500;
									color: #111111;
									line-height: 1.4;
									.tag{
										display: inline-block;
										padding: 0 6upx;
										height: 30upx;
										background: linear-gradient(90deg, #2BC7B9 0%, #2BC7A4 100%);
										border-radius: 4upx;
										margin-right: 10upx;
										font-size: 22upx;
										font-family: PingFang SC;
										font-weight: bold;
										color: #FFFFFF;
										line-height: 30upx;
										float: left;
										margin-top: 7upx;
									}
								}
								.spec{
									font-size: 24upx;
									font-family: PingFang SC;
									font-weight: 500;
									color: #999999;
									line-height: 1;
									margin-top: 14upx;
								}
							}
							.price-num{
								display: flex;
								align-items: center;
								justify-content: space-between;
								.price-box{
									display: flex;
									align-items: flex-end;
									.unit{
										font-size: 24upx;
										font-family: PingFang SC;
										font-weight: 500;
										color: #111111;
										line-height: 1.2;
										margin-right: 5upx;
									}
									.price{
										font-size: 32upx;
										font-family: PingFang SC;
										font-weight: 500;
										color: #111111;
										line-height: 1;
									}
								}
								.num{
									font-size: 24upx;
									font-family: PingFang SC;
									font-weight: bold;
									color: #666666;
								}
							}
						}
					}
					 
				}
			}
			.reason-apply{
				margin-top: 20upx;
				background: #FFFFFF;
				border-radius: 16upx;
				padding: 0 30upx;
				.title-box{
					height: 86upx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					border-bottom: 1px solid #F0F0F0;
					.label{
						font-size: 30upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #333333;
					}
					.money{
						
						font-size: 24upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
					}
					.chose-box{
						display: flex;
						align-items: center;
						.text{
							font-size: 24upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #999999;
						}
						image{
							width: 14upx;
							height: 24upx;
							margin-left: 10upx;
						}
					}
				}
				.textarea-box{
					padding: 30upx 0;
				}
			}
			.return-method{
				background: #FFFFFF;
				border-radius: 16upx;
				margin-top: 20upx;
				padding: 0 30upx 40upx;
				.title-box{
					height: 86upx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					border-bottom: 1px solid #F0F0F0;
					.text{
						font-size: 30upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #333333;
					}
				}
				.return-tips{
					margin-top: 30upx;
					margin-bottom: 30upx;
					height: 80upx;
					background: #FFF4E6;
					border-radius: 16upx;
					padding: 0 20upx;
					display: flex;
					align-items: center;
					.text{
						font-size: 24upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #FF5C03;
					}
				}
				.info-item{
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-bottom: 40upx;
					&:last-child{
						margin-bottom: 0;
					}
					.label{
						font-size: 26upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #666666;
						line-height: 1;
					}
					.text{
						font-size: 26upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #111111;
						line-height: 1;
					}
					.detail-box{
						display: flex;
						align-items: center;
						.price-box{
							display: flex;
							align-items: flex-end;
							margin-right: 18upx;
							.unit{
								font-size: 24upx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #111111;
								line-height: 1.2;
							}
							.num{
								font-size: 32upx;
								font-family: PingFang SC;
								font-weight: bold;
								color: #111111;
								line-height: 1;
							}
						}
						.det-text{
							font-size: 26upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #111111;
						}
						image{
							width: 14upx;
							height: 24upx;
							margin-left: 10upx;
						}
					}
				}
			}
		}
		 
		.reson-box{
			padding: 0 10upx 60upx;
			.reson-item{
				width: 100%;
				height: 110upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.title{
					font-size: 30upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #111111;
				}
			}
		}
	}
	.btn-box{
		height: 120upx;
		padding: 0 30upx;
		display: flex;
		align-items: center;
		justify-content: center;
		.sub-btn{
			width: 100%;
			height: 88upx;
			line-height: 88upx;
			text-align: center;
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
			background: #FF5C03;
			border-radius: 44upx;
		}
	}
	input{
		text-align: right;
	}
	.form-input{
		font-size: 30upx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #999999;
		text-align: right;
	}
</style>
