<template>
  <div class="self-container" >
    <iframe v-show="!show" width="98%" id="iframe" height="96%" :src="pageUrl"></iframe>
  </div>
</template>

<script>
import { layer } from "@layui/layer-vue";
import websocket from '../../assets/js/websocket';
import waitGit from '../../assets/images/wait.gif';
import printGit from '../../assets/images/print.gif';
import icoOK from '../../assets/images/ico/ico_ok.png';
var _this;
export default {
  data() {
	  return {
      pageUrl: '',
      show: false,
      windowName: '',
      tips: '',
      showTip: false,
      imgSrc: '',
      position: 0,
      callbackMethod: '', //打印页面回调方法
      materials: [],//材料集合
    }
	},
  created() {
    this.pageUrl =  this.$route.query.pageUrl;//页面路径
    this.materials=this.$route.query.materials;//扫描材料
    this.init(this.materials);
  },
  mounted () {
    // 将Vue方法传到全局对象window中
    window.receiveMsg = this.receiveMsg;
    window.printReport = this.printReport;
    window.printSuccess = this.printSuccess;
    window.printError = this.printError;
    window.playAudio = this.playAudio;
    window.printCallback = this.printCallback;
    window.IDCardCallback = this.IDCardCallback;
    _this = this;
  },
	methods:{
		init(materials) {
      this.$nextTick(() => {
        //获取iframe元素
        const iframe = document.getElementById("iframe");
        iframe.onload = function (e) {
          //推送数据到表单页面
          iframe.contentWindow.postMessage({
            action: "init",
            result: JSON.parse(materials)
          }, "*");
        }
      });
		},

    printCallback(bool){
      //获取iframe元素
      const iframe = document.getElementById("iframe");
      //推送数据到表单页面
      iframe.contentWindow.postMessage({
        action: "printCallback",
        result: {
          'bool': bool,
          'callbackMethod': this.callbackMethod
        }
      }, "*");
    },
  }
}

window.addEventListener("message", message => {
  var data = message.data;
  if('next' == data.action){
    var result = data.result;
    var success = result.success;
    if('0' == success || '1' == success){
      //转圈圈等待
      websocket.openClick(_this.batchId);
      //layer.load();
      _this.$refs.child.counter= result.waittime + 10; //修改子组件data
      _this.show = true;
      _this.showTip = true;
      _this.tips = "后台正在为您办理中，请稍等!";
      _this.imgSrc = waitGit;
      _this.position = 30;
      if(robotType){
        robotsdk.playAudio({
          cmd: 'playAudio',
          animation: 0,
          videocontent: '后台正在为您办理中，请稍等',
          callback: ""
        });
      }
    } else {
      //跳转到错误页面提示
      _this.$router.push({ path: '/errorPage'});
    }
  }

  //跳评价页面
  else if ('evaluation' == data.action){
    _this.$router.push({ path: '/evaluation'});
  }

  //跳资料袋页面
  else if ('information' == data.action){
    var caseData = _this.$LocalStorage.SessionGet("caseData");
    if (data.result!=null && data.result!="" && data.result!=undefined){
      var result = data.result;
      caseData.lastNum = result.lastNum;
    }
    _this.$LocalStorage.SessionPut("caseData", caseData);
    _this.$router.push({ path: '/information'});
  }

  //退出到咨询页面
  else if('closeExternalApp' == data.action){
    robotsdk.closeExternalApp({
      mode:0
    });
  }

  //语音播报
  else if ('playAudio' == data.action){
    var result = data.result;
    var videocontent = result.videocontent;
    robotsdk.playAudio({
      cmd: 'playAudio',
      animation: 0,
      videocontent: videocontent,
      callback: ""
    });
  }

  //打开键盘
  else if ('openKeyBoard' == data.action){
    robotsdk.openKeyBoard({
      oldContent: "",
      type: "",
    })
  }

  //关闭键盘
  else if ('closeKeyBoard' == data.action){
    robotsdk.closeKeyBoard();
  }

  //打印
  else if ('printFile' == data.action){
    var result = data.result;
    var base64Byte = result.base64Byte;
    var remoteprintname = result.remoteprintname;
    var replyvoice = result.replyvoice;
    _this.callbackMethod = result.callbackMethod;//打印页面回调方法
    robotsdk.printFile({
      filetype: "doc",					              //文件类型
      base64: base64Byte,		                  //文件内容base64字符串
      remoteprintname: remoteprintname,				//远程打印机名称
      replyvoice: replyvoice,	                //打印提示语音
      callback: "printCallback"		          	//接口回调函数
    })
  }

  //读取身份证
  else if ('readIDCard' == data.action){
    var result = data.result;
    var remindvoice = result.remindvoice;
    robotsdk.readIDCard({
      remindvoice: remindvoice,     //播放的声音内容
      callback: "IDCardCallback"    //接口回调函数
    })
  }

  //提示框
  else if ('showMessage' == data.action){
    var result = data.result;
    var isOpen = result.isOpen;
    var content = result.content;
    var isAutoClose = result.isAutoClose;
    robotsdk.showMessage({
      isOpen: isOpen,//true 打开 false 关闭
      content: content,//消息内容
      isAutoClose: isAutoClose//true 5秒自动关闭 false 用户点确认关闭
    });
  }

})



</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<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{
    color: #ffffffc9;
    font-size: 24px;
    position: absolute;
    bottom: 30%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .gif {
    position: relative;
    padding: 0;
    margin: 0;
  }
</style>
