// 人员管理mock数据
export const adminMockData = {
  // 人员列表
  list: [
    {
      id: 1,
      username: 'USER1',
      mobile: '13800008888',
      role_name: '管理员',
      description: '系统管理员',
      role_id: 1,
      status: 'normal',
      status_text: '正常',
      create_time: '2025-01-10 10:00:00',
      update_time: '2025-01-15 14:30:00',
    },
    {
      id: 2,
      username: 'USER2',
      mobile: '13800008888',
      role_name: '店长',
      description: '系统管理员',
      role_id: 2,
      status: 'normal',
      status_text: '正常',
      create_time: '2025-01-10 11:00:00',
      update_time: '2025-01-14 16:20:00',
    },
    {
      id: 3,
      username: 'USER3',
      mobile: '13800008888',
      role_name: '财务',
      description: '高级管理员',
      role_id: 3,
      status: 'normal',
      status_text: '正常',
      create_time: '2025-01-11 09:30:00',
      update_time: '2025-01-13 11:45:00',
    },
    {
      id: 4,
      username: 'USER4',
      mobile: '13800008888',
      role_name: '客服',
      description: '系统管理员',
      role_id: 4,
      status: 'normal',
      status_text: '正常',
      create_time: '2025-01-11 14:15:00',
      update_time: '2025-01-12 09:20:00',
    },
    {
      id: 5,
      username: 'USER5',
      mobile: '13800008888',
      role_name: '运营',
      description: '系统管理员',
      role_id: 5,
      status: 'normal',
      status_text: '正常',
      create_time: '2025-01-12 08:45:00',
      update_time: '2025-01-15 13:10:00',
    },
    {
      id: 6,
      username: 'USER6',
      mobile: '13800008888',
      role_name: '市场',
      description: '高级管理员',
      role_id: 6,
      status: 'normal',
      status_text: '正常',
      create_time: '2025-01-12 10:15:00',
      update_time: '2025-01-13 16:20:00',
    },
    {
      id: 7,
      username: 'USER7',
      mobile: '13800008888',
      role_name: '编辑1',
      description: '高级管理员',
      role_id: 7,
      status: 'normal',
      status_text: '正常',
      create_time: '2025-01-12 15:30:00',
      update_time: '2025-01-14 10:45:00',
    },
    {
      id: 8,
      username: 'USER8',
      mobile: '13800008888',
      role_name: '编辑2',
      description: '系统管理员',
      role_id: 8,
      status: 'normal',
      status_text: '正常',
      create_time: '2025-01-13 08:45:00',
      update_time: '2025-01-15 12:10:00',
    },
  ],

  // 获取列表数据
  getList(params = {}) {
    const { page = 1, size = 10, username = '', mobile = '', role_name = '' } = params;

    let filteredData = this.list;

    // 根据用户名筛选
    if (username) {
      filteredData = filteredData.filter((item) =>
        item.username.toLowerCase().includes(username.toLowerCase()),
      );
    }

    // 根据手机号筛选
    if (mobile) {
      filteredData = filteredData.filter((item) => item.mobile.includes(mobile));
    }

    // 根据角色筛选
    if (role_name) {
      filteredData = filteredData.filter((item) => item.role_name.includes(role_name));
    }

    // 分页处理
    const total = filteredData.length;
    const startIndex = (page - 1) * size;
    const endIndex = startIndex + size;
    const list = filteredData.slice(startIndex, endIndex);

    return {
      code: '200',
      message: '操作成功',
      data: {
        list: list,
        total: total,
        pageNum: page,
        pageSize: size,
        pages: Math.ceil(total / size),
        size: list.length,
        startRow: startIndex + 1,
        endRow: startIndex + list.length,
        hasNextPage: page < Math.ceil(total / size),
        hasPreviousPage: page > 1,
        isFirstPage: page === 1,
        isLastPage: page >= Math.ceil(total / size),
        firstPage: 1,
        lastPage: Math.ceil(total / size),
        navigatePages: 8,
        navigatepageNums: [],
        navigateFirstPage: 1,
        navigateLastPage: Math.ceil(total / size),
        nextPage: page < Math.ceil(total / size) ? page + 1 : 0,
        prePage: page > 1 ? page - 1 : 0,
      },
    };
  },

  // 获取详情
  getDetail(id) {
    const item = this.list.find((item) => item.id == id);
    if (item) {
      return {
        code: '200',
        message: '操作成功',
        data: item,
      };
    } else {
      return {
        code: '500',
        message: '人员不存在',
        data: null,
      };
    }
  },

  // 新增人员
  add(data) {
    const newId = Math.max(...this.list.map((item) => item.id)) + 1;
    const newItem = {
      id: newId,
      username: data.username,
      mobile: data.mobile,
      role_name: data.role_name || '普通用户',
      description: data.description || '',
      role_id: data.role_id || 1,
      status: 'normal',
      status_text: '正常',
      create_time: new Date().toLocaleString('zh-CN'),
      update_time: new Date().toLocaleString('zh-CN'),
    };

    this.list.push(newItem);

    return {
      code: '200',
      message: '新增成功',
      data: newItem,
    };
  },

  // 编辑人员
  edit(id, data) {
    const index = this.list.findIndex((item) => item.id == id);
    if (index !== -1) {
      this.list[index] = {
        ...this.list[index],
        ...data,
        update_time: new Date().toLocaleString('zh-CN'),
      };

      return {
        code: '200',
        message: '编辑成功',
        data: this.list[index],
      };
    } else {
      return {
        code: '500',
        message: '人员不存在',
        data: null,
      };
    }
  },

  // 删除人员
  delete(id) {
    const index = this.list.findIndex((item) => item.id == id);
    if (index !== -1) {
      this.list.splice(index, 1);
      return {
        code: '200',
        message: '删除成功',
        data: null,
      };
    } else {
      return {
        code: '500',
        message: '人员不存在',
        data: null,
      };
    }
  },
};

export default {
  /**
   * 人员列表接口
   */
  'admin/list': (data) => adminMockData.getList(data),

  /**
   * 人员详情接口
   */
  'admin/detail': (data) => adminMockData.getDetail(data.id),

  /**
   * 新增人员接口
   */
  'admin/add': (data) => adminMockData.add(data),

  /**
   * 编辑人员接口
   */
  'admin/edit': (data) => adminMockData.edit(data.id, data),

  /**
   * 删除人员接口
   */
  'admin/delete': (data) => adminMockData.delete(data.id),
};
