<template>
  <el-dialog v-dialogDrag ref="diaFrom" :title="title" :close-on-click-modal="false" :visible.sync="isDiaBox"
    width="45%" top="10px" :before-close="cancel">
    <el-form :model="form" style="padding:10px 20px" :rules='rules' label-width="110px" ref="ruleForm">
      <el-form-item label="密集架名称：" prop="frameName">
        <el-input v-model="form.frameName" style="width:70%" size="mini" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="密集架宽度：" prop="layerWidth">
        <el-input-number v-model="form.layerWidth" style="width:70%" placeholder="请输入" size="mini" step-strictly
          controls-position="right" :min="1">
        </el-input-number>
      </el-form-item>
      <el-form-item label="  ">
        <el-button @click="submitForm('ruleForm')" type="primary" size="mini">保存</el-button>
        <el-button @click="cancel" type="info" size="mini">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
  export default {
    props: {
      isDiaBox: {
        type: Boolean,
        default: () => false,
      },
      title: {
        type: String,
        default: () => "",
      },
    },
    data() {
      return {
        form: {
          frameName: "",
          layerWidth: undefined,
          id: ""
        },
        rules: {
          frameName: [{
            required: true,
            message: '请输入密集架名称',
            trigger: 'blur'
          }],
          layerWidth: [{
            required: true,
            message: '请输入密集架宽度',
            trigger: 'blur'
          }],
        },
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.title === '新增') {
              this.$store.dispatch('denseFrameParameters/addData', this.form).then(res => {
                this.cancel()
                this.$emit('getData')
              })
            } else {
              this.$store.dispatch('denseFrameParameters/updateData', this.form).then(res => {
                this.cancel()
                this.$emit('getData')
              })
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      cancel() {
        this.$refs['ruleForm'].resetFields();
        for (let key in this.form) {
          if (key === 'layerWidth') {
            this.form[key] = undefined
          } else {
            this.form[key] = ""
          }
        }
        this.$emit('cancel')
      },

    }
  }

</script>
<style lang='scss' scoped>
  ::v-deep .el-form .el-form-item .el-input__inner {
    text-align: left;
  }

</style>
