// 模拟静态数据
const mockConfigs = [
  { configId: 1, configName: '系统名称', configKey: 'sys.name', configValue: '智奇管理系统', configType: 'Y', status: '0', createTime: '2023-01-01' },
  { configId: 2, configName: '系统版本', configKey: 'sys.version', configValue: 'v1.0.0', configType: 'Y', status: '0', createTime: '2023-01-02' }
];

// 查询参数列表
export function listConfig(query) {
  const { pageNum, pageSize, configName, configKey, configType, status } = query;
  let filteredConfigs = [...mockConfigs];
  
  if (configName) {
    filteredConfigs = filteredConfigs.filter(config => config.configName.includes(configName));
  }
  if (configKey) {
    filteredConfigs = filteredConfigs.filter(config => config.configKey.includes(configKey));
  }
  if (configType) {
    filteredConfigs = filteredConfigs.filter(config => config.configType === configType);
  }
  if (status) {
    filteredConfigs = filteredConfigs.filter(config => config.status === status);
  }
  
  const total = filteredConfigs.length;
  const start = (pageNum - 1) * pageSize;
  const end = start + pageSize;
  const list = filteredConfigs.slice(start, end);
  
  return Promise.resolve({
    total,
    rows: list
  });
}

// 查询参数详细
export function getConfig(configId) {
  const config = mockConfigs.find(c => c.configId === parseInt(configId));
  return Promise.resolve(config || {});
}

// 根据参数键名查询参数值
export function getConfigKey(configKey) {
  const config = mockConfigs.find(c => c.configKey === configKey);
  return Promise.resolve(config ? config.configValue : '');
}

// 新增参数配置
export function addConfig(data) {
  const newConfig = {
    configId: mockConfigs.length + 1,
    ...data,
    createTime: new Date().toISOString().split('T')[0]
  };
  mockConfigs.push(newConfig);
  return Promise.resolve({ code: 200, msg: '新增成功' });
}

// 修改参数配置
export function updateConfig(data) {
  const index = mockConfigs.findIndex(c => c.configId === data.configId);
  if (index !== -1) {
    mockConfigs[index] = { ...mockConfigs[index], ...data };
  }
  return Promise.resolve({ code: 200, msg: '修改成功' });
}

// 删除参数配置
export function delConfig(configId) {
  const index = mockConfigs.findIndex(c => c.configId === parseInt(configId));
  if (index !== -1) {
    mockConfigs.splice(index, 1);
  }
  return Promise.resolve({ code: 200, msg: '删除成功' });
}

// 刷新参数缓存
export function refreshCache() {
  return Promise.resolve({ code: 200, msg: '刷新成功' });
}
