import { Component, Input, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute } from "@angular/router";
import { Router } from '@angular/router';
import { MyService } from '../my.service';
declare var rrweb:any;

@Component({
  selector: 'ydlife-e-notice',
  templateUrl: './e-notice.component.html',
  styleUrls: ['./e-notice.component.scss']
})
export class ENoticeComponent implements OnInit,OnDestroy {
  @Input() agreeFlag:boolean = true;
  pageType:any;
  timerId:any;
  events:Array<any>;
  pageInfo = {
    type:5,
    name:'保单继续率承诺书同意签署页'
  }
  constructor(private activatedRoute: ActivatedRoute,private router:Router,private myService:MyService) {

   }

  ngOnInit() {
    this.pageType = this.activatedRoute.snapshot.data.type;
    if(this.pageType == 1){
      this.pageInfo = {
        type:6,
        name:'客户告知书同意并签署页面'
      }
    }
    this.creatRecord();
    this.timerId = setInterval(()=>{
      if(this.events.length > 50){
        this.saveRecord()
      }
    },10 * 1000);
  }
  ngOnDestroy(): void {
    clearInterval(this.timerId)
  }

  creatRecord(){
    let _this = this;
    _this.events = [];
    rrweb.record({
      emit(event){
        // 将evene存入events数组中
        _this.events.push(event);
      },
      recordCanvas: true
    });
  }
  saveRecord(){
    this.myService.rrwebInfo(this.events,this.pageInfo).subscribe(res=>{
      this.creatRecord()
    })
  }
  agree(type){
    this.saveRecord();
    if(type===1){
      this.myService.customerBehaviorTrackSave(`客户告知书 - 客户点击了【同意并签署】`).subscribe(res=>{
        this.router.navigate(['/eNoticeSignature'],{ queryParams: { type:'eNotice'} })
      })
    }else{
      this.myService.customerBehaviorTrackSave(`保单继续率承诺书 - ${JSON.parse(localStorage.getItem('lifeCustomerInfo')).practitionerBasicInfo.insurerBranchName}、${JSON.parse(localStorage.getItem('lifeCustomerInfo')).practitionerBasicInfo.subordinateName}体系下，经纪人${JSON.parse(localStorage.getItem('lifeCustomerInfo')).practitionerBasicInfo.name}点击了【同意并签署】`).subscribe(res=>{
        this.router.navigate(['/eNoticeSignature'],{ queryParams: { type:'continuationRate'} })
      })
    }


  }
}
