<template>
  <div v-clickoutside="{obj: editBox, func: esc}" class="right-box">
    <div class="right-box__header">
      <div class="header__title">{{$t('config.edit_APM_settings')}}</div>
      <div class="header__operation">
        <span v-cancel="{obj: editBox, func: esc}"><i class="nz-icon nz-icon-close" :title="$t('overall.close')"></i></span>
      </div>
    </div>
    <div class="right-box__container">
      <div class="container__form">
        <el-form ref="form" :model="editBox" :rules="rules" label-position="top" label-width="120px">
          <!-- asset_ping_interval -->
          <el-form-item :label="$t('overall.name')" prop="asset_ping_interval">
            <el-input v-model.number="editBox.asset_ping_interval" size="small">
              <template slot="append"><span>{{$t('alert.config.second')}}</span></template>
            </el-input>
          </el-form-item>
          <!-- default_scrape_interval -->
          <el-form-item :label="$t('config.default_scrape_interval')" prop="default_scrape_interval">
            <el-input v-model.number="editBox.default_scrape_interval" size="small">
              <template slot="append"><span>{{$t('alert.config.second')}}</span></template>
            </el-input>
          </el-form-item>
          <!-- metrics_storage_retention -->
          <el-form-item :label="$t('config.metric_retention_days')" prop="metrics_storage_retention">
            <el-input v-model.number="editBox.metrics_storage_retention" size="small">
              <template slot="append"><span>{{$t('config.system.basic.day')}}</span></template>
            </el-input>
          </el-form-item>
          <!-- logs_storage_retention -->
          <el-form-item :label="$t('config.log_retention_days')" prop="logs_storage_retention">
            <el-input v-model.number="editBox.logs_storage_retention" size="small">
              <template slot="append"><span>{{$t('config.system.basic.day')}}</span></template>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="right-box__footer">
      <button id="asset-edit-cancel" v-cancel="{obj: editBox, func: esc}" class="footer__btn footer__btn--light">
        <span>{{$t('overall.cancel')}}</span>
      </button>
      <button id="asset-edit-save" :class="{'footer__btn--disabled': prevent_opt.save}" :disabled="prevent_opt.save" class="footer__btn" @click="save">
        <span>{{$t('overall.save')}}</span>
      </button>
    </div>
  </div>
</template>

<script>
import editRigthBox from '../mixin/editRigthBox'
export default {
  name: 'configurationBox',
  props: {
    obj: {
      type: Object
    }
  },
  mixins: [editRigthBox],
  data () {
    return {
      url: '/sys/config/monitor',
      editBox: {
        asset_ping_interval: '',
        default_scrape_interval: '',
        metrics_storage_retention: '',
        logs_storage_retention: ''
      },
      rules: {
        asset_ping_interval: [{ required: true, message: this.$t('validate.required'), trigger: 'blur' }],
        default_scrape_interval: [{ required: true, message: this.$t('validate.required'), trigger: 'blur' }],
        metrics_storage_retention: [{ required: true, message: this.$t('validate.required'), trigger: 'blur' }],
        logs_storage_retention: [{ required: true, message: this.$t('validate.required'), trigger: 'blur' }]
      }
    }
  },
  methods: {
    clickOutside () {
      this.esc(false)
    },
    /* 关闭弹框 */
    esc (refresh) {
      this.prevent_opt.save = false
      this.$emit('close', refresh)
    },
    save () {
      if (this.prevent_opt.save) {
        return
      }
      this.prevent_opt.save = true
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = {
            ...this.editBox
          }
          this.$put(this.url, params).then(res => {
            this.prevent_opt.save = false
            if (res.code === 200) {
              this.$message({ duration: 2000, type: 'success', message: this.$t('tip.saveSuccess') })
              this.esc(true)
            } else {
              this.$message.error(res.msg)
            }
          })
        } else {
          this.prevent_opt.save = false
        }
      })
    }
  },
  watch: {
    obj: {
      immediate: true,
      deep: true,
      handler (n) {
        this.isEdit = true
        this.editBox = this.$lodash.cloneDeep(n)
      }
    }
  }
}
</script>
