import {Component, OnInit} from '@angular/core';
import {ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup, Validators} from "@angular/forms";
import {ActivatedRoute, Params, Router} from "@angular/router";
import {AgentService} from "../../../services/agent.service";
import {NzModalModule, NzModalService} from "ng-zorro-antd/modal";
import {NzBreadCrumbComponent, NzBreadCrumbItemComponent} from "ng-zorro-antd/breadcrumb";
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
import {NzInputDirective} from "ng-zorro-antd/input";
import {NzFormDirective, NzFormLabelComponent} from "ng-zorro-antd/form";
import {NzButtonComponent} from "ng-zorro-antd/button";

@Component({
  selector: 'app-agent-update',
  standalone: true,
  imports: [
    NzBreadCrumbItemComponent,
    NzBreadCrumbComponent,
    NzRowDirective,
    NzColDirective,
    NzInputDirective,
    NzFormDirective,
    ReactiveFormsModule,
    NzFormLabelComponent,
    NzButtonComponent,
    NzModalModule
  ],
  templateUrl: './agent-update.component.html',
  styleUrl: './agent-update.component.less'
})
export class AgentUpdateComponent implements OnInit {

  agentForm: UntypedFormGroup;
  btnLoading = false;

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

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

  ngOnInit(): void {
    this.agentForm = this.form.group({
      agentId: [null, [Validators.required]],
      name: [null, [Validators.required]],
      contactPerson: [null, [Validators.required]],
      contactPersonPhone: [null, [Validators.required]],
      address: [null],
    });

    this.activatedRoute.queryParams.subscribe((queryParams: Params) => {
      if (queryParams['agentId'] != null && queryParams['agentId'].length > 0) {
        this.agentService.queryAgent(queryParams['agentId'], data => {
          if (data['return_code'] === '000000') {
            this.dataObject = data['return_data'];
            data['return_data']['agentDetail']['agentId'] = data['return_data']['agentDetail']['id'];
            this.agentForm.patchValue(data['return_data']['agentDetail']);
          }
        });
      }
    });

  }

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

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