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

export interface jdCardAddRecord {
  name: string;
  cookie: string;
  status: number;
  maxAmountLimit: number;
  wsKey?: string;
  remark?: string;
}

export interface JdCardUpdateRecord
  extends jdCardAddRecord,
    CommonStrIdParams {}

export interface JdCardRecord extends JdCardUpdateRecord {
  status: 0 | 1 | 2 | 3; // 账号状态
  nickname: string;
}

export interface JDCardParams
  extends Partial<JdCardRecord>,
    Partial<CommonStrIdParams>,
    Pagination {}

export function addJDCard(data: jdCardAddRecord) {
  return axios.post('/api/cardInfo/JDCard/account/create', data);
}

export function updateJDCard(data: JdCardUpdateRecord) {
  return axios.put('/api/cardInfo/JDCard/account/update', data);
}

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

export function updateJDCardStatus(data: { id: string; status: number }) {
  return axios.put('/api/cardInfo/JDCard/account/updateStatus', data);
}

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

export interface JDAccountWalletParams extends Pagination {
  accountId?: string;
}

export interface JDAccountListItem {
  id: number;
  accountId: string;
  cookie: string;
  orderNo: number;
  amount: number; // 在 TypeScript 中，float 类型通常用 number 表示
  remark: string;
  operationStatus: string;
  createdAt?: Date; // 使用了可选属性和 Date 对象来对应 *gtime.Time
  updatedAt?: Date; // 假设 gtime.Time 类似于 JavaScript 的 Date
  deletedAt?: Date; // 如果这些字段可以是 null，则需要加上 ?
}

// 获取账号名歘
export function queryJDAccountWalletList(params: JDAccountWalletParams) {
  return axios.get<CommonPageResult<JDAccountListItem>>(
    '/api/cardInfo/JDCard/account/getWalletList',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
}

// 刷新账号当前状态
export function refreshJDCardStatus(data: CommonStrIdParams) {
  return axios.post('/api/cardInfo/JDCard/account/refreshStatus', data);
}

// 下载苹果cookie数据
export async function downloadJDCardData() {
  const response = await axios.get(
    '/api/cardInfo/JDCard/account/downloadTemplate',
    {
      responseType: 'blob'
    }
  );
  handleDownLoadFile(response.data, '苹果账号批量导入模板.xlsx');
}

export function batchAdd(data: {
  list: Array<KamiApiCardInfoJdV1JDAccountCookieBatchInfo>;
}) {
  return axios.post('/api/cardInfo/JDCard/account/batchAdd', data);
}

export async function downloadDataList() {
  const response = await axios.get('/api/cardInfo/JDCard/account/download', {
    responseType: 'blob'
  });
  handleDownLoadFile(response.data, '苹果账号下载数据.xlsx');
}
