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

// 设置是否开启偷卡
export function setAppleCardStealSettings(data: { stealStatus: number }) {
  return axios.post('/api/cardInfo/appleCard/steal/setStatus', data);
}

// 获取是否开启偷卡
export function getAppleCardStealSettings() {
  return axios.get<{ stealStatus: number }>(
    '/api/cardInfo/appleCard/steal/getStatus'
  );
}

// 设置单个状态是否启用
export function setAppleCardStealStatus(data: { id: number; status: number }) {
  return axios.put('/api/cardInfo/appleCard/steal/setRuleStatus', data);
}

export interface AppleCardStealAdd {
  name: string;
  targetUserId: string;
  storageUserId: string;
  amount: number;
  targetAmount: number;
  intervalTime: number;
  status: number;
}

export interface AppleCardStealEdit extends AppleCardStealAdd {
  id: number;
}

export interface AppleCardStealList extends AppleCardStealEdit {
  stealTotalAmount: number;
  createdAt: string;
  updatedAt: string;
}

export type AppleCardStealListParams = Pagination;

// 获取偷卡列表
export function getAppleCardStealList(params: AppleCardStealListParams) {
  return axios.get<CommonPageResult<AppleCardStealList>>(
    '/api/cardInfo/appleCard/steal/getRuleList',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
}

// 添加偷卡规则
export function addAppleCardStealRule(data: AppleCardStealAdd) {
  return axios.post('/api/cardInfo/appleCard/steal/addRule', data);
}

// 编辑偷卡规则
export function editAppleCardStealRule(data: AppleCardStealEdit) {
  return axios.put('/api/cardInfo/appleCard/steal/updateRule', data);
}

// 删除偷卡规则
export function deleteAppleCardStealRule(params: { id: number }) {
  return axios.delete('/api/cardInfo/appleCard/steal/deleteRule', { params });
}
