import axios from 'axios'
import store from '@/store'
import { get_func_type } from '@/utils/index'
// 默认token
axios.defaults.headers.common['Authorization'] = 'Bearer ' + store.getters.token

// 封装接口调用后模拟点击
export function click_dowload(data, res) {
  const blobUrl = window.URL.createObjectURL(res.data)
  const a = document.createElement('a')
  a.style.display = 'none'
  a.download = `${data.file_name}.xlsx`
  a.href = blobUrl
  a.click()
}
const request_server = axios.create(
  {
    baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 8000, // request timeout
    method: 'get',
    responseType: 'blob'
  }
)

// request interceptor
request_server.interceptors.request.use(
  config => {
    if (store.getters.token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['Authorization'] = 'Bearer ' + store.getters.token
    }
    // 添加func_type参数
    if (config.params) {
      if (Object.prototype.toString.call(config.data) === '[object FormData]') {
        if (config.params.get('func_type') === null) {
          config.params.append('func_type', get_func_type())
        }
      } else {
        config.params['func_type'] = get_func_type()
      }
    } else {
      const formData = new FormData()
      formData.append('func_type', get_func_type())
      config.params = formData
    }

    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)
// 库存信息总览导出
export function dowload_stock_data_info(data) {
  return request_server(
    {
      url: '/api/report/stock_data_info',
      params: data
    }
  )
}

// 试剂信息详情or入库信息查询 导出
export function dowload_drug_details_info(data) {
  return request_server(
    {
      url: '/api/report/drug_details_info',
      params: data
    }
  )
}

// 库存消耗 导出
export function dowload_stock_loss_info(data) {
  return request_server(
    {
      url: '/api/report/stock_loss_info',
      params: data
    }
  )
}

// 试剂用量消耗 导出
export function dowload_drug_use_expend(data) {
  return request_server(
    {
      url: '/api/report/drug_use_expend',
      params: data
    }
  )
}

// 人员用量消耗 导出
export function dowload_drug_user_use_expend(data) {
  return request_server(
    {
      url: '/api/report/drug_user_use_expend',
      params: data
    }
  )
}

// 使用频率 导出
export function dowload_use_frequency(data) {
  return request_server(
    {
      url: '/api/report/use_frequency',
      params: data
    }
  )
}

// 入库、领用、归还 导出
export function dowload_drug_log_type_info(data) {
  return request_server(
    {
      url: '/api/report/drug_log_type_info',
      params: data
    }
  )
}

// 环境监测_天 导出
export function dowload_day_monitor(data) {
  return request_server(
    {
      url: '/api/monitor/day_monitor',
      params: data
    }
  )
}

// 环境监测_时 导出
export function dowload_day_monitor_info(data) {
  return request_server(
    {
      url: '/api/monitor/day_monitor_info',
      params: data
    }
  )
}

// /api/report/report_home
export function dowload_putin_acceptance_record(data) {
  return request_server({
    url: '/api/report/putin_acceptance_record',
    data
  })
}
