import { reportadd, reportedit, messagePushPost,dataBaseInterface,reportMessageSend } from "@/api/api.js";
export default {
  data() {
    return {
      isMessagePost: false,
    };
  },
  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.isMessagePost) {
              await this.publishreport(res.Data.ReportId);
              await reportMessageSend({
                ReportId: res.Data.ReportId,
              }).then(() => {});
            } else {
              this.publishreport(res.Data.ReportId);
            }
          } else {
            // this.$router.push({path:'/reportlist'});
          }
        }
      });
    },
    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.isMessagePost) {
              await this.publishreport(res.Data.ReportId);
              await reportMessageSend({
                ReportId: res.Data.ReportId,
              }).then(() => {});
            } else {
              this.publishreport(res.Data.ReportId);
            }
          } else {
            // this.$router.push({path:'/reportlist'});
          }
          //已经添加过报告
          this.isAddEnter = true;
        }
      });
    },

    //刷报告中的所有图表  
    refreshReport: _.debounce ( async function() {
      let code_arr = [];
      $('iframe').each((k,i) => {
        try {
          let href = $(i).attr('src');
          code_arr.push(href.slice(href.indexOf('code=') + 5));
    
        } catch (err) {
        }
      });
      
      // console.log(code_arr);
      if(!code_arr.length) return this.$message.warning('请插入图表');

      const loading = this.$loading({
        lock: true,
        text: '刷新中..',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.02)'
      });

      const { Ret } = await dataBaseInterface.reportRefresh({
        ChartInfoCode: code_arr
      })
  
      loading.close();
      
      if(Ret === 200) {
        $('iframe').each((k,i) => {
          $(i).attr('src',$(i).attr('src'))
        });
        this.$message.success('刷新成功');
      }

    },1000)
  },
};
