import HttpKit from '@/utils/http-kit'

export default {
  /**
   * 功能描述：申请退课
   */
  refund(form) {
    return HttpKit.post(`/admin/order/refund/refund`, form).then(
      res => res.data
    )
  },
  /**
   * 功能描述：退课查询
   * @param form
   * @returns {Function | any | Q.Promise<any> | PromiseLike<any>}
   */
  page(form) {
    return HttpKit.post(`/admin/order/refund/page`, form).then(
      res => res.data
    )
  },
  /**
   * 功能描述：审核退课
   * @param form
   * @returns {Function | any | Q.Promise<any> | PromiseLike<any>}
   */
  auditRefund(form) {
    return HttpKit.post(`/admin/order/refund/audit-refund`, form).then(
      res => res.data
    )
  },
  /**
   * 功能描述：退课导出
   * @param form
   * @returns {Function | any | Q.Promise<any> | PromiseLike<any>}
   */
  exportExcel(form) {
    return HttpKit.post(`/admin/order/refund/export-excel`, form, { responseType: 'blob' }).then(res => {
      if (!res) return

      const filename = res.headers['content-disposition'] ? res.headers['content-disposition'].split(';')[1].split('filename=')[1] : `${form.schoolYear}-${form.semester}-${form.schoolId}.xlsx`
      const blob = res.data
      if (typeof window.navigator.msSaveBlob !== 'undefined') {
        // IE workaround for "HTML7007: One or more blob URLs were
        // revoked by closing the blob for which they were created.
        // These URLs will no longer resolve as the data backing
        // the URL has been freed."
        window.navigator.msSaveBlob(blob, filename)
      } else {
        const blobURL = window.URL.createObjectURL(blob)
        const tempLink = document.createElement('a')
        tempLink.style.display = 'none'
        tempLink.href = blobURL
        tempLink.setAttribute('download', filename)

        // Safari thinks _blank anchor are pop ups. We only want to set _blank
        // target if the browser does not support the HTML5 download attribute.
        // This allows you to download files in desktop safari if pop up blocking
        // is enabled.
        if (typeof tempLink.download === 'undefined') {
          tempLink.setAttribute('target', '_blank')
        }

        document.body.appendChild(tempLink)
        tempLink.click()
        document.body.removeChild(tempLink)
        window.URL.revokeObjectURL(blobURL)
      }
    })
  }
}
