// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import DateFormat from 'dateformat'

Date.prototype.format = function(fmt) {
  return DateFormat(this, fmt)
}

Date.prototype.isToday = function() {
  return new Date().getTime() - this.getTime() < 86400000
}

Date.prototype.isSameDay = function(d) {
  if (d == undefined) {
    return false
  }
  return d.getFullYear() == this.getFullYear() && d.getMonth() == this.getMonth() && d.getDate() == this.getDate()
}

Date.prototype.toWeekDay = function() {
  switch(this.getDay()) {
    case 0:
    return "星期天"
    case 1:
    return "星期一"
    case 2:
    return "星期二"
    case 3:
    return "星期三"
    case 4:
    return "星期四"
    case 5:
    return "星期五"
    case 6:
    return "星期六"
  }
}

Date.prototype.getDays = function() {
  let copy = new Date()
  copy.setFullYear(this.getFullYear())
  copy.setMonth(this.getMonth() + 1)
  copy.setDate(0)
  return copy.getDate()
}

Date.prototype.isLate = function(d) {
  if(d) {
    return d.getTime() - this.getTime() > 0
  }
  return false
}

String.prototype.isMobile = function() {
  return (/^1[3|4|5|6|7|8|9]\d{9}$/.test(this));
}

String.prototype.formatDate = function(pattern) {
  if (pattern !== undefined) {
    try {
      return this.toDate().format(pattern)
    } catch (e) {
      return this
    }
  }
  return this
}

String.prototype.toDate = function() {
  return new Date(this.replace(/-/g, "/"))
}

String.prototype.toPrice = function() {
  let v = this.replace(/[^\d.]/g, "")
    .replace(/^\./g, "")
    .replace(/\.{2,}/g, ".")
    .replace(".", "$#$")
    .replace(/\./g, "")
    .replace("$#$", ".")
    .replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
  return v
}

String.prototype.hidePhone = function() {
  let reg = /^(\d{3})\d{4}(\d{4})$/
  return this.replace(reg, "$1****$2")
}

String.prototype.urlEncode = function() {
  let url = this.toString()
  return encodeURIComponent(url)
  .replace(/!/g, '%21')
  .replace(/'/g, '%27')
  .replace(/\(/g, '%28')
  .replace(/\)/g, '%29')
  .replace(/\*/g, '%2A')
  .replace(/%20/g, '+')
}

export default {// 设置选中的方法
// 记忆选择核心方法
  changePageCoreRecordData: function (multipleSelectionAll, multipleSelection, tableData, successFn) {
    // 标识当前行的唯一键的名称
    const idKey = 'uid'
    const that = this
    // 如果总记忆中还没有选择的数据，那么就直接取当前页选中的数据，不需要后面一系列计算
    if (multipleSelectionAll.length <= 0) {
     multipleSelectionAll=multipleSelection
      successFn(multipleSelectionAll)
      return
    }
    // 总选择里面的key集合
    const selectAllIds = []
    multipleSelectionAll.forEach(row => {
      selectAllIds.push(row[idKey])
    })
    const selectIds = []
    // 获取当前页选中的id
    multipleSelection.forEach(row => {
      selectIds.push(row[idKey])
      // 如果总选择里面不包含当前页选中的数据，那么就加入到总选择集合里
      if (selectAllIds.indexOf(row[idKey]) < 0) {
        multipleSelectionAll.push(row)
      }
    })
    const noSelectIds = []
    // 得到当前页没有选中的id
    tableData.forEach(row => {
      if (selectIds.indexOf(row[idKey]) < 0) {
        noSelectIds.push(row[idKey])
      }
    })
    noSelectIds.forEach(uid => {
      if (selectAllIds.indexOf(uid) >= 0) {
        for (let i = 0; i < multipleSelectionAll.length; i++) {
          if (multipleSelectionAll[i][idKey] == uid) {
            // 如果总选择中有未被选中的，那么就删除这条
            multipleSelectionAll.splice(i, 1)
            break
          }
        }
      }
    })
    successFn(multipleSelectionAll)
  }
}
