<template>
	<div class="pages-base-layout system-configs-page">
		<div class="tool-bar">
			<div class="left-panel">
				<a-space>
					<a-input
						v-model:value="searchForm.keyword"
						placeholder="配置名称/键名"
						allow-clear
						style="width: 180px"
					/>
					<a-select
						v-model:value="searchForm.group"
						placeholder="配置分组"
						allow-clear
						style="width: 140px"
						:options="groupOptions"
					/>
					<a-button type="primary" @click="handleSearch">
						<template #icon><search-outlined /></template>
						搜索
					</a-button>
					<a-button @click="handleReset">
						<template #icon><redo-outlined /></template>
						重置
					</a-button>
				</a-space>
			</div>
			<div class="right-panel">
				<a-button type="primary" @click="handleAdd">
					<template #icon><plus-outlined /></template>
					新增
				</a-button>
				<a-dropdown>
					<a-button>
						批量操作
						<down-outlined />
					</a-button>
					<template #overlay>
						<a-menu>
							<a-menu-item @click="handleBatchDelete">
								<delete-outlined />
								批量删除
							</a-menu-item>
							<a-menu-item @click="handleBatchStatus(1)">
								<check-outlined />
								批量启用
							</a-menu-item>
							<a-menu-item @click="handleBatchStatus(0)">
								<stop-outlined />
								批量禁用
							</a-menu-item>
						</a-menu>
					</template>
				</a-dropdown>
			</div>
		</div>

		<div class="table-content">
			<scTable
				ref="tableRef"
				:columns="columns"
				:data-source="tableData"
				:loading="loading"
				:pagination="pagination"
				:row-selection="rowSelection"
				:row-key="(record) => record.id"
				@refresh="refreshTable"
				@paginationChange="handlePaginationChange"
			>
				<template #bodyCell="{ column, record }">
					<template v-if="column.key === 'type'">
						<a-tag :color="getTypeColor(record.type)">{{
							getTypeText(record.type)
						}}</a-tag>
					</template>
					<template v-if="column.key === 'value'">
						<span
							v-if="
								['string', 'number', 'boolean'].includes(
									record.type,
								)
							"
							class="value-text"
						>
							{{ formatValue(record.value, record.type) }}
						</span>
						<span
							v-else-if="record.type === 'file'"
							class="file-value"
						>
							<file-outlined />
							{{ record.value }}
						</span>
						<a
							v-else-if="record.type === 'image'"
							:href="record.value"
							target="_blank"
							class="image-value"
						>
							<img
								:src="record.value"
								alt="预览"
								class="config-image"
							/>
						</a>
						<span v-else class="json-value">{{
							record.value
						}}</span>
					</template>
					<template v-if="column.key === 'status'">
						<a-badge
							:status="record.status ? 'success' : 'default'"
							:text="record.status ? '启用' : '禁用'"
						/>
					</template>
					<template v-if="column.key === 'action'">
						<a-space>
							<a-button
								type="link"
								size="small"
								@click="handleEdit(record)"
							>
								<edit-outlined />
								编辑
							</a-button>
							<a-button
								type="link"
								size="small"
								danger
								:disabled="record.is_system"
								@click="handleDelete(record)"
							>
								<delete-outlined />
								删除
							</a-button>
						</a-space>
					</template>
				</template>
			</scTable>
		</div>

		<!-- 新增/编辑弹窗 -->
		<SaveDialog
			v-model:visible="showSaveDialog"
			:record="currentRecord"
			@success="handleSaveSuccess"
		/>
	</div>
</template>

<script setup>
import { ref, reactive, onMounted, h } from "vue";
import { message, Modal } from "ant-design-vue";
import {
	SearchOutlined,
	RedoOutlined,
	PlusOutlined,
	DownOutlined,
	DeleteOutlined,
	CheckOutlined,
	StopOutlined,
	EditOutlined,
	FileOutlined,
} from "@ant-design/icons-vue";
import scTable from "@/components/scTable/index.vue";
import { useTable } from "@/hooks/useTable";
import systemApi from "@/api/system";
import SaveDialog from "./components/SaveDialog.vue";

// 表格引用
const tableRef = ref(null);

// 搜索表单
const searchForm = reactive({
	keyword: "",
	group: undefined,
});

// 分组选项
const groupOptions = ref([]);

// 当前记录
const currentRecord = ref(null);

// 显示新增/编辑弹窗
const showSaveDialog = ref(false);

// 使用 useTable Hook
const {
	tableData,
	loading,
	pagination,
	rowSelection,
	handleSearch,
	handleReset,
	handlePaginationChange,
	refreshTable,
} = useTable({
	api: systemApi.configs.list.get,
	searchForm,
	needPagination: true,
});

// 表格列配置
const columns = [
	{
		title: "ID",
		dataIndex: "id",
		key: "id",
		width: 80,
	},
	{
		title: "配置分组",
		dataIndex: "group",
		key: "group",
		width: 120,
	},
	{
		title: "配置键",
		dataIndex: "key",
		key: "key",
		width: 200,
		ellipsis: true,
	},
	{
		title: "配置名称",
		dataIndex: "name",
		key: "name",
		width: 180,
	},
	{
		title: "配置值",
		dataIndex: "value",
		key: "value",
		ellipsis: true,
		width: 250,
	},
	{
		title: "类型",
		dataIndex: "type",
		key: "type",
		width: 100,
		align: "center",
	},
	{
		title: "排序",
		dataIndex: "sort",
		key: "sort",
		width: 80,
		align: "center",
	},
	{
		title: "状态",
		dataIndex: "status",
		key: "status",
		width: 100,
		align: "center",
	},
	{
		title: "描述",
		dataIndex: "description",
		key: "description",
		ellipsis: true,
	},
	{
		title: "操作",
		key: "action",
		width: 150,
		fixed: "right",
	},
];

// 获取类型颜色
const getTypeColor = (type) => {
	const colors = {
		string: "blue",
		text: "cyan",
		number: "green",
		boolean: "orange",
		select: "purple",
		radio: "purple",
		checkbox: "purple",
		file: "pink",
		json: "geekblue",
	};
	return colors[type] || "default";
};

// 获取类型文本
const getTypeText = (type) => {
	const texts = {
		string: "字符串",
		text: "文本",
		number: "数字",
		boolean: "布尔值",
		select: "下拉框",
		radio: "单选框",
		checkbox: "多选框",
		file: "文件",
		json: "JSON",
	};
	return texts[type] || type;
};

// 格式化值
const formatValue = (value, type) => {
	if (type === "boolean") {
		return value === "true" || value === true ? "是" : "否";
	}
	if (type === "number") {
		return Number(value);
	}
	return value;
};

// 获取分组列表
const loadGroups = async () => {
	try {
		const res = await systemApi.configs.groups.get();
		groupOptions.value = res.data.map((item) => ({
			label: item,
			value: item,
		}));
	} catch (error) {
		console.error("获取分组列表失败:", error);
	}
};

// 新增
const handleAdd = () => {
	currentRecord.value = null;
	showSaveDialog.value = true;
};

// 编辑
const handleEdit = (record) => {
	currentRecord.value = { ...record };
	showSaveDialog.value = true;
};

// 删除
const handleDelete = (record) => {
	if (record.is_system) {
		message.warning("系统配置不能删除");
		return;
	}
	Modal.confirm({
		title: "确认删除",
		content: `确定要删除配置"${record.name}"吗？`,
		okText: "确定",
		cancelText: "取消",
		onOk: async () => {
			try {
				await systemApi.configs.delete.delete(record.id);
				message.success("删除成功");
				refreshTable();
			} catch (error) {
				message.error(error.message || "删除失败");
			}
		},
	});
};

// 批量删除
const handleBatchDelete = () => {
	const selectedRowKeys = rowSelection.selectedRowKeys;
	if (selectedRowKeys.length === 0) {
		message.warning("请先选择要删除的配置");
		return;
	}
	Modal.confirm({
		title: "确认删除",
		content: `确定要删除选中的 ${selectedRowKeys.length} 条配置吗？`,
		okText: "确定",
		cancelText: "取消",
		onOk: async () => {
			try {
				await systemApi.configs.batchDelete.post({
					ids: selectedRowKeys,
				});
				message.success("批量删除成功");
				rowSelection.selectedRowKeys = [];
				refreshTable();
			} catch (error) {
				message.error(error.message || "批量删除失败");
			}
		},
	});
};

// 批量更新状态
const handleBatchStatus = (status) => {
	const selectedRowKeys = rowSelection.selectedRowKeys;
	if (selectedRowKeys.length === 0) {
		message.warning("请先选择要操作的配置");
		return;
	}
	Modal.confirm({
		title: status === 1 ? "确认启用" : "确认禁用",
		content: `确定要${status === 1 ? "启用" : "禁用"}选中的 ${selectedRowKeys.length} 条配置吗？`,
		okText: "确定",
		cancelText: "取消",
		onOk: async () => {
			try {
				await systemApi.configs.batchStatus.post({
					ids: selectedRowKeys,
					status,
				});
				message.success(`${status === 1 ? "启用" : "禁用"}成功`);
				rowSelection.selectedRowKeys = [];
				refreshTable();
			} catch (error) {
				message.error(error.message || "操作失败");
			}
		},
	});
};

// 保存成功
const handleSaveSuccess = () => {
	showSaveDialog.value = false;
	refreshTable();
};

// 初始化
onMounted(() => {
	loadGroups();
});
</script>

<style scoped lang="scss">
.system-configs-page {
	.value-text {
		color: #666;
		font-family: monospace;
	}

	.file-value {
		display: flex;
		align-items: center;
		gap: 4px;
		color: #1890ff;
	}

	.image-value {
		display: inline-block;
		width: 40px;
		height: 40px;
		overflow: hidden;
		border-radius: 4px;
		cursor: pointer;

		.config-image {
			width: 100%;
			height: 100%;
			object-fit: cover;
		}

		&:hover .config-image {
			transform: scale(1.1);
			transition: transform 0.3s;
		}
	}

	.json-value {
		color: #999;
		font-family: monospace;
		font-size: 12px;
	}
}
</style>
