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

export function postJson(url,data) {
  return request({
    url: process.env.VUE_APP_BASE_API + url,
    method: 'post',
    timeout: 600000,
    data
  })
}


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



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


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

export function GetMaintain(data) {
  return request({
    url: '/authdata/maintain',
    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 EasSync(data) {
  return request({
    url: '/authdata/EasSync',
    method: 'post',
    data
  })
}


export function SapTrans(data) {
  return request({
    url: '/authdata/sap/transfer/send',
    method: 'post',
    data
  })
} 

export function SapQuit(data) {
  return request({
    url: '/authdata/sap/quitproof/send',
    method: 'post',
    data
  })
} 

export function SapRef(data) {
  return request({
    url: '/authdata/sap/refundproof/send',
    method: 'post',
    data
  })
} 

export function SapUse(data) {
  return request({
    url: '/authdata/sap/useproof/send',
    method: 'post',
    data
  })
} 


export function SapLaid(data) {
  return request({
    url: '/authdata/sap/laidproof/send',
    method: 'post',
    data
  })
} 


export function SapOrder(data) {
  return request({
    url: '/authdata/sap/order/send',
    method: 'post',
    data
  })
} 


export function SrmOrder(data) {
  return request({
    url: '/authdata/srm/order/send',
    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: 6000000,
    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 getJson(url,data) {
  return request({
    url: process.env.VUE_APP_BASE_API + url + data,
    method: 'get'
  })
}
export function getRecuData(data) {
  return request({
    url: '/authdata/GetRecuDataByName',
    method: 'post',
    data
  })
}

export function downloadmailreport(data) {
  return request({
    url: '/authdata/downloadmailreport',
    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 Equipment(data) {
  return request({
    url: '/data/Equipment',
    method: 'post',
    timeout: 6000000,
    data
  })
}
export function StandardPart(data) {
  return request({
    url: '/data/StandardPart',
    method: 'post',
    data
  })
}
export function PastureDepartment(data) {
  return request({
    url: '/data/PastureDepartment',
    method: 'post',
    data
  })
}
export function updateAndInster(data) {
  return request({
    url: '/data/updateAndInster',
    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)
  }
}
// 用于获取本月或指定月份的最后一天
export function getMonthFinalDay(year,month){
	var day='';
	if(year==null || year==undefined || year==''){
		year = new Date().getFullYear();
	}
	if(month==null ||  month==undefined || month==''){
		month = new Date().getMonth()+1;
	}
	day = new Date(new Date(year,month).setDate(0)).getDate();
	return year+"-"+month+"-"+day;
}