/*
  @description: 异议投诉受理/不受理--公共方法
  @Modifier: haofengxian
  @routerPath:
  @date: 2020-5-13-18:21
*/
/* 公用方法*/
/* 接口*/
import { saveObjectionReceipt, sendObjectionByObjectionId, exportObjectionReceiptOfPdf, getObjectionId } from '@api/bidding/tenderObjection'
import { download } from '@/assets/js/utils'// 常用方法
export const objectionAcceptanceMixin = {
  data() {
    return {
      confirmLoading: false, // 保存Loding
      flowStatus: this.$route.query.flowStatus, // 是否工作流进入
      receiptStatus: this.$route.params.receiptStatus, // 项目经理发送异议回复函状态01:受理 02:不受理
      // 查看异议投诉回复函文件共用
      objectionReceiptContentData: {
        bizId: this.$route.params.objectionId, // 业务主键Id（异议id）
        fileType: ''// 异议投诉回复函文件类型
      },
      fileList: [], // 异议投诉回复函下载文件
      form: {
        objectionId: '', // 异议id
        objectionGreatNature: '', // 异议是否重大影响01是 02否
        treatmentBasis: '', // 处理依据
        receiptStatus: '' //  接受状态 01:受理 02:不受理
      },
      // 配置投诉异议编辑路由 初始化和投诉异议进入路由不同
      objectionPath: {
        objectionAddPath: '',
        makeObjectionPath: 'MakeReplyLetterAdd', // 富文本编辑器编辑投诉异议路由
        submitObjectionPath: 'SubmitObjectionAcceptance', // 投诉异议提交路由
        submitListPath: '/objection/objectionList' // 投诉异议提交后跳转路由
      },
      // 受理审批单
      acceptAuthorizationFileupLoadData: {
        bizId: '', // 业务主键Id
        fileType: 'clarification_objection_pdf_accept'// 文件类型
      },
      // 不受理审批单
      rejectAuthorizationFileupLoadData: {
        bizId: '', // 业务主键Id
        fileType: 'clarification_objection_pdf_reject'// 文件类型
      }
    }
  },
  created() {
    // 查看异议投诉回复函文件类型
    this.getReceiptStatus()
    this.handlePath()
  },
  methods: {
    // 查看异议投诉回复函文件类型
    getReceiptStatus() {
      // 项目经理发送异议回复函状态01:受理 02:不受理--（受理or 不受理）获取pdf文件类型不同
      // 项目经理不受理key:objectionNotAcceptance_audit  受理key:clarification_raiseObjection
      const processDefinitionKey = this.$route.query.processDefinitionKey // 风险控制部的流程图key
      if (this.receiptStatus === '01' || processDefinitionKey === 'clarification_raiseObjection') {
        this.objectionReceiptContentData.fileType = 'clarification_objection_accept_letter'
      } else {
        this.objectionReceiptContentData.fileType = 'clarification_objection_reject_letter'
      }
    },
    // 配置投诉异议路径（区分是否工作流进入）
    handlePath() {
      const objectionAddPath = sessionStorage.getItem('objectionAddPath') // 取出第一个页面是受理/不受理页面路由
      let objectionId = '' // 异议id
      let objectionPath = {} // 存放路由
      // 审批流进入变更公告objectionAcceptanceAddFlow
      if (this.flowStatus) {
        objectionId = this.$route.query.bizId
        objectionPath = {
          makeObjectionPath: '/work/objection/makeReplyLetterAddFlow', // 富文本编辑器编辑投诉异议路由
          submitObjectionPath: '/work/objection/submitObjectionAcceptanceFlow', // 投诉异议提交路由
          submitListPath: '/work/todo' // 投诉异议提交后跳转路由
        }
        this.objectionPath = objectionPath // 配置路径
        this.flowGetObjectionId(objectionId)
      } else {
        objectionId = this.$route.params.objectionId
        this.form.objectionId = objectionId
        this.acceptAuthorizationFileupLoadData.bizId = objectionId // 受理审批单
        this.rejectAuthorizationFileupLoadData.bizId = objectionId // 不受理审批单
        this.objectionReceiptContentData.bizId = objectionId // 查看异议投诉回复函文件共用
      }
      this.objectionPath.objectionAddPath = objectionAddPath // 投诉异议生成异议回复函
    },
    // 生成异议回复函 type 00 保存 01生成异议回复函跳转页面
    saveObjection(type) {
      this.confirmLoading = true
      saveObjectionReceipt(this.form).then(res => {
        this.confirmLoading = false
        if (type === '00') {
          this.$message({
            type: 'success',
            message: '保存成功'
          })
        } else {
          sessionStorage.setItem('content', res.data.content) // 回复函编辑内容
          sessionStorage.setItem('objectionAddPath', this.$route.name) // 存储受理/不受理当前页面name
          // 工作流进入
          if (this.flowStatus) {
            this.$router.push({
              path: this.objectionPath.makeObjectionPath,
              query: {
                ...this.$route.query,
                receiptStatus: this.form.receiptStatus // 接受状态 01:受理 02:不受理
              }
            })
          } else {
            this.$router.push({
              name: this.objectionPath.makeObjectionPath,
              params: {
                ...this.$route.params,
                receiptStatus: this.form.receiptStatus // 接受状态 01:受理 02:不受理
              }
            })
          }
        }
      }).catch(() => {
        this.confirmLoading = false
      })
    },
    // 发送异议回复函/通知函
    sendReply() {
      const params = {
        objectionId: this.form.objectionId, // 异议id
        receiptStatus: this.$route.params.receiptStatus // 接受状态 01:受理 02:不受理
      }
      sendObjectionByObjectionId(params).then(() => {
        this.$message({
          type: 'success',
          message: '发送成功'
        })
        this.$router.push(this.objectionPath.submitListPath)
      }).catch(() => {

      })
    },
    // 接收详情页面传值
    objectionDetail(value) {
      this.form.treatmentBasis = value.treatmentBasis ? value.treatmentBasis : '' // 处理依据
      this.form.objectionGreatNature = value.objectionGreatNature ? value.objectionGreatNature : '02' // 异议是否重大影响01是 02否
    },
    // 导出审批单模板
    exportAuthorizationFile() {
      //  接受状态 01:受理 02:不受理
      let ObjectionTemplateName = '异议受理审批单模板'
      if (this.form.receiptStatus === '02') {
        ObjectionTemplateName = '异议不予受理审批单模板'
      }
      exportObjectionReceiptOfPdf(this.form.receiptStatus).then(res => {
        download(res, ObjectionTemplateName + '.docx')
      })
    },
    // 根据异议回复主键id获取异议主键id--工作流进入调用
    flowGetObjectionId(bizId) {
      let objectionId = '' // 异议id
      const processDefinitionKey = this.$route.query.processDefinitionKey // 风险控制部的流程图key
      // 工作流进入并且不是风险控制部（项目经理受理、不予受理进入）则需要重新获取objectionId
      if (processDefinitionKey !== 'raiseObjectionWeb_audit') {
        getObjectionId(bizId).then(res => {
          objectionId = res.data
          this.form.objectionId = objectionId
          this.acceptAuthorizationFileupLoadData.bizId = objectionId // 受理审批单
          this.rejectAuthorizationFileupLoadData.bizId = objectionId // 不受理审批单
          this.objectionReceiptContentData.bizId = objectionId // 查看异议投诉回复函文件共用
        }).catch(() => {
        })
      } else {
        this.form.objectionId = bizId
        this.acceptAuthorizationFileupLoadData.bizId = bizId // 受理审批单
        this.rejectAuthorizationFileupLoadData.bizId = bizId // 不受理审批单
        this.objectionReceiptContentData.bizId = bizId // 查看异议投诉回复函文件共用
      }
    }
  }
}
