import { reportadd, reportedit, messagePushPost,dataBaseInterface,reportMessageSend } from "@/api/api.js";
import {approveInterence} from '@/api/modules/approve.js';
import * as sheetInterface from '@/api/modules/sheetApi.js';
import { getUrlParams } from '@/utils/common'
export default {
  data() {
    return {
      isMessagePost: false,
      isRightFormShow:true,
    };
  },
  methods: {
    reporteditMsg(params2, tp) {
      reportedit(params2).then(async (res) => {
        if (res.Ret == 200) {
          this.$message.success(res.Msg);
          if (tp == "yl") {
            // this.$router.push({name:'预览报告',query:{id:res.report_id}});
            let { href } = this.$router.resolve({
              name: "预览报告",
              query: { id: res.Data.ReportId },
            });
            window.open(href, "_blank");
          } else if (tp == "fb") {
            if(this.isApprove){
                await this.publishreport(res.Data.ReportId);
                return 
            }
            if (this.isMessagePost) {
              await this.publishreport(res.Data.ReportId);
              await reportMessageSend({
                ReportId: res.Data.ReportId,
              }).then(() => {});
            } else {
              this.publishreport(res.Data.ReportId);
            }
          } else if(tp=='dsfb'){
            this.showDSFB=true
            // this.$router.push({path:'/reportlist'});
          }else if(tp=='submit'){
            this.handleSubmitReport(res.Data.ReportId)
          }
        }
      });
    },
    reportaddMsg(params, tp) {
      reportadd(params).then(async (res) => {
        if (res.Ret == 200) {
          this.$message.success(res.Msg);
          this.report_draft_id = res.Data.ReportId;
          if (tp == "yl") {
            // this.$router.push({name:'预览报告',query:{id:res.report_id}});
            let { href } = this.$router.resolve({
              name: "预览报告",
              query: { id: res.Data.ReportId },
            });
            window.open(href, "_blank");
          } else if (tp == "fb") {
            if(this.isApprove){
                await this.publishreport(res.Data.ReportId);
                return 
            }
            if (this.isMessagePost) {
              await this.publishreport(res.Data.ReportId);
              await reportMessageSend({
                ReportId: res.Data.ReportId,
              }).then(() => {});
            } else {
              this.publishreport(res.Data.ReportId);
            }
          } else if(tp=='dsfb'){
            this.showDSFB=true
            // this.$router.push({path:'/reportlist'});
          }else if(tp=='submit'){
            this.handleSubmitReport(res.Data.ReportId)
          }
          //已经添加过报告
          this.isAddEnter = true;
        }
      });
    },

    //刷报告中的所有图表和表格
    refreshReport: _.debounce ( async function() {
      let code_arr = [];
      let sheet_code_arr = []
      $('iframe').each((k,i) => {
        try {
          let href = $(i).attr('src');
          if(href.includes('chartshow')){
            code_arr.push(getUrlParams(href,'code'));
          }
          if(href.includes('sheetshow')){
            sheet_code_arr.push(getUrlParams(href,'code'))
          }
        } catch (err) {
        }
      });

      if(!code_arr.length&&!sheet_code_arr.length) return this.$message.warning('请插入图表或表格')

      const fromPage = this.$route.path === "/reportEnEditor" ? "english_report" : "report";
      if(this.$route.query.id&&code_arr.length) {
        let res = await dataBaseInterface.getReportrefreshStatus({
          Source: fromPage,
          ReportId: Number(this.$route.query.id),
          ReportChapterId: 0
        });
        
        if(!res.Data.RefreshResult) return this.$message.warning('图表正在刷新中，请勿重复操作')
        const { Ret,Msg } = await dataBaseInterface.reportRefresh({
            ChartInfoCode: code_arr
          })
          
          if(Ret === 200) {
            $('iframe').each((k,i) => {
                let href = $(i).attr('src');
                if(href.includes('chartshow')){
                    $(i).attr('src',$(i).attr('src'))
                }
            });
            this.$message.success(Msg);
          }
      }

      if(this.$route.query.id&&sheet_code_arr.length){
        //获取刷新结果
        let res = await sheetInterface.getRefreshResult({
            Source: fromPage,
            ReportId: Number(this.$route.query.id),
            ReportChapterId: 0
          });
        if(!res.Data.RefreshResult) return this.$message.warning('表格正在刷新中，请勿重复操作')
        const { Ret,Msg } = await sheetInterface.refreshSheet({
            ExcelCodes: sheet_code_arr
          })
          
          if(Ret === 200) {
            $('iframe').each((k,i) => {
                let href = $(i).attr('src');
                if(href.includes('sheetshow')){
                    $(i).attr('src',$(i).attr('src'))
                }
            });
            this.$message.success(Msg);
          }
      }
    },1000),
    //展示提交审批弹窗
    handleSubmitReport(id){
        this.submitId = id
        this.$confirm("是否确认提交该报告进入审批流程?","提示",{
            confirmButtonText:'确定',
            cancelButtonText:'取消',
            type:'warning',
        }).then(()=>{
            this.submitReport("submit")
        })
        
    },
    //提交审批
    submitReport(type){
        /* if(type==='submit'){ */
            approveInterence.reportCnSubmit({
                ReportId:Number(this.submitId)
            }).then(res=>{
                if(res.Ret!==200) return
                this.$message.success("提交成功")
                this.isPublishloading = false;
                this.$router.push({ path: '/reportlist' });
            })
        /* } */
    }
  },
};
