<template>
	<view class="main">
		<view class="one">
			<view class="one_1">
				<image v-if="info.source&&info.source.file.length>0" class="image"
					:src="info.source.file&&info.source.file.length>0?info.source.file[0].url:''">
				</image>
				<image v-else class="image" :src="config.logo_url&&config.logo_url.length>0?config.logo_url[0].url:''">
				</image>
				<text v-if="info.type=='0'">酒店民宿</text>
				<text v-else>景区门票</text>
			</view>
			<view class="one_2">
				<view class="left">
					<image v-if="info.source&&info.source.file.length>0" class="image"
						:src="info.source.file&&info.source.file.length>0?info.source.file[0].url:''">
					</image>
					<image v-else class="image"
						:src="config.logo_url&&config.logo_url.length>0?config.logo_url[0].url:''">
					</image>
				</view>
				<view class="right">
					<view class="right_1">
						<view class="left">
							<view class="name textOver">{{info.source_name||'暂无'}}</view>
							<view class="other">{{info.zhType||'暂无'}}</view>
							<view class="other">{{info.zhPerson||'暂无'}}</view>
						</view>
						<view class="right">
							<view class="other">
								￥{{info.source.money||'0'}}
							</view>
							<view class="other">
								×{{info.num||'0'}}
							</view>
						</view>
					</view>
					<view class="right_2">
						合计：￥{{info.money||'0'}}
					</view>
				</view>
			</view>
		</view>
		<view class="two">
			<uni-forms class="form" ref="form" :rules="rules" :modelValue="form" label-position="top" label-width='100'>
				<uni-forms-item label="退款理由(必填)" required name="reason">
					<uni-easyinput type="textarea" v-model="form.reason" placeholder="请输入退款理由" />
				</uni-forms-item>
				<uni-forms-item label="上传图片0/3(选填)" name="file">
					<upload class='upload' :list="form.file" name="file" :count="3" @uplSuc="uplSuc" @uplDel="uplDel">
					</upload>
				</uni-forms-item>
			</uni-forms>
			<button class="button" type="primary" @click="submit('form')">提交</button>
		</view>
	</view>
</template>

<script>
	import moment from 'moment';
	import upload from '../../components/upload/index.vue';
	export default {
		components: {
			upload
		},
		data() {
			return {
				id: '',
				config: {
					logo_url: []
				},
				user: {},
				info: {
					source: {
						file: []
					}
				},
				form: {
					file: []
				},
				statusList: [],
				personList: [],
				typeList: [],
				// 校验规则
				rules: {
					reason: {
						rules: [{
							required: true,
							errorMessage: '请输入退款理由'
						}]
					},
				},
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e && e.id || '');
			that.searchConfig();
			that.searchToken();
			await that.searchOther();
			that.search();
		},
		methods: {
			searchConfig() {
				const that = this;
				try {
					const res = uni.getStorageSync('config');
					if (res) that.$set(that, `config`, res);
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) that.$set(that, `user`, res);
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			async search() {
				const that = this;
				if (that.id) {
					const res = await that.$api(`/order/${that.id}`, 'GET', {})
					if (res.errcode == '0') {
						const status = that.statusList.find(i => i.value == res.data.status)
						if (status) res.data.zhStatus = status.label
						if (res.data && res.data.source && res.data.source.person) {
							const person = that.personList.find(i => i.value == res.data.source.person)
							if (person) res.data.zhPerson = person.label
						}
						if (res.data && res.data.source && res.data.source.type) {
							const type = that.typeList.find(i => i.value == res.data.source.type)
							if (type) res.data.zhType = type.label
						}
						that.$set(that, `info`, res.data)
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
				}
			},
			// 图片上传
			uplSuc(e) {
				const that = this;
				that.$set(that.form, `${e.name}`, [...that.form[e.name], e.data]);
			},
			// 图片删除
			uplDel(e) {
				const that = this;
				let data = that.form[e.name];
				let arr = data.filter((i, index) => index != e.data.index);
				that.$set(that.form, `${e.name}`, arr)
			},
			submit(ref) {
				const that = this;
				that.$refs[ref].validate().then(async data => {
					data.user = that.user._id
					data.order = that.id
					data.type = that.info.type
					data.apply_time = moment().format('YYYY-MM-DD HH:mm:ss')
					const res = await that.$api(`/afterSale`, 'POST', data);
					if (res.errcode == '0') {
						const arr = await that.$api(`/order/${that.id}`, 'POST', {
							status: "5"
						})
						if (arr.errcode == '0') {
							uni.showToast({
								title: '售后申请成功',
								icon: 'none'
							})
							uni.navigateBack({
								delta: 1
							})
						} else {
							uni.showToast({
								title: res.errmsg,
							});
						}
					} else {
						uni.showToast({
							title: res.errmsg,
							icon: 'none'
						})
					}
				}).catch(err => {
					console.log('err', err);
				})
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				// 查询类型
				res = await that.$api(`/dictData`, 'GET', {
					type: 'order_status',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `statusList`, res.data);
				// 出游人群
				res = await that.$api(`/dictData`, 'GET', {
					type: 'ticket_person',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `personList`, res.data);
				// 服务特色
				res = await that.$api(`/dictData`, 'GET', {
					type: 'ticket_type',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `typeList`, res.data);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main {

		.one {
			background-color: var(--mainColor);
			border-radius: 5px;
			margin: 2vw 2vw 0 2vw;
			padding: 2vw;
			border: 1px solid #d5d5da;

			.one_1 {
				display: flex;
				align-items: center;

				.image {
					width: 8vw;
					height: 8vw;
					border-radius: 8vw;
				}

				text {
					padding: 0 0 0 2vw;
				}
			}

			.one_2 {
				display: flex;
				justify-content: space-between;
				margin: 2vw 0 0 0;
				padding: 0 2vw;

				.left {
					width: 20vw;

					.image {
						width: 20vw;
						height: 20vw;
						border-radius: 1vw;
					}
				}

				.right {
					width: 65vw;
					margin: 0 0 0 2vw;

					.right_1 {
						display: flex;
						justify-content: space-between;

						.left {
							width: 55vw;

							.name {
								font-size: var(--font14Size);
							}

							.other {
								padding: 2px 0 0 0;
								font-size: var(--font12Size);
								color: var(--f85Color);
							}
						}

						.right {
							width: 10vw;
							text-align: right;

							.other {
								padding: 2px 0 0 0;
								font-size: var(--font12Size);
								color: var(--f85Color);
							}
						}
					}

					.right_2 {
						text-align: right;
						font-size: var(--font12Size);
					}
				}
			}
		}

		.two {
			padding: 2vw;

			.form-item {
				display: flex;
				align-items: center;
			}

			.button {
				background-color: var(--f3CColor);
			}
		}
	}
</style>