<template>
  <div class="self-container">
    <div class="print-content"></div>
    <div class="printing-word">
      <p>您本次申报材料不符合审查要求，</p>
      <p>您可以通过“一键呼叫”按钮，呼叫工作人员帮您进行人工审查。</p>
    </div>
    <div class="button">
      <div class="oneCall" v-show="oneCalled" @click="oneCall"></div>
      <div class="oneCall-not" v-show="!oneCalled" ></div>
      <div class="skip" @click="skip"></div>
    </div>
  </div>
  <count-down times=300 bottomOffset="10" rightOffset="10" fontSize="18" ref="child"></count-down>
  <b-page @bPageLoaded="voiceCallback"></b-page>
</template>
<script>
import { layer } from "@layui/layer-vue";
import websocket from '../assets/js/websocket';
import commonUtil from "@/assets/js/commonUtil";
import BPage from "@/components/registerVoice";

export default {
  components: {
    BPage,
  },

	data() {
		return {
      userId: '',
      batchId: '',
      enterpriseId: '',
      serviceId: '',
      oneCalled: true,
      received: false, //是否接收到审查结果，二维码扫描，websocket消息
      success: false, //人工审查结果
      artificialType:'0',
      pageUrl:'' //表单页面地址
		}
	},
	created() {
    var caseData = this.$LocalStorage.SessionGet("caseData");
    this.batchId = caseData.batchId;
    this.serviceId = caseData.serviceId;
    this.pageUrl = caseData.pageUrl;
    this.userId  = commonUtil.genUserId();
    this.enterpriseId = caseData.enterpriseId;
    this.$LocalStorage.SessionPut("caseData", caseData);

		if(robotType){
			robotsdk.playAudio({
				cmd: 'playAudio',
				animation: 0,
				videocontent: '您本次申报材料不符合审查要求，您可以通过“一键呼叫”按钮，呼叫工作人员帮您进行人工审查。',
				callback: ""
			});
		}

	},
  mounted () {
    // 将Vue方法传到全局对象window中
    window.receiveMsg = this.receiveMsg;
    window.qrCodeCallback = this.qrCodeCallback;
    window.playAudioCallback = this.playAudioCallback;
  },
	methods: {
    oneCall() {
      if(robotType){
        robotsdk.playAudio({
          cmd: 'playAudio',
          animation: 0,
          videocontent: '正在为您呼叫工作人员进行人工审查',
          callback: ""
        });
      }

      this.oneCalled = false;
      var _this = this;
      //发送小程序消息提醒协办员审查资料，发送成功同步创建websocket连接，接收小程序结果反馈
      $.ajax({
        type: "get",
        url: window.config.api + 'lilo/wxIndex/subscribeMessageSendXBY',
        async: false, //改为同步请求
        data: {enterpriseId: this.enterpriseId, batchId: this.batchId, userId: this.userId, msgType: '1'},
        /*data: {enterpriseId: "af5492d4417c44c280e480e5264b8846", batchId: "20221010155458756625", userId: this.userId, msgType: '1'},*/
        success:function(data){
          if(data.success){
            //发送成功，等待协办员处理
            _this.initWebSocket();
            //打开二维码扫描
            if(robotType){
              robotsdk.openQRCode({
                callback: "qrCodeCallback"
              });
            }
          } else {
            layer.msg("系统繁忙，请到窗口办理！", { time: 1500, icon: 3 });
            console.log(data.msg);
          }
        },error: function(error){
          layer.msg("系统繁忙，请到窗口办理！", { time: 1500, icon: 3 });
          console.log(error);
        }
      })
    },

    skip() {
      //进入预审结果页面
      this.$router.push({ path: '/report'});
    },

    initWebSocket() {
      websocket.openClick(this.userId);
    },

    receiveMsg(obj){
      var type = obj.type;
      var msg = obj.msg;
      if('4' == type){ //类型 2.资料扫描下载 3 远程核验 4人工审查 5人工审查确认
        this.$refs.child.counter = 300; //审查人员查看消息时，重置倒计时为300秒
        if(robotType){
          robotsdk.playAudio({
            cmd: 'playAudio',
            animation: 0,
            videocontent: msg,
            callback: ""
          });
        }
      }
      if('5' == type){ //类型 2.资料扫描下载 3 远程核验 4人工审查 5人工审查确认
        this.qrCodeCallback(msg);
      }

    },

    qrCodeCallback(obj){
      if(!this.received){
        this.received = true;
        if(robotType){
          robotsdk.closeQRCode();
        }
        var handleType = obj.split(",")[2]; //handleType 0通过 1不通过
        var caseData = this.$LocalStorage.SessionGet("caseData");
        this.artificialType = caseData.artificialType;
        var msg = "";
        if('0' == handleType){
          msg = "经过人工审查，您的材料已通过";
          caseData.success = true;
          this.success = true;
        } else {
          msg = "经过人工审查，您的材料不通过";
          caseData.success = false;
        }
        this.$LocalStorage.SessionPut("caseData", caseData);
        if(robotType){
          robotsdk.playAudio({
            cmd: 'playAudio',
            animation: 0,
            videocontent: msg,
            callback: "playAudioCallback"
          });
        }
      }
    },

    playAudioCallback(){
      if(this.success){
        //是否远程核验
        if(this.artificialType == '0'){
          //是否需要表单页面
          if(this.pageUrl != null){
            this.$router.push({ path: '/dataView'});
          } else {
            this.$router.push({ path: '/report'});
          }
        } else {
          this.$router.push({ path: '/remoteCheck'});//远程核验页面
        }
      } else {
        //进入预审结果页面
        this.$router.push({ path: '/report'});
      }

    },

    //语音回调（只处理退出）
    voiceCallback(res){
      if (res=="quit"){
        if(robotType){
          robotsdk.closeExternalApp({
            mode:0
          });
        }
      }
    }

	}
}
</script>

<style scoped>

  .self-container {
    background-image: url('../assets/images/background.png');
    background-repeat: no-repeat;
    background-size: cover;
    overflow: auto;
    height: 100%;
    padding: 0;
    margin: 0;
  }

  .print-content {
    background-image: url('../assets/images/ico/ico_warn.png');
    background-repeat: no-repeat;
    overflow: auto;
    width: 204px;
    height: 184px;
    position: absolute;
    top: 35%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .printing-word{
    width: 100%;
    font-size: 26px;
    color: #ffffff;
    position: absolute;
    bottom: 27%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .button{
    display: inline-flex;
    position: fixed;
    left: 50%;
    bottom: 0;
    transform: translate(-50%, 0);
  }

  .oneCall{
    width: 300px;
    height: 85px;
    margin-right: 20px;
    background-image: url('../assets/images/btn/btnCall.png');
    background-repeat:no-repeat;
  }

  .oneCall-not{
    width: 300px;
    height: 85px;
    margin-right: 20px;
    background-image: url('../assets/images/btn/btnCall_Not.png');
    background-repeat:no-repeat;
  }

  .skip{
    width: 300px;
    height: 85px;
    background-image: url('../assets/images/btn/btnSkipReview.png');
    background-repeat:no-repeat;
  }
</style>
