addMaterialGroupDia.vue 4.88 KB
<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="720px" :before-close="!dialogVisible">
      <el-form ref="materialGroupAddForm" :model="materialGroupAddForm" @submit.native.prevent label-width="110px" :rules="materialGroupAddRules">
        <el-form-item label="分组名称:" prop="materialGroupName">
          <el-input v-model="materialGroupAddForm.materialGroupName" maxlength="8" show-word-limit :size="size" placeholder="请输入分组名称" style="width: 567px" @keyup.enter.native="submitForm"></el-input>
        </el-form-item>
        <!-- <el-form-item label="业务类型:" prop="businessType">
          <el-checkbox-group class="checkbox-group" :max="3" v-model="materialGroupAddForm.businessType">
            <el-checkbox v-for="item in businessTypes" :key="item.id" :label="item.dictLabel"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <span class="tips">添加业务类型,该素材组里面的所有素材均会添加此业务类型</span> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" :size="size">取 消</el-button>
        <el-button type="primary" @click="submitForm" :size="size">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'addMaterialGroupDia',
  props: {
    eventBus: Object
    // businessTypes: {
    //   type: Array
    // }
  },
  data() {
    return {
      title: '创建素材组',
      dialogVisible: false,
      materialGroupAddForm: {
        id: '',
        materialGroupName: '',
        businessType: []
      },
      groupCounter: [],
      materialGroupAddRules: {
        materialGroupName: [
          { required: true, message: '请输入分组名称', trigger: 'blur' },
          { max: 16, message: '分组名称最长为8个字', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(addOrEdit, value, group) {
      // this.findAllDictData('business_type')
      this.dialogVisible = true
      this.materialGroupAddForm = {
        materialGroupName: ''
        // businessType: []
      }
      this.groupCounter = group
      if (addOrEdit == 2) {
        this.title = '编辑素材组'
        if (!value) {
          this.materialGroupAddForm.materialGroupName = '新车素材组'
          return
        }
        var params = {}
        params.id = value.id
        this.$apis.material.materialGroupDetail(params).then(res => {
          this.materialGroupAddForm = res
          // if (res.businessType) {
          //   this.materialGroupAddForm.businessType = this.materialGroupAddForm.businessType.split(',')
          // } else {
          //   this.materialGroupAddForm.businessType = []
          // }
        })
      }
      // Object.assign(this.materialGroupAddForm, value)
    },
    // 获取所有业务类型
    // findAllDictData(dictType) {
    //   var params = {
    //     dictType
    //   }
    //   this.$apis.material.findAllDictData(params).then(res => {
    //     if (res) {
    //       if (dictType == 'business_type') {
    //         this.businessTypes = res
    //       }
    //     }
    //   })
    // },
    getParams() {
      var params = JSON.parse(JSON.stringify(this.materialGroupAddForm))
      return params
    },
    // 提交创建素材组
    submitForm() {
      this.$refs.materialGroupAddForm.validate(valid => {
        if (valid) {
          var params = this.getParams()
          params.orderFlag = true
          // 判断是创建还是编辑
          if (this.materialGroupAddForm.id) {
            this.eventBus.$emit('editMaterialGroup', params)
            this.dialogVisible = false
            return
          }
          // 判断素材组名称是否重复
          var groupName = []
          this.groupCounter.forEach(item => {
            groupName.push(item.materialGroupName)
          })
          var idx = groupName.indexOf(this.materialGroupAddForm.materialGroupName)
          if (idx != -1) {
            this.$message({
              message: '素材组名称不能重复',
              type: 'warning'
            })
            return
          }
          this.eventBus.$emit('addMaterialGroup', params)
          this.dialogVisible = false
        } else {
          this.$message({
            message: '请输入素材组名称',
            type: 'warning'
          })
          return false
        }
      })
    }
    // handleCheckChange(val) {
    //   console.log(val)
    //   this.materialGroupAddForm.businessType = val
    //   // console.log(this.materialGroupAddForm);
    // }
  }
}
</script>

<style lang="scss" scope>
.checkbox-group {
  width: 567px;
  box-sizing: border-box;
  background: #f5f5f5;
  padding: 16px 0 0 16px;
  .el-checkbox {
    width: 140px;
    margin: 0;
    padding: 0;
  }
}
.tips {
  font-size: 12px;
  color: #666666;
  display: block;
  margin: -10px 0 0 110px;
}
</style>