<template>
  <b-page @bPageLoaded="voiceCallback"></b-page>
  <count-down times=300 bottomOffset="10" rightOffset="10" fontSize="24" ref="child"></count-down>
  <template v-if="schedule == 0">
    <div class="scanMaterial" style="height:100%">
      <!-- 左区域 -->
      <div class="left">
        <div class="example"></div>
        <div style="padding-right: 1%;padding-top: 32px;">
          <!-- 取消按钮 -->
          <img class="imgButton" v-if="!BtnIsShow"  src="../../assets/images/btn/backGray.png">
          <img class="imgButton" v-if="BtnIsShow" @click="back" src="../../assets/images/btn/btnCancel.png">&nbsp;&nbsp;
          <!-- 扫描按钮 -->
          <img class="imgButton" v-if="!BtnIsShow || !scanBtnIsShow"  src="../../assets/images/btn/scanGray.png">
          <img class="imgButton" v-if="BtnIsShow && scanBtnIsShow" @click="scanBtn" src="../../assets/images/btn/btnScan.png">&nbsp;&nbsp;
          <!-- 下一步按钮 -->
          <img class="imgButton" v-if="!isBtnNextShow" src="../../assets/images/btn/btnPrint_Not.png">
          <img class="imgButton" v-if="isBtnNextShow" @click="nextStep" src="../../assets/images/btn/btnPrint.png">
        </div>
      </div>

      <!-- 图片区域 -->
      <div class="imgList">
        <div class="imgBetween"  v-for="(material, index) in materialList">
          <img class="imgSize"  v-if="material.fileUrl!=''" :src="material.fileUrl">
          <div class="fileName" v-if="material.fileUrl!=''" >
            {{material.name}}
          </div>
          <div class="fileName" v-if="material.fileUrl==''" style="width: 74%;padding-left: 28%;color: #FFFFFF;padding-top: 22%;">
            {{material.name}}
          </div>
        </div>
      </div>

      <!-- 删除按钮 -->
      <div class="imgDel">
        <div class="imgDelBetween" v-for="(del, index) in imgDelList">
          <img @click="delImg(index,del.fileId)" v-if="del.name==''">
          <img @click="delImg(index,del.fileId)" v-if="del.name!=''" :src="del.imgSrc">
        </div>
      </div>

      <!-- 图片框 -->
      <div class="right">
        <div  class="rightFrame" v-for="(freme, index) in imgFrames">
          <img class="imgFrame" :src="freme.imgSrc">
        </div>
      </div>

    </div>
  </template>


  <template v-if="schedule == 1" >
    <div class="printMaterial">
      <div style="padding-top: 5%;">
        <div style="margin-top: 120px;">
          <img style="width: 374px;height: 294px" src="../../assets/images/print.gif" >
        </div>
        <div style="margin-top: 50px;">
          <font size="6" color="#FFFFFF">请稍等，系统正在为您打印！</font>
        </div>
      </div>
    </div>
  </template>

  <template v-if="schedule == 2" >
    <div class="printMaterial">
      <div style="padding-top: 5%;">
        <div style="margin-top: 120px;">
          <img style="width: 190px;height: 190px" src="../../assets/images/ico/ico_ok.png" >
        </div>
        <div style="margin-top: 150px;">
          <font size="6" color="#FFFFFF">已为您打印成功,稍后系统将返回首页！</font>
        </div>
      </div>
    </div>
  </template>
</template>
<script>
import material from "@/views/material";
import BPage from "@/components/registerVoice";

export default{
  components: {
    BPage,
  },



  data() {
    return {
      applyType: "",
      schedule: "",
      curPage: 1,//当前页码
      num: 5,//每页展示数量
      materials: [],//材料集合
      materialList:[],//当前页面图片展示
      imgDel:[],//删除按钮集合
      imgDelList:[],//当前页面删除按钮展示
      BtnIsShow:false,//控制取消,按钮是否显示
      scanBtnIsShow:false,//扫描按钮是否亮起
      isBtnNextShow:false,//控制打印按钮是否亮起
      imgFrames:[
        {'imgSrc':require('../../assets/images/filebox.png')},
      ],//图片框集合
      highCameraParameter:{//高拍仪参数
        x: 68,//预览界面x坐标
        y: 47,//预览界面y坐标
        width: 900,//预览界面宽度
        height: 676,//预览界面高度
      },
      userInfoParameter:{//办事人信息
        callback: "getUserInfo"
      },
      highCameraImgParameter:{//获取高拍仪照片
        callback: "HighCameraCallback"
      },
      count:0,
    }
  },
  created() {
    this.schedule = 0;
    this.applyType = "1";//1:身份证核验打印
    this.materials.push({'name':'营业执照','fileUrl':'','fileId':'','artificialType':'','type':''});

    this.imgDel.push({'name':'','fileUrl':'','fileId':'','artificialType':'','type':''});

    this.pageInfo(this.curPage,this.num);
    if (robotType)this.openVoice( "请您将营业执照放入扫描框进行扫描");
    if (robotType)robotsdk.getUserInfo(this.userInfoParameter);//获取办事人信息

    //将方法绑定到window下面，提供给外部调用
    window.PlayAudioCallback = (res)=> this.PlayAudioCallback(res);//语音播放
    window.HighCameraCallback = (success, obj)=> this.HighCameraCallback(success, obj);//获取高拍仪照片
    window.ConfirmCallback = (res)=> this.ConfirmCallback(res);//提示确认，取消对话框
    window.getUserInfo = (obj)=> this.getUserInfo(obj);//获取身份证信息
    window.printCallBack = (obj)=> this.printCallBack(obj);//打印回调
    window.PlayAudio_printCallback = (res)=> this.PlayAudio_printCallback(res);//打印语音回调
  },
  methods:{

    back() {
      if (robotType)robotsdk.closeHighCamera();//关闭高拍仪
      if(robotType){
        robotsdk.closeExternalApp({//退出跳转咨询页面
          mode:0
        });
      }
    },
    nextStep() {//打印
      if (robotType)robotsdk.closeHighCamera();//关闭高拍仪
      this.schedule = 1;
      $.post(window.config.rocrUrl + 'lilo/apply/copyImg2word', {//打印
        materials:JSON.stringify(this.materials),
      }).then(item => {
        this.masks(false);
        robotsdk.printFile({
          filetype: 'doc',
          base64: item.obj.copyImg2wordBas64,
          remoteprintname: "",
          replyvoice: '请稍等，系统正在为您进行打印！',
          callback: "printCallBack"
        });
      })
      // if (!this.isRepetition) this.$LocalStorage.SessionPut("entrustMaterials",this.materials);
      // if (this.isRepetition) this.$LocalStorage.SessionPut("entrustMaterials",this.materials[0]);
      // this.$LocalStorage.SessionPut("scanType",this.scanType);
      // this.$router.push({ path: '/material',query:{isCancel:true}});
    },

    printCallBack(bool){//打印回调
      if (bool){//打印成功返回true
        this.schedule = 2;
        this.masks(false);
        robotsdk.playAudio({
          cmd: "cmd",
          animation: 0,
          videocontent: "已为您打印成功,稍后系统将返回首页",
          callback: "PlayAudio_printCallback"
        })
      }else {
        this.$router.push({ path: '/errorPage',query: {msg: '打印失败'}});//转跳错误提示页面
      }
    },
    PlayAudio_printCallback(res){//打印语音回调
      if (res=="cmdOK") {
        let timer = setInterval(() => {
          //需要定时执行的代码
          if (robotType)robotsdk.closeHighCamera();//关闭高拍仪
          this.back();//退出
        },1000)
      }
    },
    scanBtn(){//扫描
      this.$refs.child.counter=300; //刷新时间
      this.masks(true);//遮罩层 true 打开 false 关闭
      if (robotType)robotsdk.getHighCamera(this.highCameraImgParameter);//获取高拍仪照片
    },
    pageInfo (curPage,num) {//右边图片框
      var curPage = (curPage - 1) * num;
      this.materialList = this.materials.slice(curPage,parseInt(curPage) + parseInt(num));
      this.imgDelList = this.imgDel.slice(curPage,parseInt(curPage) + parseInt(num));
    },
    delImg(index,fileId){
      this.$refs.child.counter=300; //刷新时间
      this.delMaterialByFileId(index,fileId);//删除材料数据
    },
    openVoice(content){
      robotsdk.playAudio({//语音播放参数
        cmd: "cmd",//语音指令，可参考属性voiceInstruct
        animation: 0,//虚拟任务动作编号可参考属性animationEnum
        videocontent:content,//语音内容
        callback: "PlayAudioCallback"//接口回调函数
      });//虚拟人物播放语音及动作
    },
    PlayAudioCallback (res){//初始语音回调
      if (res=="cmdOK"){
        if (robotType){
          robotsdk.openHighCamera(this.highCameraParameter);//打开高拍仪预览界面
          this.BtnIsShow=true;
          this.scanBtnIsShow = true;
          robotsdk.playAudio({//语音播放参数
            cmd: "sm",//语音指令，可参考属性voiceInstruct
            animation: 0,//虚拟任务动作编号可参考属性animationEnum
            videocontent: "",//语音内容
            callback: "PlayAudioCallback"//接口回调函数
          });//虚拟人物播放语音及动作
        }
      }
    },
    HighCameraCallback(success, obj){
      if (success) {
        let res = eval('(' + obj + ')');//高拍仪回调的数据集
        $.post( window.config.rocrUrl+'lilo/apply/uploadFile', {
          applyType: this.applyType,//1:身份证核验打印
          fileName : res.filename,
          base64 : res.file,
        }).then(item => {
          this.masks(false);//遮罩层 true 打开 false 关闭
          if (item.success) {

            let fileName=item.obj.fileName;
            if (fileName!='') {
              if (fileName.lastIndexOf("营业执照") != -1) {
                this.masks(false);//遮罩层 true 打开 false 关闭;
                this.isBtnNextShow = true;
                this.scanBtnIsShow = false;
                if(this.materialList[0].fileId==''){
                  this.upMaterials(0,item);//更新营业执照数据
                }else {
                  this.delMaterialByFileId(0,this.materialList[0].fileId);
                  this.upMaterials(0,item);//更新营业执照数据
                }
                robotsdk.playAudio({//语音播放参数
                  cmd: "sm",//语音指令，可参考属性voiceInstruct
                  animation: 0,//虚拟任务动作编号可参考属性animationEnum
                  videocontent: "营业执照扫描成功，您可以点击打印按钮进行打印",//语音内容
                  callback: ""//接口回调函数
                });//虚拟人物播放语音及动作
              }else {
                this.message("请勿提交其它文件！");//提示框
                this.openVoice("请勿提交其它文件");
              }
            }
          } else {
            this.masks(false);//遮罩层 true 打开 false 关闭
            this.message("服务器异常,扫描失败！");//提示框
            this.openVoice("服务器异常,扫描失败！");
          }
        })
      }
    },
    masks(bool){
      if (robotType) {
        robotsdk.showMasks({//遮罩层
          isOpen: bool //true 打开 false 关闭
        });
      }
    },
    message(content){
      if (robotType) {
        robotsdk.showMessage({//提示框
          isOpen: true,//true 打开 false 关闭
          content: content,//消息内容
          isAutoClose: true//true 5秒自动关闭 false 用户点确认关闭
        });
      }
    },
    getUserInfo(obj){
      let res = eval('(' + obj + ')');
      this.name=res.name;
      this.mobile=res.mobile;
      this.idcard=res.idcard;
    },
    showConfirmMessage(content){
      if (robotType) {
        robotsdk.showConfirmMessage({
          isOpen: true,
          content: content,
          isAutoClose: false,
          callback: "ConfirmCallback"
        });
      }
    },
    upMaterials(number,item){
      this.materials[number].fileUrl=item.obj.fileUrl;
      this.materials[number].fileId=item.obj.fileId;
      this.materials[number].type=item.obj.type;
      this.imgDel[number].fileId=item.obj.fileId;
      this.imgDel[number].name=item.obj.fileName;
      this.imgDel[number].imgSrc=require('../../assets/images/ico/delete.png');

    },

    delMaterialByFileId(index,fileId){
      $.post( window.config.rocrUrl+'lilo/apply/deleteFile', {
        fileId : fileId
      }).then(item => {
        if (item.success) {
          this.imgDel[index].name='';
          this.materials[index].fileUrl='';
          this.materials[index].fileId='';
          this.isBtnNextShow=false;
          this.scanBtnIsShow = true;
        }else{
          this.message("服务器异常,删除失败！");//提示框
        }
      }).catch(err => {
        this.message("服务器异常,删除失败！");//提示框
      })
    },

    //语音回调（只处理扫描,下一步,退出）
    voiceCallback(res){
      if (res=="scanning"){
        if (this.BtnIsShow&&this.scanBtnIsShow){
          this.scanBtn();
        }
      }

      if (res=="quit"){
        this.back();
      }
    }
  }
}
</script>
<style scoped>

.scanMaterial{
  background-image: url('../../assets/images/background.png');
  background-repeat:no-repeat;
  background-size:100% 100%;
  background-size: cover;
  overflow:auto;
}
.printMaterial{
  background-image: url('../../assets/images/background.png');
  background-repeat: no-repeat;
  background-size: cover;
  overflow:auto;
  margin-bottom: 20px;
  padding: 0 70px;
  height:100%;

}
.imgButton {
  width: 31%;
}
.example{
  background-image: url('../../assets/images/scanExample.gif');
  background-repeat:no-repeat;
  height: 676px;
  margin-left: 1.2%;
  margin-top: 1.4%
}
.left{
  height: 100%;
  float: left
}
.right{
  padding-left: 72%;
  padding-top: 20px;
  height: 82%;
}
.rightFrame{
  height: 140px;
}
.fileName{
  width: 74%;
  padding-left: 28%;
  color:#FFFFFF;
  padding-top: 2%;
}
.imgFrame{
  height: 92px;
  width: 45%;
}
.imgSize{
  height: 75px;
  width: 38%;
}
.imgList{
  position: absolute;
  left: 70.92%;
  padding-top: 2.2%;
  width: 30%;
}
.imgBetween{
  height: 140px;
}
.imgDel{
  position: absolute;
  padding-top: 1.5%;
  left: 87%;
  z-index: 99;
}
.imgDelBetween{
  height: 137px;
  z-index: 99;
}
</style>