import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {NzModalService} from 'ng-zorro-antd';
import {AgentService} from '../../../../services/agent.service';
import {ActivatedRoute, Router} from '@angular/router';
import {ValidatorsService} from '../../../../services/validators.service';
import {TyAgentService} from "../../../../services/ty-agent.service";

@Component({
  selector: 'app-ty-agent-edit',
  templateUrl: './ty-agent-edit.component.html',
  styleUrls: ['./ty-agent-edit.component.scss']
})
export class TyAgentEditComponent implements OnInit {

  agentForm: FormGroup;
  btnLoading = false;

  constructor(private form: FormBuilder,
              private modal: NzModalService,
              private tyAgentService: TyAgentService,
              private activatedRoute: ActivatedRoute,
              private router: Router) { }

  ngOnInit(): void {
    this.activatedRoute.queryParams.subscribe(queryParams => {
      if (queryParams['agentKey'] != null) {
        this.getDetail(queryParams['agentKey']);
      }
    });
    this.agentForm = this.form.group({
      id: [null],
      agentName: [null, [Validators.required]],
      agentUser: [null, [Validators.required]],
      agentPhone: [null, [Validators.required]],
      agentAddress: [null],
    });
  }

  /**
   * 查询详情
   * @param agentKey 代理商编号
   */
  getDetail(agentKey: string) {
    this.tyAgentService.getDetailByKey(agentKey, data => {
      if (data['return_code'] === '000000') {
        this.agentForm.patchValue(data['return_data']);
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg'],
        });
      }
    });
  }

  /**
   * 提交
   */
  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;
    }

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


  }
}
