<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-04-14 09:50:16
 * @LastEditors: 无尘
 * @LastEditTime: 2020-05-12 11:37:45
 -->
<!--
  <group-dialog  @closeGroup="closeGroup" @submitGroup="submitGroup"></group-dialog>
  import groupDialog from '@/components/set/group-dialog.vue';
 -->
<template>
  <el-dialog :title="!!editRow.categoryId ? '编辑分组' : '新增分组'" :visible.sync="dialogVisible" width="485px" :before-close="handleClose">
    <div class="">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="分组名称" prop="categoryName">
          <limitInput :inputWidth="300" :inputValue.sync="ruleForm.categoryName" :holder="'请输入分组名称'" :getByType="'word'" :maxLength="20"> </limitInput>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="toCancel">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import limitInput from '@/components/limit-input.vue';
import { _debounce } from '@/common/js/public';
import { postRequest } from '@/api/api';
import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
export default {
  props: {
    editRow: {
      type: Object,
      default() {
        return {};
      }
    },
    categoryParentId: {
      type: Object,
      default() {
        return '';
      }
    }
  },
  components: {
    limitInput
  },
  data() {
    return {
      editPersion: localStorage.getItem('userName'),
      dialogVisible: true,
      ruleForm: {
        categoryId: '',
        categoryName: '',
        categoryParentId: this.categoryParentId
      },
      rules: {
        categoryName: [{ required: true, message: '请输入模块名称', trigger: 'blur' }]
      }
    };
  },
  methods: {
    toCancel() {
      const that = this;
      that.$emit('closeGroup');
      that.$refs['ruleForm'].resetFields();
    },
    handleClose(done) {
      const that = this;
      that.$emit('closeGroup');
      that.$refs['ruleForm'].resetFields();
    },
    submitForm: _debounce(function(formName) {
      const that = this;
      that.$refs[formName].validate(valid => {
        if (valid) {
          that.postSave();
        }
      });
    }, 300),
    postSave() {
      const that = this;
      const data = {
        categoryParentId: that.ruleForm.categoryParentId || '0',
        categoryId: that.ruleForm.categoryId,
        categoryName: that.ruleForm.categoryName
      };
      postRequest('/haoban-manage3-web/mateial-add-category', data)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('操作成功', 'success');
            that.$refs['ruleForm'].resetFields();
            that.$emit('submitGroup');
          } else {
            errMsg.errorMsg(resData);
          }
        })
        .catch(error => {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    editRow(newData) {
      const that = this;
      if (Object.keys(newData).length) {
        that.ruleForm = JSON.parse(JSON.stringify(newData));
      }
    }
  },
  mounted() {
    const that = this;
    if (Object.keys(that.editRow).length) {
      that.ruleForm = JSON.parse(JSON.stringify(that.editRow));
    }
  }
};
</script>
<style lang="less" scoped>
.m-b-20 {
  margin-bottom: 20px;
}
.p-l-18 {
  padding-left: 18px;
}
</style>
