<template>
  <div>
    <el-button :disabled="disabled ? disabled : false" :type="type ? type : 'primary'" :size="size ? size : 'mini'" @click="submit">{{ btnName }}</el-button>
  </div>
</template>

<script>
export default {
  name: '',
  props:{
    type: String,
    btnName: String,
    message: String,
    idName: String,
    keyName: String,
    id: [String,Number,Array],
    disabled: Boolean,
    idIsArr: Boolean,
    paramData: {
      type: Array,
      default () {
        return []
      }
    },
    dialog: Boolean,
    dialogTitle:String,
    dialogKey:String,
    checkContent: Boolean,
    size: String
  },
  data() {
    return {
    };
  },
  computed:{
  },
  watch:{
  },
  methods: {
    submit () {
      if (this.dialog) {
        this.$prompt(this.dialogTitle, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: this.checkContent ? /^[\d.]+$/ : ''
        }).then(async ({ value }) => {
          let param = {
            content:{}
          }
          param.id = this.idName
          param.content[this.keyName] = this.idIsArr ? [this.id] : this.id
          this.paramData.forEach(item => {
            param.content[item.key] = item.value
          })
          param.content[this.dialogKey] = value
          let res = await this.$api.requested(param)
          this.tool.showMessage(res,() => {
            this.$emit('onSuccess',res.data)
          })
        })
      } else {
        this.$confirm(this.message,'提示',{
          confirmButtonText:'确定',
          cancelButtonText:'取消',
          type:'warning'
        }).then(async () => {
          let param = {
            content:{}
          }
          param.id = this.idName
          param.content[this.keyName] = this.idIsArr ? [this.id] : this.id
          this.paramData.forEach(item => {
            param.content[item.key] = item.value
          })
          let res = await this.$api.requested(param)
          this.tool.showMessage(res,() => {
            this.$emit('onSuccess',res.data)
          })
        })
      }
      
    }
  },
};
</script>

<style scoped>

</style>
