<template>
  <div class="config-form">
    <el-form ref="ruleForm" :model="ruleForm" size="small" label-width="100px">
      <el-form-item label="配置分组" prop="group" :rules="{
                    required: true, message: '配置分组不能为空', trigger: 'change'
                  }">
        <el-select clearable v-model="ruleForm.group" size="small" placeholder="请选择配置分组">
          <el-option v-for="item in groupList" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="配置标题" prop="title" :rules="[{
                    required: true, message: '配置标题不能为空', trigger: 'blur'
                  },{min: 2, max: 50, message: '长度在2 到 50 个字符', trigger: 'blur'}]">
        <el-input maxlength="50" clearable v-model="ruleForm.title" type="textarea" rows="5" size="small" placeholder="请输入配置标题"/>
      </el-form-item>
      <el-form-item label="配置名称" prop="name" :rules="[{
                    required: true, message: '配置名称不能为空', trigger: 'blur'
                  },{min: 2, max: 50, message: '长度在2 到 50 个字符', trigger: 'blur'}]">
        <el-input maxlength="50" clearable v-model="ruleForm.name" size="small" placeholder="请输入配置名称"/>
      </el-form-item>
      <el-form-item label="配置描述" prop="tip" :rules="[
          {min: 0, max: 100, message: '不超过 100 个字符', trigger: 'blur'}]">
        <el-input maxlength="100" clearable v-model="ruleForm.tip" size="small" placeholder="请输入配置描述"/>
      </el-form-item>
      <el-form-item label="验证规则" prop="rule" :rules="[
          {min: 0, max: 100, message: '不超过 100 个字符', trigger: 'blur'}]">
        <el-input maxlength="100" clearable v-model="ruleForm.rule" size="small" placeholder="请输入验证规则"/>
      </el-form-item>
      <el-form-item label="配置类型" prop="type" :rules="{
                    required: true, message: '配置类型不能为空', trigger: 'change'
                  }">
        <el-select clearable v-model="ruleForm.type" size="small" placeholder="请选择配置类型">
          <el-option v-for="item in typeList" :key="item" :label="item" :value="item"/>
        </el-select>
      </el-form-item>
      <el-form-item label="输入类型" prop="inputType" :rules="{
                    required: true, message: '输入类型不能为空', trigger: 'change'
                  }">
        <el-select clearable v-model="ruleForm.inputType" size="small" placeholder="请选择输入类型">
          <el-option v-for="item in inputTypeList" :key="item" :label="item" :value="item"/>
        </el-select>
      </el-form-item>
      <el-form-item label="配置值">
        <el-input clearable v-model="ruleForm.value" type="textarea" rows="5" size="small" placeholder="请输入配置值"/>
      </el-form-item>
      <el-form-item label="字典数据">
        <el-input clearable v-model="ruleForm.content" type="textarea" rows="5" size="small" placeholder="请输入字典数据"/>
      </el-form-item>
      <el-form-item label="备注" prop="remark" :rules="[
          {min: 0, max: 100, message: '不超过 100 个字符', trigger: 'blur'}]">
        <el-input maxlength="100" clearable v-model="ruleForm.remark" type="textarea" rows="5" size="small" placeholder="请输入备注"/>
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number size="small" v-model="ruleForm.sort"></el-input-number>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="ruleForm.status" :active-value="1" :inactive-value="2" size="small"/>
      </el-form-item>
    </el-form>
    <div style="text-align: center">
      <el-button icon="el-icon-document-add" :loading="addBtnLoading" type="primary" size="medium" @click="submitForm('ruleForm')">提交
      </el-button>
      <el-button icon="el-icon-back" v-if="ruleForm.id" style="margin-left: 15px" size="medium"
                 @click="onBack('ruleForm')">返回
      </el-button>
    </div>
  </div>
</template>

<script>
  import configApi from '@/api/system/config'

  export default {
    name: "configForm",
    data() {
      return {
        addBtnLoading: false,
        groupList: [
          {value: 1, label: '基础配置'},
          {value: 2, label: '网站配置'},
          {value: 3, label: '邮件配置'}
        ],
        typeList: ['string', 'text', 'int', 'bool', 'array', 'datetime', 'date', 'file'],
        inputTypeList: ['text', 'textarea', 'switch', 'radio', 'checkbox', 'select'],
      }
    },
    props: {
      ruleForm: {
        type: Object,
        default() {
          return {};
        }
      }
    },
    created() {
      if(!this.ruleForm.id){
        this.initRuleForm()
      }
    },
    mounted() {
      if(this.ruleForm.id){
        this.$refs['ruleForm'].resetFields()
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let method = 'configAdd'
            if (this.ruleForm.id) {
              method = 'configEdit'
            }
            this.addBtnLoading = true
            configApi[method](this.ruleForm).then(res => {
              this.addBtnLoading = false
              let group = this.ruleForm.group
              this.$refs[formName].resetFields()
              this.$message({
                type: 'success',
                message: this.ruleForm.id ? '修改成功' : '添加成功'
              })
              if (this.ruleForm.id) {
                this.initRuleForm()
                this.$emit('onBack')
                this.$emit('submit',2)
              }else{
                this.initRuleForm()
                this.$emit('submit',1,group)
              }
            }).catch(e => {
              this.addBtnLoading = false
            })
          }
        })
      },
      onBack() {
        this.initRuleForm()
        this.$refs['ruleForm'].resetFields()
        this.$emit('onBack')
      },
      initRuleForm() {
        this.ruleForm = {
          name: '',
          title: '',
          tip: '',
          rule: '',
          type: '',
          inputType: '',
          value: '',
          content: '',
          remark: '',
          sort: 1,
          status: 1,
          group: ''
        }
      }
    }
  }
</script>

<style lang="less">
  .config-form {

  }
</style>
