<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<view class="list" v-if="info._id">
					<view class="list_1">
						<text>退款商品</text>
					</view>
					<view class="list_2">
						<view class="l">
							<image class="image"
								:src="info.goods.file&&info.goods.file.length>0?info.goods.file[0].url:''" mode="">
							</image>
						</view>
						<view class="c">
							<view class="name">
								{{info.name}}
							</view>
						</view>
						<view class="r">
							<view class="price">
								¥{{info.sell_money}}
							</view>
							<view class="num">
								×{{info.buy_num}}
							</view>
							<view class="money" v-if="moneyInfo.payTotal">
								<view class="">商品总价:{{moneyInfo.goodsTotal}}</view>
								<view class="">优惠减免:{{moneyInfo.discountTotal}}</view>
								<view class="">运费总价:{{moneyInfo.freightTotal}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="two">
				<uni-forms ref="form" :rules="rules" :model="form" label-width="auto">
					<uni-forms-item label="退款商品" name="goods_id">
						<picker class="picker" mode="selector" :range="goodsList" @change="goodsChange"
							range-key="name">
							<view>{{form.goods_name||'请选择退款商品'}}</view>
						</picker>
					</uni-forms-item>
					<uni-forms-item label="售后类型" name="type" v-if="info._id">
						<picker class="picker" mode="selector" :range="typeList" @change="typeChange" range-key="label">
							<view>{{form.type_name||'请选择售后类型'}}</view>
						</picker>
					</uni-forms-item>
					<uni-forms-item label="退款金额" name="money" v-if="form.type=='0'||form.type=='2'">
						<uni-easyinput type="digit" v-model="form.money" @input="toMoney" placeholder="请输入退款金额" />
						<view>
							最大退款金额不能超过{{moneyInfo.payTotal||'暂无'}}
						</view>
					</uni-forms-item>
					<uni-forms-item label="申请理由" name="reason">
						<picker class="picker" mode="selector" :range="reasonList" @change="reasonChange"
							range-key="label">
							<view>{{form.reason_name||'请选择申请理由'}}</view>
						</picker>
					</uni-forms-item>
					<uni-forms-item label="申请售后描述" name="desc">
						<uni-easyinput type="textarea" v-model="form.desc" placeholder="请输入申请售后描述" />
					</uni-forms-item>
					<uni-forms-item label="附件" name="icon">
						<upload :list="icon" name="icon" :count="1" @uplSuc="uplSuc" @uplDel="uplDel"></upload>
					</uni-forms-item>
					<view class="btn">
						<button v-if="money_num=='0'" type="primary" size="mini" @click="onSubmit('form')">提交保存</button>
					</view>
				</uni-forms>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	import upload from '@/components/upload/index.vue';
	export default {
		components: {
			upload
		},
		data() {
			return {
				id: '',
				user: {},
				// 商品详情
				info: {},
				form: {},
				moneyInfo: {},
				// 退货商品
				goodsList: [],
				icon: [],
				rules: {
					type: {
						rules: [{
							required: true,
							errorMessage: '请选择售后类型',
						}]
					},
					goods_id: {
						rules: [{
							required: true,
							errorMessage: '请选择退款商品',
						}]
					},
					reason: {
						rules: [{
							required: true,
							errorMessage: '请选择申请理由',
						}]
					},
				},
				// 售后类型
				typeList: [],
				// 申请理由
				reasonList: [],
				money_num: '0'
			};
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			// 查询其他信息
			await that.searchOther();
			// 监听用户登录
			await that.watchLogin();
		},
		methods: {
			// 退款金额
			toMoney(value) {
				const that = this;
				let money = that.moneyInfo.payTotal;
				if (parseFloat(value) > parseFloat(money)) {
					uni.showToast({
						title: '输入金额不能超过实际支付金额',
						icon: 'none'
					})
					that.$set(that, `money_num`, '1');
				} else {
					that.$set(that, `money_num`, '0');
				}
			},
			// 选择售后类型
			async typeChange(e) {
				const that = this;
				let data = that.typeList[e.detail.value];
				if (data) {
					that.$set(that.form, `type`, data.value);
					that.$set(that.form, `type_name`, data.label);
				}
				if (data.value != '2') {
					const arr = await that.$api(`/afterSale/cgfr`, 'POST', {
						order_detail: that.id,
						goods_id: that.info._id
					});
					if (arr.errcode == '0') {
						that.$set(that, `moneyInfo`, arr.data);
					} else {
						uni.showToast({
							title: arr.errmsg,
							icon: 'none',
						})
					}
				}

			},
			// 退款商品
			goodsChange(e) {
				const that = this;
				let data = that.goodsList[e.detail.value];
				if (data) {
					console.log(data);
					that.$set(that, `info`, data);
					that.$set(that.form, `goods_id`, data._id);
					that.$set(that.form, `goods_name`, data.name);
				}
			},
			// 选择申请理由
			reasonChange(e) {
				const that = this;
				let data = that.reasonList[e.detail.value];
				if (data) {
					that.$set(that.form, `reason`, data.value);
					that.$set(that.form, `reason_name`, data.label);
				}
			},
			// 图片上传
			uplSuc(e) {
				const that = this;
				that.$set(that, `${e.name}`, [...that[e.name], e.data]);
			},
			// 图片删除
			uplDel(e) {
				const that = this;
				let data = that[e.name];
				let arr = data.filter((i, index) => index != e.data.index);
				that.$set(that, `${e.name}`, arr)
			},
			// 提交保存
			async onSubmit(ref) {
				const that = this;
				that.$refs[ref].validate().then(async params => {
					params.file = that.icon;
					params.order_detail = that.id;
					const arr = await that.$api(`/afterSale`, 'POST', params);
					if (arr.errcode == '0') {
						uni.showToast({
							title: `申请售后成功`,
							icon: 'success',
						});
						uni.navigateBack({
							detail: 1
						})
					} else {
						uni.showToast({
							title: arr.errmsg,
							icon: 'none',
						})
					}
				})
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				res = await that.$api(`/dictData`, 'GET', {
					code: "afterSale_type"
				});
				if (res.errcode == '0') {
					that.$set(that, `typeList`, res.data)
				}
				res = await that.$api(`/dictData`, 'GET', {
					code: "afterSale_reason"
				});
				if (res.errcode == '0') {
					that.$set(that, `reasonList`, res.data)
				}
			},
			// 监听用户是否登录
			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)
								}
							}
						}
					},
					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;
					}

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

						.money {
							color: var(--f85Color);
							font-size: var(--font12Size);
						}
					}
				}

				.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;

			.picker {
				border: 1px solid #3333;
				border-radius: 5px;
				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>
