<template>
  <div>
    <xy-dialog ref="dialog" :width="40" :is-show.sync="isShow" :type="'form'"
      :title="type === 'add' ? '新增基础信息' : '编辑基础信息'" :form="form" :rules='rules' @submit="submit">
      <template v-slot:name>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>配置名称：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="form.name" placeholder="请输入配置名称" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:key>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>英文标识：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="form.key" placeholder="请输入英文标识" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:value>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>配置值：
          </div>
          <div class="xy-table-item-content">
           <el-input v-model="form.value" placeholder="请输入配置值" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>

    </xy-dialog>

  </div>
</template>

<script>
  import {
    show,
    save
  } from "@/api/info/configs.js"
  export default {
    components: {

    },
    data() {
      return {
        isShow: false,
        type: 'add',
        id: '',
        form: {
          name: '',
          key: '',
          value: ''
        },
        rules: {
          name:[{
            required:true,
            message:'请输入配置名称'
          }],
          key:[{
            required:true,
            message:'请输入英文标识'
          }]
        }
      }
    },
    created() {},
    methods: {
      submit() {
        if (this.id) {
          this.form.id = this.id
        }
        if (this.type == 'add') {
          this.form.id = ''
        }
        save(this.form).then(res => {
          this.$message({
            type: 'success',
            message: this.type === 'add' ? '新增成功' : '编辑成功'
          })
          this.isShow = false
          this.$emit('refresh')
        })
      },

      getDetail() {
        show({
          id: this.id
        }).then(res => {
          this.form = this.base.requestToForm(res,this.form)
        })
      }
    },
    watch: {
      isShow(newVal) {
        if (newVal) {
          if (this.type === 'editor') {
            this.getDetail()
          }
        } else {
          this.id = ''
          this.$refs['dialog'].reset()
        }
      },
    }

  }
</script>

<style scoped lang="scss">
  ::v-deep .name,
  ::v-deep .key,
  ::v-deep .value {
    flex-basis: 100%;
  }
</style>
