<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
                    <el-form-item label="备注" prop="remark">
                      <el-input v-model="formData.remark" placeholder="请输入备注" />
                    </el-form-item>
                    <el-form-item label="资源名称" prop="docTitle">
                      <el-input v-model="formData.docTitle" placeholder="请输入资源名称" />
                    </el-form-item>
                    <el-form-item label="正文">
                      <Editor v-model="formData.docContent" :min-height="192"/>
                    </el-form-item>
                    <el-form-item label="译文">
                      <Editor v-model="formData.docContentTranslate" :min-height="192"/>
                    </el-form-item>
                    <el-form-item label="原文件名" prop="fileName">
                      <el-input v-model="formData.fileName" placeholder="请输入原文件名" />
                    </el-form-item>
                    <el-form-item label="原文件路径" prop="filePath">
                      <el-input v-model="formData.filePath" placeholder="请输入原文件路径" />
                    </el-form-item>
                    <el-form-item label="原文件后缀类型" prop="fileType">
                      <el-input v-model="formData.fileType" placeholder="请输入原文件后缀类型" />
                    </el-form-item>
                    <el-form-item label="原文件创建日期" prop="fileCrtime">
                      <el-date-picker clearable v-model="formData.fileCrtime" type="date" value-format="timestamp" placeholder="选择原文件创建日期" />
                    </el-form-item>
                    <el-form-item label="原文件修改日期" prop="fileUpdatetime">
                      <el-date-picker clearable v-model="formData.fileUpdatetime" type="date" value-format="timestamp" placeholder="选择原文件修改日期" />
                    </el-form-item>
                    <el-form-item label="分类标签" prop="docLable">
                      <el-input v-model="formData.docLable" placeholder="请输入分类标签" />
                    </el-form-item>
                    <el-form-item label="文件来源" prop="sourcename">
                      <el-input v-model="formData.sourcename" placeholder="请输入文件来源" />
                    </el-form-item>
                    <el-form-item label="语种" prop="language">
                      <el-input v-model="formData.language" placeholder="请输入语种" />
                    </el-form-item>
      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as DigsearchApi from '@/api/manager/digsearch';
  import Editor from '@/components/Editor';
      export default {
    name: "DigsearchForm",
    components: {
          Editor,
                    },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
                            id: undefined,
                            remark: undefined,
                            docTitle: undefined,
                            docContent: undefined,
                            docContentTranslate: undefined,
                            fileName: undefined,
                            filePath: undefined,
                            fileType: undefined,
                            fileCrtime: undefined,
                            fileUpdatetime: undefined,
                            docLable: undefined,
                            sourcename: undefined,
                            language: undefined,
        },
        // 表单校验
        formRules: {
                        docTitle: [{ required: true, message: '资源名称不能为空', trigger: 'blur' }],
                        fileName: [{ required: true, message: '原文件名不能为空', trigger: 'blur' }],
                        sourcename: [{ required: true, message: '文件来源不能为空', trigger: 'blur' }],
        },
                        };
    },
    methods: {
      /** 打开弹窗 */
    async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await DigsearchApi.getDigsearch(id);
            this.formData = res.data;
            this.title = "修改资源搜索";
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增资源搜索";
              },
      /** 提交按钮 */
      async submitForm() {
        // 校验主表
        await this.$refs["formRef"].validate();
                  this.formLoading = true;
        try {
          const data = this.formData;
                  // 修改的提交
          if (data.id) {
            await DigsearchApi.updateDigsearch(data);
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.$emit('success');
            return;
          }
          // 添加的提交
          await DigsearchApi.createDigsearch(data);
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      },
                      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            remark: undefined,
                            docTitle: undefined,
                            docContent: undefined,
                            docContentTranslate: undefined,
                            fileName: undefined,
                            filePath: undefined,
                            fileType: undefined,
                            fileCrtime: undefined,
                            fileUpdatetime: undefined,
                            docLable: undefined,
                            sourcename: undefined,
                            language: undefined,
        };
        this.resetForm("formRef");
      }
    }
  };
</script>
