<template>
  <el-dialog
    :title="dataId?$t1('编辑数据类型'):$t1('新增数据类型')"
    :append-to-body="true"
    :modal-append-to-body="true"
    :close-on-click-modal="false"
    :visible.sync="showDialog"
    :modal="false"
    custom-class="dialog-style list-dialog dialog-checkbox pd_8"
    width="400px"
    v-el-drag-dialog
    v-el-dialog-center
    @close="close"
  >
    <div class="cont">
      <el-form ref="editForm" :model="dataType" label-width="110px" class="adSearchForm"
               style="width: 362px;height: auto;">
        <el-form-item :label="$t1('数据类型编码')">
          <span>{{ dataType.dataTypeCode }}</span>
        </el-form-item>
        <el-form-item :label="$t1('数据类型名称')" :rules="[{ required: true, trigger: 'blur' }]">
          <el-input v-model="dataType.dataTypeName" clearable class="all-width"/>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" plain class="button-sty" @click="showDialog=false">
        <i class="el-icon-close el-icon"></i>
        {{ $t1('取 消') }}
      </el-button>
      <el-button type="primary" @click="saveData" class="button-sty">
        <i class="el-icon-check el-icon"></i>
        {{ $t1('确 定') }}
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    _dataId: [String, Number],
    dataTypeGroup: String
  },
  data() {
    return {
      isEdit: false,
      tabIndex: 'first',
      dataId: '',
      dataType: {enabled: true},
      showDialog: true
    };
  },
  created() {
    if (this._dataId && !isNaN(this._dataId)) this.dataId = this._dataId;
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData(callback) {
      if (this.dataId && !isNaN(this.dataId)) {
        this.isEdit = true;
        this.$commonHttp({
          url: USER_PREFIX + `/data_type_setting/get`,
          method: `post`,
          data: {
            id: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            let row = res.objx || {};
            this.dataType = row;
            callback && callback(row);
          }
        });
      } else {
        this.dataType.dataTypeGroup = this.dataTypeGroup
      }
    },
    saveData() {
      this.$refs.editForm.$baseValidate(valid => {
        if (valid) {
          this.$baseConfirm(this.$t1('您确定要保存吗？')).then(() => {
            var url = USER_PREFIX + (this.isEdit ? `/data_type_setting/update` : `/data_type_setting/save`);
            this.$http({
              url: url,
              method: `post`,
              data: this.dataType,
              isLoading: true,
              success: res => {
                this.$message({
                  message: res.content,
                  type: 'success',
                  duration: 500,
                  onClose: t => {
                    this.dataId = res.objx;
                    this.getData((row) => {
                      this.close();
                      this.$emit('confirm', row)
                    })
                  }
                });
              }
            });
          });
        }
      });
    },
    close() {
      this.showDialog = false;
      this.$emit('update:visiable', false)
    }
  }
}
</script>

<style scoped>

</style>
