<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="70%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="140px" :disabled="true">
                    <el-form-item label="访谈稿标题 " prop="title">
                      <el-input v-model="formData.title" placeholder="请输入标题" />
                    </el-form-item>
<!--                    <el-form-item label="访谈描述">
                      <Editor v-model="formData.description" :min-height="192"/>
                    </el-form-item>-->
        <!--                    <el-form-item label="上传时间" prop="pubtime">
                              <el-date-picker clearable v-model="formData.pubtime" type="date" value-format="timestamp" placeholder="选择上传时间" />
                            </el-form-item>
                            <el-form-item label="部门ID" prop="departmentId">
                              <el-input v-model="formData.departmentId" placeholder="请输入部门ID" />
                            </el-form-item>
                            <el-form-item label="状态" prop="reportStatus">
                              <el-select v-model="formData.reportStatus" placeholder="请选择状态">
                                    <el-option v-for="dict in this.getDictDatas(DICT_TYPE.TRS_REPORT_STATUS)"
                                               :key="dict.value" :label="dict.label" :value="dict.value" />
                              </el-select>
                            </el-form-item>
                            <el-form-item label="备注信息" prop="reportRemark">
                              <el-input v-model="formData.reportRemark" placeholder="请输入备注信息" />
                            </el-form-item>-->
                      <el-form-item label="访谈稿上传" prop="attachment">
                          <FilePreview v-model="formData.attachment"/>
                      </el-form-item>

      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as InterviewArticleApi from '@/api/server/interviewarticle';
  import FileUpload from '@/components/FileUpload';
  import Editor from '@/components/Editor';
  import FilePreview from '@/components/FilePreview';

  export default {
    name: "InterviewArticleForm",
    components: {
          FileUpload,
          Editor,
      FilePreview
                    },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
                            id: undefined,
                            title: undefined,
                            description: undefined,
                            pubtime: undefined,
                            departmentId: undefined,
                            reportStatus: undefined,
                            reportRemark: undefined,
                            attachment: undefined,
        },
        // 表单校验
        formRules: {
          title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
          agreement: [{
            validator: (rule, value, callback) => {
              if (!value) {
                callback(new Error('请确认协议内容'));
              } else {
                callback();
              }
            },
            trigger: 'change'
          }]
        },
      };
    },
    methods: {
      /** 打开弹窗 */
     async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await InterviewArticleApi.getInterviewArticle(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 InterviewArticleApi.updateInterviewArticle(data);
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.$emit('success');
            return;
          }
          // 添加的提交
          await InterviewArticleApi.createInterviewArticle(data);
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      },
                      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            title: undefined,
                            description: undefined,
                            pubtime: undefined,
                            departmentId: undefined,
                            reportStatus: undefined,
                            reportRemark: undefined,
                            attachment: undefined,
        };
        this.resetForm("formRef");
      }
    }
  };
</script>

