<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">
                    <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">
                        <FileUpload v-model="formData.attachment"
                                    :file-size="uploadConfig.fileSize"
                                    :file-type="uploadConfig.fileType"
                                    :limit="uploadConfig.limit"
                                    :required="uploadConfig.required"/>
                      </el-form-item>

        <el-form-item label=" " prop="agreement" class="agreement-item">
          <div style="display: flex; align-items: flex-start;">
            <el-checkbox v-model="formData.agreement" style="margin-right: 8px; margin-top: 0px;"></el-checkbox>
            <span style="white-space: normal; word-break: break-word;">
      我已审核确认此信息符合法律条文及信息公开相关要求，已做好保密审查，无涉密信息，请自治区人民政府网站予以发布。
    </span>
          </div>
          <div class="el-form-item__error">请确认协议内容</div>
        </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 InterviewArticleApi from '@/api/server/interviewarticle';
  import FileUpload from '@/components/FileUpload';
  import Editor from '@/components/Editor';
  import {getFileForConfigPage} from "@/api/server/fileforconfig";
      export default {
    name: "InterviewArticleForm",
    components: {
          FileUpload,
          Editor,
                    },
    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' }],
          attachment: [{required: true, message: '附件不能为空', trigger: 'blur'}],
          agreement: [{
            validator: (rule, value, callback) => {
              if (!value) {
                callback(new Error('请确认协议内容'));
              } else {
                callback();
              }
            },
            trigger: 'change'
          }]
        },
        // 文件上传配置
        uploadConfig: {
          fileSize: 1, // 默认值，单位MB
          fileType: ['doc', 'docx', 'wps', 'pdf'], // 默认文件类型
          limit: 1, // 默认限制数量
          required: true // 默认必填
        }
      };
    },
    methods: {
      /** 获取文件上传配置 */
      async fetchUploadConfig() {
        try {
          // 假设通过分页接口获取所有配置，然后根据配置键筛选
          const params = {
            pageNo: 1,
            pageSize: 100 // 获取足够多的配置
          };
          const res = await getFileForConfigPage(params);
          const configs = res.data.list;
          // 假设配置键为 'harmful_info_report_upload'
          const config = configs.find(item => item.configKey === 'InterviewArticle');
          if (config) {
            // 假设配置值是一个JSON字符串，包含fileSize, fileType, limit, required等属性
            this.uploadConfig = {
              fileSize: config.fileMaxSize || 15, // 使用 fileMaxSize
              fileType: config.fileTypes ? config.fileTypes.split(',') : ['doc', 'docx', 'wps', 'pdf'], // 将字符串转换为数组
              limit: config.fileMaxCount || 1, // 使用 fileMaxCount
              required: config.fileOther=='true' || true // 默认true
            };
          }
        } catch (error) {
          console.error('获取文件上传配置失败', error);
          // 使用默认配置
        }
      },
      /** 打开弹窗 */
     async open(id) {
        await this.fetchUploadConfig(); // 获取文件上传配置
        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>

