<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
			<el-form-item label="仓库名称" prop="repositoryName">
				<el-input v-model="queryParams.repositoryName" 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="wareHouseList">
				<el-table-column label="序号" align="center" type="index" width="50" />
				<el-table-column label="仓库编号" align="center" prop="repositoryId" />
				<el-table-column label="仓库名称" align="center" prop="repositoryName" :show-overflow-tooltip="true" />
				<el-table-column label="仓库位置" align="center" prop="repositoryPath" :show-overflow-tooltip="true" />
				<el-table-column label="部门" align="center" key="deptId" prop="deptId">
					<template #default="scope">
						<TextTag :options="deptList" column="deptId" label="deptName" :value="scope.row.deptId" :text="true"/>
					</template>
				</el-table-column>
				<el-table-column label="仓库简介" align="center" prop="remark" :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="Key" @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="repositoryRef" label-width="80px">
				<el-form-item label="仓库名称" prop="repositoryName">
					<el-input v-model="form.repositoryName" placeholder="请输入仓库名称" />
				</el-form-item>
				<el-form-item label="仓库位置" prop="repositoryPath">
					<el-input v-model="form.repositoryPath" :disabled="form.repositoryId!= undefined" placeholder="请输入仓库位置" />
				</el-form-item>
				<el-form-item label="仓库简介" prop="remark">
					<el-input type="textarea" v-model="form.remark" placeholder="请输入仓库简介" />
				</el-form-item>
				<el-form-item label="归属部门" prop="deptId">
					<el-tree-select v-model="form.deptId" :data="deptOptions"
						:props="{ value: 'deptId', label: 'deptName', children: 'children' }" value-key="deptId"
						placeholder="请选择归属部门" check-strictly />
				</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="repository">
	import { getToken } from "@/utils/auth";
	import {
		listWareHouse,
		delWareHouse,
		getWareHouse,
		updateWareHouse,
		addWareHouse,
	} from "@/api/wareHouse";
	import { listDept, treeselect	} from "@/api/system/dept";
	const router = useRouter();
	const { proxy } = getCurrentInstance();
	const wareHouseList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const total = ref(0);
	const title = ref("");
	const dateRange = ref([]);
	const deptOptions = ref(undefined);
	const deptList = ref([]);
	const userList = ref([]);
	
	const data = reactive({
		form: {},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			repositoryName: undefined
		},
		rules: {
			repositoryName: [{
				required: true,
				message: "仓库名字不能为空",
				trigger: "blur",
			}],
			repositoryPath: [{
				required: true,
				message: "仓库位置不能为空",
				trigger: "blur",
			}]
		},
	});

	const { queryParams, form, rules } = toRefs(data);
	/** 查询用户列表 */
	function getList() {
		loading.value = true;
		listWareHouse(queryParams.value).then((res) => {
			loading.value = false;
			wareHouseList.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 repositoryIds = row.repositoryId;
		proxy.$modal.confirm('是否确认删除仓库编号为"' + repositoryIds + '"的数据项？')
		.then(function() {
			return delWareHouse(repositoryIds);
		})
		.then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		})
		.catch(() => {});
	}
	/** 导出按钮操作 */
	function handleExport() {
		proxy.download("system/repository/export", { ...queryParams.value }, `repository_${new Date().getTime()}.xlsx`);
	}
	/** 重置密码按钮操作 */
	function handleCircleCheck(row) {
		
	}
	/** 重置操作表单 */
	function reset() {
		form.value = {
			repositoryId: undefined,
			repositoryName: undefined,
			repositoryPath: undefined,
			remark: undefined,
			deptId: undefined
		};
		proxy.resetForm("repositoryRef");
	}
	/** 取消按钮 */
	function cancel() {
		open.value = false;
		reset();
	}
	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		open.value = true;
		title.value = "添加仓库";
	}
	/** 修改按钮操作 */
	function handleUpdate(row) {
		reset();
		const wareHouseId = row.repositoryId;
		getWareHouse(wareHouseId).then((response) => {
			form.value = response.data;
			open.value = true;
			title.value = "修改仓库";
		});
	}
	/** 提交按钮 */
	function submitForm() {
		proxy.$refs["repositoryRef"].validate((valid) => {
			if (valid) {
				if (form.value.repositoryId != undefined) {
					updateWareHouse(form.value).then((response) => {
						proxy.$modal.msgSuccess("修改成功");
						open.value = false;
						getList();
					});
				} else {
					addWareHouse(form.value).then((response) => {
						proxy.$modal.msgSuccess("新增成功");
						open.value = false;
						getList();
					});
				}
			}
		});
	}
	/** 查询部门下拉树结构 */
	function getTreeselect() {
		// treeselect().then((response) => {
		// 	deptOptions.value = response.data;
		// });
		listDept({}).then(response => {
			deptList.value = response.data;
			deptOptions.value = proxy.handleTree(response.data, "deptId")
		});
	}
	getList();
	getTreeselect();
</script>