import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {GasClassGroupService} from '../../../services/gas-class-group.service';

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

  dataObject: any = {};
  tableLoading = true;
  searchForm: FormGroup;
  pageNum: number;
  whereObject: any = {};

  classGroupModal = false;
  classGroupForm: FormGroup;

  constructor(private modal: NzModalService,
              private message: NzMessageService,
              private gasClassGroupService: GasClassGroupService,
              private form: FormBuilder) { }

  ngOnInit(): void {
    this.searchForm = this.form.group({
      name: [null],
      principalName: [null],
      principalPhone: [null],
    });

    this.classGroupForm = this.form.group({
      id: [null],
      name: [null, [Validators.required]],
      principalName: [null, [Validators.required]],
      principalPhone: [null, [Validators.required]],
    });
    this.requestData(1);
  }

  /**
   * 请求数据
   */
  requestData(pageNum) {
    this.tableLoading = true;
    this.whereObject['pageNum'] = pageNum;
    this.whereObject['pageSize'] = 10;
    this.gasClassGroupService.getClassGroupList(this.whereObject, data => {
      if (data['return_code'] === '000000') {
        this.dataObject = data['return_data'];
      } else {
        this.modal.error(data['return_msg']);
      }
      this.tableLoading = false;
    });
  }

  /**
   * 搜索
   * @param whereObject 条件
   */
  search(whereObject: object) {
    this.whereObject = whereObject;
    this.requestData(1);
  }

  /**
   * 重置
   */
  resetForm(): void {
    this.searchForm.reset();
  }

  /**
   * 弹出删除对话框
   */
  showDeleteConfirm(groupId: number): void {
    this.modal.confirm({
      nzTitle: '警告',
      nzContent: '是否删除班组',
      nzOkText: '是',
      nzCancelText: '否',
      nzOkType: 'danger',
      nzOnOk: () => this.deleteData(groupId)
    });
  }

  /**
   * 删除数据
   *
   */
  deleteData(groupId: number) {
    this.gasClassGroupService.delClassGroup(groupId, data => {
      if (data['return_code'] === '000000') {
        this.requestData(this.whereObject['pageNum']);
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
    });
  }

  /**
   * 打开编辑班组模态框
   */
  showEditClassGroup(data: object) {
    if (data != null) {
      this.classGroupForm.patchValue(data);
    }
    this.classGroupModal = true;
  }

  /**
   * 关闭编辑班组模态框
   */
  closeEditClassGroup() {
    this.classGroupForm.reset();
    this.classGroupModal = false;
  }

  /**
   * 提交编辑班组
   */
  submitEditClassGroup(){
    for (const i in this.classGroupForm.controls) {
      this.classGroupForm.controls[i].markAsDirty();
      this.classGroupForm.controls[i].updateValueAndValidity();
    }
    if (this.classGroupForm.status == null || this.classGroupForm.status !== 'VALID') {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '请填写所有必填项',
      });
      return;
    }

    // 提交数据
    this.gasClassGroupService.editClassGroup(this.classGroupForm.value, data => {
      if (data['return_code'] === '000000') {
        this.modal.success({
          nzTitle: '提示',
          nzContent: '保存成功',
        });
        this.closeEditClassGroup();
        this.requestData(this.whereObject['pageNum']);
      } else {
        this.modal.warning({
          nzTitle: '提示',
          nzContent: data['return_msg'],
        });
      }
    });
  }

}
