<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<view class="list" v-for="(item,index) in goodsList" :key="index">
					<view class="list_1">
						<text>退款商品</text>
					</view>
					<view class="list_2">
						<view class="l">
							<image class="image"
								:src="item.goods.file&&item.goods.file.length>0?item.goods.file[0].url:''" mode="">
							</image>
						</view>
						<view class="c">
							<view class="name">
								{{item.goods.name}}
							</view>
							<view class="specs">
								{{item.name}}
							</view>
						</view>
						<view class="r">
							<view v-if="type=='0'" class="price">
								¥{{item.sell_money}}
							</view>
							<view v-else class="price">
								¥{{item.group_config.money}}
							</view>
							<view class="num">
								×{{item.buy_num}}
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="two">
				<uni-forms ref="form" :rules="rules" :model="form" label-width="auto">
					<uni-forms-item label="申请售后理由" name="desc">
						<uni-easyinput type="textarea" v-model="form.desc" placeholder="请输入申请售后描述" />
					</uni-forms-item>
					<view class="btn">
						<button type="primary" size="mini" @click="onSubmit('form')">提交保存</button>
					</view>
				</uni-forms>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				user: {},
				form: {},
				type: '',
				// 退货商品
				goodsList: [],
				icon: [],
				rules: {
					desc: {
						rules: [{
							required: true,
							errorMessage: '请输入申请售后描述',
						}]
					},
				},
			};
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			// 监听用户登录
			that.watchLogin();
		},
		methods: {
			// 提交保存
			async onSubmit(ref) {
				const that = this;
				that.$refs[ref].validate().then(async params => {
					params.order_detail = that.id
					const arr = await that.$api(`/afterSale/orderCancel`, 'POST', params);
					if (arr.errcode == '0') {
						uni.showToast({
							title: `申请售后成功`,
							icon: 'success',
						});
						uni.navigateBack({
							detail: 1
						})
					} else {
						uni.showToast({
							title: arr.errmsg,
							icon: 'none',
						})
					}
				})
			},
			// 监听用户是否登录
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: async function(res) {
						let user = that.$jwt(res.data);
						if (user) {
							that.$set(that, `user`, user);
							if (that.id) {
								let arr = await that.$api(`/orderDetail/${that.id}`, 'GET')
								if (arr.errcode == '0') {
									that.$set(that, `goodsList`, arr.data.goods)
									that.$set(that, `type`, arr.data.type)
								}
							}
						}
					},
					fail: function(err) {
						uni.reLaunch({
							url: '/pages/login/index'
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.main {
		.one {
			.list {
				margin: 2vw;
				padding: 2vw;
				border-radius: 2vw;
				border: 1px solid #3333;

				.list_1 {
					margin: 0 0 1vw 0;
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					font-size: var(--font14Size);
					font-weight: bold;
				}

				.list_2 {
					margin: 0 0 1vw 0;
					display: flex;

					.l {
						width: 20vw;

						.image {
							width: 100%;
							height: 20vw;
							border-radius: 5px;

						}
					}

					.c {
						width: 50vw;
						padding: 0 2vw;
						.specs{
							color: var(--f85Color);
							font-size: var(--font12Size);
						}
					}

					.r {
						width: 25vw;
						text-align: right;
					}
				}

				.other {
					margin: 0 0 2vw 0;
					text-align: right;

					text {
						font-size: 14px;

						padding: 0 0 0 2vw;
					}
				}

				.btn {
					text-align: right;
					margin: 2vw 0 0 0;
					border-top: 1px solid #f1fff1;

					button {
						margin: 2vw 0 0 2vw;
					}
				}
			}
		}

		.two {
			padding: 2vw;

			.btn {
				text-align: center;

				button {
					width: 30%;
					font-size: 14px;
					background-color: var(--f35BColor);
				}
			}
		}
	}

	.uni-forms-item {
		margin-bottom: 6vw !important;
		display: flex;
		flex-direction: row;
	}
</style>
