<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 class="money">¥{{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">
							<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-item label="售后类型" name="type">
							<picker class="picker" mode="selector" :range="typeList" @change="typeChange"
								range-key="label">
								<view>{{thrform.type_name||'请选择售后类型'}}</view>
							</picker>
						</uni-forms-item>
						<uni-forms-item label="退款金额" name="money" v-if="thrform.type=='1'||thrform.type=='2'">
							<uni-easyinput type="digit" v-model="thrform.money" @input="toMoney"
								placeholder="请输入退款金额" />
							<view class="money">
								最大退款金额不能超过 <text>{{moneyInfo.payTotal||0}}</text>,<text @tap="toAll">全部提现</text>
							</view>
						</uni-forms-item>
						<uni-forms-item label="申请理由" name="reason">
							<picker class="picker" mode="selector" :range="reasonList" @change="reasonChange"
								range-key="label">
								<view>{{thrform.reason_name||'请选择申请理由'}}</view>
							</picker>
						</uni-forms-item>
						<uni-forms-item label="售后描述" name="desc">
							<uni-easyinput type="textarea" v-model="thrform.desc" placeholder="请输入售后描述" />
						</uni-forms-item>
						<uni-forms-item label="附件" name="file">
							<upload :list="thrform.file" name="file" :count="1" @uplSuc="uplSuc" @uplDel="uplDel">
							</upload>
						</uni-forms-item>
						<view class="btn">
							<button type="primary" size="mini" @click="onSubmit('thrform')">提交保存</button>
						</view>
					</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>
	import upload from '@/components/upload/index.vue';
	export default {
		components: {
			upload
		},
		data() {
			return {
				id: '',
				status: '',
				// 用户信息
				user: {},
				// 信息详情
				info: {},
				// 仅退款
				form: {},
				rules: {
					desc: {
						rules: [{
							required: true,
							errorMessage: '请输入申请描述',
						}]
					},
				},
				// 已收到货
				thrform: {
					file: []
				},
				// 最大退款数
				moneyInfo: {},
				thrrules: {
					type: {
						rules: [{
							required: true,
							errorMessage: '请选择售后类型',
						}]
					},
					goods: {
						rules: [{
							required: true,
							errorMessage: '请选择退款商品',
						}]
					},
					reason: {
						rules: [{
							required: true,
							errorMessage: '请选择申请理由',
						}]
					},
				},
				// 售后类型
				typeList: [],
				// 申请理由
				reasonList: [],
			};
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			that.$set(that, `status`, e && e.status || '');
			// 查询其他信息
			await that.searchOther();
			// 监听用户登录
			await 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();
						}
					},
					fail: function() {
						uni.navigateTo({
							url: `/pages/login/index`
						})

					}
				})
			},
			async search() {
				const that = this;
				let id = that.id;
				let res = await that.$api(`/orderDetail/${id}`, 'GET')
				if (res.errcode == '0') {
					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];
				if (data) that.$set(that.info, `goods`, [data])
				that.$set(that.thrform, `goods`, data._id);
				that.$set(that.thrform, `goods_name`, data.goods_name);
			},
			// 选择售后类型
			async typeChange(e) {
				const that = this;
				let thrform = that.thrform;
				let data = that.typeList[e.detail.value];
				if (data) {
					that.$set(that.thrform, `type`, data.value);
					that.$set(that.thrform, `type_name`, data.label);
				}
				if (data.value != '3') {
					if (thrform.goods) {
						const arr = await that.$api(`/afterSale/cgfr`, 'POST', {
							order_detail: that.id,
							goods_id: thrform.goods
						});
						if (arr.errcode == '0') {
							that.$set(that, `moneyInfo`, arr.data);
						} else {
							uni.showToast({
								title: arr.errmsg,
								icon: 'none',
							})
						}
					} else {
						uni.showToast({
							title: '缺少商品信息',
							icon: 'none'
						})
					}
				}

			},
			// 选择申请理由
			reasonChange(e) {
				const that = this;
				let data = that.reasonList[e.detail.value];
				if (data) {
					that.$set(that.thrform, `reason`, data.value);
					that.$set(that.thrform, `reason_name`, data.label);
				}
			},
			// 退款金额
			toMoney(value) {
				const that = this;
				let money = that.moneyInfo.payTotal;
				if (parseFloat(value) > parseFloat(money)) {
					uni.showToast({
						title: '输入金额不能超过实际支付金额',
						icon: 'none'
					})
				}
			},
			// 图片上传
			uplSuc(e) {
				const that = this;
				that.$set(that.thrform, `${e.name}`, [...that.thrform[e.name], e.data]);
			},
			// 图片删除
			uplDel(e) {
				const that = this;
				let data = that.thrform[e.name];
				let arr = data.filter((i, index) => index != e.data.index);
				that.$set(that.thrform, `${e.name}`, arr)
			},
			// 全部提现
			toAll() {
				const that = this;
				that.$set(that.thrform, `money`, that.moneyInfo.payTotal)
			},
			// 提交保存
			async onSubmit(ref) {
				const that = this;
				let status = that.status;
				that.$refs[ref].validate().then(async params => {
					if (status == '3') {
						that.Receip(params)
					} else {
						that.orderCancel(params)
					}

				})
			},
			// 已收到货
			async Receip(e) {
				const that = this;
				e.file = that.thrform.file;
				e.order_detail = that.id;
				let money = that.moneyInfo.payTotal;
				let res;
				if (e.type != '3') {
					if (e.money) {
						if (parseFloat(e.money) > parseFloat(money)) {
							uni.showToast({
								title: '输入金额不能超过实际支付金额',
								icon: 'none'
							})
						} else {
							res = await that.$api(`/afterSale`, 'POST', e)
						}
					} else {
						uni.showToast({
							title: `请输入退款金额`,
							icon: 'none',
						});
					}
				} else {
					res = await that.$api(`/afterSale`, 'POST', e)
				}
				if (res.errcode == '0') {
					uni.showToast({
						title: '申请售后完成',
						icon: 'none'
					})
					uni.navigateBack({
						detail: 1
					})
				} else {
					uni.showToast({
						title: res.errmsg,
						icon: 'none'
					})
				}
			},
			// 未收到货
			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',
					})
				}
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				res = await that.$api(`/dictData`, 'GET', {
					code: "afterSale_type"
				});
				if (res.errcode == '0') {
					let type = res.data.filter(i => i.value != '4' && i.value != '5')
					that.$set(that, `typeList`, type)
				}
				res = await that.$api(`/dictData`, 'GET', {
					code: "afterSale_reason"
				});
				if (res.errcode == '0') {
					that.$set(that, `reasonList`, res.data)
				}
			}
		}
	}
</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 {
							.money {
								color: #ff0000 !important;
							}

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

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

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

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

			.two_1 {
				.picker {
					border: 1px solid #3333;
					border-radius: 5px;
					padding: 2vw;
				}

				.money {
					margin: 1vw 0 0 0;

					text {
						color: #ff0000;
					}

					text:last-child {
						color: #6A5ACD;
					}

				}

				.btn {
					text-align: center;
				}
			}

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

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