import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {LoginService} from '../services/login.service';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {CommonsService} from '../services/commons.service';
import {ValidatorsService} from '../services/validators.service';
import {
  FRONT_INFO_OBJECT,
  INIT_FLAG,
  LOGINTYPE,
  RESUME_INFO,
  RESUME_STATUS,
  RESUME_STATUS_NAME,
  TOKEN
} from '../services/local-storage.namespace';
import {LocalStorageService} from '../services/local-storage.service';
import {Router} from '@angular/router';
import {environment} from '../../environments/environment';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
  imageUrl = environment.imageUrl;
  topPieceList = [];

  // 验证码倒计时参数
  verifyCode: any = {
    verifyCodeTips: '获取验证码',
    countDown: 60,
    disable: false
  };
  topCompanyList = [];
  validateForm!: FormGroup;
  isVisible;  // 协议
  type = true;  // true  验证码登录  false 密码登录
  typeName = '密码登录';
  constructor(
    private fb: FormBuilder,
    private login: LoginService,
    private message: NzMessageService,
    private commons: CommonsService,
    private store: LocalStorageService, // 数据请求
    private router: Router,  // 路由
    private modal: NzModalService
  ) {
  }

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      phone: [null, [Validators.required , ValidatorsService.mobile]],
      code: [null, [Validators.required]],
      password: [null, [Validators.required]],
    });

    this.commons.getDictionary('HOME_INDUSTRY', data => {
      this.topPieceList = data['return_data'];
      for (let i = 0; i < this.topPieceList.length; i++) {
        if (i === 0) {
          this.topPieceList[i]['isClick'] = true;
          this.getHomeCompanyList(this.topPieceList[i]['codeName']);
        } else {
          this.topPieceList[i]['isClick'] = false;
        }
      }
    });
  }

  public getClick(i): void {
    // tslint:disable-next-line:forin
    for (const index in this.topPieceList) {
      this.topPieceList[index].isClick = Number(index) === Number(i);
    }
    this.getHomeCompanyList(this.topPieceList[i]['codeName']);
  }

  public getHomeCompanyList(industry) {
    const object = {
        industryCategoryName: industry,
        showNum: 15
    };
    this.commons.getHomeCompanyList(object, data => {
        this.topCompanyList = data['return_data'];
    });
  }

  // 倒计时旧方法
  public getCountDown() {
    if (this.verifyCode.countDown === 0) {
      this.verifyCode.countDown = 60;
      this.verifyCode.verifyCodeTips = '重新获取验证码';
      this.verifyCode.disable = false;
      return;
    } else {
      this.verifyCode.countDown--;
    }
    this.verifyCode.verifyCodeTips = '重新获取(' + this.verifyCode.countDown + ')';
    this.verifyCode.disable = true;
    setTimeout(() => {
      this.getCountDown();
    }, 1000);
  }

  // 获取验证码
  public getCode(): void {
    // tslint:disable-next-line:forin
    for (const i in this.validateForm.controls) {
      if (i === 'phone' && this.validateForm.controls[i].errors != null) {
        this.validateForm.controls[i].markAsDirty();
        this.validateForm.controls[i].updateValueAndValidity();
        return;
      }
    }

    this.login.sendVerificationCode(this.validateForm.value.phone, data => {
      if (data['return_code'] === '000000') {
        this.getCountDown();
      } else {
        this.message.warning(data['return_msg']);
      }
    });
  }

  // 登录or注册
  public getLogin(): void {
    // tslint:disable-next-line:forin
    for (const i in this.validateForm.controls) {
      if (this.validateForm.controls[i].errors != null) {
        this.validateForm.controls[i].markAsDirty();
        this.validateForm.controls[i].updateValueAndValidity();
        if (this.type && i === 'code' && this.validateForm.controls[i].errors != null) {
          return;
        } else if (!this.type && i === 'password' && this.validateForm.controls[i].errors != null) {
          return;
        }
      }
    }
    if (this.type) {
      this.validateForm.value.type = 2;
    } else  {
      this.validateForm.value.type = 1;
    }
    this.login.registerUser(this.validateForm.value, data => {
      if (data['return_code'] === '000000') {
        if (data['return_code'] === '000000') {
          this.store.set(FRONT_INFO_OBJECT, data['return_data']['object']);
          this.store.set(RESUME_INFO, data['return_data']['object']['bsResumeModal']);
          if (data['return_data']['object']['bsResumeModal']['bsResume'] != null) {
            this.store.set(RESUME_STATUS_NAME, data['return_data']['object']['bsResumeModal']['bsResume']['statusName']);
            this.store.set(RESUME_STATUS, data['return_data']['object']['bsResumeModal']['bsResume']['status']);
          }
          this.store.set(TOKEN, data['return_data']['uniqueCode']);
          this.store.set(INIT_FLAG, true);
          this.store.set(LOGINTYPE , data['return_data']['type']);

          if (data['return_data']['object']['bsResumeModal']['bsResume'] == null) {
            this.router.navigateByUrl('/home/resume/editResume').then();
          } else {
            this.router.navigateByUrl('/home/index').then();
          }
        } else {
          this.modal.warning({
            nzTitle: '提示',
            nzContent: data['return_msg']
          });
        }
      } else {
        this.message.warning(data['return_msg']);
      }
    });
  }

  // 弹出协议
  public showModal(): void {
    this.isVisible = true;
  }

  // 关闭协议
  public handleCancel(): void {
    this.isVisible = false;
  }

  // 密码登录
  public getLoginPassword(type): void {
    if (type) {
      this.typeName = '密码登录';
    } else {
      this.typeName = '验证码登录/注册';
    }
    this.type = type;
  }
}
