import {Component, OnInit} from '@angular/core';
import {FormsModule, ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup, Validators} from "@angular/forms";
import {NzModalComponent, NzModalModule, NzModalService} from "ng-zorro-antd/modal";
import {SalesmanService} from "../../../services/salesman.service";
import {ActivatedRoute, Params, Router, RouterLink, RouterOutlet} from "@angular/router";
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {
  NzFormControlComponent,
  NzFormDirective,
  NzFormItemComponent,
  NzFormLabelComponent,
  NzFormModule
} from "ng-zorro-antd/form";
import {NzInputDirective, NzInputGroupComponent} from "ng-zorro-antd/input";
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
import {BreadcrumbComponent} from "../../common/breadcrumb/breadcrumb.component";
import {DatePipe, NgForOf, NgIf} from "@angular/common";
import {NzButtonComponent} from "ng-zorro-antd/button";
import {NzIconDirective} from "ng-zorro-antd/icon";
import {
  NzCellFixedDirective,
  NzTableComponent, NzTableModule,
  NzTdAddOnComponent,
  NzThMeasureDirective,
  NzThSelectionComponent
} from "ng-zorro-antd/table";
import {MerStatusPipe} from "../../../pipes/mer/mer-status.pipe";
import {CompleteStatusPipe} from "../../../pipes/mer/complete-status.pipe";
import {NzDropDownDirective, NzDropdownMenuComponent} from "ng-zorro-antd/dropdown";
import {NzMenuDirective, NzMenuItemComponent, NzSubMenuComponent} from "ng-zorro-antd/menu";
import {NzTabComponent, NzTabSetComponent} from "ng-zorro-antd/tabs";
import {NzUploadComponent} from "ng-zorro-antd/upload";
import {MerTypePipe} from "../../../pipes/mer/mer-type.pipe";
import {NzTooltipDirective} from "ng-zorro-antd/tooltip";
import {NzContentComponent, NzHeaderComponent, NzLayoutComponent, NzSiderComponent} from "ng-zorro-antd/layout";
import {NzDrawerComponent} from "ng-zorro-antd/drawer";
import {NzSwitchComponent} from "ng-zorro-antd/switch";
import {NzInputNumberComponent} from "ng-zorro-antd/input-number";
import {NzBreadCrumbComponent, NzBreadCrumbItemComponent} from "ng-zorro-antd/breadcrumb";
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-salesman-update',
  standalone: true,
  imports: [
    NzOptionComponent,
    NzFormDirective,
    NzModalComponent,
    NzFormItemComponent,
    NzFormLabelComponent,
    NzFormControlComponent,
    NzSelectComponent,
    NzInputDirective,
    NzColDirective,
    BreadcrumbComponent,
    NzRowDirective,
    ReactiveFormsModule,
    NgForOf,
    NzButtonComponent,
    NzIconDirective,
    NzTableComponent,
    NzThMeasureDirective,
    NzThSelectionComponent,
    NzTdAddOnComponent,
    MerStatusPipe,
    CompleteStatusPipe,
    DatePipe,
    NzDropdownMenuComponent,
    NzMenuDirective,
    RouterLink,
    NgIf,
    NzMenuItemComponent,
    NzTabSetComponent,
    NzTabComponent,
    NzUploadComponent,
    MerTypePipe,
    NzCellFixedDirective,
    NzDropDownDirective,
    NzTooltipDirective,
    NzSubMenuComponent,
    NzSiderComponent,
    NzLayoutComponent,
    NzHeaderComponent,
    NzContentComponent,
    RouterOutlet,
    NzDrawerComponent,
    NzSwitchComponent,
    FormsModule,
    NzInputGroupComponent,
    NzInputNumberComponent,
    NzBreadCrumbComponent,
    NzBreadCrumbItemComponent,
    NzModalModule,
    NzTableModule,
    NzFormModule,
    NzSpinModule,
    NzRadioGroupComponent,
    NzRadioComponent,
    NzDatePickerModule
  ],
  templateUrl: './salesman-update.component.html',
  styleUrl: './salesman-update.component.less'
})
export class SalesmanUpdateComponent implements OnInit {

  salesmanForm: UntypedFormGroup;
  btnLoading = false;

  dataObject: any = {
    salesmanAccount : {
      loginName: ''
    }
  };

  constructor(private form: UntypedFormBuilder,
              private modal: NzModalService,
              private salesmanService: SalesmanService,
              private activatedRoute: ActivatedRoute,
              private router: Router) { }

  ngOnInit(): void {
    this.salesmanForm = this.form.group({
      salesmanId: [null, [Validators.required]],
      name: [null, [Validators.required]],
      phone: [null, [Validators.required]],
    });

    this.activatedRoute.queryParams.subscribe((queryParams: Params) => {
      if (queryParams['salesmanId'] != null && queryParams['salesmanId'].length > 0) {
        this.salesmanService.querySalesman(queryParams['salesmanId'], data => {
          if (data['return_code'] === '000000') {
            this.dataObject = data['return_data'];
            data['return_data']['salesmanDetail']['salesmanId'] = data['return_data']['salesmanDetail']['id'];
            this.salesmanForm.patchValue(data['return_data']['salesmanDetail']);
          }
        });
      }
    });

  }

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

    this.salesmanService.updateSalesman(this.salesmanForm.value, data => {
      if (data['return_code'] === '000000') {
        this.modal.success({
          nzTitle: '提示',
          nzContent: '修改成功',
          nzOnOk: () => this.router.navigateByUrl('admin/salesman/list')
        });
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg'],
        });
      }
      this.btnLoading = false;
    });
  }
}
