<template>
	<div class="container">
		<!-- 头部操作区域 -->
		<div class="header">
			<el-button type="primary" round @click="openAddDialog" size="small">添加用户</el-button>
			<el-form :inline="true" :model="state.query" class="search-form">
				<el-form-item label="账号：" class="search-item">
					<el-input
						v-model="state.query.username"
						placeholder="请输入账号"
						clearable
						@input="init"
						class="search-input"
					/>
				</el-form-item>
			</el-form>
		</div>


		<!--表格-->
		<el-row>
			<el-col :span="24">
				<div class="table-container">
					<el-table :data="state.getList">
						<!--					列表结开始-->
						<el-table-column prop="nickName" label="姓名" align="center"/>
						<el-table-column prop="username" label="账号" align="center"/>
<!--						<el-table-column prop="introduce" label="介绍" align="center"/>-->
						<el-table-column prop="createDate" label="创建时间" align="center"/>
						<!--列表结束-->
						<el-table-column label="操作" align="center" >
							<template #default="scope">
								<el-button @click="edit(scope.row)" size="small">编辑</el-button>
								<el-button @click="del(scope.row.id)" type="danger" size="small">删除</el-button>
							</template>
						</el-table-column>
						<template v-slot:empty>
							<el-empty description="数据去外太空了~" />
						</template>
					</el-table>
					<!-- 分页控件 -->
					<div class="pagination-container">
						<el-pagination
							v-if="state.query.total > 0"
							:current-page="state.query.page"
							:page-size="state.query.limit"
							:total="state.query.total"
							background
							layout="prev, pager, next"
							@current-change="handlePageChange"
						/>
					</div>
				</div>

			</el-col>
		</el-row>
		<!-- 新增/编辑对话框 -->
		<el-dialog v-model="state.dialogVisible" title="新增" width="50%">
			<el-form
				:model="state.formData"
				ref="formRef"
				label-width="100px"
			>

				<!--				表单开始===============================================================================================================================-->

				<el-form-item label="账号" prop="username" :rules="[{ required: true, message: '请输入账号', trigger: 'blur' }]">
					<el-input v-model="state.formData.username"/>
				</el-form-item>
				<el-form-item label="密码" >
					<el-input v-model="state.formData.password"/>
				</el-form-item>
				<el-form-item label="姓名" prop="nickName" :rules="[{ required: true, message: '请输入昵称', trigger: 'blur' }]">
					<el-input v-model="state.formData.nickName"/>
				</el-form-item>
<!--				<el-form-item label="介绍" prop="introduce" :rules="[{ required: true, message: '请输入介绍', trigger: 'blur' }]">-->
<!--					<el-input v-model="state.formData.introduce"/>-->
<!--				</el-form-item>-->
				<!--表单结束===============================================================================================================================-->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="state.dialogVisible = false">取消</el-button>
				<el-button type="primary" @click="saveTransaction(formRef)">保存</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script setup lang="ts">
import type { FormInstance } from 'element-plus'
const formRef = ref<FormInstance>()
const state = reactive(<any>{
	route:"sys/user-front",
	dialogVisible:false,
	getList: [],//获取数据
	query:{
		total: 0, // 总记录数
		page: 1, // 当前页码
		limit: 8, // 每页显示的记录数
		username: "",
	},
	formData:{}
})
// 获取交易记录
const init = () => {
	adminRequest.get(`${state.route}/page`, {
		params: state.query
	}).then((res:any) => {
		state.getList = res.data.list
		state.query.total = res.data.total
	})
}
// 新增交易记录
const openAddDialog = () => {
	state.formData = {}
	state.dialogVisible = true
}
// 编辑交易记录
const edit = (row: any) => {
	// 避免直接修改原对象
	state.formData = { ...row }
	state.formData.password = '' // 清空密码字段
	state.isEdit = true
	state.dialogVisible = true
}
// 保存记录
const saveTransaction = (formEl: FormInstance | undefined) => {
	if (!formEl) return
	formEl.validate((valid) => {
		if (valid) {
			if (state.formData.id) {
				// 更新交易记录
				adminRequest.put(`${state.route}`, state.formData).then(()=>{
					init()
					state.dialogVisible = false
					ElMessage.success("提交成功~")
				})
			} else {
				// 新增交易记录
				adminRequest.post(`${state.route}`, state.formData).then(()=>{
					init()
					state.dialogVisible = false
					ElMessage.success("提交成功~")
				})
			}
		}
	})
}

// 删除记录
const del = async (id: number) => {
	try {
		await adminRequest.delete(`${state.route}/${id}`)
		init()
		ElMessage.success("删除成功~")
	} catch (error) {
		ElMessage.error("删除失败~")
	}
}

// 页码改变时的处理函数
const handlePageChange = (page: number) => {
	state.query.page = page
	init()
}
// 页面加载时获取交易记录
onMounted(() => {
	init()
})
</script>
<style scoped>

.container {
	padding: 20px;
	background-color: #f5f7fa;
	min-height: calc(100vh - 60px);
}

.header {
	display: flex;
	align-items: center;
	margin-bottom: 20px;
	padding: 15px;
	background: white;
	border-radius: 8px;
	box-shadow: 0 2px 12px rgba(0,0,0,0.04);
}

.search-form {
	margin-left: auto;
}

.search-item {
	margin-bottom: 0;
}

.search-input {
	width: 240px;
}

.table-container {
	background: white;
	padding: 15px;
	border-radius: 8px;
	box-shadow: 0 2px 12px rgba(0,0,0,0.04);
}

.pagination-container {
	margin-top: 20px;
	display: flex;
	justify-content: center;
}

/* 表格样式 */
:deep(.table-header th) {
	background-color: #f8f9fa !important;
	color: #606266;
	font-weight: 600;
}

:deep(.el-table__row.table-row:hover) {
	background-color: #f5f7fa !important;
}

:deep(.el-table .cell) {
	padding: 12px 0;
}

/* 按钮间距调整 */
.el-button {
	margin: 2px;
}

/* 对话框样式调整 */
:deep(.el-dialog) {
	border-radius: 8px;
}

:deep(.el-dialog__header) {
	border-bottom: 1px solid #ebeef5;
}

:deep(.el-form-item__label) {
	color: #606266;
}

/* 响应式调整 */
@media (max-width: 768px) {
	.header {
		flex-direction: column;
		align-items: flex-start;
	}

	.search-form {
		margin-left: 0;
		margin-top: 15px;
		width: 100%;
	}

	.search-input {
		width: 100%;
	}
}
.dialog-footer {
	text-align: right;
}
:deep(.el-upload) {
	border: 1px dashed var(--el-border-color);
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
	transition: var(--el-transition-duration-fast);
}

:deep(.el-upload:hover) {
	border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	text-align: center;
}
.avatar{
	width: 178px;
	height: 178px;
	object-fit: cover;
}

</style>
