import type { Order } from '@/pages/Order/data';

// 模拟订单数据
const orders: Order[] = [
  {
    id: '1',
    orderNo: 'ORD202402140001',
    customerName: '张三',
    customerPhone: '13800138001',
    customerEmail: 'zhangsan@example.com',
    items: [
      {
        id: '1-1',
        orderId: '1',
        productName: 'iPhone 15 Pro Max',
        productImage: 'https://via.placeholder.com/80',
        quantity: 1,
        price: 9999,
        subtotal: 9999,
      },
      {
        id: '1-2',
        orderId: '1',
        productName: 'AirPods Pro',
        productImage: 'https://via.placeholder.com/80',
        quantity: 1,
        price: 1899,
        subtotal: 1899,
      },
    ],
    totalAmount: 11898,
    status: 'completed',
    paymentMethod: 'wechat',
    paymentTime: '2024-02-14 10:30:00',
    shippingAddress: '北京市朝阳区建国路88号',
    shippingCompany: '顺丰速运',
    shippingNo: 'SF1234567890',
    shippedAt: '2024-02-14 14:00:00',
    completedAt: '2024-02-15 16:30:00',
    createdAt: '2024-02-14 10:00:00',
    updatedAt: '2024-02-15 16:30:00',
  },
  {
    id: '2',
    orderNo: 'ORD202402140002',
    customerName: '李四',
    customerPhone: '13800138002',
    items: [
      {
        id: '2-1',
        orderId: '2',
        productName: 'MacBook Pro 14',
        productImage: 'https://via.placeholder.com/80',
        quantity: 1,
        price: 14999,
        subtotal: 14999,
      },
    ],
    totalAmount: 14999,
    status: 'shipped',
    paymentMethod: 'alipay',
    paymentTime: '2024-02-14 11:00:00',
    shippingAddress: '上海市浦东新区陆家嘴环路100号',
    shippingCompany: '京东物流',
    shippingNo: 'JD0987654321',
    shippedAt: '2024-02-14 15:30:00',
    createdAt: '2024-02-14 10:45:00',
    updatedAt: '2024-02-14 15:30:00',
  },
  {
    id: '3',
    orderNo: 'ORD202402140003',
    customerName: '王五',
    customerPhone: '13800138003',
    customerEmail: 'wangwu@example.com',
    items: [
      {
        id: '3-1',
        orderId: '3',
        productName: 'iPad Pro 12.9',
        productImage: 'https://via.placeholder.com/80',
        quantity: 2,
        price: 8499,
        subtotal: 16998,
      },
    ],
    totalAmount: 16998,
    status: 'paid',
    paymentMethod: 'bank',
    paymentTime: '2024-02-14 13:00:00',
    shippingAddress: '广州市天河区天河路385号',
    createdAt: '2024-02-14 12:30:00',
    updatedAt: '2024-02-14 13:00:00',
  },
  {
    id: '4',
    orderNo: 'ORD202402140004',
    customerName: '赵六',
    customerPhone: '13800138004',
    items: [
      {
        id: '4-1',
        orderId: '4',
        productName: 'Apple Watch Ultra',
        productImage: 'https://via.placeholder.com/80',
        quantity: 1,
        price: 5999,
        subtotal: 5999,
      },
    ],
    totalAmount: 5999,
    status: 'pending',
    paymentMethod: 'wechat',
    createdAt: '2024-02-14 14:00:00',
    updatedAt: '2024-02-14 14:00:00',
  },
  {
    id: '5',
    orderNo: 'ORD202402140005',
    customerName: '孙七',
    customerPhone: '13800138005',
    customerEmail: 'sunqi@example.com',
    items: [
      {
        id: '5-1',
        orderId: '5',
        productName: 'AirPods Max',
        productImage: 'https://via.placeholder.com/80',
        quantity: 1,
        price: 4399,
        subtotal: 4399,
      },
    ],
    totalAmount: 4399,
    status: 'cancelled',
    paymentMethod: 'alipay',
    cancelledAt: '2024-02-14 15:00:00',
    cancelReason: '不想买了',
    createdAt: '2024-02-14 09:00:00',
    updatedAt: '2024-02-14 15:00:00',
  },
];

export default {
  // 查询订单列表
  'GET /api/orders': (req: any, res: any) => {
    const { current = 1, pageSize = 10, orderNo, customerName, status, paymentMethod } = req.query;
    
    let filteredOrders = [...orders];
    
    // 按订单号筛选
    if (orderNo) {
      filteredOrders = filteredOrders.filter((order) =>
        order.orderNo.includes(orderNo),
      );
    }
    
    // 按客户姓名筛选
    if (customerName) {
      filteredOrders = filteredOrders.filter((order) =>
        order.customerName.includes(customerName),
      );
    }
    
    // 按订单状态筛选
    if (status) {
      filteredOrders = filteredOrders.filter((order) => order.status === status);
    }
    
    // 按支付方式筛选
    if (paymentMethod) {
      filteredOrders = filteredOrders.filter(
        (order) => order.paymentMethod === paymentMethod,
      );
    }
    
    // 分页
    const start = (current - 1) * pageSize;
    const end = start + pageSize;
    const paginatedOrders = filteredOrders.slice(start, end);
    
    res.send({
      success: true,
      data: paginatedOrders,
      total: filteredOrders.length,
    });
  },
  
  // 查询订单详情
  'GET /api/orders/:id': (req: any, res: any) => {
    const { id } = req.params;
    const order = orders.find((item) => item.id === id);
    
    if (order) {
      res.send({
        success: true,
        data: order,
      });
    } else {
      res.status(404).send({
        success: false,
        message: '订单不存在',
      });
    }
  },
  
  // 订单发货
  'POST /api/orders/:id/ship': (req: any, res: any) => {
    const { id } = req.params;
    const order = orders.find((item) => item.id === id);
    
    if (order) {
      order.status = 'shipped';
      order.shippingCompany = req.body.shippingCompany;
      order.shippingNo = req.body.shippingNo;
      order.shippedAt = new Date().toISOString();
      order.updatedAt = new Date().toISOString();
      
      res.send({
        success: true,
        message: '发货成功',
      });
    } else {
      res.status(404).send({
        success: false,
        message: '订单不存在',
      });
    }
  },
  
  // 取消订单
  'POST /api/orders/:id/cancel': (req: any, res: any) => {
    const { id } = req.params;
    const order = orders.find((item) => item.id === id);
    
    if (order) {
      order.status = 'cancelled';
      order.cancelReason = req.body.description || req.body.reason;
      order.cancelledAt = new Date().toISOString();
      order.updatedAt = new Date().toISOString();
      
      res.send({
        success: true,
        message: '订单已取消',
      });
    } else {
      res.status(404).send({
        success: false,
        message: '订单不存在',
      });
    }
  },
};
