<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="boxName">
        <el-input v-model="form.boxName" style="width:70%" size="mini" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="档案盒长度：" prop="boxLength">
        <el-input-number v-model="form.boxLength" style="width:70%" placeholder="请输入" size="mini" step-strictly
          controls-position="right" :min="1">
        </el-input-number>
      </el-form-item>
      <el-form-item label="档案盒宽度：" prop="boxWidth">
        <el-input-number v-model="form.boxWidth" style="width:70%" placeholder="请输入" size="mini" step-strictly
          controls-position="right" :min="1">
        </el-input-number>
      </el-form-item>
      <el-form-item label="档案盒高度：" prop="boxHigh">
        <el-input-number v-model="form.boxHigh" style="width:70%" placeholder="请输入" size="mini" step-strictly
          controls-position="right" :min="1">
        </el-input-number>
      </el-form-item>
      <el-form-item label="档案盒硬度：" prop="boxPachy">
        <el-input-number v-model="form.boxPachy" 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: {
          boxName: "",
          boxLength: undefined,
          boxWidth: undefined,
          boxHigh: undefined,
          boxPachy: undefined,
          id: ""
        },
        rules: {
          boxName: [{
            required: true,
            message: '请输入档案盒名称',
            trigger: 'blur'
          }],
          boxLength: [{
            required: true,
            message: '请输入档案盒长度',
            trigger: 'blur'
          }],
          boxWidth: [{
            required: true,
            message: '请输入档案盒宽度',
            trigger: 'blur'
          }],
          boxHigh: [{
            required: true,
            message: '请输入档案盒高度',
            trigger: 'blur'
          }],
          boxPachy: [{
            required: true,
            message: '请输入档案盒硬度',
            trigger: 'blur'
          }]
        },
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.title === '新增') {
              this.$store.dispatch('fileParameters/parameterAdd', this.form).then(res => {
                this.cancel()
                this.$emit('getData')
              })
            } else {
              this.$store.dispatch('fileParameters/parameterUpdate', 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 === 'boxName') {
            this.form[key] = ""
          } else {
            this.form[key] = undefined
          }
        }
        this.$emit('cancel')
      },

    }
  }

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

</style>
