import type { Pagination } from '@/types/global';
import qs from 'query-string';
import type { CommonResult } from './common';
import axios from 'axios';
import type {
  KamiApiCardInfoJdV1JDConfigGetRes,
  KamiApiCardInfoJdV1JDConfigSetReq,
  KamiApiCardInfoJdV1ListRes
} from './generated';

export interface JdCardOrderRecord {
  /**
   * @description
   */
  id: number;

  /**
   * @description
   */
  orderNo: string;

  /**
   * @description
   */
  merchantId: string;

  /**
   * @description
   */
  attach: string;

  /**
   * @description
   */
  cookie: string;

  /**
   * @description
   */
  accountId: number;

  /**
   * @description
   */
  giftCardPwd: string;

  /**
   * @description 回调
   */
  notifyUrl: string;

  notifyStatus: number;

  /**
   * @description
   */
  amount: number;

  /**
   * @description 备注
   */
  remark: string;

  /**
   * @description 1.兑换成功 2.兑换失败
   */
  status: number;

  /**
   * @description
   */
  createdAt?: Date;

  /**
   * @description
   */
  updatedAt?: Date;

  /**
   * @description
   */
  deletedAt?: Date;
}

export interface JdCardOrderParams
  extends Partial<JdCardOrderRecord>,
    Pagination {
  accountNickName?: string;
  dateRange?: Date[];
}

// 获取充值列表
export function queryJdCardOrderList(params: JdCardOrderParams) {
  // 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;
  //     }
  //   }
  // }
  // if (isArray(params.dateRange)) {
  //   params.dateRange.forEach(element => {
  //     params['dateRange[]'] = element;
  //   });
  // }
  // delete params.dateRange;
  return axios.get<KamiApiCardInfoJdV1ListRes>(
    '/api/cardInfo/JDCard/order/list',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj, { arrayFormat: 'bracket' });
      }
    }
  );
}

export interface JDOrderHistoryList {
  id: number;
  orderNo: string;
  accountName: string;
  accountId: string;
  accountCookie: string;
  operationStatus: number;
  responseRawData: string;
  amount: number; // TypeScript 默认没有浮点数类型，使用 number 表示
  remark: string;
  createdAt: string; // 使用 ? 表示该字段是可选的
  updatedAt: string;
  deletedAt?: string;
}

// 获取充值操作记录
export function queryJDOrderHistoryList(params: { orderNo: string }) {
  return axios.get<CommonResult<JDOrderHistoryList>>(
    '/api/cardInfo/JDCard/order/getHistoryList',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
}

export function getJDConfig() {
  return axios.get<KamiApiCardInfoJdV1JDConfigGetRes>(
    '/api/cardInfo/JDCard/config/get'
  );
}

export function updateJDConfig(data: KamiApiCardInfoJdV1JDConfigSetReq) {
  return axios.post('/api/cardInfo/JDCard/config/set', data);
}
