<template>
  <div>
    <el-dialog
      :title="titleName?titleName:'同步状态'"
      :visible.sync="syncPollingVisible"
      :show-close="false"
      width="35%"
      :before-close="handleClose"
    >
      <div
        v-loading="loading"
        element-loading-text="进行中..."
        element-loading-spinner="el-icon-loading"
      ></div>
      <div v-html="msg" class="syncPollingMsg"></div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose()">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {
    syncPollingVisible: {
      type: Boolean,
      default: false,
    },
    batchCode: {
      type: String,
      default: "",
		},
		titleName:{
			type:String,
			default:''
		}
  },
  data() {
    return {
      batchCode: "",
      completed: false, //异步是否完成
      msg: "",
      loading: true,
    };
  },
  mounted() {},
  methods: {
    getPollSelectSyncStatus() {
      this.axios({
        url: "assistant/info/get/pollSelectSyncStatus",
        params: {
          batchCode: this.batchCode,
        },
        method: "post",
      }).then((res) => {
        if (res.success) {
          this.msg = res.module.msg;
          this.completed = res.module.code == 1 ? true : false;
          if (res.module.code == 1) {
            this.loading = false;
          }
        }
      });
    },
    pollingQuery() {
      let setTime;
      var _this = this;
      var i = 1;
      setTime = setInterval(function () {
        if (!_this.completed && _this.syncPollingVisible) {
          //未完成且窗口打开则需要查询
          console.log(
            "第" +
              i +
              "次查询,完成" +
              _this.completed +
              ",是否打开" +
              _this.syncPollingVisible
          );
          _this.getPollSelectSyncStatus();
          i++;
        } else {
          clearInterval(setTime);
        }
      }, 3000);
    },
    stopSync() {
      console.log("停止同步,同步批次" + this.batchCode);
      this.completed = false;
      this.syncPollingVisible = false;
      this.loading = true;
      this.msg = '';
      this.axios({
        url: "assistant/info/get/stopSync",
        params: {
          batchCode: this.batchCode,
        },
        method: "post",
      }).then((res) => {});
    },
    handleClose() {
      if (this.completed) {
        this.$emit("closeSync", false);
        this.stopSync();
      } else {
        this.$confirm("确认关闭？将停止同步！")
          .then((_) => {
            this.$emit("closeSync", false);
            this.stopSync();
          })
          .catch((_) => {});
      }
    },
  },
};
</script>
<style lang="less" scoped>
.syncPollingMsg {
  margin-top: 30px;
  margin-left: 30px;
}
</style>