import { Component } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import {
  FormControl,
  FormGroup,
  NonNullableFormBuilder,
  Validators,
} from '@angular/forms';
import { CommonCompModule } from '../common.modules';
import { textbookServer } from '../../../services/textbook';
import { AuthServr } from '../../../services/auth.service';
@Component({
  selector: 'app-login',
  standalone: true,
  imports: [ReactiveFormsModule, CommonCompModule],
  templateUrl: './login.component.html',
  styleUrl: './login.component.scss',
})
export class LoginComponent {
  validateForm: FormGroup<{
    userName: FormControl<string>;
    password: FormControl<string>;
    code: FormControl<string>;
    remember: FormControl<boolean>;
  }> = this.fb.group({
    userName: ['', [Validators.required]],
    password: ['', [Validators.required]],
    code: ['', [Validators.required]],
    remember: [true],
  });
  selector: Array<any> = [
    {
      name: '国家级管理员',
      type: 1,
      route: '/nav-admin/home',
    },
    {
      name: '省级教育行政部门',
      type: 2,
      route: '/nav-province-submit/home',
    },
    {
      name: '流程管理员登录',
      type: 3,
      route: '/nav-province-contact/home',
    },
    {
      name: '省属高校联系人',
      type: 4,
      route: '/nav-province-school-contact/home',
    },
    {
      name: '教材评审组成员',
      type: 5,
      route: '/nav-review/home',
    },
    {
      name: '作者/教师/主编',
      type: 6,
      route:'/nav-author/textbook/list'
    },
  ];
  currentProfile: any = this.selector[0];
  constructor(
    public tbookSer: textbookServer,
    private fb: NonNullableFormBuilder,
    public router: Router,
    private authServr: AuthServr
  ) {}
  submitForm(): void {
    if (this.validateForm.valid) {
      console.log('submit', this.currentProfile);
      localStorage.setItem('profile', JSON.stringify(this.currentProfile));
      this.tbookSer.profile = this.currentProfile;
      this.authServr.login('admin', '12345', this.tbookSer.company).then(() => {
        this.router.navigate([this.currentProfile.route]);
      });
    } else {
      Object.values(this.validateForm.controls).forEach((control) => {
        if (control.invalid) {
          control.markAsDirty();
          control.updateValueAndValidity({ onlySelf: true });
        }
      });
    }
  }

  onChange(e: string) {
    this.currentProfile = e;
  }

  goUrl(path: string) {
    this.router.navigate([
      path,
      {
        // type: this.currentProfile.type,
      },
    ]);
  }
}
