<template>
  <div class="app-container">
    <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
      <el-row :gutter="1">
        <el-col :span="5">
          <el-form-item label="模板名称" prop="tempName">
            <el-input v-model="formData.tempName" placeholder="请输入模板名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="模板编码" prop="tempCode">
            <el-input v-model="formData.tempCode" placeholder="请输入模板编码"/>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="模板类型" prop="tempType">
            <el-select v-model="formData.tempType" placeholder="请选择模板类型" @keyup.enter.native="handleQuery">
              <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DICT_BASE_CONT_TEMP_TYPE)"
                         :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="模板用途" prop="tempUse">
            <el-select v-model="formData.tempUse" placeholder="请选择模板类型" @keyup.enter.native="handleQuery">
              <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DICT_BASE_CONT_TEMP_USE)"
                         :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="模板内容">
        <Editor :height="500" v-model="formData.tempContent" :min-height="192"/>
      </el-form-item>
    </el-form>
    <div slot="footer" style="text-align: right;" class="dialog-footer">
      <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
      <el-button @click="close()">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import * as ContTempApi from '@/api/base/cont';
import Editor from '@/components/Editor';

export default {
  name: "ContTempForm",
  components: {
    Editor,
  },
  data() {
    return {

      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        tempCode: undefined,
        tempType: undefined,
        tempUse: undefined,
        tempName: undefined,
        isValid: undefined,
        status: undefined,
        tempContent: undefined,
      },
      // 表单校验
      formRules: {
        tempName: [
          { required: true, message: '请输入模板名称', trigger: 'blur' },
        ],
        tempCode: [
          { required: true, message: '请输入模板编码', trigger: 'blur' },
        ],
        tempType: [
          { required: true, message: '请选择模板类型', trigger: 'blur' },
        ],
        tempUse: [
          { required: true, message: '请选择模板用途', trigger: 'blur' },
        ],
      },
    };
  },
  created() {
    const id = this.$route.params.id
    this.open(id)
  },
  methods: {
    close(){
      this.$router.go(-1);
    },
    /** 打开弹窗 */
    async open(id) {
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await ContTempApi.getContTemp(id);
          this.formData = res.data;
        } finally {
          this.formLoading = false;
        }
      }
    },
    /** 提交按钮 */
    async submitForm() {
      debugger
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await ContTempApi.updateContTemp(data);
          this.$modal.msgSuccess("修改成功");
          this.$router.go(-1);
          return;
        }
        // 添加的提交
        await ContTempApi.createContTemp(data);
        this.$modal.msgSuccess("新增成功");
        this.$router.go(-1);
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        tempCode: undefined,
        tempType: undefined,
        tempUse: undefined,
        tempName: undefined,
        isValid: undefined,
        status: undefined,
        tempContent: undefined,
      };
      debugger
      this.resetForm("formRef");
    }
  }
};
</script>
