import { ChangeDetectorRef, Component, OnInit, TemplateRef } from '@angular/core';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { interval } from 'rxjs';
import { take } from 'rxjs/operators';
import { DunHelper } from 'src/app/shared/components/captcha/dun.helper';
import { EACaptchaService } from 'src/app/shared/services/business/captcha.service';

@Component({
  selector: 'app-buyer-transpower',
  templateUrl: './transpower.component.html',
  styleUrls: ['./transpower.less']
})
export class BuyerTranspowerComponent implements OnInit {
  record: any = {};
  count = 0;
  interval$: any;
  i: any;
  smsVerifyCode = '';
  superPhone = '';
  staffId = 1;
  staffList: any = [];
  constructor(
    private modal: NzModalRef,
    public captchaService: EACaptchaService,
    private dunHelper: DunHelper,
    private cdr: ChangeDetectorRef
  ) {}

  ngOnInit(): void {
    this.getPhone();
  }
  getPhone() {
    // this.service.request(this.service.$api_getAppLesseeAdmin).subscribe((res) => {
    //   console.log(res);
    //   if (res) {
    //     this.superPhone = res.telephone;
    //   }
    // });
  }
  sure() {
    const params = {
      appUserId: this.i.appUserId,
      smsVerifyCode: this.smsVerifyCode
    };
    // this.service.request(this.service.$api_shiftResellerAdmin, params).subscribe((res) => {
    //   if (res) {
    //     this.service.msgSrv.success('操作成功！');
    //     this.modal.close(true);
    //   }
    // });
  }
  close() {
    this.modal.destroy();
  }
  /**
   * 获取手机验证码
   */
  sendCode() {
    this.captchaService.getAppLesseeAdminSMVerificationCode().subscribe(res => {
      if (res.success && res.data.code === '1') {
        this.captchaService.msgSrv.success('发送验证码成功');
        this.createInterval();
      } else if (res.data.code === '503046') {
        this.dunHelper.popUp().subscribe(_ => {
          this.createInterval();
          this.dunHelper.destory();
        });
      } else {
        this.captchaService.msgSrv.warning(res.msg);
      }
    });
  }

  private createInterval() {
    this.count = 59;
    interval(1000)
      .pipe(take(60))
      .subscribe(x => {
        this.count = 59 - (x + 1);
        this.cdr.detectChanges();
      });
  }
}
