<template>
  <div class="bind-team-drawer">
    <el-drawer
      :title="$t('pages.bdLead.dialog.bindSubBd')"
      :visible="true"
      :before-close="handleClose"
      :close-on-press-escape="false"
      :wrapper-closable="false"
      :modal-append-to-body="true"
      :append-to-body="true"
      custom-class="drawer-auto-layout"
    >
      <div class="bind-team">

        <div class="drawer-form">
          <el-form ref="form" :model="form" :rules="formRules" label-width="80px">
            <el-form-item prop="userId" :label="$t('pages.bdLead.form.bd')">
              <account-input v-model="form.userId" :sys-origin="sysOrigin" :placeholder="$t('pages.bdLead.placeholder.bdAccount')" />
            </el-form-item>
          </el-form>
        </div>
        <div class="drawer-footer">
          <el-button @click="handleClose()">{{ $t('pages.bdLead.action.close') }}</el-button>
          <el-button type="primary" :disabled="submitLoading" :loading="submitLoading" @click="submitForm">{{ $t('pages.bdLead.action.save') }}</el-button>
        </div>
      </div>
    </el-drawer>

  </div>
</template>
<script>
import { leadBindBd } from '@/api/room-anchor'
import { deepClone } from '@/utils'

export default {
  name: 'BindBd',
  props: {
    row: {
      type: Object,
      require: false,
      default: () => {}
    }
  },
  data() {
    const commonRules = [
      { required: true, message: this.$t('pages.bdLead.validation.requiredField'), trigger: 'blur' }
    ]
    return {
      submitLoading: false,
      sysOrigin: '',
      form: {
        userId: '',
        bdLeadUserId: ''
      },
      formRules: {
        userId: commonRules
      }
    }
  },
  computed: {
    textOptTitle() {
      return this.$t('pages.bdLead.dialog.bindSubBd')
    }
  },
  watch: {
    row: {
      handler(newVal) {
        const _newData = deepClone(newVal)
        this.sysOrigin = _newData.userProfile.originSys
        this.form.bdLeadUserId = _newData.userProfile.id
      },
      immediate: true
    }
  },
  methods: {
    handleClose() {
      if (this.submitLoading) {
        this.$opsMessage.warn(this.$t('pages.bdLead.message.submitting'))
        return
      }
      this.$emit('close')
    },
    submitForm() {
      const that = this
      that.$refs.form.validate(valid => {
        if (!valid) {
          console.error('error submit!!')
          return false
        }
        that.submitLoading = true
        leadBindBd(that.form).then(res => {
          that.submitLoading = false
          that.$opsMessage.success()
          this.$emit('success')
        }).catch(er => {
          that.submitLoading = false
        })
      })
    }
  }
}
</script>

