import { Component } from '@angular/core';
import {NzBreadCrumbComponent, NzBreadCrumbItemComponent} from "ng-zorro-antd/breadcrumb";
import {NzFormDirective, NzFormLabelComponent, NzFormModule} from "ng-zorro-antd/form";
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
import {NzInputDirective} from "ng-zorro-antd/input";
import {NzButtonComponent} from "ng-zorro-antd/button";
import {ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup, Validators} from "@angular/forms";
import {CommonsService} from "../../../services/commons.service";
import {CompanyService} from "../../../services/company.service";
import {ActivatedRoute, Router} from "@angular/router";
import {NzModalModule, NzModalService} from "ng-zorro-antd/modal";
import {ValidatorsService} from "../../../services/validators.service";
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {NzTableModule} from "ng-zorro-antd/table";
import {NzSpinModule} from "ng-zorro-antd/spin";
import {NzRadioComponent, NzRadioGroupComponent} from "ng-zorro-antd/radio";
import {NzDatePickerModule} from "ng-zorro-antd/date-picker";

@Component({
  selector: 'app-company-add',
  standalone: true,
  imports: [
    NzBreadCrumbComponent,
    NzFormDirective,
    NzRowDirective,
    NzColDirective,
    NzInputDirective,
    NzFormLabelComponent,
    NzButtonComponent,
    NzBreadCrumbItemComponent,
    ReactiveFormsModule,
    NzSelectComponent,
    NzOptionComponent,
    NzModalModule,
    NzTableModule,
    NzFormModule,
    NzSpinModule,
    NzRadioGroupComponent,
    NzRadioComponent,
    NzDatePickerModule
  ],
  templateUrl: './company-add.component.html',
  styleUrl: './company-add.component.less'
})
export class CompanyAddComponent {

  companyForm: UntypedFormGroup;
  btnLoading = false;
  provinceArray = [];

  constructor(private form: UntypedFormBuilder,
              private modal: NzModalService,
              private companyService: CompanyService,
              private commonsService: CommonsService,
              private activatedRoute: ActivatedRoute,
              private router: Router) { }

  ngOnInit(): void {
    this.commonsService.getProvinceList(data => {
      this.provinceArray = data['return_data'];
    });

    this.companyForm = this.form.group({
      regionId: [null, [Validators.required]],
      name: [null, [Validators.required]],
      loginName: [null, [Validators.required, ValidatorsService.minLength(6), ValidatorsService.maxLength(20)]],
      password: [null, [Validators.required, ValidatorsService.minLength(6), ValidatorsService.maxLength(20)]],
      address: [null],
      phone: [null],
    });
  }

  submitFrom() {
    this.btnLoading = true;
    for (const i in this.companyForm.controls) {
      this.companyForm.controls[i].markAsDirty();
      this.companyForm.controls[i].updateValueAndValidity();
    }
    if (this.companyForm.status == null || this.companyForm.status !== 'VALID') {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '请规范填写所有的必填项信息',
      });
      this.btnLoading = false;
      return;
    }

    this.companyService.insertCompany(this.companyForm.value, data => {
      if (data['return_code'] === '000000') {
        this.modal.success({
          nzTitle: '提示',
          nzContent: '添加成功',
          nzOnOk: () => this.router.navigateByUrl('admin/company/list')
        });
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg'],
        });
      }
      this.btnLoading = false;
    });
  }
}
