<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<view class="one_1">
					<text>售后商品</text>
				</view>
				<view class="one_2">
					<view class="list" v-for="(item,index) in info.goods" :key="index">
						<view class="list_1">
							<image class="image" :src="item.goods.file&&item.goods.file.length>0?item.goods.file[0].url:''" mode="">
							</image>
						</view>
						<view class="list_2">
							<view class="name">
								{{item.goods_name}}
							</view>
							<view class="other_1">
								<text>规格：</text>
								<text>{{item.name}}</text>
							</view>
							<view class="other_1">
								<text>金额：</text>
								<text>¥{{info.type=='0'?item.sell_money:item.group_config.money}}</text>
							</view>
							<view class="other_1">
								<text>数量：</text>
								<text>×{{item.buy_num}}</text>
							</view>

						</view>
					</view>
				</view>
			</view>
			<view class="two">
				<view class="two_1" v-if="status=='3'">
					<uni-forms ref="thrform" :rules="thrrules" :model="thrform" label-width="auto">
						<uni-forms-item label="售后商品" name="goods_id">
							<picker class="picker" mode="selector" :range="info.goods" @change="goodsChange" range-key="goods_name">
								<view>{{thrform.goods_name||'请选择退款商品'}}</view>
							</picker>
						</uni-forms-item>
					</uni-forms>
				</view>
				<view class="two_2" v-else>
					<uni-forms ref="form" :rules="rules" :model="form" label-width="auto">
						<uni-forms-item label="申请描述" name="desc">
							<uni-easyinput type="textarea" autoHeight v-model="form.desc" placeholder="请输入申请描述" />
						</uni-forms-item>
						<view class="btn">
							<button type="default" size="mini" @click="onSubmit('form')">提交保存</button>
						</view>
					</uni-forms>
				</view>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				id: '',
				status: '',
				// 用户信息
				user: {},
				// 信息详情
				info: {},
				// 仅退款
				form: {},
				rules: {
					desc: {
						rules: [{
							required: true,
							errorMessage: '请输入申请描述',
						}]
					},
				},
				// 已收到货
				thrform: {},
				thrrules: {}
			};
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '636221e5e25ed78601dd4fbb');
			that.$set(that, `status`, e && e.status || '3');
			that.watchLogin();
		},
		methods: {
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: function(res) {
						let user = that.$jwt(res.data);
						if (user) {
							that.$set(that, `user`, user);
							that.search();
						}
					}
				})
			},
			async search() {
				const that = this;
				let id = that.id;
				console.log(id);
				let res = await that.$api(`/orderDetail/${id}`, 'GET')
				if (res.errcode == '0') {
					console.log(res.data);
					for (let val of res.data.goods) val.goods_name = val.goods.name;
					that.$set(that, `info`, res.data)
				} else {
					uni.showToast({
						title: res.errmsg,
						icon: 'none'
					})
				}
			},
			// 售后请求
			// 已收到货
			goodsChange(e) {
				const that = this;
				let data = that.info.goods[e.detail.value];
				console.log(data);
			},
			// 提交保存
			async onSubmit(ref) {
				const that = this;
				let status = that.status;
				that.$refs[ref].validate().then(params => {
					if (status == '3') {
						console.log('1');
					} else {
						that.orderCancel(params)
					}

				})
			},
			// 未收到货
			async orderCancel(e) {
				const that = this;
				e.order_detail = that.id;
				const res = await that.$api(`/afterSale/orderCancel`, 'POST', e);
				if (res.errcode == '0') {
					uni.showToast({
						title: `申请售后成功`,
						icon: 'success',
					});
					uni.navigateBack({
						detail: 1
					})
				} else {
					uni.showToast({
						title: res.errmsg,
						icon: 'none',
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			margin: 2vw;
			border: 1px solid #f1f1f1;
			border-radius: 5px;
			padding: 2vw;

			.one_1 {
				margin: 0 0 2vw 0;

				text {
					font-size: 16px;
					font-weight: bold;
				}
			}

			.one_2 {
				.list {
					display: flex;
					background-color: #f1f1f1;
					padding: 2vw;
					margin: 0 0 2vw 0;
					border-radius: 5px;

					.list_1 {
						width: 22vw;
						height: 22vw;
						border-radius: 5px;

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

					.list_2 {
						width: 63vw;
						padding: 0 0 0 2vw;

						.name {
							font-size: 16px;
							font-weight: bold;
							margin: 0 0 0.5vw 0;
						}

						.other_1 {

							text {
								font-size: 13px;
								color: #858585;
							}

							text:last-child {
								color: #000000;
							}
						}
					}
				}

				.list:last-child {
					margin: 0;
				}
			}
		}

		.two {
			margin: 2vw;
			border: 1px solid #ff0000;
			border-radius: 5px;
			padding: 2vw;

			.two_1 {}

			.two_2 {
				.btn {
					text-align: center;

					button {
						background-color: var(--f35BColor);
						color: #fff;
					}
				}
			}
		}
	}
</style>
