import {Component, OnInit} from '@angular/core';
import {ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup, Validators} from "@angular/forms";
import {NzModalModule, NzModalService} from "ng-zorro-antd/modal";
import {CompanyService} from "../../../services/company.service";
import {CommonsService} from "../../../services/commons.service";
import {ActivatedRoute, Params, Router} from "@angular/router";
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 {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-update',
  standalone: true,
  imports: [
    NzBreadCrumbComponent,
    NzFormDirective,
    NzRowDirective,
    NzColDirective,
    NzInputDirective,
    NzFormLabelComponent,
    NzButtonComponent,
    NzBreadCrumbItemComponent,
    ReactiveFormsModule,
    NzSelectComponent,
    NzOptionComponent,
    NzModalModule,
    NzTableModule,
    NzFormModule,
    NzSpinModule,
    NzRadioGroupComponent,
    NzRadioComponent,
    NzDatePickerModule
  ],
  templateUrl: './company-update.component.html',
  styleUrl: './company-update.component.less'
})
export class CompanyUpdateComponent implements OnInit {
  companyForm: UntypedFormGroup;
  btnLoading = false;
  provinceArray = [];

  dataObject: any = {};


  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.activatedRoute.queryParams.subscribe((queryParams: Params) => {
      if (queryParams['companyId'] != null && queryParams['companyId'].length > 0) {
        this.companyService.companyView(queryParams['companyId'], data => {
          if (data['return_code'] === '000000') {
            this.dataObject = data['return_data']['company'];
            this.dataObject['loginName'] = data['return_data']['user']['loginName'];
            this.dataObject['password'] = data['return_data']['user']['password'];
            this.companyForm.patchValue(this.dataObject);
          }
        });
      }
    });
    this.companyForm = this.form.group({
      regionId: [null, [Validators.required]],
      name: [null, [Validators.required]],
      loginName: [{ value: null, disabled: true}],
      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.companyForm.value['companyId'] = this.dataObject['id'];
    this.companyService.updateCompany(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;
    });
  }
}
