import {Component, OnInit} from '@angular/core';
import {HttpService} from '../../service/http.service';
import {RegService} from '../../service/reg.service';
import {ToastService} from 'ng-zorro-antd-mobile';
import {ConfigService} from '../../service/config.service';
import {ActivatedRoute, Router} from '@angular/router';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.less']
})
export class LoginComponent implements OnInit {
  locks: any[] = [];
  type: 'login' | 'register' = 'login';
  mobile: string = '';
  smsCode: string = '';
  userName: string = '';
  countDown: string = '获取验证码';
  timer: any = null;

  constructor(
    private http: HttpService,
    private toast: ToastService,
    public config: ConfigService,
    private reg: RegService,
    private router: Router
  ) {
  }

  ngOnInit(): void {
    if (this.config.getUserInfo()) {
      // location.replace(this.config.rootUrl + '/index');
      this.router.navigate(['/index']).then();
      return;
    }
    const locks: any[] = [];
    for (let i = 0; i < 12; i++) {
      locks.push(i);
    }
    this.locks = locks;
  }


  setType(): void {
    if (this.type === 'login') {
      this.type = 'register';
    } else {
      this.type = 'login';
    }
    this.mobile = '';
    this.smsCode = '';
  }

  // 获取验证码
  getSmsCode = () => {
    if (!this.reg.mobileReg.test(this.mobile)) {
      this.toast.hide();
      this.toast.show('请输入11位有效手机号');
      return;
    }
    if (this.timer == null) {
      let time = 60;
      this.countDown = `${time}秒后再试`;
      this.timer = setInterval(() => {
        time -= 1;
        if (time <= 0) {
          clearInterval(this.timer);
          this.timer = null;
          this.countDown = '获取验证码';
        } else {
          this.countDown = `${time}秒后再试`;
        }
      }, 1000);
      this.http.post(this.http.apis.smsCode, {
        data: {
          phoneNo: this.mobile
        },
        success: (res: any) => {
          this.toast.show('验证码发送成功');
        }
      });
    }
  };

  update() {
    if (!this.reg.mobileReg.test(this.mobile)) {
      this.toast.hide();
      this.toast.show('请输入11位有效手机号');
      return;
    }
    if (!this.smsCode) {
      this.toast.hide();
      this.toast.show('请输入短信验证码');
      return;
    }
    const campaignId = localStorage.getItem('campaignId');
    if (campaignId) {
      let url = this.http.apis.login;
      let params: any = {
        mobile: this.mobile,
        smsCode: this.smsCode,
        campaignId: campaignId,
        reqChannel: this.config.reqChannel
      };
      if (this.type == 'register') {
        if (!this.userName) {
          this.toast.hide();
          this.toast.show('请输入推荐人称呼');
          return;
        }
        url = this.http.apis.register;
        params = {
          ...params,
          userName: this.userName,
          address: '',
        };
      }
      this.http.post(url, {
        data: params,
        success: (res: any) => {
          if (this.type == 'login') {
            // 登陆
            this.toast.show('登陆成功');
            localStorage.clear();
            localStorage.setItem('params', JSON.stringify(res));
            localStorage.setItem('campaignId', campaignId);
            // location.replace(this.config.rootUrl + '/exclusive');
            this.router.navigate(['/exclusive']).then();
          }
          if (this.type == 'register') {
            // 注册
            this.toast.show('注册成功，请重新登陆');
            this.type = 'login';
            this.smsCode = '';
            if (this.timer) {
              clearInterval(this.timer);
              this.timer = null;
              this.countDown = '获取验证码';
            }
          }
        }
      });
    }
  }
}
