<template>
  <!-- #ifdef H5 -->
	<zj-page-layout :hasFooter="true">
		<view class="status-container">请先联系客服再进行退货操作</view>
		<view class="main-container">
			<view class="goods-container card">
				<view class="title">商品信息</view>
				<block v-for="(item, index) in orderGoodsList" :key='index'>
					<view class="item">
						<view class="check" v-if="item.selected" @tap="selectList(index)">
							<text class="iconfont icon-danxuan2 active"></text>
						</view>
						<view class="check" v-else @tap="selectList(index)">
							<text class="iconfont icon-danxuan"></text>
						</view>
						<view class="img"><image :src="item.imgUrl" mode="aspectFill"></image></view>
						<view class="right">
							<view class="top">
								<view class="name ellipsis-2">{{item.goodsName}}</view>
								<view class="des">{{item.goodsSpecValue}}</view>
							</view>
							<view class="bottom">
								<view class="price">￥{{item.price | priceFilter}}</view>
								<u-number-box
									v-model="item.num"
									:min="1"
									:max="item.oldNum"
									:buttonSize="26"
									iconStyle="font-size: 12px;">
								</u-number-box>
							</view>
						</view>
					</view>
				</block>
			</view>

			<view class="total-container card">
				<view class="top">
					<view class="item">
						<view>商品金额</view>
						<view>￥{{orderDetail.totalProductAmount | priceFilter}}</view>
					</view>
					<view class="item">
						<view>优惠</view>
						<view>-￥{{orderDetail.couponValue | priceFilter}}</view>
					</view>
				</view>
				<view class="bottom">
					<view class="total">付款合计：<text>￥{{orderDetail.payAmount | priceFilter}}</text></view>
					<!-- <view class="total">预计收入：<text>￥{{1 | priceFilter}}</text></view> -->
				</view>
			</view>

			<view class="form-container card">
				<view class="title">退货信息填写</view>
				<view class="item">
					<view class="label">请选择类型：</view>
					<view class="radio-group">
						<view class="radio" @tap="returnType = 'REFUND_AMOUNT'">
							<text class="iconfont icon-danxuan" v-if="returnType == 'REFUND_GOODS'"></text>
							<text class="iconfont icon-danxuan2 active" v-if="returnType == 'REFUND_AMOUNT'"></text>
							仅退款
						</view>
						<view class="radio" @tap="returnType = 'REFUND_GOODS'" v-if="is_REFUND_GOODS && has_INSTALL_GOODS">
							<text class="iconfont icon-danxuan2 active" v-if="returnType == 'REFUND_GOODS'"></text>
							<text class="iconfont icon-danxuan" v-if="returnType == 'REFUND_AMOUNT'"></text>
							退货退款
						</view>
					</view>
				</view>
				<view class="item">
					<view class="label">退换货原因：</view>
					<view class="picker">
						<picker @change="changeReason" :value="reasonIndex" :range="reasonArray">
							<view>{{reasonArray[reasonIndex]}}</view>
						</picker>
					</view>
				</view>
				<view class="item">
					<view class="label">退换货说明：</view>
					<view class="textarea">
						<textarea maxlength="80" placeholder="退换货说明(选填)" v-model="returnExplain" />
					</view>
				</view>
				<view class="item">
					<view class="label">*上传凭证：</view>
					<view class="images">
						<block v-for="(item, index) in images" :key='index'>
							<view class="img">
								<image :src="item.url" mode="aspectFill"></image>
								<text @tap="delImage(index)">x</text>
							</view>
						</block>
						<view class="add"  @tap="addImage" v-if="images.length < 3">
							<text class="iconfont icon-xiangji"></text>
							<text class="text">上传凭证</text>
							<text class="text">(最多3张)</text>
						</view>
					</view>
				</view>
			</view>
		</view>

		<template slot="footer">
			<view class="footer-btn-group">
				<u-button type="primary" text="提交申请" @click="submitApply"></u-button>
			</view>
		</template>
	</zj-page-layout>
  <!-- #endif -->
  <!-- #ifndef H5 -->
  <web-view :src="webViewHref(`/packageMine/pages/order/return/apply`, pam,crossPagePam)" @message="crossPage.$listener"></web-view>
  <!-- #endif -->
</template>

<script>
  // #ifdef H5
  import { uploadImgFull } from '@/common/utils/util.js'

	export default {
		data() {
			return {
				orderId: null, // 订单id
				orderDetail: {}, // 订单详情
				orderGoodsList: [],
				reasonArray: [], // 退货原因列表
				reasonIndex: 0, // 退货原因选择值
				images: [], // 凭证
				returnType: 'REFUND_AMOUNT', // 退款类型：REFUND_AMOUNT=仅退款 REFUND_GOODS=退货退款
				returnExplain: '', // 退换货说明
				canClickSave: true, // 能否点击提交
			}
		},

		computed: {
			is_REFUND_GOODS() {
				if(this.orderDetail.orderStatus === 'DFH') {
					this.returnType = 'REFUND_AMOUNT';
					return false;
				}
				if(this.orderDetail.orderStatus === 'REFUND' && !this.orderDetail.logisticsNo) {
					this.returnType = 'REFUND_AMOUNT';
					return false;
				}

				return true;
			},

			has_INSTALL_GOODS() {
				// 只要选中安装产品，就true
				for (let i=0; i<this.orderGoodsList.length; i++) {
					if(this.orderGoodsList[i].selected && this.orderGoodsList[i].workOrderType === 'INSTALL') {
						return true;
						break;
					}
				}

				return false;
			}
		},

		onLoad({orderId}) {
			this.orderId = orderId;
			this.getOrderDetail();
			this.getReasonList();
		},

		methods: {
			getOrderDetail() {
				this.$api.get('/order/detail', {
					orderId: this.orderId
				}).then(res => {
					this.orderDetail = res.data;
					this.orderGoodsList = res.data.orderDetails;
					this.orderGoodsList.forEach((item, index) => {
						item.selected = true;
						item.oldNum = item.num;
					})
				})
			},

			getReasonList() {
        // this.reasonArray = ['7天无理由退货', '与商品信息描述不符', '质量问题', '卖家发错货', '包装/商品破损/污渍', '其它原因']
				this.$api.post('/pay/sysDict', {
          dictType: 'REFUND_REASON'
        }).then(res => {
					this.reasonArray = res.data.map(item => item.dictValue);
				})
			},

			selectList(index) {
				this.orderGoodsList[index].selected = !this.orderGoodsList[index].selected;

				var ocuritem = this.orderGoodsList.pop();
				this.orderGoodsList.push(ocuritem);
			},

			changeReason(e) {
				this.reasonIndex = e.target.value
			},

			// 删除图片
			delImage(index) {
				this.images.splice(index, 1);
			},

			// 添加图片
			async addImage() {
				uni.chooseImage({
					count: 3 - this.images.length,
					sizeType: ['original'],
					success: res => {
						uni.showLoading();
						res.tempFilePaths.forEach(async item => {
							let data = await uploadImgFull(item);
							this.images.push(data);
						});
						uni.hideLoading();
					},
					fail: err => {}
				})
			},

			// 验证数据
			vailateData() {
				let goodsList = [];
				this.orderGoodsList.forEach(item => {
					if(item.selected) {
						goodsList.push(item);
					}
				})
				if(goodsList.length < 1) {
					this.$toast('请选择商品');
					return false;
				}

				if(this.images.length < 1) {
					this.$toast('请上传凭证');
					return false;
				}
				return true;
			},

			// 提交申请
			submitApply() {
				if (!this.canClickSave) return false;
				this.canClickSave = false;
				setTimeout(() => { this.canClickSave = true }, 3000)
				if(!this.vailateData())return;

				let goodsList = [];
				this.orderGoodsList.forEach(item => {
					let obj = {};
					if(item.selected) {
						obj = {
							orderDetailId: item.orderDetailId,
							num: item.num
						}
						goodsList.push(obj);
					}
				})

				let imgIds = [];
				this.images.forEach(item => {
					imgIds.push(item.id);
				})

				this.$api.postJson('/order/refund/apply', {
          orderId: this.orderId,
          refundReason: this.reasonArray[this.reasonIndex],
          refundExplain: this.returnExplain,
          refundType: this.returnType,
          imgIds: imgIds,
          refundGoods: goodsList,
				}).then(res => {
					this.$successToast('申请已提交');
					this.crossPage.$emit('refreshOrderList', res.data);
					this.crossPage.$emit('refreshOrderDetail', res.data);
					setTimeout(() => {
						// uni.navigateBack({
						// 	delta: 1
						// })
						this.$navToPage({
							url: '/packageMine/pages/order/return/detail?orderRefundId=' + res.data
						}, 'redirectTo')
					}, 1000)
				})
			},
		}
	}
  // #endif
  // #ifndef H5
  export default {
    data() {
      return {
        pam: {},
      }
    },

    onLoad(pam) {
      this.pam = pam;
    },
  }
  // #endif
</script>

<style lang="scss" scoped>
	.app-container {
		background: #F4F2F2;
		box-sizing: border-box;
	}
	.status-container {
		background: $theme-color;
		line-height: 80rpx;
		text-align: center;
		font-size: 32rpx;
		color: #FFFFFF;
	}
	.card {
		background: #FFFFFF;
		border-radius: 20rpx;
		margin-bottom: 20rpx;
	}
	.main-container {
		padding: 20rpx 20rpx 120rpx;
	}
	.goods-container {
		padding: 0 20rpx;
		.title {
			font-size: 32rpx;
			color: #333333;
			line-height: 32rpx;
			padding-top: 20rpx;
		}
		.item {
			padding: 20rpx 0;
			border-bottom: 1px solid #eaeaea;
			display: flex;
			justify-content: space-between;
			align-items: center;
			&:last-child {
				border: none;
			}
			.check {
				margin-right: 20rpx;
				.iconfont {
					font-size: 36rpx;
					color: #999999;
					&.active {
						color: $theme-color;
					}
				}
			}
			.img image {
				width: 180rpx;
				height: 180rpx;
				flex-shrink: 0;
				margin-right: 20rpx;
				display: block;
			}
			.right {
				width: 470rpx;
				height: 180rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				.top {
					.name {
						font-size: 28rpx;
						color: #333333;
						line-height: 36rpx;
					}
					.des {
						font-size: 28rpx;
						color: #999999;
						margin-top: 10rpx;
					}
				}
				.bottom {
					display: flex;
					justify-content: space-between;
					align-items: center;
					.price {
						font-size: 28rpx;
						color: $minor-color;
						line-height: 28rpx;
					}
					::v-deep .u-number-box {
						.u-number-box__minus, .u-number-box__plus {
							width: 26px;
						}
					}
				}
			}
		}
	}
	.total-container {
		padding: 0 20rpx;
		font-size: 28rpx;
		color: #333333;
		.top {
			padding: 10rpx 0;
			border-bottom: 1px solid #eaeaea;
		}
		.item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 64rpx;
		}
		.bottom {
			padding: 10rpx 0;
		}
		.total {
			display: flex;
			justify-content: flex-end;
			height: 50rpx;
			align-items: center;
			text {
				color: $minor-color;
			}
		}
	}
	.form-container {
		padding: 0 20rpx 20rpx;
		.title {
			font-size: 32rpx;
			color: #333333;
			line-height: 32rpx;
			padding-top: 20rpx;
		}
		.item {
			display: flex;
			margin-top: 30rpx;
			.label {
				line-height: 48rpx;
				width: 180rpx;
			}
			.radio-group {
				display: flex;
				height: 48rpx;
				align-items: center;
				.radio {
					display: flex;
					align-items: center;
					margin-right: 40rpx;
					.iconfont {
						font-size: 36rpx;
						color: #999999;
						margin-right: 10rpx;
						&.active {
							color: $theme-color;
						}
					}
				}
			}
			.picker {
				picker {
					width: 480rpx;
					height: 52rpx;
					line-height: 52rpx;
					border: 1px solid #eaeaea;
					border-radius: 10rpx;
					padding: 0 20rpx;
					box-sizing: border-box;
					view {
						line-height: 52rpx;
					}
				}
			}
			.textarea {
				textarea {
					width: 480rpx;
					height: 200rpx;
					border: 1px solid #eaeaea;
					border-radius: 10rpx;
					padding: 10rpx 20rpx;
					box-sizing: border-box;
				}
			}
			.images {
				display: flex;
				.add {
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					width: 146rpx;
					height: 146rpx;
					border: 2rpx dashed #dadada;
					border-radius: 10rpx;
					.iconfont {
						font-size: 48rpx;
						color: #999999;
						margin-bottom: 4rpx;
					}
					.text {
						font-size: 22rpx;
						color: #999999;
						line-height: 24rpx;
					}
				}
				.img {
					position: relative;
					margin-right: 18rpx;
					&:nth-child(3) {
						margin-right: 0;
					}
					image {
						width: 150rpx;
						height: 150rpx;
						border-radius: 10rpx;
						overflow: hidden;
						display: block;
					}
					text {
						position: absolute;
						right: -10rpx;
						top: -10rpx;
						width: 40rpx;
						height: 40rpx;
						border-radius: 50%;
						background: #FF3F42;
						font-size: 28rpx;
						color: #FFFFFF;
						text-align: center;
						line-height: 36rpx;
						display: block;
					}
				}
			}
		}
	}
</style>
