import type { Pagination } from '@/types/global';
import axios from 'axios';
import qs from 'query-string';
import type { CommonPageResult, CommonResult } from './common';
import { isUndefined } from '@/utils/is';

export interface ShopOrderRecord {
  id: number;
  orderNo: string;
  tMallOrderNo: number;
  created: Date | null;
  buyerNick: string;
  buyerRate: number;
  status: string;
  payment: string;
  totalFee: string;
  payTime: Date | null;
  tradeStatus: string;
  endTime: Date | null;
  buyerMemo: string;
  alipayNo: string;
  buyerMessage: string;
}

export interface TMallOrderParams extends Pagination, Partial<ShopOrderRecord> {
  dateRange?: Date[];
  startDate?: Date;
  endDate?: Date;
}

export function queryTMallShopOrderList(params: TMallOrderParams) {
  if (!isUndefined(params.dateRange)) {
    switch (params.dateRange.length) {
      case 2: {
        params.startDate = params.dateRange[0];
        params.endDate = params.dateRange[1];
        break;
      }
      case 1: {
        params.startDate = params.dateRange[0];
        break;
      }
    }
  }
  delete params.dateRange;
  return axios.get<CommonPageResult<ShopOrderRecord>>(
    '/api/recharge/tMallGame/shopOrder/getList',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
}

export enum RechargeTMallShopOrderStatus {
  ReceiveCallback = 'receive_callback', // 接收回调
  Paid = 'paid', // 主动请求天猫订单
  DeliverySucceed = 'delivery_succeed', // 发货成功
  DeliveryFailed = 'delivery_failed', // 发货失败
  ConfirmOrder = 'confirm_order', // 确认收货
  BindOrderFail = 'bind_order_fail', // 绑定订单失败
  BindOrderSucceed = 'bind_order_succeed', // 绑定订单成功
  EvaluationGood = 'evaluation_good', // 用户好评
  EvaluationBad = 'evaluation_bad', // 用户差评
  EvaluatedSucceed = 'evaluated_succeed', // 好评成功
  EvaluatedFailed = 'evaluated_failed' // 好评失败
}

export interface RechargeTMallShopOrderHistory {
  id: number;
  shopId: number;
  remark: string;
  status: RechargeTMallShopOrderStatus;
  originalData: string;
  createdAt?: Date | null; // 使用 Date 类型
}

export function queryTMallOrderOrderHistory(params: { shopId: number }) {
  return axios.get<CommonResult<RechargeTMallShopOrderHistory>>(
    '/api/recharge/tMallGame/shopOrder/getShopHistoryList',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
}
