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

export interface AppleCardRechargeOrderRecord {
  id: number;
  orderNo: string;
  actualAmount: number; // 实际充值金额
  cardAmount: number;
  balance: number; // 账户余额
  accountName: string;
  attach: string;
  notifyStatus: number;
  merchantId: string;
  createdAt: string; // 订单创建时间，发起充值时间
}

export interface AppleCardRechargeOrderParams
  extends Partial<AppleCardRechargeOrderRecord>,
    Pagination {
  dateRange?: Date[];
  startDate?: Date | null;
  endDate?: Date | null;
}

export interface AppleCardOperationHistoryList {
  id: number;
  rechargeId: string;
  account: string;
  operation: string; // 操作
  remark: string;
  createdAt: string; // 操作时间
}

// 获取充值列表
export function queryAppleCardRechargeOrderList(
  params: AppleCardRechargeOrderParams
) {
  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<AppleCardRechargeOrderRecord>>(
    '/api/cardInfo/appleCard/rechargeOrder/list',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
}

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

// 手动回调订单
export function callBackOrderManualAPI(data: {
  orderNo?: string;
  rechargeId?: string;
}) {
  return axios.post(
    '/api/cardInfo/appleCard/rechargeOrder/callbackByManual',
    data
  );
}

export function modityActualAmountAPI(data: {
  orderNo: string;
  actualAmount: number;
  totpCode: string;
}) {
  return axios.post(
    '/api/cardInfo/appleCard/rechargeOrder/modifyActualAmount',
    data
  );
}

export function modifyStatusToSucceedAPI(data: { orderNo: string }) {
  return axios.post('/cardInfo/appleCard/rechargeOrder/setOrderSucceed', data);
}

export async function downloadDataListApi(params: { orderNo: string }) {
  const response = await axios.get<Blob>(
    '/api/cardInfo/appleCard/rechargeOrder/download',
    {
      responseType: 'blob',
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
  handleDownLoadFile(response.data, 'apple.xlsx');
}

// 重置订单状态
export function resetOrderStatusAPI(data: { orderNo: string }) {
  return axios.post('/api/cardInfo/appleCard/rechargeOrder/resetStatus', data);
}
