<template>
  <div class="team-member-add-drawer">
    <el-drawer
      :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 slot="title" class="flex-l">
        <sys-origin-icon :icon="form.sysOrigin" size="18px" />&nbsp;{{ textOptTitle }}
      </div>
      <div class="team-member-add">

        <div class="drawer-form">
          <el-form ref="form" :model="form" :rules="formRules" label-width="80px">
            <el-form-item v-if="!isUpdate" prop="userId" :label="$t('pages.bdLead.form.leader')">
              <account-input v-model="form.userId" :sys-origin="form.sysOrigin" :placeholder="$t('pages.bdLead.placeholder.bdLeadUserIdWithSpace')" />
            </el-form-item>
            <el-form-item prop="regionId" :label="$t('pages.bdLead.form.region')">
              <select-system-region
                ref="regionSelectPolicy"
                v-model="form.regionId"
                :sys-origin="form.sysOrigin"
                :placeholder="$t('pages.bdLead.placeholder.selectRegion')"
              />
            </el-form-item>
            <el-form-item prop="contact" :label="$t('pages.bdLead.form.contact')">
              <el-input v-model="form.contact" :sys-origin="form.sysOrigin" :placeholder="$t('pages.bdLead.placeholder.contact')" />
            </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 { addBdLead, updateBDLead } from '@/api/room-anchor'
import { deepClone } from '@/utils'

export default {
  name: 'LeadAddBd',
  props: {
    row: {
      type: Object,
      require: false,
      default: () => {}
    }
  },
  data() {
    const commonRules = [
      { required: true, message: this.$t('pages.bdLead.validation.requiredField'), trigger: 'blur' }
    ]
    return {
      submitLoading: false,
      form: {
        id: '',
        userId: '',
        sysOrigin: '',
        contact: '',
        regionId: ''
      },
      formRules: {
        userId: commonRules,
        regionId: commonRules
      }
    }
  },
  computed: {
    textOptTitle() {
      return this.row && this.row.id ? this.$t('pages.bdLead.dialog.edit') : this.$t('pages.bdLead.dialog.create')
    },
    isUpdate() {
      return this.row && this.row.id
    }
  },
  watch: {
    row: {
      handler(newVal) {
        this.form = deepClone(newVal)
      },
      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
        if (that.isUpdate) {
          updateBDLead(that.form).then(res => {
            that.$emit('success', that.form)
            that.submitLoading = false
          }).catch(er => {
            that.$emit('fail', that.form)
            that.submitLoading = false
          })
          return
        }
        addBdLead(that.form).then(res => {
          that.submitLoading = false
          this.$emit('success', that.form)
        }).catch(er => {
          this.$emit('fail', that.form)
          that.submitLoading = false
        })
      })
    }
  }
}
</script>

