import Mock from "mockjs"

const student = Mock.mock({
  "list|100": [
    {
      "Id|+1": 1,
      "name": "@cname",
      "gender": `@pick(['男', '女'])`,
      "studentId": "@integer(1000000000,9999999999)",
      "department": `@pick(['机电工程系', '护理分院','建筑系','材料科学与工程系','环境科学与工程系'])`,
      "major": `@pick(['机械制造与自动化', '材料科学与工程', '环境科学与工程', '建筑工程', '护理学'])`,
      "classes": `@pick(['机制1班', '材料1班', '环境1班', '建筑1班','护理1班'])`,
      "status": `@pick(['休学', '退学', '复学'])`,
    },
  ],
});

export default [
  {
    url: '/api-list',
    method: 'get',
    response: () => {
      return {
        code: 200,
        data: student
      }
    }
  },
  {
    url: '/api-updateStudent',
    method: 'post',
    response(req) {
      // 假设req.body的结构是{Id: '唯一标识', ...其他字段}
      const requestBody = req.body;
      const studentId = requestBody.Id;
  
      // 模拟检查数据库中是否存在该id
      // 在真实环境中,这一步应当由后端服务完成
      const students = getMockStudents(); // 假设这是存储模拟学生数据的地方
      const existingStudent = students.find(student => student.id === studentId);
  
      if (existingStudent) {
        // 更新数据库中的学生信息(在模拟中,我们直接修改内存中的数据)
        Object.assign(existingStudent, requestBody);
        return {
          code: 200,
          message: '更新成功',
          data: existingStudent
        };
      } else {
        return {
          code: 404,
          message: '未找到具有该ID的学生'
        };
      }
    }
  },
  {
    url: '/api-student',
    method: 'post',
    response: (req) => {
      if (req.body.name !== '') {
        return student.list.filter(item => item.name.includes(req.body.name))
      }
      else if (req.body.studentId !== '') {
        const studentId = Number(req.body.studentId)
        if (!isNaN(studentId)) {
          return student.list.filter(item => item.studentId === studentId)
        } else {
          return {
            code: 400,
            message: 'Invalid student number format'
          }
        }
      }
      else if (req.body.name !== '') {
        return student.list.filter(item => item.name.includes(req.body.name))
      }
      else if (req.body.gender !== '') {
        return student.list.filter(item => item.gender.includes(req.body.gender))
      }
      else if (req.body.department !== '') {
        return student.list.filter(item => item.grade.includes(req.body.department))
      }
      else if (req.body.major !== '') {
        return student.list.filter(item => item.major.includes(req.body.major))
      }
      else if (req.body.classes !== '') {
        return student.list.filter(item => item.classes.includes(req.body.classes))
      }
      else if (req.body.status !== '') {
        return student.list.filter(item => item.major.includes(req.body.status))
      }
      return {
        code: 200,
      }
    }
  }
]