import { ElMessage,ElMessageBox } from "element-plus"
let messageDom = null
export function showMessage(option) {
 if (messageDom) messageDom.close()
 messageDom = ElMessage({
  ...option
 })
}
export {
 ElMessageBox as MyElMessageBox
}

export function elAlert(content,name='系统提示') {
 ElMessageBox.alert(content,name,{
  type:"warning",
  confirmButtonText: "确定",
 })
}
export function showConfirm(delFun,...params) {
 ElMessageBox.confirm(...params)
  .then(() => {
   delFun && delFun()
   // showMessage({
   //   type: 'success',
   //   message: '删除成功!'
   //  });
  })
  .catch(() => {
   showMessage({
    type: "info",
    message: "已取消"
   })
  })
}

export function handleMes(
 res,
 successFun,
 failCallback,
 successMessage,
 failMessage
) {
 let isTrue
 try {
  isTrue = Boolean(res.msg[0].success + "" === 'true' || res.msg[0].Rows[0].status + "" === 'success')
 }catch {
  isTrue = false
 }
 let successMes = '操作成功'
 let failMes = '操作失败2'
 try {
  successMes = successMessage || res.msg[0].error  || res.message || '操作成功'
 }catch {

 }
 try {

  failMes =failMessage || res.msg[0].error || res.message || '操作失败'
 }catch {

 }
 showMessage({
  message: isTrue
   ? successMes
   : failMes,
  type: isTrue ? "success" : "error"
 })
 isTrue && successFun && successFun()
 !isTrue && failCallback && failCallback()
}
