// 模拟查核组数据（参考截图）
const checkGroups = [
  {
    id: 1,
    name: '急诊手术查核组',
    groupManagerName: '杨晓琳',
    remark: '负责急诊手术相关查核工作',
    createUserName: '系统管理员',
    createTime: '2024-01-15 10:30:00',
  },
  {
    id: 2,
    name: '信息系统查核组',
    groupManagerName: '杨晓琳',
    remark: '负责信息系统安全查核',
    createUserName: '系统管理员',
    createTime: '2024-01-15 11:00:00',
  },
  {
    id: 3,
    name: '消防查核组',
    groupManagerName: '杨晓琳',
    remark: '负责消防安全相关查核工作',
    createUserName: '系统管理员',
    createTime: '2024-01-15 14:20:00',
  },
  {
    id: 4,
    name: '医疗组',
    groupManagerName: '杨晓琳',
    remark: '负责医疗相关查核工作',
    createUserName: '系统管理员',
    createTime: '2024-01-15 15:45:00',
  },
  {
    id: 5,
    name: '急诊急外手术核组',
    groupManagerName: '杨晓琳',
    remark: '负责急诊急外手术查核',
    createUserName: '系统管理员',
    createTime: '2024-01-15 16:10:00',
  },
  {
    id: 6,
    name: '护理组',
    groupManagerName: '杨晓琳',
    remark: '负责护理质量管理查核',
    createUserName: '系统管理员',
    createTime: '2024-01-15 16:30:00',
  },
  {
    id: 7,
    name: '药事服务组',
    groupManagerName: '杨晓琳',
    remark: '负责药事服务查核',
    createUserName: '系统管理员',
    createTime: '2024-01-15 17:00:00',
  },
  {
    id: 8,
    name: '药品器械组',
    groupManagerName: '董丽群',
    remark: '负责药品器械查核',
    createUserName: '系统管理员',
    createTime: '2024-01-15 17:30:00',
  },
  {
    id: 9,
    name: '糖尿病组',
    groupManagerName: '何亦红',
    remark: '负责糖尿病相关查核',
    createUserName: '系统管理员',
    createTime: '2024-01-15 18:00:00',
  },
  {
    id: 10,
    name: '消毒隔离组',
    groupManagerName: '金艳艳',
    remark: '负责消毒隔离查核',
    createUserName: '系统管理员',
    createTime: '2024-01-15 18:30:00',
  },
];

// 模拟人员数据
const employees = [
  {
    id: 1,
    employeeCode: '005018',
    employeeName: '王金鸿',
    deptName: '急诊手术组织',
  },
  {
    id: 2,
    employeeCode: '005019',
    employeeName: '杨晓琳',
    deptName: '信息系统查核组织',
  },
  {
    id: 3,
    employeeCode: '005020',
    employeeName: '张琳',
    deptName: '消防系统查核组织',
  },
  {
    id: 4,
    employeeCode: '005030',
    employeeName: '胡庚芳',
    deptName: '医疗组织',
  },
  {
    id: 5,
    employeeCode: '005032',
    employeeName: '陈雨萍',
    deptName: '护理组织',
  },
  {
    id: 6,
    employeeCode: '005033',
    employeeName: '陈海霞',
    deptName: '护理组织',
  },
  {
    id: 7,
    employeeCode: '005034',
    employeeName: '李明',
    deptName: '急诊手术组织',
  },
  {
    id: 8,
    employeeCode: '005035',
    employeeName: '刘强',
    deptName: '信息系统查核组织',
  },
  {
    id: 9,
    employeeCode: '005036',
    employeeName: '赵敏',
    deptName: '消防系统查核组织',
  },
  {
    id: 10,
    employeeCode: '005037',
    employeeName: '孙丽',
    deptName: '医疗组织',
  },
];

// 模拟查核组人员关系数据
const checkGroupEmployees = [
  {
    id: 1,
    groupId: 1,
    employeeId: 1,
    employeeCode: '005018',
    employeeName: '王金鸿',
    deptName: '急诊手术组织',
    isManager: 1,
  },
  {
    id: 2,
    groupId: 1,
    employeeId: 7,
    employeeCode: '005034',
    employeeName: '李明',
    deptName: '急诊手术组织',
    isManager: 0,
  },
  {
    id: 3,
    groupId: 2,
    employeeId: 2,
    employeeCode: '005019',
    employeeName: '杨晓琳',
    deptName: '信息系统查核组织',
    isManager: 1,
  },
  {
    id: 4,
    groupId: 2,
    employeeId: 8,
    employeeCode: '005035',
    employeeName: '刘强',
    deptName: '信息系统查核组织',
    isManager: 0,
  },
  {
    id: 5,
    groupId: 3,
    employeeId: 6,
    employeeCode: '005033',
    employeeName: '陈海霞',
    deptName: '护理组织',
    isManager: 1,
  },
  {
    id: 6,
    groupId: 4,
    employeeId: 3,
    employeeCode: '005020',
    employeeName: '张琳',
    deptName: '消防系统查核组织',
    isManager: 1,
  },
  {
    id: 7,
    groupId: 4,
    employeeId: 9,
    employeeCode: '005036',
    employeeName: '赵敏',
    deptName: '消防系统查核组织',
    isManager: 0,
  },
  {
    id: 8,
    groupId: 5,
    employeeId: 5,
    employeeCode: '005032',
    employeeName: '陈雨萍',
    deptName: '护理组织',
    isManager: 1,
  },
  {
    id: 9,
    groupId: 5,
    employeeId: 10,
    employeeCode: '005037',
    employeeName: '孙丽',
    deptName: '医疗组织',
    isManager: 0,
  },
];

export default {
  // 获取查核组列表
  'GET /manager/CheckGroup/getCheckGroupList': (req, res) => {
    const {
      current = 1,
      pageSize = 10,
      groupName = '',
      groupManagerName = '',
    } = req.query;

    let filteredGroups = checkGroups;

    // 按查核组名称过滤
    if (groupName) {
      filteredGroups = filteredGroups.filter((group) =>
        group.name.includes(groupName),
      );
    }

    // 按组长名称过滤
    if (groupManagerName) {
      filteredGroups = filteredGroups.filter(
        (group) =>
          group.groupManagerName &&
          group.groupManagerName.includes(groupManagerName),
      );
    }

    const startIndex = (current - 1) * pageSize;
    const endIndex = startIndex + parseInt(pageSize);
    const list = filteredGroups.slice(startIndex, endIndex);

    res.json({
      list,
      totalCount: filteredGroups.length,
      currPage: parseInt(current),
      pageSize: parseInt(pageSize),
      totalPage: Math.ceil(filteredGroups.length / pageSize),
    });
  },

  // 新增查核组
  'POST /manager/CheckGroup/addCheckGroup': (req, res) => {
    const { name, remark, groupManagerId } = req.body;
    const manager = employees.find((emp) => emp.id === groupManagerId);

    const newGroup = {
      id: checkGroups.length + 1,
      name,
      groupManagerName: manager ? manager.employeeName : '',
      remark,
      createUserName: '系统管理员',
      createTime: new Date().toISOString(),
    };

    checkGroups.push(newGroup);

    res.json({ success: true, message: '新增成功' });
  },

  // 编辑查核组
  'POST /manager/CheckGroup/editCheckGroup': (req, res) => {
    const { id, name, remark, groupManagerId } = req.body;
    const groupIndex = checkGroups.findIndex((group) => group.id === id);

    if (groupIndex !== -1) {
      const manager = employees.find((emp) => emp.id === groupManagerId);
      checkGroups[groupIndex] = {
        ...checkGroups[groupIndex],
        name,
        remark,
        groupManagerName: manager
          ? manager.employeeName
          : checkGroups[groupIndex].groupManagerName,
      };
      res.json({ success: true, message: '编辑成功' });
    } else {
      res.status(404).json({ success: false, message: '查核组不存在' });
    }
  },

  // 删除查核组
  'POST /manager/CheckGroup/deleteCheckGroup': (req, res) => {
    const { id } = req.query;
    const groupIndex = checkGroups.findIndex(
      (group) => group.id === parseInt(id),
    );

    if (groupIndex !== -1) {
      checkGroups.splice(groupIndex, 1);
      // 同时删除相关的人员关系
      const employeeIndices = [];
      for (let i = checkGroupEmployees.length - 1; i >= 0; i--) {
        if (checkGroupEmployees[i].groupId === parseInt(id)) {
          employeeIndices.push(i);
        }
      }
      employeeIndices.forEach((index) => checkGroupEmployees.splice(index, 1));

      res.json({ success: true, message: '删除成功' });
    } else {
      res.status(404).json({ success: false, message: '查核组不存在' });
    }
  },

  // 获取查核组人员列表
  'GET /manager/CheckGroup/getCheckGroupEmployees': (req, res) => {
    const { groupId, filter = '', current = 1, pageSize = 10 } = req.query;

    let groupEmployees = checkGroupEmployees.filter(
      (emp) => emp.groupId === parseInt(groupId),
    );

    // 按姓名或工号过滤
    if (filter) {
      groupEmployees = groupEmployees.filter(
        (emp) =>
          emp.employeeName.includes(filter) ||
          emp.employeeCode.includes(filter),
      );
    }

    const startIndex = (current - 1) * pageSize;
    const endIndex = startIndex + parseInt(pageSize);
    const list = groupEmployees.slice(startIndex, endIndex);

    res.json({
      list,
      totalCount: groupEmployees.length,
      currPage: parseInt(current),
      pageSize: parseInt(pageSize),
      totalPage: Math.ceil(groupEmployees.length / pageSize),
    });
  },

  // 获取可添加的人员列表
  'GET /manager/CheckGroup/getAvailableEmployees': (req, res) => {
    const { groupId, filter = '', current = 1, pageSize = 10 } = req.query;

    // 获取已经在该查核组的人员ID
    const existingEmployeeIds = checkGroupEmployees
      .filter((emp) => emp.groupId === parseInt(groupId))
      .map((emp) => emp.employeeId);

    // 过滤出不在该查核组的人员
    let availableEmployees = employees.filter(
      (emp) => !existingEmployeeIds.includes(emp.id),
    );

    // 按姓名或工号过滤
    if (filter) {
      availableEmployees = availableEmployees.filter(
        (emp) =>
          emp.employeeName.includes(filter) ||
          emp.employeeCode.includes(filter),
      );
    }

    const startIndex = (current - 1) * pageSize;
    const endIndex = startIndex + parseInt(pageSize);
    const list = availableEmployees.slice(startIndex, endIndex);

    res.json({
      list,
      totalCount: availableEmployees.length,
      currPage: parseInt(current),
      pageSize: parseInt(pageSize),
      totalPage: Math.ceil(availableEmployees.length / pageSize),
    });
  },

  // 添加查核组人员
  'POST /manager/CheckGroup/addCheckGroupEmployee': (req, res) => {
    const { groupId, employeeIds } = req.body;

    employeeIds.forEach((employeeId) => {
      const employee = employees.find((emp) => emp.id === parseInt(employeeId));
      if (employee) {
        const newRelation = {
          id: checkGroupEmployees.length + 1,
          groupId: parseInt(groupId),
          employeeId: employee.id,
          employeeCode: employee.employeeCode,
          employeeName: employee.employeeName,
          deptName: employee.deptName,
          isManager: 0,
        };
        checkGroupEmployees.push(newRelation);
      }
    });

    res.json({ success: true, message: '添加成功' });
  },

  // 删除查核组人员
  'POST /manager/CheckGroup/delCheckGroupEmployee': (req, res) => {
    const employeeIds = req.body;

    employeeIds.forEach((id) => {
      const index = checkGroupEmployees.findIndex(
        (emp) => emp.id === parseInt(id),
      );
      if (index !== -1) {
        checkGroupEmployees.splice(index, 1);
      }
    });

    res.json({ success: true, message: '删除成功' });
  },

  // 获取人员字典
  'GET /manager/CheckGroup/getEmployeeDict': (req, res) => {
    res.json(employees);
  },
};
