<template>
  <div class="team-status-drawer">
    <el-drawer
      :title="$t('pages.teamBill.statusDrawer.title')"
      :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-status">
        <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.teamBill.label.remarks')">
              <el-input
                v-model="form.remarks"
                type="textarea"
                :placeholder="$t('pages.teamBill.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.teamBill.action.close') }}</el-button>
          <el-button type="primary" :disabled="submitLoading" :loading="submitLoading" @click="submit">{{ $t('pages.teamBill.action.save') }}</el-button>
        </div>
      </div>
    </el-drawer>

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

export default {
  name: 'TeamStatus',
  props: {
    billId: {
      type: String,
      require: true,
      default: ''
    },
    status: {
      type: String,
      require: true,
      default: ''
    }
  },
  data() {
    return {
      submitLoading: false,
      form: {
        billId: '',
        status: '',
        remarks: ''
      },
      formRules: {}
    }
  },
  computed: {
  },
  watch: {
    status: {
      handler(newVal) {
        this.form.status = newVal
      },
      immediate: true
    },
    billId: {
      handler(newVal) {
        this.form.billId = newVal
      },
      immediate: true
    }
  },
  methods: {
    handleClose() {
      if (this.submitLoading) {
        this.$opsMessage.warn(this.$t('pages.teamBill.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
        updateBillStatus(that.form).then(res => {
          that.submitLoading = false
          this.$emit('success', that.form)
        }).catch(er => {
          that.submitLoading = false
        })
      })
    }
  }
}
</script>

