<template>
  <b-page @bPageLoaded="voiceCallback"></b-page>
  <count-down times=100 bottomOffset="0" rightOffset="10" fontSize="16" ref="child"></count-down>
  <template v-if="schedule == 0">
    <div class="information">
      <div style="padding-top: 5%;">
        <div style="margin-top: 45px;">
          <img src="../assets/images/baleremind.gif" >
        </div>
        <div style="margin-top: 45px;">
          <font size="6" color="#FFFFFF">您本次申报一共提交了{{count+lastNum}}张资料，把资料放入资料袋中，拉上拉链。取出资料带左侧的塑料条，穿过拉链以及材料袋右侧孔洞，扣上塑料扣。然后将资料袋的二维码对准设备上的扫码区扫描</font>
        </div>
      </div>
    </div>
  </template>
  <template v-else-if="schedule==1">
    <div class="information">
      <div style="padding-top: 5%;">
        <div style="margin-top: 200px;">
          <img style="width: 240px;height: 200px" src="../assets/images/ico/ico_warn.png" >
        </div>
        <div style="margin-top: 90px;">
          <font size="6" color="#FFFFFF">抱歉这个资料袋暂时不能使用，请重新更换资料袋！</font>
        </div>
        <div style="position: absolute;margin-left: 248px;margin-top: 90px;">
          <img style="width: 90%;" src="../assets/images/btn/btnConfirm.png" @click="confirmClick" id="confirm">
        </div>
        <div style="position: absolute;margin-left: 600px;margin-top: 90px;">
          <img style="width: 90%;" src="../assets/images/btn/btnCancel.png" @click="cancelClick" id="cancel">
        </div>
      </div>
    </div>
  </template>
  <template v-else-if="schedule==2">
    <div class="information">
      <div style="padding-top: 5%;">
        <div style="margin-top: 10px;">
          <img style="width: 500px;height: 500px" src="../assets/images/deliverremind2.gif" >
        </div>
        <div style="margin-top: 90px;">
          <font size="6" color="#FFFFFF">扫描完后，将材料袋投递到设备的文件投递柜中即可。</font>
        </div>
      </div>
    </div>
  </template>

  <template v-else-if="schedule==3">
    <div class="information">
      <div style="padding-top: 5%;">
        <div style="margin-top: 10px;">
          <img style="width: 500px;height: 500px" src="../assets/images/deliverremind2.gif" >
        </div>
        <div style="margin-top: 90px;">
          <font size="6" color="#FFFFFF">抱歉，递送机器人暂无空闲，请您将资料投递在资料柜中！！</font>
        </div>
      </div>
    </div>
  </template>

  <template v-else-if="schedule==4">
    <div class="information">
      <div style="padding-top: 5%;">
        <div style="margin-top: 45px;">
          <img style="margin-top: 73px;margin-left: -801px;width: 410px;height: 450px" src="../assets/images/robot.gif" >
        </div>
        <div style="margin-top: -387px;margin-left: 435px;">
          <font size="6" color="#FFFFFF">机器人易小递正在火速赶来，请耐心等待一下哦！</font>
          <br>
          <br>
          <br>
          <font size="6" color="#FFFFFF">开柜密码: {{putCode}}</font>
        </div>
      </div>
    </div>
  </template>

  <template v-else-if="schedule==5">
    <div class="information">
      <div style="padding-top: 5%;">
        <div style="margin-top: 45px;">
          <img style="margin-top: 73px;margin-left: -801px;width: 410px;height: 450px" src="../assets/images/robot.gif" >
        </div>
        <div style="margin-top: -387px;margin-left: 435px;">
          <font size="6" color="#FFFFFF">机器人易小递正在火速赶来，请耐心等待一下哦！</font>
          <br>
          <br>
          <br>
          <font size="6" color="#FFFFFF">开柜密码： {{putCode}}</font>
          <br>
          <br>
          <br>
          <font size="6" color="#FFFFFF">输入密码后，将资料袋放入递送机器人中</font>
        </div>
      </div>
    </div>
  </template>

  <template v-else-if="schedule==6">
    <div class="information">
      <div style="padding-top: 5%;">
        <div style="margin-top: 200px;">
          <img style="width: 90px;height: 90px" src="../assets/images/wait.gif" >
        </div>
        <div style="margin-top: 50px;">
          <font size="6" color="#FFFFFF">正在为您打印回执单！</font>
        </div>
      </div>
    </div>
  </template>

  <template v-else-if="schedule==7">
    <div class="information">
      <div style="padding-top: 5%;">
        <div style="margin-top: 272px;">
          <font size="6" color="#FFFFFF">回执单打印成功！</font>
        </div>
      </div>
    </div>
  </template>

</template>
<style scoped>
.information{
  background-image: url('../assets/images/background.png');
  background-repeat: no-repeat;
  background-size: cover;
  overflow:auto;
  margin-bottom: 20px;
  padding: 0 70px;
  height:100%;
}
</style>
<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 {
      count:'',
      lastNum:"",
      serviceId:'',
      batchId:'',
      projectId: '',
      schedule:'',
      putCode:"",
      userId:"",
      res:"",
    }
  },
  created() {
    let caseData = this.$LocalStorage.SessionGet("caseData");
    if (caseData.lastNum==undefined || caseData.lastNum==null || caseData.lastNum==""){
      this.lastNum = 0;
    }else {
      this.lastNum = caseData.lastNum;
    }
    this.count = caseData.totalNum;
    this.projectId = caseData.projectId;
    this.batchId = caseData.batchId;
    this.serviceId = caseData.serviceId;
    this.enterpriseId = caseData.enterpriseId;
    this.isReceipt = caseData.isReceipt;
    this.receiptData = caseData.receiptData;
    this.userId  = commonUtil.genUserId();
    // this.enterpriseId = "1e64495836fb43fcacaf574ac67366be";
    // this.serviceId = "S975104601";
    // this.projectId = "8160784";
    // this.batchId = "2022109112754844826";
    // this.count = "3";

    this.schedule = 0;
    this.res = "";
    this.isOpenRQCode = 1;//控制扫描枪是否扫描 0；不扫描 1：扫描
    window.printReport = (res) => this.printReport(res);//打印回调
    window.PlayAudio_printReportCallback = (res) => this.PlayAudio_printReportCallback(res);//打印回执单语音回调
    window.QRCodeCallback = (res) => this.QRCodeCallback(res);//扫描二维码回调
    window.RFIDCallback = (mode, code) => this.RFIDCallback(mode, code);//扫描RFID回调
    window.receiveMsg = this.receiveMsg;//websocket回调
    this.PlayAudio();
  },
  methods:{
    timing(){//调试页面使用
      let timer = setInterval(() => {
        //需要定时执行的代码
        this.schedule++;
        console.log(this.schedule);
        if (this.schedule>6){
          this.schedule=6;
          //清除多次执行定时器
          clearInterval(timer)
          // this.$router.push({ path: '/scanMaterial' , query: {projectId: this.projectId}});
        }
      },1)
    },

    PlayAudio(){
      if (robotType) {
        robotsdk.playAudio({
          cmd: "cmd",
          animation: 0,
          videocontent: "您本次申报一共提交了" + (this.count+this.lastNum) + "张资料，把资料放入资料袋中，拉上拉链。取出资料带左侧的塑料条，穿过拉链以及材料袋右侧孔洞，扣上塑料扣。然后将资料袋的二维码对准设备上的扫码区扫描",
          callback: ""
        })
        if (this.isOpenRQCode==1){
          robotsdk.openQRCode({//开启扫描枪
            callback: "QRCodeCallback"
          })
        }
      }else {
        this.timing();
      }
    },

    //扫描二维码回调，(res：二维码编号)
    QRCodeCallback(res) {
      this.res = res;
      if (this.isOpenRQCode==1){
        // this.isOpenRQCode=0;//关闭扫描枪
        console.info(res)
        $.post(window.config.api + 'lilo/aiCase/checkByRfid', {
          rfid:res
        }).then(item => {
          if (item.success) {//资料袋未被使用
            console.info("进入绑定rfid")
            this.stockpile(res);//办件绑定Rfid
          }else {//资料袋已被使用
            this.schedule=1;//页面显示控制
            this.isOpenRQCode=1;//开启扫描枪
            robotsdk.playAudio({
              cmd: "cmd",
              animation: 0,
              videocontent: "抱歉这个资料袋暂时不能使用，请重新更换资料袋！",
              callback: ""
            })
          }
        })
      }
    },

    //办件信息绑定Rfid
    stockpile(res){
      console.info(res)
      $.post(window.config.api + 'lilo/aiCase/upRfidByBatchId', {
        batchId:this.batchId,
        rfid:res,
        serviceId:this.serviceId,
        status:"0"
      }).then(item => {
        console.info(item);
        //二维码编号绑定rfid成功
        if (item.success) {
          robotsdk.closeQRCode();//关闭二维码扫描枪
          this.queryBindList(res);//查询该政务中心是否配备递送机器人,绑定则跳呼叫递送机器人，没有绑定则跳存柜
        }else {//资料袋不存在
          this.schedule=1;//页面显示控制
          robotsdk.playAudio({
            cmd: "cmd",
            animation: 0,
            videocontent: "抱歉这个资料袋暂时不能使用，请重新更换资料袋！",
            callback: ""
          })
        }
      })
    },

    //查询该政务中心是否配备递送机器人、是否事项绑定了站点，都绑定则跳呼叫递送机器人，没有绑定则跳存柜
    queryBindList(res){
      $.post(window.config.api + 'lilo/aiCase/queryBindList', {
        beBoundClient:this.serviceId
      }).then(item => {
        console.info(item);
        if (item.success) {//接口调用成功
          if (item.obj==null || item.obj.length==0){//没有绑定递送
            this.deposit(res);//存柜
          }else {//绑定了递送
            $.post(window.config.api + 'lilo/nineRobot/isBandStation', {//查询事项是否绑定了站点
              projectId:this.projectId
            }).then(item => {
              if (item.success) {//接口调用成功
                if (item.obj.isBandStation){//事项绑定了站点
                  this.delivering(res);//递送流程
                }else {//事项没有绑定站点
                  this.deposit(res);//存柜
                }
              }else {//接口调用失败
                this.$router.push({ path: '/errorPage'});//转跳错误提示页面
              }
            })
          }
        }else {//接口调用失败
          this.$router.push({ path: '/errorPage'});//转跳错误提示页面
        }
      })
    },

                                  //存柜流程
    //存柜
    deposit(res){
      this.schedule=2;//页面显示控制
      robotsdk.playAudio({
        cmd: "cmd",
        animation: 0,
        videocontent: "扫描完后，将材料袋投递到设备的文件投递柜中即可。",
        callback: ""
      })
      let obj = robotsdk.startRFIDRead({//打开RFID扫描
        code: res,
        callback: "RFIDCallback"
      })
    },

    //RFID扫描回调
    RFIDCallback(mode, code){
      $.post(window.config.api + 'lilo/aiCase/upRfidByBatchId', {//修改办件状态为存柜状态
        batchId:this.batchId,
        rfid:code,
        serviceId:this.serviceId,
        status:"1"
      }).then(item => {
        console.info(item);
        if (this.isReceipt=="0"){//不打回执单
          this.$router.push({ path: '/evaluation'});//跳转到好差评页面
        }else {//打回执单
          this.printReceipt();
        }
      })
    },

    confirmClick(){
      this.schedule=0;
      this.isOpenRQCode=1;
      robotsdk.playAudio({
        cmd: "cmd",
        animation: 0,
        videocontent: "将资料袋的二维码对准设备上的扫码区扫描",
        callback: ""
      })
    },

    //点击取消
    cancelClick(){
      if(robotType){
        //退出到咨询页面
        robotsdk.closeExternalApp({
          mode:0
        });
      }
    },

                                              //递送流程
    //递送流程——下发运单
    delivering(res){
      this.initWebSocket();
      $.post(window.config.api + 'lilo/nineRobot/createTask', {//下发运单
        enterpriseId:this.enterpriseId,
        serviceId:this.serviceId,
        projectId:this.projectId,
        batchId:this.batchId,
        userId:this.userId,
      }).then(item => {
        console.info(item);
        if (item.success) {
          //是否有空闲机器人
          if(item.obj.isIdle) {//有空闲则下发运单
            this.putCode = item.obj.putCode;//获取放件码
            this.schedule=4;//页面显示控制
            robotsdk.playAudio({
              cmd: "cmd",
              animation: 0,
              videocontent: "机器人易小递正在火速赶来，请耐心等待一下哦！",
              callback: ""
            })
            var date = new Date();
            console.log("建立链接时间="+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());
          } else {//没有空闲
            this.schedule=3;//页面显示控制
            robotsdk.playAudio({
              cmd: "cmd",
              animation: 0,
              videocontent: "抱歉，递送机器人暂无空闲，请您将资料投递在资料柜中！",
              callback: ""
            })
            //打开RFID扫描
            let obj = robotsdk.startRFIDRead({
              code: res,
              callback: "RFIDCallback"
            })
          }
        }else {//接口调用失败
          // this.$router.push({ path: '/errorPage'});//转跳错误提示页面
        }
      })
    },

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

    receiveMsg(obj){
      let _taskId = obj.taskId;
      let timer;
      console.log(obj.taskStatus);
      if ("Waiting"==obj.taskStatus){
        timer = setTimeout(() => {
          //需要定时执行的代码
          this.schedule=3;//页面显示控制
          robotsdk.playAudio({
            cmd: "cmd",
            animation: 0,
            videocontent: "抱歉，递送机器人暂无空闲，请您将资料投递在资料柜中！",
            callback: ""
          })
          $.post(window.config.api + 'lilo/nineRobot/cancelTask', {//取消订单
            enterpriseId:this.enterpriseId,
            taskId:_taskId,
          }).then(item => {
            console.log(item);
          })
          //打开RFID扫描
          let obj = robotsdk.startRFIDRead({
            code: this.res,
            callback: "RFIDCallback"
          })
        },8000)
      }

      if ("Taking"==obj.taskStatus){//到达第一个取件点
        this.schedule=5;//页面显示控制
        robotsdk.playAudio({
          cmd: "cmd",
          animation: 0,
          videocontent: "输入密码后，将资料袋放入递送机器人中！",
          callback: ""
        })
      }
      if ("Delivering"==obj.taskStatus){//取到件
        $.post(window.config.api + 'lilo/aiCase/robotPickup', {//修改办件状态为存柜状态
          batchId:this.batchId,
          status:"3"//办件状态3为存柜状态
        }).then(item => {
          console.info(item);
          if (this.isReceipt=="0"){//不打回执单
            this.$router.push({ path: '/evaluation'});//跳转到好差评页面
          }else {//打回执单
            this.printReceipt();
          }
        })
      }
    },

                                //打印回执流程
    //打印回执
    printReceipt(){
      if(robotType){
        $.post(window.config.rocrUrl + 'lilo/aiCase/callReceipt', {//打印回执单接口
          receiptData:this.receiptData,
        }).then(item => {
          console.info(item);
          if (item.success) {//接口调用成功
            this.schedule=6;//页面显示控制
            robotsdk.printFile({
              filetype: 'doc',
              base64: item.obj.base64Byte,
              remoteprintname: item.obj.remotePrintName,
              replyvoice: '请稍等，正在为您打印！',
              callback: "printReport"
            });
          }else {//接口调用失败
            this.$router.push({ path: '/errorPage'});//转跳错误提示页面
          }
        })
      }
    },

    //打印回执回调
    printReport(bool){
      if (bool){//打印成功返回true
        this.schedule=7;
        robotsdk.playAudio({
          cmd: "cmd",
          animation: 0,
          videocontent: "回执单打印成功",
          callback: "PlayAudio_printReportCallback"
        })
      }else {
        this.$router.push({ path: '/errorPage',query: {msg: '回执单打印失败'}});//转跳错误提示页面
      }
    },

    //"回执单打印成功_语音回调"
    PlayAudio_printReportCallback(res){
      if (res=="cmdOK") {
        let timer = setInterval(() => {
          //需要定时执行的代码
          this.$router.push({ path: '/evaluation'});//语音播报完2秒后跳转到好差评页面
        },2000)
      }
    },

    //语音回调（只处理退出）
    voiceCallback(res){
      if (res=="quit"){
        this.cancelClick();
      }
    }

  }
}
</script>