import request from '@/utils/request'
import parseTime from '@/utils/index.js'

export function GetDataByName(data) {
  return request({
    url: '/authdata/GetDataByName',
    method: 'post',
    data
  })
}
export function GetReportform(data) {
  return request({
    url: '/authdata/GetReportform',
    method: 'post',
    data
  })
}
export function requestbyname(data, requestname) {
  return request({
    url: '/authdata/' + requestname,
    method: 'post',
    data
  })
}

export function GetDataByNameXlsx(data) {
  return request({
    url: '/authdata/GetDataByName',
    method: 'post',
    data,
    responseType: 'blob'
  })
}

export function GetDataByNames(data) {
  return request({
    url: '/authdata/GetDataByNames',
    method: 'post',
    data
  })
}

export function PostDataByName(data) {
  return request({
    url: '/authdata/PostDataByName',
    method: 'post',
    data
  })
}
export function GetUpkeepPlan(data) {
  return request({
    url: '/authdata/GetUpkeepPlan',
    method: 'post',
    data
  })
}
export function GetAccount(data) {
  return request({
    url: '/authdata/GetAccount',
    method: 'post',
    timeout: 600000,
    data
  })
}

export function PostDataByNames(data) {
  return request({
    url: '/authdata/PostDataByNames',
    method: 'post',
    data
  })
}

export function ExecDataByConfig(data) {
  return request({
    url: '/authdata/ExecDataByConfig',
    method: 'post',
    data
  })
}

export function removeimage(data) {
  return request({
    url: '/authdata/removeimage',
    method: 'post',
    data
  })
}

export function getRecuData(data) {
  return request({
    url: '/authdata/GetRecuDataByName',
    method: 'post',
    data
  })
}

export function failproccess(data, notify) {
  if (data.data.includes('Duplicate')) {
    notify({
      title: '失败',
      message: '不可以录入重复数据',
      type: 'error',
      duration: 2000
    })
  } else {
    notify({
      title: '失败',
      message: '数据存在错误,请校验好重新录入,不可以录入数据',
      type: 'error',
      duration: 2000
    })
  }
}

export function UpdateDataRelation(data) {
  return request({
    url: '/authdata/UpdateDataRelation',
    method: 'post',
    data
  })
}

export function transData(a, idStr, pidStr, chindrenStr) {
  var r = []; var hash = {}; var id = idStr; var pid = pidStr; var children = chindrenStr; var i = 0; var j = 0; var len = a.length
  for (; i < len; i++) {
    hash[a[i][id]] = a[i]
  }
  for (; j < len; j++) {
    var aVal = a[j]; var hashVP = hash[aVal[pid]]
    if (hashVP) {
      !hashVP[children] && (hashVP[children] = [])
      hashVP[children].push(aVal)
    } else {
      r.push(aVal)
    }
  }
  return r
}

export function checkButtons(buttonsList, PermissionButtons) {
  // console.log(PermissionButtons)
  for (let i = 0; i < buttonsList.length; i++) {
    if (buttonsList[i].path === PermissionButtons && buttonsList[i].menu_id && buttonsList[i].path) { // path不为空且menu_id不为空时返回true
      return true
    }
  }
  return false
}

export function formatJson(filterVal, jsonData) {
  return jsonData.map(v =>
    filterVal.map(j => {
      if (j === 'timestamp') {
        return parseTime(v[j])
      } else {
        return v[j]
      }
    })
  )
}

export function DownloadExcel(data, filename) {
  const content = data
  const blob = new Blob([content])
  const fileName = filename + '.xlsx'
  if ('download' in document.createElement('a')) { // 非IE下载
    const elink = document.createElement('a')
    elink.download = fileName
    elink.style.display = 'none'
    elink.href = URL.createObjectURL(blob)
    document.body.appendChild(elink)
    elink.click()
    URL.revokeObjectURL(elink.href) // 释放URL 对象
    document.body.removeChild(elink)
  } else { // IE10+下载
    navigator.msSaveBlob(blob, fileName)
  }
}