import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import {BsModalRef} from 'ngx-bootstrap/modal';
import { FrameDetailComponent } from '../../../frame/core/detail/frame.detail';
import { AjaxService } from '../../../../core/service/ajax.service';
import { FuncService } from '../../../../core/service/func.service';
import { MsgService } from '../../../../core/service/msg.service';
import { SignaturePad } from 'ngx-signaturepad/signature-pad';

declare var $: any;


@Component({
  selector: 'app-final-review-sign',
  templateUrl: './final-review-sign.component.html',
  styleUrls: ['./final-review-sign.component.scss']
})
export class FinalReviewSignComponent extends FrameDetailComponent implements OnInit {
  @ViewChild(SignaturePad, {static: false}) public signaturePad: SignaturePad;

  signed = false;//是否签名
  retArray = [];
  categoryArray = [];
  signaturePadOptions = {
    'minWidth': 1,
    'maxWidth': 2,
    'canvasWidth': 500,
    'canvasHeight': 300
  };

  constructor(bsModalRef: BsModalRef, fb: FormBuilder, private detectRef: ChangeDetectorRef) {
    super(bsModalRef);
  }

  ngOnInit() {
    //debugger;
    let _this = this;
    $('.js-signature').jqSignature();
    $('.js-signature').on('jq.signature.changed', function() {
      _this.signed = true;
    }); 
  }

  canvasClear() {
    //this.signaturePad.clear();
    $('.js-signature').jqSignature('clearCanvas');
  }

  //签名回调
  drawStart() {
    
  }
  //签名回调
  drawComplete() {

  }

  //签名确认
  doSignConfirm() {
    debugger;
    let base64 = $('.js-signature').jqSignature('getDataURL');
    if (!this.signed) {
      MsgService.errorAlert("请签名后再提交");
      return;
    }
    let _this = this;
    let param = {
      batch_id: this.ResponseEntity.batch_id,
      //sgin_base64: this.signaturePad.toDataURL()
      apply_id: this.ResponseEntity.apply_id,
      sgin_base64: base64
    };
    MsgService.showLoading();
    AjaxService.requestEntity('final/review/comfirm/sign.htm', param, (entity) => {
      MsgService.hideLoading();
      //debugger;
      if(entity == true) {
        MsgService.successToastr("提交成功!");
        this.closeWindow();
      } else {
        MsgService.errorToastr(entity.errorMsg);
      }
    });
  }

  //返回修改
  goBack() {
    this.closeWindow();
  }

}
