<template>
  <app-dialog v-model="visible" :loading="loading" :title="title" append-to-body class="detail-dialog" width="60%"
              @cancel="onCancel" @confirm="onConfirm"
  >
    <el-form ref="form" :model="form" :rules="rules" class="demo-form ml-150 mr-150" 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>
      <div class="form-content ml-120">
        <el-row :gutter="20">
          <app-filter-item :span="13" label="资源项名称" prop="name">
            <el-input v-model="form.name" clearable placeholder="请输入" />
          </app-filter-item>
          <app-filter-item :span="13" label="资源项编码" prop="code">
            <el-input v-model="form.code" v-pinyin="form.name" clearable placeholder="请输入" />
          </app-filter-item>
        </el-row>
        <!--        <el-row :gutter="20">-->
        <!--          <app-filter-item :span="13" label="实体表中文名" prop="tableNameCh">-->
        <!--            <el-input v-model="form.tableNameCh" clearable placeholder="请输入" />-->
        <!--          </app-filter-item>-->
        <!--          <app-filter-item :span="13" label="实体表英文名" prop="tableNameEn">-->
        <!--            <el-input v-model="form.tableNameEn" clearable placeholder="请输入" />-->
        <!--          </app-filter-item>-->
        <!--        </el-row>-->
        <el-row :gutter="20">
          <app-filter-item :span="13" label="数据库表名" prop="tableNameEn">
            <el-input v-model="form.tableNameEn" clearable placeholder="请输入" />
          </app-filter-item>
        </el-row>
        <el-row :gutter="20">
          <app-filter-item :span="12" label="实体表类别" prop="tableClass">
            <el-radio-group v-model="form.tableClass">
              <el-radio-button v-for="item in getDictionaryByCode('stblb')" :key="item.value" :label="item.value">
                {{ item.name }}
              </el-radio-button>
            </el-radio-group>
          </app-filter-item>
          <app-filter-item :span="12" label="实体表类型" prop="tableType">
            <el-radio-group v-model="form.tableType">
              <el-radio-button v-for="item in getDictionaryByCode('stblx')" :key="item.value" :label="item.value">
                {{ item.name }}
              </el-radio-button>
            </el-radio-group>
          </app-filter-item>
        </el-row>
        <el-row :gutter="20" type="flex">
          <app-filter-item :span="20" label="实体表描述" prop="tableDesc">
            <el-input v-model="form.tableDesc" :rows="4" clearable placeholder="请输入" type="textarea" />
          </app-filter-item>
        </el-row>
      </div>
    </el-form>
  </app-dialog>
</template>

<script>
import dialogMixin from '@/mixins/dialog.js';
import { getDictionaryByCode } from '@/utils/dictionary.js';
import codeEditor from '@/components/codeEditor';
import API from '@/api/business/dataDWCatalog';

export default {
  name: 'SourceDialog',
  components: { codeEditor },
  mixins: [dialogMixin],
  props: {},
  data() {
    return {
      form: {
        sourceType: '1',
        level: '1',
        tableClass: '1',
        tableType: '1',
        updateRate: '1',
        createTableSql: '',
        name: '',
        code: '',
      },
      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: '实体中文名表不能为空' },
          { pattern: /^[a-zA-Z][a-zA-Z0-9_]*$/, message: '必须以字母开头,中间可以包含数字、英文、下划线。' },
        ],
        tableClass: [{ required: true, message: '实体表类别名不能为空' }],
        tableType: [{ required: true, message: '实体表类型名不能为空' }],
        tableCode: [{ required: true, message: '数据库表名不能为空' }],
        updateRate: [{ required: true, message: '更新周期名不能为空' }],
      },
      loading: false,
    };
  },
  mounted() {
  },
  methods: {
    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 {
            this.form.tableNameCh = this.form.name;
            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 {
  .app-dialog-content {
    height: 50vh !important;
  }
}

.form-content {
  width: 96%;
  //margin: 0 auto;
}

.demo-form {
  border: solid 1px #eff0f5;
  height: 95%;
  overflow: hidden;
}
</style>
