<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<input type="text" v-model="searchInfo.name" @input="toInput" placeholder="搜索商品" @scroll="toScroll">
			</view>
			<view class="two">
				<tabs :tabs="tabs" @tabsChange="tabsChange">
					<view class="tabsList">
						<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage">
							<view class="list-scroll-view">
								<view class="list" v-for="(item,index) in list" :key="index" @tap="toInfo(item)">
									<view class="list_1">
										<view class="shopname">
											<text class="iconfont icon-shangdian"></text>
											<text>{{item.shop.name}}</text>
										</view>
										<view class="type">
											{{item.zhType||'暂无'}}
										</view>
									</view>
									<view class="list_2">
										<image class="image" :src="item.url&&item.url.length>0?item.url[0].url:''" mode="">
										</image>
										<view class="other">
											<view class="name">
												{{item.goods.goods.name||'暂无'}}
											</view>
											<view class="other_1">
												商品规格：<text>{{item.goods.name||'暂无'}}</text>
											</view>
											<view class="other_1" v-if="item.type!='2'">
												退款：<text>¥{{item.money||0}}</text>
											</view>
											<view class="other_1">
												申请时间：<text>{{item.apply_time||'暂无'}}</text>
											</view>
											<view class="other_1">
												售后描述：<text>{{item.desc||'暂无'}}</text>
											</view>
											<view class="other_1">
												售后处理人：<text>{{item.deal_person.name||'暂无'}}</text>
											</view>
										</view>
									</view>
									<view class="btn">
										<button v-if="item.status=='0'||item.status=='1'" type="default" size="mini" @tap.stop="toCancel(item)">取消售后</button>
										<button v-if="item.type!='0'" type="default" size="mini" @tap.stop="toRevise(item)">维护信息</button>
									</view>
								</view>
								<view class="is_bottom" v-if="is_bottom">
									<text>我们也是有底线的！</text>
								</view>
							</view>
						</scroll-view>
					</view>
				</tabs>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	import tabs from '@/components/tabs/index.vue';
	export default {
		components: {
			tabs
		},
		data() {
			return {
				user: {},
				list: [],
				typeList: [],
				total: 0,
				skip: 0,
				limit: 5,
				page: 0,
				tabs: {
					active: '0',
					menu: [{
							title: '申请售后',
							active: '0'
						},
						{
							title: '正在处理退款',
							active: '1'
						},
						{
							title: '已退款',
							active: '-1'
						},
						{
							title: '已退货',
							active: '-2'
						},
						{
							title: '正在处理换货',
							active: '3'
						},
						{
							title: '已换货',
							active: '-3'
						}
					]
				},
				status: '0',
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
			};
		},
		onShow: async function() {
			const that = this;
			that.clearPage();
			// 查询其他信息
			await that.searchOther();
			// 监听用户登录
			await that.watchLogin();
		},
		methods: {
			// 选择选项卡
			tabsChange(e) {
				const that = this;
				that.$set(that.tabs, `active`, e.active)
				that.$set(that, `status`, e.active);
				that.clearPage();
				that.search()
			},
			// 订单详细
			toInfo(item) {
				uni.navigateTo({
					url: `/pagesMy/order/info?id=${item.order_detail._id}&status=${item.order_detail.status}`
				})
			},
			// 维护信息
			toRevise(e) {
				uni.navigateTo({
					url: `/pagesMy/order/detail?id=${e.id||e._id}`
				})
			},
			// 取消售后
			toCancel(e) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定删除售后申请吗?',
					success: async function(res) {
						if (res.confirm) {
							const arr = await that.$api(`/afterSale/${e._id}`, 'DELETE');
							if (arr.errcode == '0') {
								uni.showToast({
									title: '删除售后成功',
									icon: 'none'
								})
								that.clearPage();
								that.search();
							} else {
								uni.showToast({
									title: arr.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 监听用户是否登录
			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(err) {
						uni.navigateTo({
							url: `/pages/login/index`
						})
					}
				});
			},
			// 查询列表
			async search() {
				const that = this;
				let user = that.user;
				let status = that.status;
				const arr = await that.$api(`/afterSale`, 'GET', {
					customer: user._id,
					status: that.status,
				});
				if (arr.errcode == '0') {
					let list = [...that.list, ...arr.data];
					for (let val of list) {
						let type = that.typeList.find(i => i.value == val.type)
						if (type) val.zhType = type.label;
						val.url = val?.goods?.goods?.file;
					}
					that.$set(that, `list`, list)
					that.$set(that, `total`, arr.total)
				} else {
					uni.showToast({
						title: arr.errmsg,
					});
				}
			},
			// 查询其他信息
			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)
				}
			},
			// 分页
			toPage(e) {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.search();
					uni.hideLoading();
				} else that.$set(that, `is_bottom`, true)
			},
			toScroll(e) {
				const that = this;
				let up = that.scrollTop;
				that.$set(that, `scrollTop`, e.detail.scrollTop);
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 5)
				that.$set(that, `page`, 0)
			}
		}
	}
</script>

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

		.one {
			// border-bottom: 1px solid var(--f85Color);
			padding: 2vw;

			input {
				padding: 2vw;
				background-color: var(--f1Color);
				font-size: var(--font14Size);
				border-radius: 5px;
			}
		}

		.two {
			position: relative;
			flex-grow: 1;
			background-color: var(--f9Color);

			.tabsList {
				position: relative;
				width: 100vw;
				height: 82vh;

				.list {
					margin: 2vw;
					background-color: var(--fffColor);
					padding: 2vw;
					width: 92vw;
					border-radius: 5px;

					.list_1 {
						display: flex;
						justify-content: space-between;
						font-size: var(--font16Size);
						margin: 0 0 2vw 0;

						.shopname {
							text {
								margin: 0 0 0 1vw;
							}
						}

						.type {
							color: var(--ff0Color);
						}
					}

					.list_2 {
						display: flex;
						justify-content: space-between;
						align-items: center;

						.image {
							width: 25vw;
							height: 25vw;
							margin: 0 2vw 1vw 0;
						}

						.other {
							flex-grow: 1;

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

							.other_1 {
								font-size: var(--font14Size);

								text {
									color: var(--f85Color);
								}
							}
						}
					}

					.btn {
						text-align: right;

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

				.list:nth-child(2n) {
					margin: 0 0 2vw 0;
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}
</style>
