<template>
  <el-dialog
    :title="'应用日志'"
    :visible.sync="visible"
    width="80%"
    :before-close="handleClose"
  >
    <el-form>
        
    </el-form>
    <div style = "white-space: pre-wrap" v-loading="loading">{{applicationLog}}</div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      //定义可见性，即获取数据成功与否后再可见
      visible: false,
      loading:true,
      name: "",
      applicationLog: "",
      timer:null,
      eventsData: [], //定义应用事件数据结构体
      totalPage: 0,
      pageIndex: 1,
      pageSize: this.$store.state.settings.pageSize,
    };
  },
  activated(){
    
  },
  methods: {
    init(name) {
      this.name = name;
      this.visible = true;
      this.loading=true;
      this.applicationLog="";
      this.$nextTick(() => {
        // 获取路由
        this.$http({
          url: this.$http.adornUrl(
            `/generator/applications/appRunningLog/${this.name}`
          ),
          method: "get",
          //
        })
          .then((res) => {
            if(res.data.code==500){
                this.$message({
                    type: 'info',
                    message: '暂无数据'
                });  
            }
            
            this.applicationLog=res.data.log;
            this.loading=false;
          })
          .catch((error) => {
            
            console.log("获取日志信息错误", error);
          });
      });
      this.timer=window.setInterval(()=>{
      setTimeout(()=>{
        console.log("执行定时任务");
        this.getOutput();
      },0)
      },1000);
    },
    getOutput(){
      this.$http({
          url: this.$http.adornUrl(
            `/generator/applications/appRunningLog/${this.name}`
          ),
          method: "get",
          //
        })
          .then((res) => {
            if(res.data.code==500){
                this.$message({
                    type: 'info',
                    message: '暂无数据'
                });  
            }
            
            this.applicationLog=res.data.log;
          })
          .catch((error) => {
            console.log("获取日志信息错误", error);
          });
    },
    handleClose(){
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
        
      }
      this.visible=false;
    },
    //切换路由时，清楚定时器
    beforeRouteLeave(to, from, next){
      next();
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },

  },
};
</script>

