var i = 0
export default function (option, placeholder = '请选择', errMsg = '请先选择数据') {
  return new Promise((r, j) => {
    if (option.length == 1) {
      r(option?.[0]?.value)
      return
    }
    const h = this.$createElement
    const refName = `selectView${++i}`
    var _this = this
    var done_ = null
    this.$msgbox({
      title: placeholder,
      message: h(
        'el-select',
        {
          style: {
            width: '100%'
          },
          props: {
            value: '',
            filterable: true,
            clearable: true,
            size: 'mini'
          },
          ref: refName,
          on: {
            change: e => {
              _this.$refs[refName].value = e
            }
          }
        },
        [
          option.map(it => {
            return h('el-option', {
              props: {
                key: it.value,
                label: it.label,
                value: it.value
              }
            })
          })
        ]
      ),
      beforeClose: function (action, instance, done) {
        if (action === 'confirm') {
          if (_this?.$refs?.[refName]?.value) {
            done()
          } else {
            done_ = done
            this.$message({
              message: errMsg,
              type: 'warning'
            })
          }
        } else {
          done()
        }
      },
      showCancelButton: true,
      closeOnClickModal: false,
      confirmButtonText: '确定',
      cancelButtonText: '取消'
    })
      .then(_ => {
        if (_this?.$refs?.[refName]?.value) {
          r(_this?.$refs?.[refName]?.value)
          done_?.()
        } else {
          j()
        }
      })
      .catch(_ => {
        j()
      })
  })
}
