<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px">
      <el-form-item label="编号" prop="code">
        <el-input v-model="ruleForm.code" placeholder="请输入编号"/>
      </el-form-item>
      <el-form-item label="参数值" prop="value">
        <el-input v-model="ruleForm.value" placeholder="请输入参数值"/>
      </el-form-item>
      <el-form-item label="说明" prop="des">
        <el-input v-model="ruleForm.des" type="textarea" autosize placeholder="请输入说明"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getBasicData, addBasicData, editBasicData } from '@/api/systemSetup/sysSetting/basicData'
import { filterContent } from '@/utils'

export default {
  name: 'AddOrEdit',
  props: {
    rowid: {
      type: Number,
      default: 0
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ruleForm: {
        id: '',
        code: '',
        value: '',
        des: ''
      },
      rules: {
        code: [{
          required: true,
          trigger: 'blur',
          message: '请输入编号'
        }],
        value: [{
          required: true,
          trigger: 'blur',
          message: '请输入参数值'
        }]
      },
      loading: false
    }
  },
  created() {
    if (this.rowid) {
      this.ruleForm.id = this.rowid
      this.getDetail(this.rowid)
    }
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          const datas = {
            id: this.ruleForm.id,
            code: this.ruleForm.code,
            value: this.ruleForm.value,
            des: filterContent.delHtmlTag(this.ruleForm.des)
          }

          // 添加
          if (!this.rowid) {
            addBasicData(datas).then(response => {
              this.loading = false
              if (response.state.toLowerCase() === 'success') {
                this.$parent.$layer.close(this.layerid)
                this.$parent.getList() // 重新加载父级数据
                this.$message.success('恭喜你，基础数据添加成功!')
              }
            }).catch(() => {
              this.loading = false
            })
            return
          }
          // 编辑
          editBasicData(datas).then(response => {
            this.loading = false
            if (response.state.toLowerCase() === 'success') {
              this.$parent.$layer.close(this.layerid)
              this.$parent.getList() // 重新加载父级数据
              this.$message.success('恭喜你，基础数据编辑成功!')
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.$message.error('请输入有效的必填项信息!')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
    },
    // 获取详情
    getDetail(rid) {
      getBasicData(rid).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data
          this.ruleForm.code = res.F_ParamCode
          this.ruleForm.value = res.F_ParamValue
          this.ruleForm.des = res.F_ParamDes
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
