import { AxiosResponse } from 'axios'

import {ContentType, HttpClient, RequestParams} from "/@/api/admin/http-client";
import {
  softwarePackageManagement_DtoResult,
  softwarePackageManagement_PageInput, softwarePackageManagement_TableData,ResultOutputPageOutputDownloadRecordDto,pageInputDownloadRecordDto
} from "/@/api/admin/deviceAuthorization/softwarePackageManagementDto";
import {oilSdkAuthorPageOutput, oilSdkTableModel} from "/@/api/admin/deviceAuthorization/oilSdkAuthorDto";

export class SoftwarePackageManagementApi<SecurityDataType = unknown> extends HttpClient<SecurityDataType>{

  timer = 500
  /**
   * No description
   *
   * @tags
   * @name getPageData
   * @summary 分页查询
   * @request GET:
   * @secure
   */

  getPageData = (data: softwarePackageManagement_PageInput, params: RequestParams = {}) =>
    this.request<softwarePackageManagement_TableData, any>({
      path: `/api/app/software/get-page`,
      method: 'POST',
      body: data,
      secure: true,
      type: ContentType.Json,
      format: 'json',
      ...params,
    })
  getPageData2 = (query: softwarePackageManagement_PageInput) =>{
    debugger
   var dd = this.request<AxiosResponse, any>({
      path: `/api/app/software/get-page`,
      method: 'POST',
      body: query,
      secure: true,
      type: ContentType.Json,
      format: 'json',
    })
    debugger
    return new Promise(resolve => {
      setTimeout(() => {
        resolve({
          success: true,
          code: 200,
          msg: '',
          data: {
            total: 100,
            list:[{
              guid: 1,
              date: '2020-11-22',
              softwareType: 0,
              equipmentType:0,
              softwareName: 'test',
              version: 'test',
              explain: 'test',
              state: 0,
              numberOfDownloads: 1234,
              remark: 'tast',
              releaseTime: '2020-10-21',
              uploadTime: '2020-10-21',
              downloadUrl:'upload/2023/10/27/653b8909-14d2-7870-0097-10007db02419.json',
              fileName:'22.json'
            },{
              guid: 2,
              date: '2020-10-22',
              softwareType: 1,
              equipmentType:1,
              softwareName: 'test',
              version: 'test',
              explain: 'test',
              state: 1,
              numberOfDownloads: 3214,
              remark: 'test',
              releaseTime: '2020-10-22',
              uploadTime: '2020-10-22',
              downloadUrl:'',
              fileName:'22.json'
            },{
              guid: 3,
              date: '2020-10-23',
              softwareType: 2,
              equipmentType:2,
              softwareName: 'test',
              version: 'test',
              explain: 'test',
              state: 2,
              numberOfDownloads: 4321,
              remark: 'tast',
              releaseTime: '2020-10-23',
              uploadTime: '2020-10-23',
              downloadUrl:'',
              fileName:'22.json'
            }]
          }
        })
      },this.timer)
    })
  }
    // this.request<softwarePackageManagement_DtoResult, any>({
    //   path:'/api/app/fueiling-sdk-auth/get-page',
    //   method: 'GET',
    //   secure: true,
    //   type: ContentType.Json,
    //   format: 'json',
    //   query:query,
    //   ...params
    // })

  /**
   * No description
   *
   * @tags
   * @name getPageData
   * @summary 分页查询
   * @request GET:
   * @secure
   */
 updateData = (data:softwarePackageManagement_TableData) => {
    return new Promise(resolve => {
      // 设置定时器
      setTimeout(() => {
        // 返回更新后的数据
        resolve({
          success: true,
          code: 200,
          msg: '',
          data: {}
        })
      },this.timer)
    })
  }

  uploadFile = (
    data: {
      /** @format binary */
      file: File
    },
    query?: {
      /**
       * 文件目录
       * @default ""
       */
      fileDirectory?: string
      /**
       * 文件重命名
       * @default true
       */
      fileReName?: boolean
    },
    params: RequestParams = {}
  ) =>
    this.request<softwarePackageManagement_DtoResult, any>({
      path: `/api/app/software/upload-file`,
      method: 'POST',
      query: query,
      body: data,
      secure: true,
      type: ContentType.FormData,
      format: 'json',
      ...params,
    })

  /**
   * No description
   *
   * @tags
   * @name getPageData
   * @summary 分页查询
   * @request GET:
   * @secure
   */
  addData = (data:softwarePackageManagement_TableData) =>
  this.request<AxiosResponse, any>({
    path: `/api/app/software/upload-file`,
    method: 'POST',
    body: data,
    secure: true,
    type: ContentType.Json,
    format: 'json',
  })

/**
   * No description
   *
   * @tags
   * @name getPageData
   * @summary 分页查询
   * @request GET:
   * @secure
   */
/* DownloadFile = (data:softwarePackageManagement_TableData) =>
this.request<AxiosResponse, any>({
  path: `/api/app/software/download-file`,
  method: 'GET',
  body: data,
  secure: true,
  type: ContentType.Json,
  format: 'json',
}) */
DownloadFile = async (data: softwarePackageManagement_TableData) => {
  try {
    const response = await this.request<AxiosResponse, any>({
      path: `/api/app/software/download-file?guid=`+data.guid,
      method: 'GET',
      body: data,
      secure: true,
      type: ContentType.Json,
      format: 'json',
      responseType: 'blob', // 添加这一行
    });
    const filename =  data.fileName?.toString();
    const blob = new Blob([response.data], { type: 'application/x-rar-compressed' });
    const link = document.createElement('a');
    debugger
    link.href = URL.createObjectURL(blob);
    link.download = filename; // 请替换为实际文件名
    link.click();
  } catch (error) {
    console.error('下载失败：', error);
  }
};
    getDownloadData = (data: pageInputDownloadRecordDto,params: RequestParams = {}) =>
    this.request<ResultOutputPageOutputDownloadRecordDto, any>({
      path: `/api/app/software/get-download-record`,
      method: 'POST',
      body: data,
      secure: true,
      type: ContentType.Json,
      format: 'json',
      ...params
    }).then((res) => {
      //console.log('getDownloadData result:', res);
      return res;
    })
}
