import request from '@/utils/request'
import { Message, MessageBox } from 'element-ui'
import Vue from 'vue'
import InputConfirmDialog from '@/views/components/dialogDiy.vue'
import dialogDiyReportDetails from '@/views/components/dialogDiyReportDetails.vue'
import dialogDiyAcceptanceDetails from '@/views/components/dialogDiyAcceptanceDetails.vue'
// 组件映射表
const COMPONENT_MAP = {
  ReportDetails: Vue.extend(dialogDiyReportDetails),
  AcceptanceDetails: Vue.extend(dialogDiyAcceptanceDetails),
  default: Vue.extend(InputConfirmDialog)
}

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

// examine 开始

// 审核通过/驳回
// const allocation = {
//    status:0无弹窗确认 ， 1 通过 ， 2 取消/删除   , 3 自定义弹窗
//    url: '', 接口路径
//    method : 'put'   请求方式
//    confirmText: ''   确认信息
//    dome:''   动态切换自定义弹窗
// }

// 使用示例
// const allocation = {
//   status: 3,
//   url: '',
//   method: 'post',
//   prop: 'xxxx',
//   dome?: ''
// }
// const params = {}
// examine(allocation, params)
// 统一弹窗处理
async function handleDialog(allocation, params) {
  try {
    // 状态 0 直接调用接口
    if (allocation.status === 0) {
      return await dialogApi(allocation, params)
    }

    // 状态 1/2 确认弹窗
    if ([1, 2].includes(allocation.status)) {
      await MessageBox.confirm(
        `${allocation.confirmText}, 是否继续?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: allocation.status === 1 ? 'success' : 'warning'
        }
      )
      return await dialogApi(allocation, params)
    }

    // 状态 3 自定义弹窗
    if (allocation.status === 3) {
      const value = await showCustomDialog(allocation)
      return await dialogApi(allocation, { ...params, ...value })
    }

    throw new Error('无效的操作状态')
  } catch (error) {
    if (error !== 'cancel') {
      Message.error(error.message || '操作失败')
    }
    throw error
  }
}

// 自定义弹窗封装
function showCustomDialog(allocation) {
  return new Promise((resolve, reject) => {
    const Component = COMPONENT_MAP[allocation.dome] || COMPONENT_MAP.default
    const instance = new Component({ propsData: allocation })

    // 挂载实例
    instance.$mount()
    document.body.appendChild(instance.$el)
    instance.$props.visible = true

    // 事件处理
    const cleanUp = () => {
      instance.$props.visible = false
      setTimeout(() => {
        document.body.removeChild(instance.$el)
        instance.$destroy()
      }, 300)
    }

    instance.$once('confirm', (value) => {
      resolve(value)
      cleanUp()
    })

    instance.$once('cancel', () => {
      reject(new Error('cancel'))
      cleanUp()
    })
  })
}

// 接口请求封装
async function dialogApi(allocation, params) {
  try {
    const { data } = await request({
      url: allocation.url,
      method: allocation.method || 'put',
      params
    })
    return data
  } catch (error) {
    throw new Error('请求处理失败')
  }
}

export const examine = handleDialog

// examine 结束

