<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :before-close="close"
    :visible.sync="visible"
  >
    <el-form ref="form" :model="departmentInfo" label-width="100px" :rules="formRules">
          <el-form-item label="分类名称:" prop="departmentNumber">
            <el-input style="width:300px;" v-model="departmentInfo.mainTypeName"></el-input>
          </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">添加</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isMobile } from "@/utils/validate";
export default {
  data() {
    var validatePassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error("密码不能为空"));
      } else {
        callback();
      }
    };
    var validateComfirmPassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error("确认密码不能为空"));
      } else if (this.dataForm.password !== value) {
        callback(new Error("确认密码与密码输入不一致"));
      } else {
        callback();
      }
    };
    var validateEmail = (rule, value, callback) => {
      if (!isEmail(value)) {
        callback(new Error("邮箱格式错误"));
      } else {
        callback();
      }
    };
    var validateMobile = (rule, value, callback) => {
      if (!isMobile(value)) {
        callback(new Error("手机号格式错误"));
      } else {
        callback();
      }
    };
    return {
        // 部门信息
      departmentInfo: {
        mainTypeName:"",
      },
      roleList: [],
      dataForm: {
        id: 0,
        userName: "",
        password: "",
        comfirmPassword: "",
        salt: "",
        email: "",
        mobile: "",
        roleIdList: [],
        status: 1,
      },
    //   表单校验规则
      formRules: {
        mainTypeName: [
            { required: true, message: '请输入分类名称', trigger: 'blur' }
        ],
      },
    };
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    companyStructural: {
      type: [Object, Array],
      default: () => {
        return [];
      },
    },
  },
  methods: {
    close(){
        this.$emit('close')
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
            let {data}=await this.$api.saveNewContentType(
                this.departmentInfo.mainTypeName
            )
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                //   关闭窗口
                this.$emit('close')
                //   刷新父组件列表
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
        }
      });
    },
  },
};
</script>
