import { MockMethod } from 'vite-plugin-mock'
import Mock from 'mockjs'

const mockData = Array.from({ length: 56 }, () => {
  return Mock.mock({
    id: '@id',
    status: '@pick(0,1)',
    areaCode: '@city(true) @areacode',
    code: '@integer(1000, 9000)',
    funcType: '@types',
    operator: '@cname',
    createTime: '@datetime',
    lastModified: '@datetime'
  })
})

const mockMethodOne: MockMethod[] = [
  {
    url: '/mock/api/one',
    method: 'get',
    timeout: 1000,
    response: ({ query }: { query: any }) => {
      const { code, areaCode, pageIndex, pageSize } = query
      let filterData = mockData
      if (areaCode) {
        filterData = mockData.filter((item) => {
          return item.areaCode == areaCode
        })
      }
      if (code) {
        filterData = mockData.filter((item) => item.code == code)
      }
      const _pageSize = Number(pageSize) ?? 20
      const total = filterData.length
      const pages = Math.ceil(total / _pageSize)
      const offset = (pageIndex - 1) * _pageSize
      const list = filterData.slice(offset, offset + _pageSize)
      return {
        code: '0',
        data: {
          pageNum: Number(pageIndex),
          pageSize: Number(pageSize) ?? 20,
          size: list.length,
          pages: pages,
          total: total,
          list: list
        }
      }
    }
  }
]

export default mockMethodOne
