<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="fileNum" v-if="title==='新增'">
        <el-input-number v-model="form.fileNum" style="width:70%" placeholder="请输入" size="mini" step-strictly
          controls-position="right" :min="1">
        </el-input-number>
      </el-form-item>
      <el-form-item label="档案盒类型：" prop="fileboxId">
        <el-select style="width:70%" v-model="form.fileboxId" clearable size="mini" placeholder="请选择">
          <el-option v-for="(item, index) in boxLIst" :key="index" :label="item.boxName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="档案盒编号：" prop="labelId" v-if="title!=='新增'">
        <el-input style="width:70%" v-model="form.labelId" placeholder="请输入"></el-input>
      </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,
      },
      boxLIst: {
        type: Array,
        default: () => [],
      },
      title: {
        type: String,
        default: () => "",
      },
      tableVal: {
        type: Object,
        default: () => {},
      },
    },
    data() {
      return {
        form: {
          fileNum: undefined,
          fileboxId: "",
          labelId: "",
          id: ""
        },
        rules: {
          fileNum: [{
            required: true,
            message: '请输入档案盒数量',
            trigger: 'blur'
          }],
          fileboxId: [{
            required: true,
            message: '请输入档案盒类型',
            trigger: 'blur'
          }],
          labelId: [{
            required: true,
            message: '请输入档案盒编号',
            trigger: 'blur'
          }],
        },
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.title === '新增') {
              const params = {
                fileType: this.form.fileboxId,
                fileNum: this.form.fileNum,
              }
              this.$store.dispatch('fileBoxManagement/addData', params).then(res => {
                this.cancel()
                this.$emit('getData')
              })
            } else {
              let data = {
                fileboxId: this.form.fileboxId,
                labelId: this.tableVal.labelId,
                labelIdNew:this.form.labelId,
                id: this.form.id,
              }
              this.$store.dispatch('fileBoxManagement/updateData', data).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 === 'fileNum') {
            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>
