<template>
  <app-dialog v-model="visible" :loading="loading" :title="title" append-to-body class="detail-dialog" @cancel="onCancel" width="60%" :hideFooter="true">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-divider content-position="left"
        ><i class="el-icon-document" style="color: rgb(24, 144, 255)" /><span class="divider-top">数据资源项</span></el-divider
      >
      <el-descriptions :column="2" border labelClassName="label-w" class="form-content">
        <el-descriptions-item label="资源项名称">{{ form.name || '-' }}</el-descriptions-item>
        <el-descriptions-item label="资源项编码">{{ form.code || '-' }}</el-descriptions-item>
        <el-descriptions-item label="实体表中文名">{{ form.tableNameCh || '-' }}</el-descriptions-item>
        <el-descriptions-item label="实体表英文名">{{ form.tableNameEn || '-' }}</el-descriptions-item>
        <el-descriptions-item label="数据库表名" :span="2">{{ form.tableCode || '-' }}</el-descriptions-item>
        <el-descriptions-item label="实体表类别">
          <el-tag size="small">{{ dictionaryEscape('stblb', form.tableClass) || '-' }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="实体表类型"
          ><el-tag size="small">{{ dictionaryEscape('stblx', form.tableType) || '-' }}</el-tag></el-descriptions-item
        >
        <el-descriptions-item label="实体表描述" :span="2">{{ form.tableDesc || '-' }}</el-descriptions-item>
      </el-descriptions>
    </el-form>
  </app-dialog>
</template>

<script>
import dialogMixin from '@/mixins/dialog.js';
import { dictionaryEscape, getDictionaryByCode } from '@/utils/dictionary.js';
import codeEditor from '@/components/codeEditor';
import API from '@/api/business/dataDWCatalog';
export default {
  name: 'sourceDialog',
  mixins: [dialogMixin],
  components: { codeEditor },
  props: {},
  data() {
    return {
      form: {
        sourceType: '1',
        level: '1',
        tableClass: '1',
        tableType: '1',
        updateRate: '1',
        createTableSql: '',
      },
      rules: {
        name: [{ required: true, message: '资源项名称不能为空' }],
        code: [{ required: true, message: '资源项编码不能为空' }],
        sourceType: [{ required: true, message: '资源项格式' }],
        level: [{ required: true, message: '资源项密级' }],
        tableNameCh: [{ required: true, message: '实体表中文名不能为空' }],
        tableNameEn: [{ required: true, message: '实体表英文名不能为空' }],
        tableClass: [{ required: true, message: '实体表类别名不能为空' }],
        tableType: [{ required: true, message: '实体表类型名不能为空' }],
        tableCode: [{ required: true, message: '数据库表名不能为空' }],
        updateRate: [{ required: true, message: '更新周期名不能为空' }],
      },
      loading: false,
    };
  },
  mounted() {},
  methods: {
    dictionaryEscape,
    getDictionaryByCode,
    /**
     * @description: dialog每次显示时触发的init方法
     * @param {*} data 传入data
     * @return {*}
     */
    initData(data) {
      const orgForm = this.$options.data().form;
      this.form = { ...orgForm, ...data };
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },

    onCancel() {
      this.visible = false;
      this.loading = false;
    },
    onConfirm() {
      this.loading = true;
      this.$refs.form.validate(async (valid, object) => {
        if (valid) {
          try {
            let APIMethod;
            if (this.form?.id) {
              APIMethod = API.editDWSource;
            } else {
              APIMethod = API.addDWSource;
            }
            const { state, message } = await APIMethod(this.form);
            if (state) {
              this.$message.success(this.form?.id ? '编辑' : '新增' + '数据资源项');
              this.$emit('confirm');
              this.visible = false;
            } else {
              this.$message.warning(message || '操作失败');
            }
          } catch (err) {
            this.$message.warning(err || '操作失败');
          } finally {
            this.loading = false;
          }
        } else {
          this.$message.warning(`有${Object.keys(object).length}个字段未通过校验，请正确填写表单。`);
          this.loading = false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-descriptions-row .label-w {
  width: 120px;
  background-color: #f7faff;
}
::v-deep .el-descriptions-row .label-w2 {
  width: 160px;
  background-color: #f7faff;
}
::v-deep {
  .app-dialog-content {
    height: 50vh !important;
  }
}
.form-content {
  width: 96%;
  margin: 0 auto;
}
</style>
