import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {NzModalService} from 'ng-zorro-antd';
import {ActivatedRoute, Router} from '@angular/router';
import {TySalesmanService} from '../../../../services/ty-salesman.service';

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

  agentForm: FormGroup;
  btnLoading = false;

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

  ngOnInit(): void {
    this.agentForm = this.form.group({
      id: [null],
      salesmanName: [null, [Validators.required]],
      salesmanPhone: [null, [Validators.required]],
    });
    this.activatedRoute.queryParams.subscribe(queryParams => {
      if (queryParams['salesmanKey'] != null) {
        this.getDetail(queryParams['salesmanKey']);
      }
      if (queryParams['tyAgentId'] != null) {
        this.agentForm.patchValue({tyAgentId : queryParams['tyAgentId']});
      }
    });
  }

  /**
   * 查询详情
   * @param agentKey 代理商编号
   */
  getDetail(agentKey: string) {
    this.tySalesmanService.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: '请规范填写所有的必填项信息',
      });
      this.btnLoading = false;
      return;
    }

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