// 拼团活动mock数据
export const groupMockData = {
  // 拼团活动列表
  list: [
    {
      id: 1,
      activity_no: 'PT202501001',
      title: '新年特惠拼团活动',
      status: 'pending',
      status_text: '待开始',
      start_time: '2025-01-20',
      end_time: '2025-01-25',
      people_num: 5,
      countdown_hours: 24,
      warning_notice: '活动即将开始',
      goods_ids: [1, 2],
      goods_list: [
        { id: 1, title: 'BOLON Classic Aviator Polarized Sunglasses', price: 1000 },
        { id: 2, title: 'Premium Wireless Bluetooth Headphones', price: 1500 },
      ],
      goods_count: 2,
      created_at: '2025-01-15 10:00:00',
      updated_at: '2025-01-15 10:00:00',
    },
    {
      id: 2,
      activity_no: 'PT202501002',
      title: '春节大促拼团',
      status: 'active',
      status_text: '进行中',
      start_time: '2025-01-10',
      end_time: '2025-01-30',
      people_num: 10,
      countdown_hours: 48,
      warning_notice: '活动进行中',
      goods_ids: [3],
      goods_list: [{ id: 3, title: 'Smart Watch Series 5', price: 2500 }],
      goods_count: 1,
      created_at: '2025-01-08 10:00:00',
      updated_at: '2025-01-10 00:00:00',
    },
    {
      id: 3,
      activity_no: 'PT202501003',
      title: '元宵节限时拼团',
      status: 'active',
      status_text: '进行中',
      start_time: '2025-01-12',
      end_time: '2025-01-18',
      people_num: 20,
      countdown_hours: 72,
      warning_notice: '活动即将结束',
      goods_ids: [],
      goods_list: [],
      goods_count: 0,
      created_at: '2025-01-10 15:30:00',
      updated_at: '2025-01-12 08:00:00',
    },
    {
      id: 4,
      activity_no: 'PT202412001',
      title: '圣诞节拼团活动',
      status: 'ended',
      status_text: '已结束',
      start_time: '2024-12-20',
      end_time: '2024-12-26',
      people_num: 5,
      countdown_hours: 24,
      warning_notice: '活动已结束',
      created_at: '2024-12-18 10:00:00',
      updated_at: '2024-12-26 23:59:59',
    },
    {
      id: 5,
      activity_no: 'PT202412002',
      title: '年终清仓拼团',
      status: 'ended',
      status_text: '已结束',
      start_time: '2024-12-28',
      end_time: '2024-12-31',
      people_num: 10,
      countdown_hours: 48,
      warning_notice: '活动已结束',
      created_at: '2024-12-25 14:20:00',
      updated_at: '2024-12-31 23:59:59',
    },
    {
      id: 6,
      activity_no: 'PT202501004',
      title: '情人节浪漫拼团',
      status: 'pending',
      status_text: '待开始',
      start_time: '2025-02-10',
      end_time: '2025-02-15',
      people_num: 5,
      countdown_hours: 12,
      warning_notice: '活动尚未开始',
      created_at: '2025-01-15 16:45:00',
      updated_at: '2025-01-15 16:45:00',
    },
    {
      id: 7,
      activity_no: 'PT202501005',
      title: '开学季拼团优惠',
      status: 'pending',
      status_text: '待开始',
      start_time: '2025-02-20',
      end_time: '2025-03-05',
      people_num: 20,
      countdown_hours: 168,
      warning_notice: '活动尚未开始',
      created_at: '2025-01-16 09:15:00',
      updated_at: '2025-01-16 09:15:00',
    },
    {
      id: 8,
      activity_no: 'PT202501006',
      title: '女神节专属拼团',
      status: 'active',
      status_text: '进行中',
      start_time: '2025-01-14',
      end_time: '2025-01-22',
      people_num: 10,
      countdown_hours: 72,
      warning_notice: '活动火热进行中',
      created_at: '2025-01-12 11:30:00',
      updated_at: '2025-01-14 00:00:00',
    },
  ],

  // 状态统计
  statusCount: {
    all: 8,
    pending: 3,
    active: 3,
    ended: 2,
  },

  // 获取详情
  getDetail: (id) => {
    const item = groupMockData.list.find((item) => item.id == id);
    if (item) {
      return {
        error: 0,
        msg: '获取成功',
        data: { ...item },
      };
    } else {
      return {
        error: 1,
        msg: '记录不存在',
        data: null,
      };
    }
  },

  // 添加记录
  add: (data) => {
    const newId = Math.max(...groupMockData.list.map((item) => item.id)) + 1;
    const newItem = {
      id: newId,
      activity_no: `PT${new Date().getFullYear()}${String(new Date().getMonth() + 1).padStart(2, '0')}${String(newId).padStart(3, '0')}`,
      title: data.title,
      status: 'pending',
      status_text: '待开始',
      start_time: data.start_time,
      end_time: data.end_time,
      people_num: data.people_num,
      countdown_hours: data.countdown_hours,
      warning_notice: '活动尚未开始',
      created_at: new Date().toISOString().slice(0, 19).replace('T', ' '),
      updated_at: new Date().toISOString().slice(0, 19).replace('T', ' '),
    };

    groupMockData.list.unshift(newItem);
    groupMockData.statusCount.all++;
    groupMockData.statusCount.pending++;

    return {
      error: 0,
      msg: '添加成功',
      data: newItem,
    };
  },

  // 编辑记录
  edit: (id, data) => {
    const index = groupMockData.list.findIndex((item) => item.id == id);
    if (index !== -1) {
      const oldItem = groupMockData.list[index];
      const updatedItem = {
        ...oldItem,
        title: data.title,
        start_time: data.start_time,
        end_time: data.end_time,
        people_num: data.people_num,
        countdown_hours: data.countdown_hours,
        updated_at: new Date().toISOString().slice(0, 19).replace('T', ' '),
      };

      groupMockData.list[index] = updatedItem;

      return {
        error: 0,
        msg: '更新成功',
        data: updatedItem,
      };
    } else {
      return {
        error: 1,
        msg: '记录不存在',
        data: null,
      };
    }
  },

  // 删除记录
  delete: (id) => {
    const index = groupMockData.list.findIndex((item) => item.id == id);
    if (index !== -1) {
      const deletedItem = groupMockData.list[index];
      groupMockData.list.splice(index, 1);

      // 更新状态统计
      groupMockData.statusCount.all--;
      if (deletedItem.status === 'pending') {
        groupMockData.statusCount.pending--;
      } else if (deletedItem.status === 'active') {
        groupMockData.statusCount.active--;
      } else if (deletedItem.status === 'ended') {
        groupMockData.statusCount.ended--;
      }

      return {
        error: 0,
        msg: '删除成功',
        data: null,
      };
    } else {
      return {
        error: 1,
        msg: '记录不存在',
        data: null,
      };
    }
  },

  // 批量删除
  batchDelete: (ids) => {
    let deletedCount = 0;
    ids.forEach((id) => {
      const index = groupMockData.list.findIndex((item) => item.id == id);
      if (index !== -1) {
        const deletedItem = groupMockData.list[index];
        groupMockData.list.splice(index, 1);
        deletedCount++;

        // 更新状态统计
        groupMockData.statusCount.all--;
        if (deletedItem.status === 'pending') {
          groupMockData.statusCount.pending--;
        } else if (deletedItem.status === 'active') {
          groupMockData.statusCount.active--;
        } else if (deletedItem.status === 'ended') {
          groupMockData.statusCount.ended--;
        }
      }
    });

    return {
      error: 0,
      msg: `成功删除${deletedCount}条记录`,
      data: null,
    };
  },

  // 更新活动商品
  updateGoods: (activityId, goodsIds, goodsList) => {
    const index = groupMockData.list.findIndex((item) => item.id == activityId);
    if (index !== -1) {
      const updatedItem = {
        ...groupMockData.list[index],
        goods_ids: goodsIds,
        goods_list: goodsList,
        goods_count: goodsList.length,
        updated_at: new Date().toISOString().slice(0, 19).replace('T', ' '),
      };

      groupMockData.list[index] = updatedItem;

      return {
        error: 0,
        msg: `成功设置${goodsList.length}个商品`,
        data: updatedItem,
      };
    } else {
      return {
        error: 1,
        msg: '活动不存在',
        data: null,
      };
    }
  },
};
