<template>
  <div>
    <el-button style="width: 100%" type="success" icon="el-icon-link" size="small" plain @click="showDialog = true">绑定代理商</el-button>

    <sm-user-dialog :show.sync="showDialog" @select="handleSubmit" :query="{type: SmUserType.AGENT}"/>
  </div>
</template>

<script>
import SmUserDialog from '@/components/Business/SmUser/smUserDialog.vue'
import { bindAgent, bindMch } from '@/api/system/device'
import { SmUserType } from '@/utils/constants'

export default {
  name: "BindAgentButton",
  computed: {
    SmUserType() {
      return SmUserType
    }
  },
  components: { SmUserDialog },
  props: {
    deviceId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      showDialog: false,
    }
  },
  methods: {
    handleSubmit(user) {
      this.$prompt("请输入代理商设备服务费（%）", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^\d+$/,
        inputErrorMessage: '请输入数字',
        inputPlaceholder: "请输入代理商设备服务费",
        inputValue: user.agentDeviceService,
      }).then(({value}) => {
        bindAgent(this.deviceId, user.userId, value).then(res => {
          if (res.code === 200) {
            this.$message.success("绑定成功");
            this.showDialog = false;
            this.$emit('success');
          }
        })
      })
    }
  }
}
</script>
