<template>
	<view class="report-schedule">
		<!-- header始终显示 -->
		<view class="search-bar">
			<view class="search-container">
				<view class="task-type-button-container">
					<view class="task-type-button" @click="openDrawer">
						<uni-icons type="settings" color="#2979ff" size="22"></uni-icons>
					</view>
				</view>
				<view class="search-input custom-search-input">
					<input v-model="queryParams.userName" class="input" type="text" placeholder="搜索用户名称" @confirm="onSearch"
						@keyup.enter="onSearch" />
					<view class="search-icon" @click="onSearch">
						<uni-icons type="search" color="#bbb" size="20"></uni-icons>
					</view>
					<view v-if="searchName" class="clear-icon" @click="onClearSearch">
						<uni-icons type="closeempty" color="#bbb" size="18"></uni-icons>
					</view>
				</view>
				<view class="add-button" @click="handleAdd">
					<uni-icons type="plusempty" color="#2979ff" size="22"></uni-icons>
				</view>
			</view>
		</view>

		<!-- 筛选抽屉 -->
		<uni-drawer ref="drawerRef" mode="left" :width="320">
			<view class="drawer-content">
				<view class="drawer-title">筛选</view>
				<view class="drawer-form">
					<view class="drawer-form-item">
						<text class="drawer-label">手机号</text>
						<uni-easyinput  v-model="queryParams.phonenumber" placeholder="请输入手机号" />
					</view>
					<view class="drawer-form-item">
						<text class="drawer-label">状态</text>
						<oa-dict-select v-model="queryParams.status" dictType="sys_normal_disable"/>
					</view>
				</view>
				<view class="drawer-btns">
					<button class="drawer-btn-primary" @click="applyFilterAndClose">确定</button>
					<button class="drawer-btn" @click="resetFilterAndClose">重置</button>
					<button class="drawer-btn" @click="closeDrawer">关闭</button>
				</view>
			</view>
		</uni-drawer>

		<view>
			<!-- 自定义列表，右滑菜单（uni-ui实现） -->
			<scroll-view scroll-y style="height: 100vh;" @scrolltolower="loadMore">
				<view v-if="reportScheduleList.length">
					<uni-swipe-action>
						<block v-for="(item, index) in reportScheduleList" :key="item.userId">
							<uni-swipe-action-item :right-options="getSwipeOptions(item)" @click="swipeActionClick($event, item)"
								style="margin-bottom: 16rpx;">
								<view class="card">
									<view class="card-title">
										<text class="project">{{ item.nickName }}</text>
									</view>
									<view class="card-content">
										<view>用户名称：{{ item.userName }}</view>
										<view>部门：{{ item.dept.deptName }}</view>
										<view>手机号码：{{ item.phonenumber }}</view>
										<view>注册时间：{{ item.createTime }}</view>
									</view>
								</view>
							</uni-swipe-action-item>
						</block>
					</uni-swipe-action>
				</view>
				<view v-else class="empty">暂无数据</view>
				<view class="load-more-tips">
					<u-loading-icon v-if="loadingMore" text="加载中..." size="20" textSize="14" />
					<text v-else-if="!hasMore && reportScheduleList.length">没有更多了</text>
				</view>
			</scroll-view>

			<!-- 新增/编辑弹窗 -->
			<uni-popup ref="popupRef" type="bottom">
				<view class="popup-content">
					<view class="uni-form">
						<view class="uni-form-item">
							<text class="uni-form-label">用户昵称</text>
							<uni-easyinput v-model="form.nickName" placeholder="请输入用户昵称" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">手机号码</text>
							<uni-easyinput type='phone' :disabled="form.userId" v-model="form.phonenumber" placeholder="请输入用户手机号" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">用户邮箱</text>
							<uni-easyinput type="email" v-model="form.email" placeholder="请输入用户邮箱" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">用户名称</text>
							<uni-easyinput  v-model="form.userName" placeholder="请输入用户名称" />
						</view>
						<view class="uni-form-item" v-if="form.userId">
							<text class="uni-form-label">用户密码</text>
							<uni-easyinput type="password" v-model="form.password" placeholder="请输入用户密码" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">用户性别</text>
							<oa-dict-select v-model="form.sex" dictType="sys_user_sex"></oa-dict-select>
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">薪资</text>
							<uni-easyinput  v-model="form.laborCost" placeholder="请输入薪资（工人为日薪）" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">保险金</text>
							<uni-easyinput  v-model="form.insure" placeholder="请输入保险金" />
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">岗位</text>
							<uni-data-select v-model="form.postIds" multiple :localdata="roleOptions"></uni-data-select>
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">角色</text>
							<uni-data-select v-model="form.roleIds" multiple :localdata="roleOptions"></uni-data-select>
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">部门</text>
							<uni-data-picker :localdata="deptOptions" v-model="form.deptId"></uni-data-picker>
						</view>
						<view class="uni-form-item">
							<text class="uni-form-label">备注</text>
							<u-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
						</view>
					</view>
					<view class="popup-btns">
						<u-button type="primary" @click="submitForm">确定</u-button>
						<u-button @click="closePopup">取消</u-button>
					</view>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import {
		addUser,
		changeUserStatus,
		delUser,
		deptTreeSelect,
		getUser,
		listUser,
		resetUserPwd,
		updateUser
	} from "@/api/oa/user";

	export default {
		data() {
			return {
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					userName: undefined,
					phonenumber: undefined,
					status: undefined,
					deptId: undefined
				},
				projectOptions: [],
				headerOptions: [],
				reportScheduleList: [],
				total: 0,
				single: true,
				multiple: true,
				form: {},
				viewType: 'table',
				showStartDate: false,
				showEndDate: false,
				swipeOptions: [{
						text: '编辑',
						style: {
							backgroundColor: '#2979ff',
							color: '#fff'
						}
					},
					{
						text: '删除',
						style: {
							backgroundColor: '#fa3534',
							color: '#fff'
						}
					},
				],
				// 新增：筛选相关
				searchName: '',
				filterProject: '',
				filterHeader: '',
				filterDateRange: [],
				pageNum: 1,
				pageSize: 10,
				loadingMore: false,
				hasMore: true,
				startForm: {
					reportTitle: '',
					reportDate: '',
					reporter: '',
					projectId: '',
					remark: '',
					type: 1
				},
				startLoading: false,
				startPopupOpen: false,
				_startuserId: null,
				showGanttView: false,
				postOptions: [],
				roleOptions: [],
				deptOptions: [],
			}
		},
		onLoad() {
			this.pageNum = 1
			this.hasMore = true
			this.handleQuery()
			deptTreeSelect().then(res => {
				console.log(res.data, '部门数据')
				// 递归转换函数（value直接使用原id）
				function transformStructure(source) {
				    // 处理单个节点的转换
				    function transformNode(node) {
				        // 基础转换：text映射label，value直接使用原id
				        const transformed = {
				            text: node.label,
				            value: node.id  // 直接使用原始id作为value
				        };
				        
				        // 递归处理子节点（如果存在）
				        if (node.children && node.children.length > 0) {
				            transformed.children = node.children.map(child => transformNode(child));
				        }
				        
				        return transformed;
				    }
				    
				    // 处理整个数组
				    return source.map(item => transformNode(item));
				}
				this.deptOptions = transformStructure(res.data)
			})
		},
		methods: {
			openDrawer() {
				this.$refs.drawerRef.open();
			},
			closeDrawer() {
				this.$refs.drawerRef.close();
			},
			applyFilterAndClose() {
				this.applyFilter();
				this.closeDrawer();
			},
			resetFilterAndClose() {
				this.resetFilter();
				this.closeDrawer();
			},
			// 搜索框检索
			onSearch() {
				this.searchName = this.searchName
				this.handleQuery()
			},
			onClearSearch() {
				this.searchName = ''
				this.queryParams.nickName = ''
				this.handleQuery()
			},
			// 筛选抽屉应用
			applyFilter() {
				this.queryParams.projectId = this.filterProject
				this.queryParams.header = this.filterHeader
				if (this.filterDateRange && this.filterDateRange.length === 2) {
					this.queryParams.dateRange = this.filterDateRange
				} else {
					this.queryParams.dateRange = []
				}
				this.handleQuery()
			},
			// 筛选抽屉重置
			resetFilter() {
				this.queryParams.phonenumber = '';
				this.queryParams.status = null;
			},
			handleQuery() {
				this.pageNum = 1
				this.hasMore = true
				this.queryParams.pageNum = 1
				if (this.queryParams.dateRange && this.queryParams.dateRange.length === 2) {
					this.queryParams.startDate = this.queryParams.dateRange[0]
					this.queryParams.endDate = this.queryParams.dateRange[1]
				} else {
					this.queryParams.startDate = ''
					this.queryParams.endDate = ''
				}
				this.getList()
			},
			resetQuery() {
				this.queryParams = {
					pageNum: 1,
					pageSize: 10,
					projectId: '',
					nickName: '',
					header: '',
					dateRange: []
				}
				this.searchName = ''
				this.pageNum = 1
				this.hasMore = true
				this.getList()
			},
			getList() {
				this.loadingMore = true
				this.queryParams.pageNum = this.pageNum
				this.queryParams.pageSize = this.pageSize
				listUser(this.queryParams).then(res => {
					const rows = res.rows || []
					if (this.pageNum === 1) {
						this.reportScheduleList = rows
					} else {
						this.reportScheduleList = this.reportScheduleList.concat(rows)
					}
					// 判断是否还有更多
					this.hasMore = rows.length === this.pageSize
					this.total = res.total || 0
				}).finally(() => {
					this.loadingMore = false
				})
			},
			loadMore() {
				if (!this.hasMore || this.loadingMore) return
				this.pageNum++
				this.getList()
			},
			handleAdd() {
				getUser().then((res) => {
					this.postOptions = res.data.posts.map(item => ({
						text: item.postName,
						value: item.postId
					}));
					this.roleOptions = res.data.roles.map(item => ({
						text: item.roleName,
						value: item.roleId
					}));
					this.form = {}
					this.$refs.popupRef.open('bottom')
				})
				
			},
			handleUpdate(row) {
				getUser(row.userId).then(res => {
					this.postOptions = res.data.posts.map(item => ({
						text: item.postName,
						value: item.postId
					}));
					this.roleOptions = res.data.roles.map(item => ({
						text: item.roleName,
						value: item.roleId
					}));
					this.form = res.data.user || {}
					this.$set(this.form, "postIds", res.data.postIds);
					this.$set(this.form, "roleIds", res.data.roleIds);
					this.$refs.popupRef.open('bottom')
				})
			},
			handleDelete(item) {
				uni.showModal({
					title: '确认删除',
					content: `确定要删除用户“${item.nickName}”吗？`,
					success: (res) => {
						if (res.confirm) {
							const ids = item ? [item.userId] : this.selectedIds
							delUser(ids).then(() => {
								uni.showToast({
									title: '删除成功',
									icon: 'success'
								})
								this.getList()
							})
						}
					}
				})
			},
			handleExport() {
				// 导出逻辑可根据实际API实现
				uni.showToast({
					title: '导出功能开发中',
					icon: 'none'
				})
			},
			submitForm() {
				if (this.form.userId) {
					updateUser(this.form).then(() => {
						uni.showToast({
							title: '修改成功',
							icon: 'success'
						})
						this.closePopup()
						this.getList()
					})
				} else {
					addUser(this.form).then(() => {
						uni.showToast({
							title: '新增成功',
							icon: 'success'
						})
						this.closePopup()
						this.getList()
					})
				}
			},
			closePopup() {
				this.$refs.popupRef.close()
			},
			getSwipeOptions(item) {
				const options = [{
						text: '编辑',
						style: {
							backgroundColor: '#2979ff',
							color: '#fff'
						}
					},
					{
						text: '删除',
						style: {
							backgroundColor: '#fa3534',
							color: '#fff'
						}
					}
				];
				return options;
			},
			swipeActionClick(e, item) {
				const text = e.content.text;
				if (text === '编辑') {
					this.handleUpdate(item);
				} else if (text === '删除') {
					this.handleDelete(item);
				}
			},
		}
	}
</script>

<style scoped>
	.report-schedule {
		min-height: 100vh;
		background-color: #f5f5f5;
		padding-bottom: 120rpx;
	}

	.search-bar {
		padding: 20rpx;
		position: sticky;
		top: 0;
		z-index: 100;
		background: #fff;
	}

	.search-container {
		display: flex;
		align-items: center;
		gap: 20rpx;
	}

	.task-type-button-container {
		display: flex;
		gap: 12rpx;
	}

	.task-type-button {
		width: 60rpx;
		height: 60rpx;
		background-color: transparent;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.search-input {
		flex: 1;
		position: relative;
		display: flex;
		align-items: center;
		background: #f5f5f5;
		border-radius: 100rpx;
		padding: 0 24rpx;
		height: 60rpx;
	}

	.input {
		flex: 1;
		border: none;
		background: transparent;
		font-size: 30rpx;
		outline: none;
		height: 60rpx;
		line-height: 60rpx;
	}

	.search-icon {
		margin-left: 8rpx;
		display: flex;
		align-items: center;
	}

	.clear-icon {
		margin-left: 8rpx;
		display: flex;
		align-items: center;
	}

	.drawer-content {
		padding: 32rpx 24rpx 24rpx 24rpx;
	}

	.drawer-title {
		font-size: 32rpx;
		font-weight: bold;
		margin-bottom: 24rpx;
	}

	.drawer-form {
		margin-bottom: 32rpx;
	}

	.drawer-form-item {
		margin-bottom: 24rpx;
	}

	.drawer-label {
		display: block;
		font-size: 28rpx;
		color: #333;
		margin-bottom: 8rpx;
	}

	.drawer-btns {
		display: flex;
		gap: 16rpx;
		margin-top: 24rpx;
	}

	.drawer-btn-primary {
		flex: 1;
		background: #2979ff;
		color: #fff;
		border: none;
		border-radius: 8rpx;
		padding: 16rpx 0;
		font-size: 28rpx;
	}

	.drawer-btn {
		flex: 1;
		background: #f5f5f5;
		color: #333;
		border: none;
		border-radius: 8rpx;
		padding: 16rpx 0;
		font-size: 28rpx;
	}

	.card {
		background: #fff;
		border-radius: 16rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
		padding: 24rpx;
	}

	.card-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-weight: bold;
		margin-bottom: 12rpx;
	}

	.project {
		color: #2979ff;
	}

	.status {
		font-size: 24rpx;
		padding: 0 12rpx;
		border-radius: 8rpx;
	}

	.status-1 {
		background: #e0f7fa;
		color: #009688;
	}

	.status-0,
	.status-undefined {
		background: #fffbe6;
		color: #faad14;
	}

	.status-other {
		background: #fbeff2;
		color: #e91e63;
	}

	.card-content view {
		margin-bottom: 8rpx;
		color: #666;
		font-size: 26rpx;
	}

	.empty {
		text-align: center;
		color: #bbb;
		margin: 40rpx 0;
	}

	.popup-content {
		padding: 24rpx;
		background: #fff;
		max-height: 80vh;
		overflow-y: scroll;
		border-radius: 16rpx 16rpx 0 0;
	}

	.uni-form {
		margin-bottom: 32rpx;
	}

	.uni-form-item {
		margin-bottom: 32rpx;
	}

	.uni-form-label {
		display: block;
		font-size: 28rpx;
		color: #333;
		margin-bottom: 8rpx;
	}

	.load-more-tips {
		text-align: center;
		color: #bbb;
		padding: 24rpx 0 32rpx 0;
		font-size: 28rpx;
	}

	.card-ops {
		margin-top: 16rpx;
		display: flex;
		gap: 16rpx;
	}

	.gantt-toggle-bar {
		display: flex;
		gap: 16rpx;
		padding: 16rpx 0 8rpx 0;
		background: #fff;
		justify-content: flex-end;
	}
</style>