import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {ValidatorsService} from '../../../services/validators.service';
import {LocalStorageService} from '../../../services/local-storage.service';
import {LoginService} from '../../../services/login.service';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {CommonsService} from '../../../services/commons.service';
import {CompanyTwoPwdService} from '../../../services/company-two-pwd.service';
import {HttpClient} from  '@angular/common/http';
import {IconService} from '../../../services/icon.service';

@Component({
  selector: 'app-pay-pwd',
  templateUrl: './pay-pwd.component.html',
  styleUrls: ['./pay-pwd.component.scss']
})
export class PayPwdComponent implements OnInit {
  setPayPwdStatus = false;
  passwordVisible = false;
  setTwoPwdForm: FormGroup; // 定义表单
  updateTwoPwdForm: FormGroup; // 定义表单

  constructor(
      private storage: LocalStorageService,
      private loginService: LoginService,
      private message: NzMessageService,
      private commonsService: CommonsService,
      private companyTwoPwdService: CompanyTwoPwdService,
      private http: HttpClient, // http请求
      private iconService: IconService,
      private modal: NzModalService,
      private fb: FormBuilder, // 表单
  ) { }

  ngOnInit(): void {
    this.isSetPayPwd();

    this.setTwoPwdForm = this.fb.group({
      password: [null, [Validators.required, ValidatorsService.minLength(6), ValidatorsService.maxLength(16)]],
      checkPassword: [null, [Validators.required, this.checkSetTowPwd]],
    });

    this.updateTwoPwdForm = this.fb.group({
      oldPassword: [null, [Validators.required, ValidatorsService.minLength(6), ValidatorsService.maxLength(16)]],
      newPassword: [null, [Validators.required, ValidatorsService.minLength(6), ValidatorsService.maxLength(16)]],
      checkNewPassword: [null, [Validators.required, this.checkUpdateTowPwd]],
    });
  }

  // 设置密码
  isSetPayPwd() {
    this.companyTwoPwdService.isSetTwoPwd(data => {
      if (data['return_data'] === true) {
        this.setPayPwdStatus = true;
      } else {
        this.setPayPwdStatus = false;
      }
    });
  }

  // 设置二级密码
  submitSetTowPwd(): void {
    for (const i in this.setTwoPwdForm.controls) {
      this.setTwoPwdForm.controls[i].markAsDirty();
      this.setTwoPwdForm.controls[i].updateValueAndValidity();
    }
    if (this.setTwoPwdForm.status == null || this.setTwoPwdForm.status !== 'VALID') {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '请规范填写所有的必填项信息',
      });
      return;
    }
    this.companyTwoPwdService.setTwoPwd(this.setTwoPwdForm.value, data => {
      if (data['return_code'] === '000000') {
        this.modal.success({
          nzTitle: '提示',
          nzContent: '设置成功',
        });
        this.setTwoPwdForm.reset();
        this.isSetPayPwd();
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg'],
        });
      }
    });
  }

  checkSetTowPwd = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (control.value !== this.setTwoPwdForm.controls.password.value) {
      return { confirm: true, error: true };
    }
    return {};
  }

  // 修改二级密码
  submitUpdateTowPwd(): void {
    for (const i in this.updateTwoPwdForm.controls) {
      this.updateTwoPwdForm.controls[i].markAsDirty();
      this.updateTwoPwdForm.controls[i].updateValueAndValidity();
    }
    if (this.updateTwoPwdForm.status == null || this.updateTwoPwdForm.status !== 'VALID') {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '请规范填写所有的必填项信息',
      });
      return;
    }
    this.companyTwoPwdService.updateTwoPwd(this.updateTwoPwdForm.value, data => {
      if (data['return_code'] === '000000') {
        this.modal.success({
          nzTitle: '提示',
          nzContent: '修改成功',
        });
        this.updateTwoPwdForm.reset();
        this.isSetPayPwd();
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg'],
        });
      }
    });
  }

  checkUpdateTowPwd = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (control.value !== this.updateTwoPwdForm.controls.newPassword.value) {
      return { confirm: true, error: true };
    }
    return {};
  }

}
