<template>
	<div>
		<el-button type="primary" round @click="openAddDialog" size="small">添加</el-button>
		<el-row>
			<el-col :span="24">
				<el-form :inline="true" :model="state.query" class="demo-form-inline">
					<el-form-item label="账号：">
						<el-input v-model="state.query.username" placeholder="请输入账号" clearable @input="init" />
					</el-form-item>
				</el-form>
			</el-col>
		</el-row>
		<!--表格-->
<!--		9ebf7eb9f32c21ab2858173c447f2884d9a4b243235ae92b86412e6f6253fe44-->
		<el-row>
			<el-col :span="24">
				<el-table :data="state.getList">
					<!--					列表结开始-->
					<el-table-column prop="id" label="患者编号"/>
					<el-table-column prop="username" label="联系电话"/>
					<el-table-column prop="nickName" label="姓名"/>
					<el-table-column prop="role" label="性别">
						<template #default="{ row }">
							{{ row.role === '0' ? '女' : row.role === '1' ? '男' : '其他' }}
						</template>
					</el-table-column>
					<el-table-column prop="introduce" label="介绍"/>
					<el-table-column prop="createDate" label="创建时间"/>
					<!--列表结束-->
					<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>
				<!-- 分页控件 -->
				<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"
				/>
			</el-col>
		</el-row>

		<!-- 新增/编辑对话框 -->
		<el-dialog v-model="state.dialogVisible" title="新增" width="30%">
			<el-form
				:model="state.formData"
				ref="formRef"
				label-width="100px"
			>
				<!--				表单开始===============================================================================================================================-->
				<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="username" :rules="[{ required: true, message: '请输入联系电话', trigger: 'blur' }]">
					<el-input v-model.number="state.formData.username"/>
				</el-form-item>
				<el-form-item label="性别" prop="role" :rules="[{ required: true, message: '请输入性别', trigger: 'blur' }]">
					<el-select v-model="state.formData.role" placeholder="请选择性别">
						<el-option label="男" value=1></el-option>
						<el-option label="女" value=0></el-option>
						<el-option label="其他" value=2></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="密码" prop="password"  :rules="[{ required: state.formData.isAdd, message: '请输入密码', trigger: 'blur' }]">
					<el-input v-model="state.formData.password" clearable/>
				</el-form-item>
				<el-form-item label="介绍" >
					<el-input v-model="state.formData.introduce"  :rows="4"
										type="textarea"/>
				</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: 10, // 每页显示的记录数
		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 = { isAdd: true } // 添加操作
	state.dialogVisible = true
}
// 编辑交易记录
const edit = (row: any) => {
	state.formData = { ...row, isAdd: false } // 编辑操作
	delete  state.formData.password
	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)
			} else {
				// 新增交易记录
				adminRequest.post(`${state.route}`, state.formData)
			}
			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>
.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);
}
</style>
<route lang="json">
{
"meta": {
"layout": "admin"
}
}
</route>
