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 GETNurJudge(data) {
  return request({
    url: '/authdata/GETNurJudge',
    method: 'post',
    data
  })
}

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


export function ExeSqlJiade(data) {
  return request({
    url: '/authdata/exesql',
    method: 'post',
    data
  })
}
export function dashboardListJiade(data) {
  return request({
    url: '/authdata/Dashboards',
    method: 'post',
    data
  })
}

export function updateDashboardJiade(data) {
  return request({
    url: '/authdata/Updatedashboard',
    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 GetArrList(data) {
  return request({
    url: '/authdata/GetArrList',
    method: 'post',
    data
  })
}
export function spillage(data) {
  return request({
    url: '/authdata/spillage',
    method: 'post',
    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 getDorm(data) {
  return request({
    url: '/authdata/getDorm',
    method: 'post',
    timeout: 6000000,
    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 Autogeneration(data) {
  return request({
    url: '/authdata/autogeneration',
    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
    if (buttonsList[i].path === PermissionButtons && buttonsList[i].editbutton === 1) { // 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)
  }
}
// 取小数
export function formatNum(f, digit) {
  var m = Math.pow(10, digit);
  return parseInt(f * m, 10) / m;
}

export function compareSort(property){
  return function(a,b){
    var value1 = a[property];
    var value2 = b[property];
    return value1 - value2;
  }
}
