/*
 * @Author: code4eat awesomedema@gmail.com
 * @Date: 2024-01-01 10:00:00
 * @LastEditors: code4eat awesomedema@gmail.com
 * @LastEditTime: 2024-01-01 10:00:00
 * @FilePath: /pfmBackMana/mock/checkItem.js
 * @Description: 查核项管理mock数据
 */

// 查核项分类树mock数据
const checkItemTreeData = [
  {
    key: '1',
    title: '地面干净无水迹',
    children: [
      {
        key: '1-1',
        title: '科内无专管',
      },
      {
        key: '1-2',
        title: '科内不知晓专管员',
      },
      {
        key: '1-3',
        title: '麻醉药品未放入保险柜',
      },
      {
        key: '1-4',
        title: '使用PDA时扫描帐后未查看PDA各项信息是否准确',
      },
      {
        key: '1-5',
        title: '对无法有效沟通的患者，未让陪同人员除述患者姓名或未查看腕带',
      },
    ],
  },
  {
    key: '2',
    title: '防火门关情况',
    children: [
      {
        key: '2-1',
        title: '防火门未正常关闭',
      },
      {
        key: '2-2',
        title: '防火门被锁死',
      },
    ],
  },
  {
    key: '3',
    title: '防烟面具、指挥棒、任意门其他应急物品情况',
    children: [
      {
        key: '3-1',
        title: '应急物品缺失',
      },
      {
        key: '3-2',
        title: '应急物品损坏',
      },
    ],
  },
  {
    key: '4',
    title: '疏散指示灯情况',
    children: [
      {
        key: '4-1',
        title: '指示灯不亮',
      },
      {
        key: '4-2',
        title: '指示灯损坏',
      },
    ],
  },
  {
    key: '5',
    title: '科室消防安全自查表',
    children: [
      {
        key: '5-1',
        title: '未按要求填写自查表',
      },
      {
        key: '5-2',
        title: '自查表内容不完整',
      },
    ],
  },
  {
    key: '6',
    title: '消防情况',
    children: [
      {
        key: '6-1',
        title: '消防设施损坏',
      },
      {
        key: '6-2',
        title: '消防通道堵塞',
      },
    ],
  },
];

// 查核项列表mock数据
const checkItemListData = {
  1: [
    {
      id: 1,
      name: '科内无专管',
      score: 2.0,
      description: '科室内未配置专门管理人员',
      categoryId: '1',
    },
    {
      id: 2,
      name: '科内不知晓专管员',
      score: 1.5,
      description: '科室人员不知道专管员是谁',
      categoryId: '1',
    },
    {
      id: 3,
      name: '麻醉药品未放入保险柜',
      score: 1.0,
      description: '麻醉药品未按规定放入保险柜',
      categoryId: '1',
    },
    {
      id: 4,
      name: '使用PDA时扫描帐后未查看PDA各项信息是否准确',
      score: 0.5,
      description: '使用PDA时未核实信息准确性',
      categoryId: '1',
    },
    {
      id: 5,
      name: '对无法有效沟通的患者，未让陪同人员除述患者姓名或未查看腕带',
      score: 1.0,
      description: '对沟通困难患者未按规定核实身份',
      categoryId: '1',
    },
  ],
  2: [
    {
      id: 6,
      name: '防火门未正常关闭',
      score: 3.0,
      description: '防火门应保持关闭状态',
      categoryId: '2',
    },
    {
      id: 7,
      name: '防火门被锁死',
      score: 5.0,
      description: '防火门不得锁死，影响疏散',
      categoryId: '2',
    },
  ],
  3: [
    {
      id: 8,
      name: '应急物品缺失',
      score: 2.0,
      description: '防烟面具、指挥棒等应急物品缺失',
      categoryId: '3',
    },
    {
      id: 9,
      name: '应急物品损坏',
      score: 1.5,
      description: '应急物品存在损坏情况',
      categoryId: '3',
    },
  ],
  4: [
    {
      id: 10,
      name: '指示灯不亮',
      score: 1.0,
      description: '疏散指示灯不能正常工作',
      categoryId: '4',
    },
    {
      id: 11,
      name: '指示灯损坏',
      score: 2.0,
      description: '疏散指示灯外观损坏',
      categoryId: '4',
    },
  ],
  5: [
    {
      id: 12,
      name: '未按要求填写自查表',
      score: 1.0,
      description: '科室未按时填写消防安全自查表',
      categoryId: '5',
    },
    {
      id: 13,
      name: '自查表内容不完整',
      score: 0.5,
      description: '自查表填写内容不完整或不准确',
      categoryId: '5',
    },
  ],
  6: [
    {
      id: 14,
      name: '消防设施损坏',
      score: 3.0,
      description: '消防器材、设施存在损坏',
      categoryId: '6',
    },
    {
      id: 15,
      name: '消防通道堵塞',
      score: 4.0,
      description: '消防通道被堵塞，影响疏散',
      categoryId: '6',
    },
  ],
};

let currentId = 16; // 用于新增数据时的ID生成

export default {
  // 获取查核项分类树
  'GET /manager/checkItem/getCheckItemTree': (req, res) => {
    setTimeout(() => {
      res.json({
        success: true,
        data: checkItemTreeData,
      });
    }, 200);
  },

  // 获取查核项列表
  'GET /manager/checkItem/getCheckItemList': (req, res) => {
    const { current = 1, pageSize = 10, categoryId, keywords } = req.query;

    let data = checkItemListData[categoryId] || [];

    // 关键字搜索
    if (keywords) {
      data = data.filter(
        (item) =>
          item.name.includes(keywords) ||
          (item.description && item.description.includes(keywords)),
      );
    }

    const start = (current - 1) * pageSize;
    const end = start + parseInt(pageSize);
    const list = data.slice(start, end);

    setTimeout(() => {
      res.json({
        success: true,
        list,
        totalCount: data.length,
        pageSize: parseInt(pageSize),
        currPage: parseInt(current),
        totalPage: Math.ceil(data.length / pageSize),
      });
    }, 300);
  },

  // 新增查核项
  'POST /manager/checkItem/addCheckItem': (req, res) => {
    const { name, score, description, categoryId } = req.body;

    const newItem = {
      id: currentId++,
      name,
      score,
      description,
      categoryId,
      createTime: new Date().toISOString(),
    };

    if (!checkItemListData[categoryId]) {
      checkItemListData[categoryId] = [];
    }
    checkItemListData[categoryId].push(newItem);

    setTimeout(() => {
      res.json({
        success: true,
        message: '新增成功',
        data: newItem,
      });
    }, 500);
  },

  // 编辑查核项
  'POST /manager/checkItem/editCheckItem': (req, res) => {
    const { id, name, score, description, categoryId } = req.body;

    if (checkItemListData[categoryId]) {
      const index = checkItemListData[categoryId].findIndex(
        (item) => item.id === id,
      );
      if (index !== -1) {
        checkItemListData[categoryId][index] = {
          ...checkItemListData[categoryId][index],
          name,
          score,
          description,
          updateTime: new Date().toISOString(),
        };
      }
    }

    setTimeout(() => {
      res.json({
        success: true,
        message: '编辑成功',
      });
    }, 500);
  },

  // 删除查核项
  'POST /manager/checkItem/deleteCheckItem': (req, res) => {
    const { id } = req.query;

    // 从所有分类中查找并删除
    Object.keys(checkItemListData).forEach((categoryId) => {
      const index = checkItemListData[categoryId].findIndex(
        (item) => item.id == id,
      );
      if (index !== -1) {
        checkItemListData[categoryId].splice(index, 1);
      }
    });

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

  // 获取分类详情
  'GET /manager/checkItem/getCategoryDetail': (req, res) => {
    const { categoryId } = req.query;

    // 模拟分类详情数据
    const categoryDetails = {
      1: {
        id: '1',
        name: '地面干净无水迹',
        type: 'multiple',
        score: 5,
        checkMethod: 'observe',
        isStatistics: true,
        description: '应知应会：疏散通道畅通，无杂物堆品',
      },
    };

    setTimeout(() => {
      res.json({
        success: true,
        data: categoryDetails[categoryId] || null,
      });
    }, 200);
  },
};
