import { Component, OnInit } from '@angular/core';
import {environment} from '../../../../environments/environment';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {LocalStorageService} from '../../../services/local-storage.service';
import {OrganizationService} from '../../../services/organization.service';
import {CompanyAccountService} from '../../../services/company-account.service';
import {Router} from '@angular/router';
import {ADMIN_INFO_OBJECT} from '../../../services/local-storage.namespace';
import {GasStaffService} from '../../../services/gas-staff.service';
import {CommonsService} from "../../../services/commons.service";

@Component({
  selector: 'app-gas-staff-list',
  templateUrl: './gas-staff-list.component.html',
  styleUrls: ['./gas-staff-list.component.scss']
})
export class GasStaffListComponent implements OnInit {
  FILE_URL = environment.imageUrl;
  roleType;
  adminFlag;
  loadingObject = {
    spinning: false,
    msg: '加载中'
  };

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

  positionTypeArray = [];

  gasStaffQrCodeImgUrl: string;
  gasStaffQrCodeModal = false;

  constructor(private modal: NzModalService,
              private message: NzMessageService,
              private store: LocalStorageService,
              private gasStaffService: GasStaffService,
              private commonsService: CommonsService,
              private router: Router,
              private form: FormBuilder) {
    this.roleType = Number(this.store.get(ADMIN_INFO_OBJECT)['secRole'].roleType);
    this.adminFlag = Number(this.store.get(ADMIN_INFO_OBJECT)['secUser'].adminFlag);
  }

  ngOnInit(): void {
    this.searchForm = this.form.group({
      name: [null],
      telephone: [null],
      positionType: [null],
      status: [null],
    });

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

    this.requestData(1);
  }

  /**
   * 请求数据
   */
  requestData(pageNum) {
    this.tableLoading = true;
    this.whereObject['pageNum'] = pageNum;
    this.whereObject['pageSize'] = 10;
    this.gasStaffService.getStaffList(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();
  }

  /**
   * 弹出删除对话框
   */
  showDisabledConfirm(id: number): void {
    this.modal.confirm({
      nzTitle: '警告',
      nzContent: '是否禁用该员工',
      nzOkText: '是',
      nzCancelText: '否',
      nzOkType: 'danger',
      nzOnOk: () => this.disabledData(id)
    });
  }

  /**
   * 禁用数据
   */
  disabledData(id: number) {
    this.gasStaffService.disabled({ staffId: id }, data => {
      if (data['return_code'] === '000000') {
        this.requestData(this.whereObject['pageNum']);
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
    });
  }

  /**
   * 弹出恢复对话框
   */
  showRecoverConfirm(id: number): void {
    this.modal.confirm({
      nzTitle: '警告',
      nzContent: '是否恢复该员工状态',
      nzOkText: '是',
      nzCancelText: '否',
      nzOkType: 'danger',
      nzOnOk: () => this.recoverData(id)
    });
  }

  /**
   * 恢复数据
   */
  recoverData(id: number) {
    this.gasStaffService.recover({ staffId: id }, data => {
      if (data['return_code'] === '000000') {
        this.requestData(this.whereObject['pageNum']);
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
    });
  }

  /**
   * 弹出删除对话框
   */
  showDelConfirm(id: number): void {
    this.modal.confirm({
      nzTitle: '警告',
      nzContent: '是否删除该员工',
      nzOkText: '是',
      nzCancelText: '否',
      nzOkType: 'danger',
      nzOnOk: () => this.delData(id)
    });
  }

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

  /**
   * 查询工作人员二维码
   * @param id
   */
  showGasStaffQrCodeModal(id: number) {
    this.gasStaffService.queryQrCode({ gasStaffId: id }, data => {
      if (data['return_code'] === '000000') {
        this.gasStaffQrCodeImgUrl = data['return_data'];
        this.gasStaffQrCodeModal = true;
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
    });
  }

  /**
   * 关闭二维码模态框
   */
  closeGasStaffQrCodeModal() {
    this.gasStaffQrCodeModal = false;
  }

}
