<template>
  <div class="app-content" style="height:380px;overflow:auto">
    <a-spin :spinning="loading" style="width: 100%;height: 100%;">
      <a-form-model ref="form" :model="formData" :rules="rules">
        <a-form-model-item label="名称" prop="name">
          <a-input v-model="formData.name" placeholder="名称" style="width:240px;" />
        </a-form-model-item>
        <a-form-model-item label="字典类型" prop="typeId">
          <parameter-type-select v-model="formData.typeId" :width="240" :isAll="true" />
        </a-form-model-item>
        <a-form-model-item label="父类型" prop="parentId">
          <parent-select v-model="formData.parentId" :width="240" :isAll="true" />
        </a-form-model-item>
        <a-form-model-item label="树编码" prop="treeCode">
          <a-input v-model="formData.treeCode" placeholder="树编码" style="width:240px;" />
        </a-form-model-item>
        <a-form-model-item label="标准编码" prop="gbCode">
          <a-input v-model="formData.gbCode" placeholder="标准编码" style="width:240px;" />
        </a-form-model-item>
        <a-form-model-item label="系统编码" prop="systemCode">
          <a-input v-model="formData.systemCode" placeholder="系统编码" style="width:240px;" />
        </a-form-model-item>
        <a-form-model-item label="是否为必填项" prop="isRequired">
          <a-radio-group v-model="formData.isRequired">
            <a-radio :value="true">是</a-radio>
            <a-radio :value="false">否</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="排序" prop="displayOrder">
          <a-input-number v-model="formData.displayOrder" :min="0" :step="1" style="width:240px;" />
        </a-form-model-item>
        <a-row>
          <a-col :span="24" style="text-align: center">
            <a-button type="primary" @click="submit">提交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </div>
</template>

<script>
import { isEmptyParams } from "@/views/utils/common"
import parameterTypeSelect from '@/views/system/parameterType/components/parameterTypeSelect.vue'
import parentSelect from '@/views/system/parameter/components/parentSelect.vue'

export default {
  components: { parameterTypeSelect, parentSelect },
  name: 'parameterManager',
  data () {
    return {
      formData: { id: null, typeId: null, parentId: null, name: null, gbCode: null, treeCode: null, displayOrder: null, noteState: null, systemCode: null },
      rules: {
        typeId: { required: true, message: '请选择字典类型', trigger: 'change' },
        parentId: { required: false, message: '请选择父类型', trigger: 'change' },
        name: { required: true, message: '请输入字典名称', trigger: 'blur' },
        gbCode: { required: false, message: '请输入标准编码', trigger: 'blur' },
        treeCode: { required: false, message: '请输入树编码', trigger: 'blur' },
        displayOrder: { required: true, message: '请输入排序', trigger: 'blur' },
        systemCode: { required: false, message: '请输入系统编码', trigger: 'blur' },
        isRequired: { required: false, message: '请选择是否必填', trigger: 'blur' }
      },
      loading: false,
    }
  },
  props: {
    value: {
      type: String,
      default: () => {
        return null
      }
    },
  },
  created () {
    if (!!this.value) {
      this.getParameterById()
    }
  },
  methods: {
    getParameterById () {//提交字典信息
      this.loading = true
      this.$api.parameter.getParameterById({ id: this.value }).then(({ data = {} }) => {
        if (data) {
          data.typeId = data.typeId ? data.typeId + '' : ''
          data.parentId = data.parentId ? data.parentId + '' : ''
          this.formData = data
        } this.loading = false
      }).catch(() => { this.loading = false })
    },
    submit () {//提交字典信息
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          let pars = isEmptyParams(this.formData)
          let par = { ...pars }
          this.$api.parameter.updateSystemParameter(par).then(({ data = {} }) => {
            if (data) {
              this.$message.success('成功！')
              this.$emit('close', 'edit')
            }
            this.loading = false
          }).catch(() => { this.loading = false })
        }
        else { }
      })
    },
  }
}
</script>

<style scoped lang="less">
.app-content {
  .ant-form-item {
    margin-bottom: 6px;
  }
  ::v-deep .ant-row {
    .ant-col {
      display: inline-block;
    }
    .ant-form-item-label {
      width: 110px;
    }
    .ant-form-item-control-wrapper {
      width: calc(100% - 110px);
    }
    .ant-form-explain {
      margin-left: 5px;
      display: inline-block;
    }
  }
}
</style>
