<template>
  <app-dialog
    v-model="visible"
    :loading="loading"
    :show-close="false"
    :title="title"
    append-to-body
    class="detail-dialog"
    fullscreen
    @cancel="onCancel"
    @confirm="onConfirm"
  >
    <template #title-area>
      <div class="flex-row-sb">
        <el-page-header content="" @back="visible = false" />
        <el-button-group>
          <el-button type="primary">保 存 </el-button>
          <!-- v-if="!formData.deployed" -->
          <el-button>发 布 </el-button>
          <el-button>创建表单</el-button>
        </el-button-group>
      </div>
    </template>
    <el-form ref="form" :model="formData" class="h-100" label-width="100px">
      <el-container class="container h-100 pa-10">
        <el-header class="bg-white py-10" height="auto">
          <model-header ref="objHeader" :data-view="{}" :form-data="formData" @blur="blur()" />
        </el-header>
        <el-container class="mt-10">
          <el-aside v-if="asideShow" class="aside-tree__wrap" width="320px">
            <model-ents
              :form-data="formData"
              @addEntRows="addEntRows"
              @addGrandSonEnt="addGrandSonEnt"
              @chineseFormat="chineseFormat"
              @deleteEntRows="deleteEntRows"
              @deleteGrandSonEntRows="deleteGrandSonEntRows"
              @entBlur="entBlur"
              @getEntsByIndex="getEntsByIndex"
              @getGrandSonEntsByIndex="getGrandSonEntsByIndex"
            />
          </el-aside>
          <!-- <div
            class="navbar-collapse"
            :class="{'navbar-collapse-right': !asideShow}"
            @click="asideShow = !asideShow"
          >
            <div class="navbar-collapse-bg">
              <i
                class="navbar-collapse-arrow"
                :class="{
                  'el-icon-arrow-left': asideShow,
                  'el-icon-arrow-right': !asideShow,
                }"
              ></i>
            </div>
          </div> -->
          <el-main class="pa-0 ml-10">
            <model-attr
              :attr-table-data="attrTableData"
              :ent-index.sync="entIndex"
              :form-data.sync="formData"
              :grand-son-index="grandSonIndex"
              :open-grand-son-attr="openGrandSonAttr"
              @chineseFormat="chineseFormat"
              @entBlur="entBlur"
              @getEntsByIndex="getEntsByIndex"
              @getFormData="getFormData"
              @handleDialogClose="handleDialogClose"
            />
          </el-main>
        </el-container>
      </el-container>
    </el-form>
  </app-dialog>
</template>

<script>
import dialogMixin from '@/mixins/dialog.js';
import ModelHeader from './ModelHeader.vue';
import ModelEnts from './ModelEnts.vue';
import ModelAttr from './ModelAttr.vue';

import form from '@/api/form.js';
import util from '@/hotent-ui-util.js';
import req from '@/request.js';
import main from '@/main.js';

export default {
  name: 'CompanyDialog',
  components: { ModelHeader, ModelEnts, ModelAttr },
  mixins: [dialogMixin],
  props: {
    selectedRows: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      formData: {
        alias: '',
        categoryId: '',
        categoryName: '',
        deployed: 0,
        description: '',
        dsName: '',
        isCreateTable: '',
        isExternal: '',
        status: 'normal',
        supportDb: true,
        ents: [],
      },
      rules: {
        name: [{ required: true, message: '资源名称不能为空' }],
        is_active: [{ required: true, message: '资源类型不能为空' }],
        type: [{ required: true, message: '来源格式不能为空' }],
        is_publish: [{ required: true, message: '是否发布不能为空' }],
      },
      loading: false,
      asideShow: true,
      entIndex: 0,
      grandSonIndex: -1,
      openGrandSonAttr: false,
      attrTableData: [],
      messageCations: {},
    };
  },
  mounted() {},
  methods: {
    /**
     * @description: dialog每次显示时触发的init方法
     * @param {*} data 传入data
     * @return {*}
     */
    initData(data) {
      const orgForm = this.$options.data().formData;
      this.formData = { ...orgForm, ...data };
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    // 表单数据加载
    getFormData() {
      if (this.dataView.id) {
        form.getEntData(this.dataView.id, (resp) => {
          resp.data.ents.forEach((entData, index) => {
            if (entData.isExternal == '1' && index == 0) {
              entData.show = '主实体-外';
            } else if (entData.isExternal == '1') {
              entData.show = '子实体-外';
            }
            if (index === 0) {
              entData.isCheck = true;
            }
            if (entData.children) {
              entData.children.forEach((v) => {
                if (v.isExternal == '1') {
                  v.show = '孙实体-外';
                }
              });
            }
            for (let q = 0; q < entData.attributeList.length; q++) {
              if (entData.attributeList[q]) {
                if (entData.attributeList[q].isRequired == 0) {
                  entData.attributeList[q].isRequired = '0';
                } else {
                  entData.attributeList[q].isRequired = '1';
                }
              }
              for (let i = 0; i < entData.attributeList.length - 1 - q; i++) {
                if (entData.attributeList[i].index > entData.attributeList[i + 1].index) {
                  const temp = entData.attributeList[i];
                  entData.attributeList[i] = entData.attributeList[i + 1];
                  entData.attributeList[i + 1] = temp;
                }
              }
            }
          });
          this.formData = resp.data;
        });
      }
    },
    // 新增实体
    addEntRows() {
      const this_ = this;
      util
        .validateForm(this_, 'form')
        .then(() => {
          if (this_.formData.ents.length > 0) {
            for (let i = 0; i < this_.formData.ents.length; i++) {
              if (this_.checkGrandSonEnts(i)) {
                return;
              }
            }
            this_.countAttrLength = 0;
            if (!this_.checkEntFunc(this_.formData.ents)) {
              return false;
            }
            this_.extLen = 0;
          }
          this_.formData.ents.forEach((data) => {
            data.isCheck = false;
          });
          this_.entIndex = ++this_.entIndex;
          let row = {
            desc: '',
            index: this_.entIndex,
            name: '',
            packageId: '',
            show: '子实体',
            children: [],
            relation: 'onetomany',
            attributeList: [],
            status: '',
            isCheck: true,
          };
          if (this_.formData.ents.length == 0) {
            this_.entIndex = 0;
            row = {
              desc: '',
              index: 0,
              name: '',
              packageId: '',
              show: '主实体',
              relation: 'main',
              status: '',
              attributeList: [],
              isCheck: true,
            };
          }
          if (this_.formData.ents.length > 0 && this_.checkGrandSonEnts(this_.formData.ents.length - 1)) {
            return;
          }
          this_.formData.ents.push(row);
        })
        .catch((reason) => {
          if (!Array.isArray(reason)) {
            console.log(reason);
            return;
          }
          let rule = reason.map((v) => {
            return v.rule;
          });
          rule = rule.unique();
          if (rule.includes('required')) {
            const n = this.$message.warning('请将信息填写完整');
            this.messageCations[n.id] = n;
          } else if (rule.includes('regex')) {
            this.$message.warning('只能输入字母、数字、下划线，且以字母开头');
          }
        });
    },
    // 删除实体
    deleteEntRows(index) {
      this.entIndex = index;
      if (!this.formData.ents[this.entIndex].attributeList) {
        this.formData.ents[this.entIndex].attributeList = [];
      }
      this.formData.ents.splice(index, 1);
      if (this.entIndex >= this.formData.ents.length) {
        this.entIndex = this.formData.ents.length - 1;
        this.getEntsByIndex(this.entIndex);
      }
      if (this.formData.ents.length > 0) {
        if (index == 0) {
          if (this.formData.ents[0].isExternal != '1') {
            this.formData.ents[0].show = '主实体';
            this.formData.ents[0].relation = 'main';
            this.formData.ents[0].children = [];
          } else {
            this.formData.ents[0].show = '主实体-外';
            this.formData.ents[0].relation = 'main';
          }
        }
      }
    },
    checkGrandSonEnts(index, param) {
      if ((this.formData.ents[index].children && this.formData.ents[index].children.length == 0) || !this.formData.ents[index].children) {
        return false;
      }
      const child = this.formData.ents[index].children;
      const len = this.formData.ents[index].children.length - 1;
      child[len].comment = child[len].desc;
      child[len].description = child[len].desc;
      if (!param && child[len].attributeList.length == 0) {
        this.$message({ message: '请添加字段信息', type: 'warning' });
        return true;
      }
    },
    addGrandSonEnt(index) {
      this.getEntsByIndex(index);
      const row = {
        desc: '',
        index: this.entIndex,
        name: '',
        packageId: '',
        show: '孙实体',
        relation: 'onetomany',
        attributeList: [],
        status: '',
        isCheck: true,
      };
      if (!this.formData.ents[index].children) {
        this.$set(this.formData.ents[index], 'children', []);
      }
      if (this.checkGrandSonEnts(index)) {
        return;
      }
      this.formData.ents[index].children.push(row);
      this.grandSonIndex = this.formData.ents[index].children.length - 1;
      this.openGrandSonAttr = true;
      this.attrTableData = this.formData.ents[index].children[this.grandSonIndex].attributeList;
    },
    getEntsByIndex(index, param) {
      // 如果是删除自动选择上一个实体
      if (param == 'delete') {
        if (this.formData.ents[index]) {
          this.entIndex = index;
          this.formData.ents.forEach((data) => {
            data.isCheck = false;
          });
          this.formData.ents[index].isCheck = true;
          return true;
        }
      } else {
        const this_ = this;
        util
          .validateForm(this, 'form')
          .then(() => {
            this.openGrandSonAttr = false;
            if (param == 'grandSonEnt') {
              this.openGrandSonAttr = true;
            }
            if (this.formData.ents[index]) {
              this.entIndex = index;
              this.formData.ents.forEach((data) => {
                data.isCheck = false;
              });
              this.formData.ents[index].isCheck = true;
              return true;
            }
          })
          .catch((reason) => {
            let rule = reason.map((v) => {
              return v.rule;
            });
            rule = rule.unique();
            this_.closeAllNotification();
            if (rule.includes('required')) {
              const n = this_.$message.warning('请将信息填写完整');
              this_.messageCations[n.id] = n;
            } else if (rule.includes('regex')) {
              const n = this_.$message.warning('只能输入字母、数字、下划线，且以字母开头');
              this_.messageCations[n.id] = n;
            }
          });
      }
    },
    chineseFormat(id, list, param, index, v) {
      const _this = this;
      const d = document.getElementById(id);
      // disabled 或者 readonly 时 不需要请求
      let timer = null;
      if (d === null || d.__vue__.disabled || d.__vue__.readonly || !v) return;
      if (timer) {
        clearTimeout(timer);
      }
      timer = setTimeout(function () {
        req
          .request({
            url: `${window.context.uc}/base/tools/v1/getPinyin`,
            method: 'GET',
            params: { chinese: v, type: 0 },
          })
          .then((res) => {
            if (res.data.state) {
              list[index][param] = res.data.value;
              main.$nextTick(() => {
                main.$validator.validate();
              });
            }
          });
      }, 500);
    },
    entBlur() {
      if (
        this.openGrandSonAttr &&
        this.formData.ents[this.entIndex].children[this.grandSonIndex].attributeList &&
        this.formData.ents[this.entIndex].children[this.grandSonIndex].attributeList.length > 0
      ) {
        this.formData.ents[this.entIndex].children[this.grandSonIndex].attributeList.forEach((item) => {
          item.desc = item.comment;
        });
      }
      if (typeof this.entBlurDescRepeatIndex === 'number') {
        if (document.getElementById(`changeEntsDesc${this.entBlurDescRepeatIndex}`)) {
          document.getElementById(`changeEntsDesc${this.entBlurDescRepeatIndex}`).getElementsByTagName('div')[0].firstElementChild.style.border = '';
        }
        if (document.getElementById(`changeGrandSonEntsDesc${this.entBlurDescRepeatIndex}`)) {
          document.getElementById(`changeGrandSonEntsDesc${this.entBlurDescRepeatIndex}`).getElementsByTagName('div')[0].firstElementChild.style.border = '';
        }
      }
      if (typeof this.entBlurNameRepeatIndex === 'number') {
        if (document.getElementById(`entName${this.entBlurNameRepeatIndex}`)) {
          document.getElementById(`entName${this.entBlurNameRepeatIndex}`).getElementsByTagName('div')[0].firstElementChild.style.border = '';
        }
        if (document.getElementById(`changeGrandSonEntsName${this.entBlurNameRepeatIndex}`)) {
          document.getElementById(`changeGrandSonEntsName${this.entBlurNameRepeatIndex}`).getElementsByTagName('div')[0].firstElementChild.style.border = '';
        }
      }
      if (this.attrCommentElementId && document.getElementById(this.attrCommentElementId)) {
        document.getElementById(this.attrCommentElementId).getElementsByTagName('div')[0].firstElementChild.style.border = '';
      }
      if (typeof this.checkAttrNameIndex === 'number') {
        if (document.getElementById(`${this.formData.ents[this.entAttrIndex].attributeList[this.checkAttrNameIndex].name}attrName${this.checkAttrNameIndex}`)) {
          document
            .getElementById(`${this.formData.ents[this.entAttrIndex].attributeList[this.checkAttrNameIndex].name}attrName${this.checkAttrNameIndex}`)
            .getElementsByTagName('div')[0].firstElementChild.style.border = '';
        }
        const sonAttr = this.formData.ents[this.sonAttrIndex];
        if (sonAttr) {
          document
            .getElementById(`${sonAttr.children[this.entAttrIndex].attributeList[this.checkAttrNameIndex].name}attrName${this.checkAttrNameIndex}`)
            .getElementsByTagName('div')[0].firstElementChild.style.border = '';
        }
        this.checkAttrNameIndex = null;
      }
      if (this.defaultValueType) {
        document.getElementsByName(`defaultValue${this.defaultValueIndex}`)[0].style.border = '';
        this.defaultValueType = null;
      }
      if (this.attrLenElementId && document.getElementById(this.attrLenElementId)) {
        document.getElementById(this.attrLenElementId).style.border = '';
      }
      if (this.attrDecimaElementId && document.getElementById(this.attrDecimaElementId)) {
        document.getElementById(this.attrDecimaElementId).style.border = '';
      }
    },
    // 关闭所有校验通知
    closeAllNotification() {
      for (const key in this.messageCations) {
        this.messageCations[key].close();
        delete this.messageCations[key];
      }
    },
    getGrandSonEntsByIndex(i, index) {
      this.getEntsByIndex(index, 'grandSonEnt');
      this.grandSonIndex = i;
      if (this.formData.ents[this.entIndex].children && this.formData.ents[this.entIndex].children[i]) {
        this.attrTableData = this.formData.ents[this.entIndex].children[i].attributeList;
      }
    },
    deleteGrandSonEntRows(i, index) {
      this.getEntsByIndex(index, 'noCheck');
      this.grandSonIndex = i;
      if (!this.formData.ents[this.entIndex].attributeList) {
        this.formData.ents[this.entIndex].children[i].attributeList = [];
      }
      this.formData.ents[this.entIndex].children.splice(i, 1);
      this.openGrandSonAttr = false;
      this.attrTableData = this.formData.ents[this.entIndex].attributeList;
      if (this.formData.ents[this.entIndex].children[i - 1] && this.formData.ents[this.entIndex].children[i - 1].attributeList.length > 0) {
        this.attrTableData = this.formData.ents[this.entIndex].children[i - 1].attributeList;
      }
    },
    checkEntFunc(ent, sonIndex) {
      const entVal = {};
      if (ent.length < 1) {
        this.$message({ type: 'warning', message: '请添加实体对象！' });
        return false;
      }
      for (let i = 0; i < ent.length; i++) {
        if (ent[i].show != '孙实体') {
          ent[i].description = ent[i].comment;
          ent[i].desc = ent[i].comment;
        }
        if (!entVal[`desc${ent[i].desc}`]) {
          entVal[`desc${ent[i].desc}`] = ent[i].desc;
        } else {
          this.entBlurDescRepeatIndex = i;
          if (document.getElementById(`changeEntsDesc${i}`) && ent[i].show == '子实体') {
            this.$message({
              message: `已存在子实体描述为【${ent[i].desc}】的bo实体`,
              type: 'warning',
            });
            document.getElementById(`changeEntsDesc${i}`).getElementsByTagName('div')[0].lastElementChild.focus();
            document.getElementById(`changeEntsDesc${i}`).getElementsByTagName('div')[0].firstElementChild.style.border = '1px solid red';
          } else {
            this.$message({
              message: `已存在孙实体描述为【${ent[i].desc}】的bo实体`,
              type: 'warning',
            });
            document.getElementById(`changeGrandSonEntsDesc${i}`).getElementsByTagName('div')[0].lastElementChild.focus();
            document.getElementById(`changeGrandSonEntsDesc${i}`).getElementsByTagName('div')[0].firstElementChild.style.border = '1px solid red';
          }
          return false;
        }
        if (!entVal[`name${ent[i].name}`]) {
          entVal[`name${ent[i].name}`] = ent[i].name;
        } else {
          this.entBlurNameRepeatIndex = i;
          if (document.getElementById(`entName${i}`) && ent[i].show == '子实体') {
            this.$message({
              message: `已存在子实体名称为【${ent[i].name}】的bo实体`,
              type: 'warning',
            });
            document.getElementById(`entName${i}`).getElementsByTagName('div')[0].lastElementChild.focus();
            document.getElementById(`entName${i}`).getElementsByTagName('div')[0].firstElementChild.style.border = '1px solid red';
          } else {
            this.$message({
              message: `已存在孙实体名称为【${ent[i].name}】的bo实体`,
              type: 'warning',
            });
            document.getElementById(`changeGrandSonEntsName${i}`).getElementsByTagName('div')[0].lastElementChild.focus();
            document.getElementById(`changeGrandSonEntsName${i}`).getElementsByTagName('div')[0].firstElementChild.style.border = '1px solid red';
          }
          return false;
        }
        if (ent[i].isExternal == '1') {
          ++this.extLen;
          if (i != 0 && this.extLen > 1 && !ent[i].fk) {
            this.$message({
              message: `请给${ent[i].comment}实体添加外键！`,
              type: 'warning',
            });
            return false;
          }
        }
        if (ent[i].attributeList.length > 0) {
          if (!this.checkAttrFunc(ent[i], i, sonIndex)) {
            return false;
          }
        } else {
          this.$message({ message: '请添加字段！', type: 'warning' });
          return false;
        }
        if (ent[i].children && ent[i].children.length > 0) {
          this.countAttrLength = 0;
          if (!this.checkEntFunc(ent[i].children, i)) {
            return false;
          }
        }
        this.countAttrLength = 0;
      }
      return true;
    },
    checkAttrFunc(ent, i, sonIndex) {
      const regPos = new RegExp('^\\d*(?:\\.\\d{1,2})?$');
      const attrVal = {};
      const attr = ent.attributeList;
      for (let j = 0; j < attr.length; j++) {
        attr[j].desc = attr[j].comment;
        if (attr[j].defaultValue == null) {
          attr[j].defaultValue = '';
        }
        attr[j].defaultValue = attr[j].defaultValue.trim();
        this.attrCommentElementId = `${attr[j].name}comment${j}`;
        if (ent.isExternal != '1') {
          if (!attrVal[`name${attr[j].name}`]) {
            attrVal[`name${attr[j].name}`] = attr[j].name;
          } else {
            this.entAttrIndex = i;
            if (sonIndex) {
              this.sonAttrIndex = sonIndex;
            }
            this.attrNameElementId = attr;
            this.checkAttrNameIndex = j;
            const msg = `实体：${ent.desc}（${ent.name}）中存在重复字段名【${attr[j].name}】`;
            this.$message({ message: msg, type: 'warning' });
            document.getElementById(`${this.attrNameElementId[j].name}attrName${j}`).focus();
            document.getElementById(`${this.attrNameElementId[j].name}attrName${j}`).getElementsByTagName('div')[0].firstElementChild.style.border =
              '1px solid red';
            return false;
          }
          this.attrLenElementId = `${attr[j].name}attrLen${j}`;
          if (attr[j].attrLength && attr[j].attrLength !== 0) {
            if (!regPos.test(attr[j].attrLength)) {
              this.entAttrIndex = i;
              if (sonIndex) {
                this.sonAttrIndex = sonIndex;
              }
              document.getElementById(this.attrLenElementId).focus();
              document.getElementById(this.attrLenElementId).style.border = '1px solid red';
              const n = this.$message.warning('请输入正确数字');
              this.messageCations[n.id] = n;
              return false;
            }
          }
          this.attrDecimaElementId = `${attr[j].name}decima${j}`;
          if (attr[j].decimalLen && attr[j].decimalLen !== 0) {
            if (!regPos.test(attr[j].decimalLen)) {
              this.entAttrIndex = i;
              if (sonIndex) {
                this.sonAttrIndex = sonIndex;
              }
              document.getElementById(this.attrDecimaElementId).focus();
              document.getElementById(this.attrDecimaElementId).style.border = '1px solid red';
              const n = this.$message.warning('请输入正确数字');
              this.messageCations[n.id] = n;
              return false;
            }
          }
          if (attr[j].decimalLen) {
            if (attr[j].decimalLen.length > 1) {
              const decimalLen = attr[j].decimalLen.substr(0, 1);
              if (decimalLen == '0') {
                attr[j].decimalLen = attr[j].decimalLen.substr(1, attr[j].decimalLen.length);
              }
            }
            if (attr[j].decimalLen < 0 || attr[j].decimalLen > 30) {
              this.entAttrIndex = i;
              if (sonIndex) {
                this.sonAttrIndex = sonIndex;
              }
              document.getElementById(this.attrDecimaElementId).focus();
              document.getElementById(this.attrDecimaElementId).style.border = '1px solid red';
              this.$message({
                message: '小数长度不能小于0或者大于30',
                type: 'warning',
              });
              return false;
            }
          }
          if (attr[j].dataType === 'varchar') {
            this.countAttrLength += parseInt(attr[j].attrLength);
          }
          if (attr[j].attrLength) {
            if (attr[j].attrLength.length > 1) {
              const attrLength = attr[j].attrLength.substr(0, 1);
              if (attrLength == '0') {
                attr[j].attrLength = attr[j].attrLength.substr(1, attr[j].attrLength.length);
              }
            }
            if (attr[j].dataType === 'varchar') {
              if (attr[j].attrLength < 1 || this.countAttrLength > 21500) {
                this.entAttrIndex = i;
                if (sonIndex) {
                  this.sonAttrIndex = sonIndex;
                }
                document.getElementById(this.attrLenElementId).focus();
                document.getElementById(this.attrLenElementId).style.border = '1px solid red';
                this.$message({
                  message: '整数长度不能小于1或者长度总和超出21500',
                  type: 'warning',
                });
                return false;
              }
            }

            if (attr[j].dataType === 'number' || attr[j].dataType == '数字') {
              const defaultVal = attr[j].defaultValue.split('.');
              if (Number(attr[j].attrLength) < 1) {
                this.entAttrIndex = i;
                if (sonIndex) {
                  this.sonAttrIndex = sonIndex;
                }
                document.getElementById(this.attrLenElementId).focus();
                document.getElementById(this.attrLenElementId).style.border = '1px solid red';
                this.$message({
                  message: '整数长度不能小于1',
                  type: 'warning',
                });
                return false;
              }
              if (Number(attr[j].attrLength) + Number(attr[j].decimalLen) > 38) {
                this.entAttrIndex = i;
                if (sonIndex) {
                  this.sonAttrIndex = sonIndex;
                }
                document.getElementById(this.attrLenElementId).focus();
                document.getElementById(this.attrLenElementId).style.border = '1px solid red';
                this.$message({
                  message: '整数长度与小数长度之和不能超过38',
                  type: 'warning',
                });
                return false;
              }
              if (defaultVal[1] && Number(defaultVal[1].length) > Number(attr[j].decimalLen)) {
                this.defaultValueIndex = j;
                this.defaultValueType = 'number';
                document.getElementsByName(`defaultValue${j}`)[0].focus();
                document.getElementsByName(`defaultValue${j}`)[0].style.border = '1px solid red';
                this.$message({
                  message: '默认值中小数长度超出设置值',
                  type: 'warning',
                });
                return false;
              }
              if (attr[j].defaultValue && attr[j].defaultValue.length > 21) {
                this.defaultValueIndex = j;
                this.defaultValueType = 'number';
                document.getElementsByName(`defaultValue${j}`)[0].focus();
                document.getElementsByName(`defaultValue${j}`)[0].style.border = '1px solid red';
                this.$message({ message: '整数长度不能超过21', type: 'warning' });
                return false;
              }
              if (!regPos.test(Number(attr[j].defaultValue))) {
                this.defaultValueIndex = j;
                this.defaultValueType = 'number';
                document.getElementsByName(`defaultValue${j}`)[0].focus();
                document.getElementsByName(`defaultValue${j}`)[0].style.border = '1px solid red';
                this.$message({ message: '默认值应为数字', type: 'warning' });
                return false;
              }
            }
            let d = attr[j].defaultValue;
            if (d.indexOf('.') > -1) {
              const i = d.indexOf('.');
              const start = d.substring(0, i);
              const end = d.substring(i + 1, attr[j].defaultValue.length);
              d = start + end;
            }
            if (d.length > attr[j].attrLength + attr[j].decimalLen) {
              this.defaultValueIndex = j;
              this.defaultValueType = 'varchar';
              document.getElementsByName(`defaultValue${j}`)[0].focus();
              document.getElementsByName(`defaultValue${j}`)[0].style.border = '1px solid red';
              this.$message({
                message: '默认值长度需小于整数长度',
                type: 'warning',
              });
              return false;
            }
          } else if (attr[j].dataType === 'varchar' || attr[j].dataType === 'number') {
            this.entAttrIndex = i;
            if (sonIndex) {
              this.sonAttrIndex = sonIndex;
            }
            document.getElementById(this.attrLenElementId).focus();
            document.getElementById(this.attrLenElementId).style.border = '1px solid red';
            this.$message({ message: '请添加整数长度', type: 'warning' });
            return false;
          }
        }
      }
      return true;
    },
    // 关闭表单对话框
    handleDialogClose() {
      this.$emit('loadTableData');
      this.visible = false;
      this.isSave = false;
      this.entIndex = 0;
      this.grandSonIndex = -1;
      this.openGrandSonAttr = false;
      this.countAttrLength = 0;
      this.formData = this.$options.data().formData;
      this.$router.push({ path: '/sysplat/businessObj' }); // form#businessObj
    },
    onCancel() {
      this.visible = false;
    },
    onConfirm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          // const params = this.handleParams()
          // try {
          //   this.loading = true
          //   if (this.data?.id) {
          //     await updateData(params)
          //   } else {
          //     await dataAdd(params)
          //   }
          //   this.$message.success('操作成功')
          //   this.onCancel()
          // this.$emit('confirm', {...this.formData})
          // } finally {
          //   this.loading = false
          // }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep {
  .gz-dialog {
    .el-dialog__header {
      padding: 10px;
    }
    .app-dialog-content {
      height: 100%;
    }
  }

  .el-dialog__body {
    padding: 0 !important;
  }
  .app-dialog-content {
    height: 100%;
    padding: 0;
  }

  .el-main {
    .el-card__body {
      padding: 15px;
    }
  }
}

.container {
  background: #f0f2f5;

  .navbar-collapse-bg {
    margin-left: -1px;
    margin-top: 1px;
    height: 28px;
    border-top: 8px solid transparent;
    border-bottom: 8px solid transparent;
    border-left: 10px solid #fff;
    border-right: none;
    position: relative;
  }

  .navbar-collapse-bg:hover {
    cursor: pointer;
  }

  .navbar-collapse-arrow {
    position: absolute;
    left: -12px;
    top: 7px;
  }
}
</style>
