import store from '@/store'
import {
  MessageBox,
  Message
} from 'element-ui'
import {
  Access,
  State
} from '@/constant'
import request from '@/utils/request'
import {
  showLoading,
  closeLoading
} from '@/utils/pop'

export function addTenant (data) {
  if (data.tenant && store.getters.isSuperAdmin) {
    return data
  }
  data.tenant = store.getters.tenant
  return data
}

export function addStatusScope (data) {
  switch (data.status) {
    case State.RESOLVED:
      data.tenant = store.getters.tenant
      break
    case State.REVIEW:
      data.status = State.SUBMITTED
      if (store.getters.isGroupAdmin) {
        if (store.getters.isTopGroupAdmin) {
          data.tenant = store.getters.tenant
        } else {
          data.org = store.getters.org
        }
      }
      break
    default:
      data.user = store.getters.userId
      break
  }
  return data
}

export function addScope (data) {
  if (store.getters.isGroupAdmin) {
    if (store.getters.isTopGroupAdmin) {
      data.tenant = store.getters.tenant
    } else {
      data.org = store.getters.org
    }
  } else {
    data.user = store.getters.userId
  }
  return data
}

export function addOrg (data) {
  data.tenant = store.getters.tenant
  data.org = store.getters.org
  return data
}

export function addUser (data) {
  data.user = store.getters.userId
  return data
}

export function canDel ({ createBy, org }) {
  if (store.getters.isSuperAdmin) {
    return true
  }
  if (store.getters.isGroupAdmin) {
    if (store.getters.isTopGroupAdmin) {
      return true
    }
    return new RegExp(`^${store.getters.org}`).test(org)
  }
  if (store.getters.accesses.has(Access.MANAGE_CALENDAR)) {
    return createBy === store.getters.userId
  }
  return false
}

export function send (config, service = request) {
  const loading = showLoading()
  return service(config).finally(() => {
    closeLoading(loading)
  })
}

export function messageSend (config, message, service = request) {
  return (config.onUploadProgress ? service(config) : send(config, service)).then(data => {
    message && Message({
      type: 'success',
      message: `${message}成功`
    })
    return data
  })
}

export function confirm (message) {
  return MessageBox.confirm(message, { type: 'warning' })
}

export function confirmAndSend (type, tip, config, service = request) {
  return confirm(
    tip ? `${type} ${tip} ？` : `确定${type}？`
  ).then(() => messageSend(config, type, service))
}

export function add (config, service = request) {
  return messageSend(config, '新增', service)
}

export function update (config, message, service = request) {
  return messageSend(config, message || '更新', service)
}

export function submit (config, tip, service = request) {
  return confirmAndSend('提交', tip, config, service)
}

export function del (config, tip, service = request) {
  return confirmAndSend('删除', tip, config, service)
}

export function resolve (config, tip, service = request) {
  return confirmAndSend('通过', tip, config, service)
}

export function reject (config, tip, service = request) {
  return confirmAndSend('驳回', tip, config, service)
}
