import request from '@/utils/request'
import { getCommunityId } from '@/api/community/communityApi'

// 获取退费申请单列表
export function listReturnPayFees(params) {
  return new Promise((resolve, reject) => {
    // 确保社区ID存在
    if (!params.communityId) {
      params.communityId = getCommunityId()
    }
    
    request({
      url: '/returnPayFee.listReturnPayFees',
      method: 'get',
      params
    }).then(response => {
      const res = response.data
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
}

// 更新退费申请单状态
export function updateReturnPayFee(data) {
  return new Promise((resolve, reject) => {
    // 确保社区ID存在
    if (!data.communityId) {
      data.communityId = getCommunityId()
    }
    
    request({
      url: '/returnPayFee.updateReturnPayFee',
      method: 'post',
      data,
      headers: {
        'Content-Type': 'application/json'
      }
    }).then(response => {
      const res = response.data
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
}

// 尝试退款
export function tryRefundMoney(data) {
  return new Promise((resolve, reject) => {
    // 确保社区ID存在
    if (!data.communityId) {
      data.communityId = getCommunityId()
    }
    
    request({
      url: '/returnPayFee.tryRefundMoney',
      method: 'post',
      data,
      headers: {
        'Content-Type': 'application/json'
      }
    }).then(response => {
      const res = response.data
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
}

// 获取字典数据
export function getDict(dictType, state) {
  return new Promise((resolve, reject) => {
    request({
      url: '/dict.getDict',
      method: 'get',
      params: {
        dictType,
        state,
        communityId: getCommunityId()
      }
    }).then(response => {
      const res = response.data
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
}