import {
  Message,
  Loading
} from 'element-ui'
import Vue from 'vue'
Vue.prototype.$bus = new Vue();
let loadingInstance

//封装
//全局实例
const bus = new Vue();
window.bus = bus;


import localStronge from "@/api/localData";

export const URL_PREFIX = process.env.NODE_ENV === 'development' ?
  ("http://" + localStronge.getServiceip().ip + ":" + localStronge.getServiceip().host) :
  'http://' + window.location.host;
export const message = (type, message, duration = 3000) => {
  bus.$message({
    type,
    message,
    duration
  })
}
export const clearObjValue = (obj) => {
  Object.keys(obj).forEach(key => obj[key] = '')
}
export const falseObjValue = (obj, value) => {
  Object.keys(obj).forEach(key => obj[key] = value)
}
export const loading = () => {
  loadingInstance = Loading.service({
    fullscreen: true,
    text: '拼命加载中,请耐心等待！',
    spinner: 'el-icon-loading'
  })
}
export const loadingClose = () => {
  loadingInstance.close()
}
export const getCurrentTime = (split) => {
  //获取当前时间并打印
  let time = "";
  if(split == '' || split == null){
    split = "/";
  }
  const yy = new Date().getFullYear();
  const mm = new Date().getMonth() + 1;
  const dd = new Date().getDate();
  const hh = new Date().getHours();
  const mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
  const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
  time = yy + split + mm + split + dd + ' ' + hh + ':' + mf + ":" + ss;
  return time
}

export const getCurrentTimes = () => {
  //获取当前时间并打印
  let time = "";
  const yy = new Date().getFullYear();
  const mm = new Date().getMonth() + 1;
  const dd = new Date().getDate();
  const hh = new Date().getHours();
  const mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
  const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
  time = yy + '-' + mm + '-' + dd + ' ' + hh + '-' + mf;
  return time
}

export const getGreeting = ()=>{
    let str = null;
    const now = new Date();
    const hour = now.getHours();

    if (hour >= 0 && hour < 12) {
        str = '上午好！';
    } else if (hour >= 12 && hour < 18) {
        str = '下午好！';
    } else {
        str = '晚上好！';
    }
    return str;
}
// 连接符
export const dateFormatT = dateArray => {
  if (!dateArray[0]) {
    return
  }
  const startTime = new Date(dateArray[0])
  const endTime = new Date(dateArray[1])
  const sy = startTime.getFullYear()
  const sm =
    startTime.getMonth() + 1 < 10 ?
    `0${startTime.getMonth() + 1}` :
    startTime.getMonth() + 1
  const sd =
    startTime.getDate() < 10 ?
    `0${startTime.getDate()}` :
    startTime.getDate()
  const sh =
    startTime.getHours() < 10 ?
    `0${startTime.getHours()}` :
    startTime.getHours()
  const smin =
    startTime.getMinutes() < 10 ?
    `0${startTime.getMinutes()}` :
    startTime.getMinutes()
  const sec =
    startTime.getSeconds() < 10 ?
    `0${startTime.getSeconds()}` :
    startTime.getSeconds()
  const ey = endTime.getFullYear()
  const em =
    endTime.getMonth() + 1 < 10 ?
    `0${endTime.getMonth() + 1}` :
    endTime.getMonth() + 1
  const ed =
    endTime.getDate() < 10 ? `0${endTime.getDate()}` : endTime.getDate()
  const eh =
    endTime.getHours() < 10 ? `0${endTime.getHours()}` : endTime.getHours()
  const emin =
    endTime.getMinutes() < 10 ?
    `0${endTime.getMinutes()}` :
    endTime.getMinutes()
  const eec =
    endTime.getSeconds() < 10 ?
    `0${endTime.getSeconds()}` :
    endTime.getSeconds()
  return {
    start: `${sy}-${sm}-${sd} ${sh}:${smin}:${sec}`,
    end: `${ey}-${em}-${ed} ${eh}:${emin}:${eec}`
  }
}

// 连接符
export const dateFormatTa = dateArray => {
  if (!dateArray[0]) {
    return
  }
  const startTime = new Date(dateArray[0])
  const endTime = new Date(dateArray[1])
  const sy = startTime.getFullYear()
  const sm =
    startTime.getMonth() + 1 < 10 ?
    `0${startTime.getMonth() + 1}` :
    startTime.getMonth() + 1
  const sd =
    startTime.getDate() < 10 ?
    `0${startTime.getDate()}` :
    startTime.getDate()
  const sh =
    startTime.getHours() < 10 ?
    `0${startTime.getHours()}` :
    startTime.getHours()
  const smin =
    startTime.getMinutes() < 10 ?
    `0${startTime.getMinutes()}` :
    startTime.getMinutes()
  const sec =
    startTime.getSeconds() < 10 ?
    `0${startTime.getSeconds()}` :
    startTime.getSeconds()
  const ey = endTime.getFullYear()
  const em =
    endTime.getMonth() + 1 < 10 ?
    `0${endTime.getMonth() + 1}` :
    endTime.getMonth() + 1
  const ed =
    endTime.getDate() < 10 ? `0${endTime.getDate()}` : endTime.getDate()
  const eh =
    endTime.getHours() < 10 ? `0${endTime.getHours()}` : endTime.getHours()
  const emin =
    endTime.getMinutes() < 10 ?
    `0${endTime.getMinutes()}` :
    endTime.getMinutes()
  const eec =
    endTime.getSeconds() < 10 ?
    `0${endTime.getSeconds()}` :
    endTime.getSeconds()
  return {
    start: `${sy}${sm}${sd}`,
    end: `${ey}${em}${ed}`
  }
}
// 连接符
export const dateFormatTs = dateArray => {
  if (!dateArray[0]) {
    return
  }
  const startTime = new Date(dateArray[0])
  const endTime = new Date(dateArray[1])
  const sy = startTime.getFullYear()
  const sm =
    startTime.getMonth() + 1 < 10 ?
    `0${startTime.getMonth() + 1}` :
    startTime.getMonth() + 1
  const sd =
    startTime.getDate() < 10 ?
    `0${startTime.getDate()}` :
    startTime.getDate()
  const sh =
    startTime.getHours() < 10 ?
    `0${startTime.getHours()}` :
    startTime.getHours()
  const smin =
    startTime.getMinutes() < 10 ?
    `0${startTime.getMinutes()}` :
    startTime.getMinutes()
  const sec =
    startTime.getSeconds() < 10 ?
    `0${startTime.getSeconds()}` :
    startTime.getSeconds()
  const ey = endTime.getFullYear()
  const em =
    endTime.getMonth() + 1 < 10 ?
    `0${endTime.getMonth() + 1}` :
    endTime.getMonth() + 1
  const ed =
    endTime.getDate() < 10 ? `0${endTime.getDate()}` : endTime.getDate()
  const eh =
    endTime.getHours() < 10 ? `0${endTime.getHours()}` : endTime.getHours()
  const emin =
    endTime.getMinutes() < 10 ?
    `0${endTime.getMinutes()}` :
    endTime.getMinutes()
  const eec =
    endTime.getSeconds() < 10 ?
    `0${endTime.getSeconds()}` :
    endTime.getSeconds()
  return {
    start: `${sy}-${sm}-${sd}`,
    end: `${ey}-${em}-${ed}`
  }
}
//单个年度//
export const dateFormatYear = timestamp => {
  var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  const Y = date.getFullYear();
  return Y;
}
//单个日期//
export const dateFormat = timestamp => {
  var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  const Y = date.getFullYear() + '-';
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  const D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  return Y + M + D;
}

// 下载文件流pdf
export const downlaod = (data, name) => {
  const dom = document.createElement('a') // 隐藏的a标签，href为输出流
  const blob = new Blob([data], {
    type: 'application/pdf'
  }) // 创建一个类文件对象：Blob对象表示一个不可变的、原始数据的类文件对象
  const url = window.URL.createObjectURL(blob) // 生成一个Blob对象
  dom.style.display = 'none'
  dom.href = url
  document.body.appendChild(dom)
  dom.setAttribute('download', name) // 指示浏览器下载url,而不是导航到它
  dom.click()
  document.body.removeChild(dom)
  // 手动释放创建的URL对象所占内存
  URL.revokeObjectURL(url)
}
// 下载文件流pdf
export const downlaodzip = (data, name) => {
  const dom = document.createElement('a') // 隐藏的a标签，href为输出流
  const blob = new Blob([data], {
    type: 'application/zip'
  }) // 创建一个类文件对象：Blob对象表示一个不可变的、原始数据的类文件对象
  const url = window.URL.createObjectURL(blob) // 生成一个Blob对象
  dom.style.display = 'none'
  dom.href = url
  document.body.appendChild(dom)
  dom.setAttribute('download', name) // 指示浏览器下载url,而不是导航到它
  dom.click()
  document.body.removeChild(dom)
  // 手动释放创建的URL对象所占内存
  URL.revokeObjectURL(url)
}
// 下载文件流xls
export const downlaodxls = (data, name) => {
  const dom = document.createElement('a') // 隐藏的a标签，href为输出流
  const blob = new Blob([data], {
    type: 'application/vnd.ms-excel'
  }) // 创建一个类文件对象：Blob对象表示一个不可变的、原始数据的类文件对象
  const url = window.URL.createObjectURL(blob) // 生成一个Blob对象
  dom.style.display = 'none'
  dom.href = url
  document.body.appendChild(dom)
  dom.setAttribute('download', name) // 指示浏览器下载url,而不是导航到它
  dom.click()
  document.body.removeChild(dom)
  // 手动释放创建的URL对象所占内存
  URL.revokeObjectURL(url)
}

//递归获取sortData
export const sortDG = (data, id) => {
  data.childClass.forEach(item => {

    if (item.id === id) {
      return item
    } else {
      if (item.childClass.length) {
        sortDG(item)
      }
    }
  })

}
//数组对象去重

export const arrayRepeat = (array1, array2) => {
  var result = []
  for (var i = 0; i < array2.length; i++) {
    var obj = array2[i]
    var num = obj.id
    var isExist = false
    for (var j = 0; j < array1.length; j++) {
      var aj = array1[j]
      var n = aj.id
      if (n === num) {
        isExist = true
        break
      }
    }
    if (!isExist) {
      result.push(obj)
    }
  }
  return result
}
//件号补零
export const repairZero = (val) => {
  if (val.toString().length === 1) {
    return (`000${val}`)
  } else if (val.toString().length === 2) {
    return (`00${val}`)
  } else if (val.toString().length === 3) {
    return (`0${val}`)
  } else {
    return (val)
  }
}
//案卷号补零
export const repairZeros = (val) => {
  if (val.toString().length === 1) {
    return (`00${val}`)
  } else if (val.toString().length === 2) {
    return (`0${val}`)
  } else {
    return (val)
  }
}
//代码下拉框
export const getcode = (val, form) => {
  let arr
  for (let key in val) {
    form.forEach(item => {
      if (val[key] === item.fieldName) {
        item['note'].note.forEach(ele => {
          if (ele.label === val.value) {
            arr = ele.value
          }
        })
      }
    })
  }
  return arr
}
//判断对象里的盒号是否相同
export const ifCase = (val) => {
  let flage = false
  let arr = val[0].case_no
  val.forEach(ele => {
    if (ele.case_no != arr) {
      flage = true
    }
  })
  return flage
}


export const dragControllerDiv = () => {
  var resize = document.getElementsByClassName('resize');
  var left = document.getElementsByClassName('tree-box');
  var right = document.getElementsByClassName('form-box');
  var content = document.getElementsByClassName('box');
  for (let i = 0; i < resize.length; i++) {
    // 鼠标按下事件
    resize[i].onmousedown = function (e) {
      //颜色改变提醒
      resize[i].style.background = '#818181';
      var startX = e.clientX;
      resize[i].left = resize[i].offsetLeft;
      // 鼠标拖动事件
      document.onmousemove = function (e) {
        var endX = e.clientX;
        var moveLen = resize[i].left + (endX - startX); // （endx-startx）=移动的距离。resize[i].left+移动的距离=左边区域最后的宽度
        var maxT = content[i].clientWidth - resize[i].offsetWidth; // 容器宽度 - 左边区域的宽度 = 右边区域的宽度

        if (moveLen < 32) moveLen = 32; // 左边区域的最小宽度为32px
        if (moveLen > maxT - 150) moveLen = maxT - 150; //右边区域最小宽度为150px

        resize[i].style.left = moveLen; // 设置左侧区域的宽度

        for (let j = 0; j < left.length; j++) {
          left[j].style.width = moveLen + 'px';
          right[j].style.width = (content[i].clientWidth - moveLen - 10) + 'px';
        }
      };
      // 鼠标松开事件
      document.onmouseup = function (evt) {
        //颜色恢复
        resize[i].style.background = '#d6d6d6';
        document.onmousemove = null;
        document.onmouseup = null;
        resize[i].releaseCapture && resize[i].releaseCapture(); //当你不在需要继续获得鼠标消息就要应该调用ReleaseCapture()释放掉
      };
      resize[i].setCapture && resize[i].setCapture(); //该函数在属于当前线程的指定窗口里设置鼠标捕获
      return false;
    };
  }
}

export const retentionList = {
  "永久": null,
  "长期": 30,
  "短期": 10,
  "定期30年": 30,
  "定期10年": 10,
  "30年": 30,
  "25年": 25,
  "15年": 15,
  "10年": 10,
  "5年": 5,
}

// 去除对象中的null 或者undefined 对象
export const removeEmpty = (obj)=> {
  Object.keys(obj).forEach((key) => {
    if (obj[key] && typeof obj[key] === 'object') {
      removeEmpty(obj[key]);
    } else if (obj[key] === null || obj[key] === undefined || obj[key] === '') {
      delete obj[key];
    }
  });

  return obj;
}
