<template>
	<view class="my-fish-page">
		<view class="header" :style="{ paddingTop: statusBarTop + 'px' }">
			<view class="back" @tap="back"><uni-icons type="left" size="22"></uni-icons></view>
			<view class="title">我的校园二手</view>
		</view>
		<view class="tabs">
			<view class="tab" :class="{ active: activeTab == item.key }" v-for="item in tabs" :key="item.key" @tap="switchTab(item.key)">
				{{item.name}}
			</view>
		</view>
		<view class="search-bar" v-if="canManageCurrentTab">
			<uni-icons type="search" size="18" color="#8d9693"></uni-icons>
			<input
				v-model.trim="titleKeyword"
				class="search-input"
				confirm-type="search"
				placeholder="按标题搜索"
				@confirm="handleSearch"
			/>
			<view class="search-clear" v-if="titleKeyword" @tap="clearSearch">清空</view>
			<view class="search-btn" @tap="handleSearch">搜索</view>
		</view>
		<scroll-view scroll-y class="list" :class="{ 'list--with-search': canManageCurrentTab }" @scrolltolower="loadMore">
			<u-swipe-action v-if="canManageCurrentTab">
				<u-swipe-action-item
					v-for="(item,index) in list"
					:key="item.id"
					:name="index"
					:options="swipeOptions(item)"
					@click="handleSwipeAction"
				>
					<view class="card" @tap="openItem(item)">
						<image class="cover" :src="item.coverImage || item.pic || defaultCover" mode="aspectFill"></image>
						<view class="main">
							<view class="name">{{nameText(item)}}</view>
							<view class="desc">{{descText(item)}}</view>
							<view class="meta">{{item.createTime || item.lastBrowseTime || item.lastChatTime || ''}}</view>
						</view>
						<view class="status" :class="{ 'status--off': isOffShelf(item) }">{{statusText(item)}}</view>
					</view>
				</u-swipe-action-item>
			</u-swipe-action>
			<block v-if="!canManageCurrentTab">
				<view class="card" v-for="item in list" :key="item.id" @tap="openItem(item)">
					<image class="cover" :src="item.coverImage || item.pic || defaultCover" mode="aspectFill"></image>
					<view class="main">
						<view class="name">{{nameText(item)}}</view>
						<view class="desc">{{descText(item)}}</view>
						<view class="meta">{{item.createTime || item.lastBrowseTime || item.lastChatTime || ''}}</view>
					</view>
					<view class="status" :class="{ 'status--off': isOffShelf(item) }">{{statusText(item)}}</view>
				</view>
			</block>
			<view class="empty" v-if="!list.length && !loading">暂无数据</view>
			<view class="load-more" v-if="loading">加载中...</view>
			<view class="bottom-space"></view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		pageMyFishGoods,
		pageMyFishWants,
		updateMyFishGoodsStatus,
		updateMyFishWantStatus,
		pageMyFishFavorites,
		pageMyFishComments,
		pageMyFishLikes,
		pageMyFishComplaints
	} from '@/common/fishMarketApi.js'

	export default {
		data() {
			return {
				statusBarTop: 24,
				activeTab: 'goods',
				list: [],
				pageNum: 1,
				pageSize: 10,
				totalPages: 1,
				loading: false,
				titleKeyword: '',
				defaultCover: '/static/images/img/songshu.png',
				tabs: [
					{ key: 'goods', name: '我的发布' },
					{ key: 'wants', name: '我的求购' },
					{ key: 'favorites', name: '我的收藏' },
					{ key: 'comments', name: '我的评论' },
					{ key: 'likes', name: '我的点赞' },
					{ key: 'complaints', name: '我的举报' }
				]
			}
		},
		computed: {
			canManageCurrentTab() {
				return this.activeTab == 'goods' || this.activeTab == 'wants'
			}
		},
		onLoad() {
			if (uni.getMenuButtonBoundingClientRect) {
				this.statusBarTop = uni.getMenuButtonBoundingClientRect().top || this.statusBarTop
			}
			this.load()
		},
		methods: {
			back() {
				uni.navigateBack()
			},
			swipeOptions(item) {
				const offShelf = this.isOffShelf(item)
				return [{
					text: offShelf ? '上架' : '下架',
					style: {
						backgroundColor: offShelf ? '#24b979' : '#ff9f2d',
						color: '#fff',
						fontSize: '26rpx'
					}
				}, {
					text: '删除',
					style: {
						backgroundColor: '#ef4c3f',
						color: '#fff',
						fontSize: '26rpx'
					}
				}]
			},
			handleSwipeAction(e) {
				const item = this.list[Number(e.name)]
				if (!item) return
				if (e.index == 0) {
					this.toggleShelf(item)
					return
				}
				this.deleteItem(item)
			},
			isOffShelf(item) {
				return Number(item.status) == 2 || Number(item.offShelf) == 1
			},
			updateItemStatus(item, status) {
				const request = this.activeTab == 'goods'
					? updateMyFishGoodsStatus({ goodsId: item.id, status })
					: updateMyFishWantStatus({ wantId: item.id, status })
				return request.then(res => {
					if (!res && res !== true) return
					uni.showToast({ title: status == 3 ? '已删除' : (status == 2 ? '已下架' : '已上架'), icon: 'none' })
					this.reload()
				})
			},
			toggleShelf(item) {
				const nextStatus = this.isOffShelf(item) ? 1 : 2
				this.updateItemStatus(item, nextStatus)
			},
			deleteItem(item) {
				uni.showModal({
					title: '删除确认',
					content: '删除后不可恢复，确定删除吗？',
					confirmText: '删除',
					confirmColor: '#ef4c3f',
					success: (res) => {
						if (!res.confirm) return
						this.updateItemStatus(item, 3)
					}
				})
			},
			switchTab(key) {
				this.activeTab = key
				this.titleKeyword = ''
				this.reload()
			},
			handleSearch() {
				uni.hideKeyboard()
				this.reload()
			},
			clearSearch() {
				this.titleKeyword = ''
				this.reload()
			},
			reload() {
				this.pageNum = 1
				this.totalPages = 1
				this.list = []
				this.load()
			},
			loadMore() {
				if (this.loading || this.pageNum >= this.totalPages) return
				this.pageNum += 1
				this.load()
			},
			load() {
				this.loading = true
				const query = {
					pageNum: this.pageNum,
					pageSize: this.pageSize,
					titleKeyword: this.canManageCurrentTab ? this.titleKeyword : ''
				}
				const map = {
					goods: pageMyFishGoods,
					wants: pageMyFishWants,
					favorites: pageMyFishFavorites,
					comments: pageMyFishComments,
					likes: pageMyFishLikes,
					complaints: pageMyFishComplaints
				}
				map[this.activeTab](query).then(page => {
					const records = page && (page.records || page.content) ? (page.records || page.content) : []
					this.totalPages = Number(page && (page.pages || page.totalPages)) || 1
					this.list = this.pageNum == 1 ? records : this.list.concat(records)
				}).finally(() => {
					this.loading = false
				})
			},
			openItem(item) {
				const goodsId = item.goodsId || (this.activeTab == 'goods' ? item.id : '')
				if (goodsId) uni.navigateTo({ url: '/package2/IdleTrad/detail?id=' + goodsId })
			},
			statusText(item) {
				if (this.activeTab == 'likes') return item.likeType == 'comment' ? '评论点赞' : '商品点赞'
				if (this.activeTab == 'complaints') return item.status == 1 ? '已通过' : (item.status == 2 ? '已驳回' : '待处理')
				if (item.saleStatus == 2) return '已预定'
				if (item.saleStatus == 3) return '已卖出'
				if (item.status == 2) return '已下架'
				return '正常'
			},
			nameText(item) {
				if (item.title) return item.title
				if (this.activeTab == 'comments') return '我发表的评论'
				if (this.activeTab == 'likes') return item.likeType == 'comment' ? '我点赞的评论' : '我点赞的商品'
				if (this.activeTab == 'complaints') return '我的举报记录'
				return item.goodsId || item.id || '校园二手'
			},
			descText(item) {
				if (this.activeTab == 'favorites') return '商品ID：' + item.goodsId
				if (this.activeTab == 'comments') return item.content || '评论内容'
				if (this.activeTab == 'likes') return '关联商品：' + (item.goodsId || '评论 ' + (item.commentId || ''))
				if (this.activeTab == 'complaints') return item.reason || '举报内容'
				return item.content || ''
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #f7f9f7;
	}
	.my-fish-page {
		min-height: 100vh;
		background: #f7f9f7;
		color: #17362f;
	}
	.header {
		padding-left: 26rpx;
		padding-right: 26rpx;
		background: linear-gradient(180deg, #e5fbf3, #f7f9f7);
	}
	.back {
		width: 68rpx;
		height: 68rpx;
		display: flex;
		align-items: center;
	}
	.title {
		padding: 10rpx 0 24rpx;
		font-size: 40rpx;
		font-weight: 900;
	}
	.tabs {
		display: flex;
		overflow-x: scroll;
		padding: 0 22rpx 18rpx;
		white-space: nowrap;
	}
	.tab {
		flex-shrink: 0;
		margin-right: 18rpx;
		padding: 14rpx 24rpx;
		border-radius: 28rpx;
		background: #fff;
		color: #758782;
		font-size: 24rpx;
	}
	.tab.active {
		background: #24b979;
		color: #fff;
		font-weight: 900;
	}
	.search-bar {
		height: 70rpx;
		margin: 0 24rpx 16rpx;
		padding: 0 12rpx 0 22rpx;
		display: flex;
		align-items: center;
		border-radius: 38rpx;
		background: #fff;
		box-shadow: 0 8rpx 22rpx rgba(20, 60, 48, 0.045);
		box-sizing: border-box;
	}
	.search-input {
		flex: 1;
		height: 68rpx;
		margin-left: 10rpx;
		color: #293632;
		font-size: 24rpx;
		line-height: 68rpx;
	}
	.search-clear {
		padding: 0 12rpx;
		color: #9aa8a4;
		font-size: 22rpx;
	}
	.search-btn {
		width: 86rpx;
		height: 54rpx;
		border-radius: 28rpx;
		background: linear-gradient(135deg, #38c897, #19af7e);
		color: #fff;
		font-size: 23rpx;
		font-weight: 800;
		line-height: 54rpx;
		text-align: center;
	}
	.list {
		height: calc(100vh - 210rpx);
	}
	.list--with-search {
		height: calc(100vh - 296rpx);
	}
	.card {
		margin: 18rpx 24rpx 0;
		padding: 18rpx;
		display: flex;
		align-items: center;
		border-radius: 24rpx;
		background: #fff;
		box-shadow: 0 8rpx 22rpx rgba(20, 60, 48, 0.05);
	}
	.cover {
		width: 126rpx;
		height: 126rpx;
		border-radius: 18rpx;
		background: #edf3ef;
	}
	.main {
		flex: 1;
		min-width: 0;
		margin-left: 18rpx;
	}
	.name {
		overflow: hidden;
		font-size: 28rpx;
		font-weight: 900;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
	.desc {
		margin-top: 8rpx;
		overflow: hidden;
		color: #71817c;
		font-size: 23rpx;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
	.meta {
		margin-top: 8rpx;
		color: #a0aaa6;
		font-size: 20rpx;
	}
	.status {
		margin-left: 12rpx;
		color: #22a879;
		font-size: 22rpx;
		font-weight: 800;
	}
	.status--off {
		color: #9aa8a4;
	}
	.empty,
	.load-more {
		padding: 80rpx 0;
		color: #9aa8a4;
		text-align: center;
	}
	.bottom-space {
		height: 120rpx;
	}
</style>
