<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '125px'">
        <el-form-item label="有效响应时间(h)" prop="validRespondTime">
          <el-input-number v-model="dataForm.validRespondTime" :min="1"></el-input-number>
        </el-form-item>
        <el-form-item label="有效结案时间(h)" prop="validCloseTime">
          <el-input-number v-model="dataForm.validCloseTime" :min="1"></el-input-number>
        </el-form-item>
        <el-form-item label="项目类别" prop="categoryId" v-if="!this.dataForm.id" >
          <el-cascader clearable v-model="dataForm.categoryIds" :props="{multiple :true }" :options="categoryOptions" filterable></el-cascader>
        </el-form-item>
        <el-form-item label="所属项目类别" prop="allCategoryNames" v-if="this.dataForm.id">
          <el-input v-model="dataForm.allCategoryNames" placeholder="所有项目类别名称" readonly></el-input>
        </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        validRespondTime: '',
        validCloseTime: '',
        allCategoryNames: '',
        categoryIds: []
      },
      categoryOptions: [],
      disabled: false
    }
  },
  computed: {
    dataRule () {
      return {
        validRespondTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        validCloseTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getCategoryList()
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.dataForm.categoryIds = []
        if (this.dataForm.id) {
          this.getInfo()
          this.getCategoryList()
          this.disabled = true
        } else {
          this.disabled = false
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/kpi/timeLimitItem/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        if (!new RegExp('^[1-9][0-9]*$').test(this.dataForm.validRespondTime)) {
          return this.$message.error('有效响应时间请输入正整数')
        }
        if (!new RegExp('^[1-9][0-9]*$').test(this.dataForm.validCloseTime)) {
          return this.$message.error('有效结案时间请输入正整数')
        }
        if (!this.dataForm.id && this.dataForm.categoryIds.length === 0) {
          return this.$message.error('请选择考核类别')
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/kpi/timeLimitItem/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    getCategoryList () {
      return this.$http.get('/events/category/getCategoryTree').then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.categoryOptions = res.data.options
      }).catch(() => { })
    }
  }
}
</script>
