<template>
  <!-- Deprecated status change: closing clears all team data. -->
  <div class="team-status-drawer">
    <el-drawer
      :title="$t('pages.teamList.teamStatus.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-alert
            v-if="form.status === 'CLOSE'"
            :title="$t('pages.teamList.teamStatus.warning')"
            type="error"
            :closable="false"
          />
          <el-form ref="form" :model="form" :rules="formRules" label-width="80px" style="margin-right: 15px;">
            <el-form-item prop="status" :label="$t('pages.teamList.teamStatus.status')">
              <el-radio-group v-model="form.status">
                <el-radio label="CLOSE">{{ $t('pages.teamList.teamStatus.close') }}</el-radio>
                <el-radio label="AVAILABLE">{{ $t('pages.teamList.teamStatus.available') }}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item prop="remarks" :label="$t('pages.teamList.teamStatus.remarks')">
              <el-input
                v-model="form.remarks"
                type="textarea"
                :placeholder="$t('pages.teamList.remark.placeholder')"
                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.teamList.action.close') }}</el-button>
          <el-button type="primary" :disabled="submitLoading" :loading="submitLoading" @click="submit">{{ $t('pages.teamList.action.save') }}</el-button>
        </div>
      </div>
    </el-drawer>

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

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

