import axios from 'axios';
import qs from 'query-string';
import type { CommonPageResult, CommonStrIdParams } from './common';
import { handleDownLoadFile } from './utils';
import type { Pagination } from '@/types/global';
import type { KamiApiCardInfoJdV1JDAccountCookieCheckRes } from './generated';

export interface AppleCardAddRecord {
  account: string;
  password: string;
}

export interface AppleCardRecord extends AppleCardAddRecord {
  status: 0 | 1 | 2 | 3; // 账号状态
  todayRechargeAmount: number;
  todayRechargeCount: number;
  todayRechargeDatetime: string;
  balance: number;
  uploadUser: {
    id: string;
    username: string;
    userNickname: string;
  };
}

export interface AppleCardParams
  extends Partial<AppleCardRecord>,
    Partial<CommonStrIdParams>,
    Pagination {}

export interface AppleCardResRecord
  extends AppleCardRecord,
    CommonStrIdParams {}

export interface AppleCardRecordWithID
  extends AppleCardAddRecord,
    CommonStrIdParams {}

// 获取苹果账号列表
export function queryAppleCardList(params: AppleCardParams) {
  return axios.get<CommonPageResult<AppleCardResRecord>>(
    '/api/cardInfo/AppleCard/account/getList',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
}

export function addAppleCard(data: AppleCardAddRecord) {
  return axios.post('/api/cardInfo/AppleCard/account/create', data);
}

export function updateAppleCard(data: AppleCardRecordWithID) {
  return axios.put('/api/cardInfo/AppleCard/account/update', data);
}

export function deleteAppleCard(params: CommonStrIdParams) {
  return axios.delete('/api/cardInfo/AppleCard/account/delete', { params });
}

export function batchUploadAppleCardAPI(data: Blob) {
  // 上传xlsx
  return axios.post('/api/cardInfo/AppleCard/account/batchAdd', data, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

// 添加苹果卡
export async function downloadAppleAccountTemplteAPI() {
  const response = await axios.get<Blob>(
    '/api/cardInfo/AppleCard/account/downloadTemplate',
    {
      responseType: 'blob'
    }
  );
  handleDownLoadFile(response.data, '苹果账号批量导入模板.xlsx');
}

export interface AppleWalletParams extends Pagination {
  accountId?: string;
  accountName?: string;
}

export interface AppleWalletListItem {
  accountName: string;
  orderNo: string;
  balanceBeforeItunes: number;
  balanceBeforeAutoIncrement: number;
  balanceAfterItunes: number;
  balanceAfterAutoIncrement: number;
  amount: number;
  description: string;
  created: string;
}

export function queryAppleWalletList(params: AppleWalletParams) {
  return axios.get<CommonPageResult<AppleWalletListItem>>(
    '/api/cardInfo/AppleCard/account/getWalletList',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
}

export function continueOrSuspendRechargeAPI(data: { id: string }) {
  return axios.put(
    '/api/cardInfo/AppleCard/account/status/continueOrRestart',
    data
  );
}

export function detectCookie(data: { cookie: string }) {
  return axios.post<KamiApiCardInfoJdV1JDAccountCookieCheckRes>(
    '/api/cardInfo/JDCard/account/checkCookie',
    data
  );
}
