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 {GasStaffService} from '../../../services/gas-staff.service';
import {CommonsService} from "../../../services/commons.service";
import {ValidatorsService} from "../../../services/validators.service";

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

  staffFrom: FormGroup;
  btnLoading = false;

  positionTypeArray = [];

  constructor(private formBuilder: FormBuilder,
              private modal: NzModalService,
              private gasStaffService: GasStaffService,
              private activatedRoute: ActivatedRoute,
              private commonsService: CommonsService,
              private router: Router) { }

  ngOnInit(): void {
    this.activatedRoute.queryParams.subscribe(queryParams => {
      if (queryParams['id'] != null) {
        this.getDetail(queryParams['id']);
      }
    });

    this.commonsService.getDictionary('GAS_STAFF_POSITION_TYPE', data => {
      this.positionTypeArray = data['return_data'];
    });

    this.staffFrom = this.formBuilder.group({
      id: [null],
      positionType: ['1', [Validators.required]],
      name: [null, [Validators.required]],
      telephone: [null, [Validators.required, ValidatorsService.mobile]],
    });
  }

  /**
   * 查询详情
   * @param id
   */
  getDetail(id: number) {
    this.gasStaffService.getStaffDetail(id, data => {
      if (data['return_code'] === '000000') {
        data['return_data']['positionType'] = String(data['return_data']['positionType']);
        this.staffFrom.patchValue(data['return_data']);
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg'],
        });
      }
    });
  }

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

    this.btnLoading = true;
    if (this.staffFrom.value.id == null) {
      this.gasStaffService.addStaff(this.staffFrom.value, data => {
        if (data['return_code'] === '000000') {
          this.modal.success({
            nzTitle: '提示',
            nzContent: '添加成功',
            nzOnOk: () => this.router.navigateByUrl('admin/gas-staff/list')
          });
        } else {
          this.modal.error({
            nzTitle: '提示',
            nzContent: data['return_msg'],
          });
        }
        this.btnLoading = false;
      });
    } else {
      this.gasStaffService.updateStaff(this.staffFrom.value, data => {
        if (data['return_code'] === '000000') {
          this.modal.success({
            nzTitle: '提示',
            nzContent: '修改成功',
            nzOnOk: () => this.router.navigateByUrl('admin/gas-staff/list')
          });
        } else {
          this.modal.error({
            nzTitle: '提示',
            nzContent: data['return_msg'],
          });
        }
        this.btnLoading = false;
      });
    }
  }
}
