<!--
 * @Description: 综合类型表单组件
 * @Author: wanqian
 * @Date: 2024-03-04 13:25:52
-->
<template>
  <div class="body-generic-type-form">
    <el-dialog :title="dialogTitle" :visible.sync="visible"	@open="openDialog" width="457px" center 
		  :before-close="handleCancel"	:close-on-click-modal="false">
      <div style="display: flex; justify-content: center;">
        <el-form class="cont-generic-type-form" label-position="left" :model="form" :rules="rules" ref="form" 
          label-width="100px">
          <el-form-item label="名称：" prop="name">
            <el-input size="small" style="width: 218px" v-model.trim="form.name" placeholder="请输入综合类型" 
              maxlength="20" clearable @input="inputName">
            </el-input>
          </el-form-item>
          <el-form-item label="主索引：">
            <el-input size="small" style="width: 218px" v-model.trim="form.idText" disabled></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="dlg-footer bk-wizard-bottons" >
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" class="gold-gradient" @click="handleSaveClick">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'GenericTypeForm',
  
  props: ["visible", "operate", "genericTypeId"],

  data() {
    return {
      form: {},
      rules: {
        name: [{required: true, message: '请输入综合类型', trigger: 'blur'}],
      },
    }
  },

  computed: {
		dialogTitle() {
			return (this.operate == 'add') ? '新增综合类型' : '编辑综合类型';
		},
  },
  
  methods:{
    openDialog() {
      this.initData();
    },

    initData() {
      let url = (this.operate == 'add') ? this.commonUrl.URL_GET_COMMON_CFG_GENERIC_TYPE_DO_4_ADD 
				: this.commonUtil.fillUrlArgments(this.commonUrl.URL_GET_COMMON_CFG_GENERIC_TYPE_DO_4_UPDATE, this.genericTypeId);
      let requestData = {
        method: 'GET',
        url: url,
      }
      this.vueRequest(requestData).then(result => {
        if (!result) return;
				this.form = result.data.commonCfgGenericType;
        this.$set(this.form, 'idText', '系统自动生成');
      });
    },

    inputName() {
      this.form.name = this.commonUtil.filterChineseAndLetters(this.form.name);
    },

    handleCancel() {
      this.clearData();
			this.$emit("close", false);
    },

    handleSaveClick() {
      this.$refs.form.validate((valid) => {
				if (!valid) return false;
				this.submitData();
			})
    },

    submitData() {
      let url = (this.operate == 'add') ? this.commonUrl.URL_SAVE_COMMON_CFG_GENERIC_TYPE : this.commonUrl.URL_UPDATE_COMMON_CFG_GENERIC_TYPE;
      let method = (this.operate == 'add') ? 'POST' : 'PUT';
      this.$delete(this.form, 'idText');
      let requestData = {
        method: method,
        url: url,
        param: this.form,
        contentType: this.commonConst.CNTYPE_JSON
      }
      this.vueRequest(requestData).then(result => {
        if (!result) return;

				this.clearVaild();
        this.$emit("close", true);
      });
    },

    clearData() {
      this.form = {};
			this.clearVaild();
		},

    clearVaild() {
			this.$nextTick(() => {
				this.$refs["form"].clearValidate();
			});
		},
  },
}
</script>

<style scoped lang="scss">
.body-generic-type-form {
  .dlg-footer {
    text-align: center;
  }
}
</style>
<style lang="scss">
.body-generic-type-form {
  .el-form-item__label {
    text-align: right !important;
  }
}
</style>