<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<input type="text" v-model="searchInfo.goods" @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 textOver">
												{{item.goods.goods.name||'暂无'}}
											</view>
											<view class="other_1">
												商品规格：<text>{{item.goods.name||'暂无'}}</text>
											</view>
											<view class="other_1" v-if="item.type!='3'">
												退款：<text>¥{{item.money||0}}</text>
											</view>
											<view class="other_1">
												申请时间：<text>{{item.apply_time||'暂无'}}</text>
											</view>
											<view class="other_1">
												售后类型：<text>{{item.zhStatus||'暂无'}}</text>
											</view>
											<view class="other_1">
												<text v-if="item.goods.gift.length>0" class="gift">赠品</text>
												<text v-if="item.goods.sp_price" class="act">特价</text>
											</view>
										</view>
									</view>
									<view class="btn">
										<button type="default" size="mini" @tap.stop="toView(item)">详细信息</button>
										<button v-if="item.status=='0'" type="default" size="mini"
											@tap.stop="toCancel(item)">取消售后</button>
										<button
											v-if="item.type!='1'&&item.status=='2'||item.status=='3'&&!item.transport.shop_receive"
											type="default" size="mini" @tap.stop="toMaintain(item)">维护单号</button>
										<button
											v-if="item.type=='3'&&item.status=='3'&&item.transport.shop_receive==true"
											type="default" size="mini" @tap.stop="toLogi(item)">查看物流</button>
										<button
											v-if="item.type=='3'&&item.status=='3'&&item.transport.shop_receive==true&&!item.transport.customer_receive"
											type="default" size="mini" @tap.stop="toConfirm(item)">确认收货</button>
									</view>
								</view>
								<view class="is_bottom" v-if="is_bottom">
									<text>{{config.bottom_title}}</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 {
				// 系统设置
				config: {},
				user: {},
				searchInfo: {},
				// 售后类型
				typeList: [],
				// 售后状态
				statusList: [],
				tabs: {
					active: '1',
					menu: []
				},
				type: '1',
				list: [],
				total: 0,
				skip: 0,
				limit: 10,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
			};
		},
		onLoad: async function() {
			const that = this;
			that.searchConfig();
			// 查询其他信息
			await that.searchOther();
			// 监听用户登录
			await that.watchLogin();

		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			// 查询基本设置
			searchConfig() {
				const that = this;
				uni.getStorage({
					key: 'config',
					success: function(res) {
						if (res.data) that.$set(that, `config`, res.data)
					},
					fail: function(err) {
						console.log(err);
					}
				})
			},
			// 监听用户是否登录
			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 type = that.type;
				const arr = await that.$api(`/afterSale`, 'GET', {
					customer: user._id,
					type: that.type,
					...that.searchInfo
				});
				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;
						let status = that.statusList.find(i => i.value == val.status)
						if (status) val.zhStatus = status.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)
					var menu = res.data.map((item) => {
						return {
							title: item.label,
							active: item.value
						}
					})
					that.$set(that.tabs, `menu`, menu)
				}
				res = await that.$api(`/dictData`, 'GET', {
					code: "afterSale_status"
				});
				if (res.errcode == '0') that.$set(that, `statusList`, 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);
			},
			// 输入框
			toInput(e) {
				const that = this;
				if (that.searchInfo.goods) that.$set(that.searchInfo, `goods`, e.detail.value)
				else that.$set(that, `searchInfo`, {})
				that.clearPage();
				that.search();
			},
			// 选择选项卡
			tabsChange(e) {
				const that = this;
				that.$set(that.tabs, `active`, e.active)
				that.$set(that, `type`, e.active);
				that.clearPage();
				that.search()
			},
			// 售后详细信息
			toView(item) {
				uni.navigateTo({
					url: `/pagesMy/order/afterInfo?id=${item._id}&status=${'0'}`
				})
			},
			// 订单详细
			toInfo(item) {
				uni.navigateTo({
					url: `/pagesMy/order/info?id=${item.order_detail._id}&status=${item.order_detail.status}`
				})
			},
			// 维护单号
			toMaintain(item) {
				uni.navigateTo({
					url: `/pagesMy/order/detail?id=${item._id}$status=${'0'}`
				})
			},
			// 确认收货
			async toConfirm(item) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '是否确认收货?',
					success: async function(res) {
						if (res.confirm) {
							item.transport.customer_receive = true;
							const arr = await that.$api(`/afterSale/${item.id}`, 'POST', {
								transport: item.transport
							});
							if (arr.errcode == '0') {
								uni.showToast({
									title: `确认收货成功`,
									icon: 'success',
								});
								that.clearPage();
								that.search();
							} else {
								uni.showToast({
									title: arr.errmsg,
									icon: 'none',
								})
							}
						}
					}
				});
			},
			// 查看物流
			toLogi(e) {
				uni.navigateTo({
					url: `/pagesMy/logistics/index?id=${e._id}&type=${'afterSale'}`
				})
			},
			// 取消售后
			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'
								})
							}
						}
					}
				});
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 6)
				that.$set(that, `page`, 0)
			}
		}
	}
</script>

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

		.one {
			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 2vw 0 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;

						.image {
							width: 25vw;
							height: 25vw;
							margin: 0 2vw 1vw 0;
							border: 1px solid var(--f85Color);
						}

						.other {
							width: 65vw;
							flex-grow: 1;

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

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

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

								.gift {
									margin: 0 1vw 0 0;
									font-size: 12px;
									color: #FFA500;
									border: 1px solid #FFA500;
									border-radius: 5px;
									padding: 0 1vw;
								}

								.act {
									font-size: 12px;
									border: 1px solid var(--fFB1Color);
									color: var(--fFB1Color);
									border-radius: 5px;
									padding: 0 1vw;
								}
							}
						}
					}

					.btn {
						text-align: right;

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

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

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}

	.is_bottom {
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
			color: #858585;
			font-size: 14px;
		}
	}
</style>
