<template>
  <el-dialog :close-on-click-modal="false" :title="type" class="add" :visible.sync="dialogVisible" v-if="dialogVisible">
    <el-form ref="formData" :model="formData" :rules="rules" label-width="120px" v-loading="dialogLoading">
      <el-row :gutter="20">
        <el-col :span="20">
          <el-form-item label="字段名：" prop="name">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
          <el-form-item label="字段类型：" prop="type">
            <el-select v-model="formData.type" style="width:100%;">
              <el-option v-for="(item, index) in options" :key="index" :label="item.name" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      options: [
        { name: '文本', value: 'text' },
        { name: '数字', value: 'number' },
        { name: '日期', value: 'date' },
        { name: '对象', value: 'object' }
      ],
      dialogVisible: this.value,
      dialogLoading: false,
      formData: {},
      rules: {
        type: [{ required: true, message: '字段类型不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '字段名不能为空', trigger: 'blur' }]
      }
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: ''
    },
    checkItems: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  watch: {
    value(val) {
      this.dialogVisible = val
      if (val) {
        this.formData = this.type === '编辑' ? this.checkItems : {}
      }
    },
    dialogVisible(val) {
      this.$emit('input', val)
    }
  },
  methods: {
    submit() {
      this.$refs['formData'].validate((valid) => {
        if (valid) {
          this.$emit('confirm', this.formData)
          this.dialogVisible = false
        }
      })
    }
  },
  mounted() {
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
  .add /deep/ .el-input {
    width: 80%;
  }
</style>
