<template>
  <div class="team-member-remarks-drawer">
    <el-drawer
      :title="$t('pages.teamMember.dialog.editRemarks')"
      :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="team-member-remarks">
        <div class="drawer-form">
          <el-form ref="form" :model="form" :rules="formRules" label-width="80px" style="margin-right: 15px;">
            <el-form-item prop="remarks" :label="$t('pages.teamMember.form.remarks')">
              <el-input
                v-model="form.remarks"
                type="textarea"
                :placeholder="$t('pages.teamMember.placeholder.remarks')"
                maxlength="100"
                show-word-limit
                resize="none"
                rows="5"
              />
            </el-form-item>
          </el-form>
        </div>

        <div class="drawer-footer">
          <el-button :disabled="submitLoading" @click="handleClose()">{{ $t('pages.teamMember.action.close') }}</el-button>
          <el-button type="primary" :disabled="submitLoading" :loading="submitLoading" @click="submit">{{ $t('pages.teamMember.action.save') }}</el-button>
        </div>
      </div>
    </el-drawer>

  </div>
</template>
<script>
import { updateMemberRemark } from '@/api/team'

export default {
  name: 'TeamMemberRemarks',
  props: {
    row: {
      type: Object,
      require: true,
      default: () => {}
    }
  },
  data() {
    return {
      submitLoading: false,
      form: {
        id: '',
        remarks: ''
      },
      formRules: {}
    }
  },
  computed: {
  },
  watch: {
    row: {
      handler(newVal) {
        if (newVal) {
          this.form.id = newVal.id
          this.form.remarks = newVal.remarks
        }
      },
      immediate: true
    }
  },
  methods: {
    handleClose() {
      if (this.submitLoading) {
        this.$opsMessage.warn(this.$t('pages.teamMember.message.submitting'))
        return
      }
      this.$emit('close')
    },
    submit() {
      const that = this
      that.$refs.form.validate(valid => {
        if (!valid) {
          console.error('error submit!!')
          return false
        }
        that.submitLoading = true
        updateMemberRemark(that.form).then(res => {
          that.submitLoading = false
          this.$emit('success', that.form)
        }).catch(er => {
          that.submitLoading = false
        })
      })
    }
  }
}
</script>
