<template>
  <el-dialog
    :title="dataId?'编辑表单类型':'新增表单类型'"
    :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="formType" label-width="110px" class="adSearchForm"
               style="width: 362px;height: auto;">
        <el-form-item label="表单类型编码">
          <span>{{ formType.formTypeCode }}</span>
        </el-form-item>
        <el-form-item label="表单类型名称" :rules="[{ required: true, trigger: 'blur' }]">
          <el-input v-model="formType.formTypeName" 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]
  },
  data() {
    return {
      isEdit: false,
      tabIndex: 'first',
      dataId: '',
      formType: {},
      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 + `/form_type/get`,
          method: `post`,
          data: {
            id: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            let row = res.objx || {};
            this.formType = row;
            callback && callback(row);
          }
        });
      }
    },
    saveData() {
      this.$refs.editForm.$baseValidate(valid => {
        if (valid) {
          this.$baseConfirm(this.$t1('您确定要保存吗？')).then(() => {
            var url = USER_PREFIX + (this.isEdit ? `/form_type/update` : `/form_type/save`);
            this.$http({
              url: url,
              method: `post`,
              data: this.formType,
              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>
