<template>
	<view class="page-container">
		<!-- 列表内容 -->
		<view class="list-container">
			<u-list @scrolltolower="scrolltolower">
				<u-list-item v-for="(item, index) in channelList" :key="index" class="list-item">
					<view class="item-content" @click="editChannel(item)">
						<view class="item-header">
							<view class="item-title">{{ item.channelName || '未命名渠道' }}</view>
							<view class="item-status" :class="{'status-active': item.status === 1}">
								{{ item.status === 1 ? '启用' : '禁用' }}
							</view>
						</view>
						<view class="item-info">
							<view class="info-row" v-if="item.contactPhone">
								<uni-icons type="phone" size="16" color="#999"></uni-icons>
								<text class="info-text">{{ item.contactPhone }}</text>
							</view>
							<view class="info-row" v-if="item.wechatId">
								<uni-icons type="chat" size="16" color="#999"></uni-icons>
								<text class="info-text">{{ item.wechatId }}</text>
							</view>
							<view class="info-row" v-if="item.remark">
								<uni-icons type="info" size="16" color="#999"></uni-icons>
								<text class="info-text">{{ item.remark }}</text>
							</view>
						</view>
						<view class="item-footer">
							<text class="item-time">{{ formatTime(item.createTime) }}</text>
						</view>
					</view>
				</u-list-item>
				<u-loadmore :status="loadStatus" />
			</u-list>
		</view>
		
		<!-- 底部添加按钮 -->
		<view class="fab-container">
			<button class="fab-button" @click="addChannel">
				<uni-icons type="plus" size="24" color="#fff"></uni-icons>
				<text class="fab-text">新增渠道</text>
			</button>
		</view>
	</view>
</template>

<script>
	import {
		request
	} from '@/utils/request';
	
	export default {
		data() {
			return {
				channelList: [],
				page: 1,
				pageSize: 20,
				loadStatus: 'loadmore', // loadmore | loading | nomore
				total: 0
			}
		},
		onLoad() {
			uni.setNavigationBarTitle({
				title: '渠道管理'
			});
			this.loadChannelList();
		},
		onShow() {
			// 从新增/编辑页返回时刷新列表
			if (this.page === 1) {
				this.loadChannelList();
			}
		},
		onPullDownRefresh() {
			// #ifdef MP-WEIXIN
			this.page = 1;
			this.channelList = [];
			this.loadChannelList();
			// #endif
		},
		methods: {
			// 加载渠道列表
			loadChannelList() {
				this.loadStatus = 'loading';
				request({
					url: '/v1/frontend/business/channel/list',
					data: {
						page: this.page,
						limit: this.pageSize
					}
				}).then((response) => {
					// #ifdef MP-WEIXIN
					uni.stopPullDownRefresh();
					// #endif
					
					if (response.data.code === 10000 || response.data.code === 200) {
						const list = response.data.data.list || response.data.data.records || [];
						if (this.page === 1) {
							this.channelList = list;
						} else {
							this.channelList = [...this.channelList, ...list];
						}
						this.total = response.data.data.total || 0;
						
						if (this.channelList.length >= this.total) {
							this.loadStatus = 'nomore';
						} else {
							this.loadStatus = 'loadmore';
						}
					} else {
						uni.showToast({
							title: response.data.message || '加载失败',
							icon: 'none',
							duration: 2000
						});
						this.loadStatus = 'loadmore';
					}
				}).catch((error) => {
					// #ifdef MP-WEIXIN
					uni.stopPullDownRefresh();
					// #endif
					
					console.error('加载渠道列表失败:', error);
					uni.showToast({
						title: '网络错误，请重试',
						icon: 'none',
						duration: 2000
					});
					this.loadStatus = 'loadmore';
				});
			},
			
			// 滚动到底部加载更多
			scrolltolower() {
				if (this.loadStatus === 'loadmore') {
					this.page++;
					this.loadChannelList();
				}
			},
			
			// 新增渠道
			addChannel() {
				uni.navigateTo({
					url: '/pages/channel/add'
				});
			},
			
			// 编辑渠道
			editChannel(item) {
				uni.navigateTo({
					url: `/pages/channel/add?id=${item.id || item.lId}`
				});
			},
			
			// 格式化时间
			formatTime(time) {
				if (!time) return '';
				const date = new Date(time);
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');
				const hours = String(date.getHours()).padStart(2, '0');
				const minutes = String(date.getMinutes()).padStart(2, '0');
				return `${year}-${month}-${day} ${hours}:${minutes}`;
			}
		}
	}
</script>

<style lang="scss" scoped>
.page-container {
	min-height: 100vh;
	background-color: #f5f5f5;
	padding-bottom: 100rpx;
}

.list-container {
	padding: 20rpx;
}

.list-item {
	margin-bottom: 20rpx;
	background-color: #fff;
	border-radius: 16rpx;
	overflow: hidden;
	box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.item-content {
	padding: 30rpx;
}

.item-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 20rpx;
}

.item-title {
	font-size: 32rpx;
	font-weight: 600;
	color: #333;
	flex: 1;
}

.item-status {
	font-size: 24rpx;
	color: #999;
	padding: 8rpx 16rpx;
	background-color: #f5f5f5;
	border-radius: 8rpx;
}

.status-active {
	color: #22c55e;
	background-color: #f0fdf4;
}

.item-info {
	margin-bottom: 20rpx;
}

.info-row {
	display: flex;
	align-items: center;
	margin-bottom: 12rpx;
	
	&:last-child {
		margin-bottom: 0;
	}
}

.info-text {
	font-size: 28rpx;
	color: #666;
	margin-left: 12rpx;
	flex: 1;
}

.item-footer {
	display: flex;
	justify-content: flex-end;
	padding-top: 20rpx;
	border-top: 1rpx solid #f0f0f0;
}

.item-time {
	font-size: 24rpx;
	color: #999;
}

/* 底部悬浮按钮 */
.fab-container {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	padding: 20rpx;
	background-color: #fff;
	border-top: 1rpx solid #f0f0f0;
	/* #ifdef H5 */
	box-shadow: 0 -2rpx 8rpx rgba(0, 0, 0, 0.05);
	/* #endif */
	z-index: 100;
}

.fab-button {
	width: 100%;
	height: 88rpx;
	background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
	border: none;
	border-radius: 12rpx;
	color: #fff;
	font-size: 32rpx;
	font-weight: 600;
	display: flex;
	align-items: center;
	justify-content: center;
	box-shadow: 0 8rpx 24rpx rgba(99, 102, 241, 0.3);
	
	/* #ifdef H5 */
	cursor: pointer;
	transition: all 0.3s ease;
	
	&:hover {
		transform: translateY(-2rpx);
		box-shadow: 0 12rpx 32rpx rgba(99, 102, 241, 0.4);
	}
	
	&:active {
		transform: translateY(0);
	}
	/* #endif */
}

.fab-text {
	margin-left: 12rpx;
}

/* H5适配 */
/* #ifdef H5 */
.page-container {
	padding-bottom: 120rpx;
}

.fab-container {
	padding: 30rpx 40rpx;
}
/* #endif */

/* 小程序适配 */
/* #ifdef MP-WEIXIN */
.fab-container {
	padding-bottom: env(safe-area-inset-bottom);
}
/* #endif */
</style>

