<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="true"
    :title="title"
    :type="type"
    :visible.sync="isVisible"
    :width="width"
    top="50px"
  >
    <div slot="footer" class="dialog-footer">
      <el-button plain type="warning" @click="isVisible = false">
        {{ $t("common.cancel") }}
      </el-button>
      <el-button plain type="primary" @click="submitForm">
        {{ $t("common.confirm") }}
      </el-button>
    </div>
    <el-form
      ref="form" :model="parameter" :rules="rules"
      label-position="right"
      label-width="100px"
    >
      <el-form-item :label="$t('table.parameter.key')" prop="key">
        <el-input v-model="parameter.key" :disabled="type==='edit'" type="" placeholder="参数键" />
      </el-form-item>
      <el-form-item :label="$t('table.parameter.name')" prop="name">
        <el-input v-model="parameter.name" type="" placeholder="参数名称" />
      </el-form-item>
      <el-form-item :label="$t('table.parameter.value')" prop="value">
        <el-input v-model="parameter.value" type="" placeholder="参数值" />
      </el-form-item>
      <el-form-item :label="$t('table.parameter.describe')" prop="describe">
        <el-input v-model="parameter.describe" type="textarea" placeholder="描述" />
      </el-form-item>
      <el-form-item :label="$t('table.parameter.status')" prop="status">
        <el-radio-group v-model="parameter.status" size="medium">
          <el-radio-button :label="true">{{ $t("common.status.valid") }}</el-radio-button>
          <el-radio-button :label="false">{{ $t("common.status.invalid") }}</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('table.parameter.readonly')" prop="readonly">
        <el-radio-group v-model="parameter.readonly" size="medium">
          <el-radio-button :label="true">{{ $t("common.yes") }}</el-radio-button>
          <el-radio-button :label="false">{{ $t("common.no") }}</el-radio-button>
        </el-radio-group>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import parameterApi from "@/api/Parameter.js"

export default {
  name: "ParameterEdit",
  components: { },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: "add"
    }
  },
  data() {
    return {
      parameter: this.initParameter(),
      screenWidth: 0,
      width: this.initWidth(),
      rules: {

      }
    }
  },
  computed: {
    isVisible: {
      get() {
        return this.dialogVisible
      },
      set() {
        this.close()
        this.reset()
      }
    },
    title() {
      return this.$t("common." + this.type)
    }
  },
  watch: {},
  mounted() {
    window.onresize = () => {
      return (() => {
        this.width = this.initWidth()
      })()
    }
  },
  methods: {
    initParameter() {
      return {
        id: "",
        key: '',
        name: '',
        value: '',
        describe: '',
        status: true,
        readonly: false
      }
    },
    initWidth() {
      this.screenWidth = document.body.clientWidth
      if (this.screenWidth < 991) {
        return "90%"
      } else if (this.screenWidth < 1400) {
        return "45%"
      } else {
        return "800px"
      }
    },
    setParameter(val) {
      const vm = this
      if (val) {
        vm.parameter = { ...val }
      }
    },
    close() {
      this.$emit("close")
    },
    reset() {
      // 先清除校验，再清除表单，不然有奇怪的bug
      this.$refs.form.clearValidate()
      this.$refs.form.resetFields()
      this.parameter = this.initParameter()
    },
    submitForm() {
      const vm = this
      this.$refs.form.validate(valid => {
        if (valid) {
          vm.editSubmit()
        } else {
          return false
        }
      })
    },
    editSubmit() {
      const vm = this
      if (vm.type === "edit") {
        vm.update()
      } else {
        vm.save()
      }
    },
    save() {
      const vm = this
      parameterApi.save(this.parameter).then(response => {
        const res = response.data
        if (res.isSuccess) {
          vm.isVisible = false
          vm.$message({
            message: vm.$t("tips.createSuccess"),
            type: "success"
          })
          vm.$emit("success")
        }
      })
    },
    update() {
      parameterApi.update(this.parameter).then(response => {
        const res = response.data
        if (res.isSuccess) {
          this.isVisible = false
          this.$message({
            message: this.$t("tips.updateSuccess"),
            type: "success"
          })
          this.$emit("success")
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped></style>
