<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
			<el-form-item label="组别名称" prop="groupName">
				<el-input v-model="queryParams.groupName" placeholder="请输入组别名称" clearable style="width: 240px"
					@keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
				<el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
				<el-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
			</el-form-item>
		</el-form>
		<el-card shadow="never">
			<el-table ref="tableRef" v-loading="loading" :data="groupList">
				<el-table-column label="序号" align="center" type="index" width="50" />
				<el-table-column label="组别名称" align="center" prop="groupName" :show-overflow-tooltip="true" />
				<el-table-column label="描述" align="center" prop="remarks" :show-overflow-tooltip="true" />
				<el-table-column label="创建时间" align="center" prop="createTime">
					<template #default="scope">
						<span>{{ parseTime(scope.row.createTime) }}</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width"
					fixed="right" v-hasRole="['admin', 'root']">
					<template #default="scope">
						<template
							v-if="scope.row.userId !== 1 && !(scope.row.userId === 17 && $store.state.user.user.admin == false)">
							<el-tooltip content="修改" placement="top">
								<el-button link type="success" icon="Edit" @click="handleUpdate(scope.row)"
									v-hasPermi="['system:group:edit']">
								</el-button>
							</el-tooltip>
							<el-tooltip content="删除" placement="top">
								<el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"
									v-hasPermi="['system:group:remove']">
								</el-button>
							</el-tooltip>
							<!-- <el-tooltip content="人员管理" placement="top">
								<el-button link type="primary" icon="CircleCheck" @click="handleCircleCheck(scope.row)"
									v-hasPermi="['system:group:circlecheck']"></el-button>
							</el-tooltip> -->
						</template>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
			v-model:limit="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改用户配置对话框 -->
		<el-dialog :title="title" v-model="open" width="600px" append-to-body>
			<el-form :model="form" :rules="rules" ref="groupRef" label-width="80px">
				<el-form-item label="组别名称" prop="groupName">
					<el-input v-model="form.groupName" placeholder="请输入组别名称" />
				</el-form-item>
				<el-form-item label="描述" prop="remarks">
					<el-input type="textarea" v-model="form.remarks" placeholder="请输入描述" />
				</el-form-item>
				<el-form-item label="人员选择">
					<el-select v-model="form.userIds" clearable multiple placeholder="请选择" style="width: 100%;">
						<template v-for="item in userList" :key="item.userId">
							<el-option :label="item.nickName" :value="item.userId" 
								v-if="item.status == 1 || !item.admin">
								<span style="float: left">{{ item.nickName }}</span>
								<span style="float: right; color: #8492a6; font-size: 13px">{{ item.phonenumber }}</span>
							</el-option>
						</template>
					</el-select>
				</el-form-item>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="Group">
	import { getToken } from "@/utils/auth";
	import {
		listGroup,
		delGroup,
		getGroup,
		updateGroup,
		addGroup,
	} from "@/api/system/group";
	import { listUser } from "@/api/system/user";
	const router = useRouter();
	const { proxy } = getCurrentInstance();

	const groupList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const total = ref(0);
	const title = ref("");
	const dateRange = ref([]);
	const userOptions = ref([]);
	const userList = ref([]);
	
	const data = reactive({
		form: {},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			groupName: undefined
		},
		rules: {
			groupName: [{
				required: true,
				message: "组别名字不能为空",
				trigger: "blur",
			}]
		},
	});

	const { queryParams, form, rules } = toRefs(data);
	/** 查询用户列表 */
	function getList() {
		loading.value = true;
		let res = {
			"total": 9,
			"rows": [{"createTime":"2023-10-23 09:15:20","groupId":1,"remarks":"","groupName":"硬件组"},
					 {"createTime":"2023-10-23 09:15:32","groupId":2,"remarks":"","groupName":"嵌入式工程组"},
					 {"createTime":"2023-10-23 09:15:40","groupId":3,"remarks":"","groupName":"软件组"},
					 {"createTime":"2023-10-23 09:15:50","groupId":4,"remarks":"","groupName":"产品组"},
					 {"createTime":"2023-10-23 09:16:00","groupId":5,"remarks":"","groupName":"江苏组"},
					 {"createTime":"2023-10-23 17:17:08","groupId":6,"remarks":"商务人员","groupName":"商务组"},
					 {"createTime":"2023-11-03 13:50:08","groupId":7,"remarks":"财务人员","groupName":"财务组"},
					 {"createTime":"2023-12-06 17:30:41","groupId":8,"remarks":"","groupName":"采购组"},
					 {"createTime":"2023-12-11 15:21:59","groupId":9,"remarks":"","groupName":"安装交付组"}],
			"code": 200 
		}
		// listGroup(queryParams.value).then((res) => {
			loading.value = false;
			groupList.value = res.rows;
			total.value = res.total;
		// });
	}
	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}
	/** 重置按钮操作 */
	function resetQuery() {
		proxy.resetForm("queryRef");
		queryParams.value.pageNum = 1;
		// handleQuery();
	}
	/** 删除按钮操作 */
	function handleDelete(row) {
		const groupIds = row.groupId;
		proxy.$modal.confirm('是否确认删除用户编号为"' + groupIds + '"的数据项？')
		.then(function() {
			return delGroup(groupIds);
		})
		.then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		})
		.catch(() => {});
	}
	/** 导出按钮操作 */
	function handleExport() {
		proxy.download("system/group/export", { ...queryParams.value }, `group_${new Date().getTime()}.xlsx`);
	}
	/** 重置密码按钮操作 */
	function handleCircleCheck(row) {
		
	}
	/** 重置操作表单 */
	function reset() {
		form.value = {
			groupId: undefined,
			groupName: undefined,
			remarks: undefined,
			userIds: []
		};
		proxy.resetForm("groupRef");
	}
	/** 取消按钮 */
	function cancel() {
		open.value = false;
		reset();
	}
	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		getGroup().then((response) => {
			userOptions.value = response.users;
			open.value = true;
			title.value = "添加组别";
		});
	}
	/** 修改按钮操作 */
	function handleUpdate(row) {
		reset();
		const userId = row.userId;
		// getGroup(userId).then((response) => {
			// form.value = response.data;
			// userOptions.value = response.users;
			// form.value.userIds = response.userIds;
			form.value = row;
			userOptions.value = userList.value;
			form.value.userIds = [19, 22];
			open.value = true;
			title.value = "修改组别";
		// });
	}
	/** 提交按钮 */
	function submitForm() {
		proxy.$refs["groupRef"].validate((valid) => {
			if (valid) {
				if (form.value.groupId != undefined) {
					updateGroup(form.value).then((response) => {
						proxy.$modal.msgSuccess("修改成功");
						open.value = false;
						getList();
					});
				} else {
					addGroup(form.value).then((response) => {
						proxy.$modal.msgSuccess("新增成功");
						open.value = false;
						getList();
					});
				}
			}
		});
	}
	listUser(
	  proxy.addDateRange(
	    {
	      pageNum: 1,
	      pageSize: 9999,
	    },
	    undefined
	  )
	).then((response) => {
		userList.value = response.rows;
	});
	getList();
</script>