import { Component, OnInit } from '@angular/core';
import {environment} from '../../../../environments/environment';
import {FormBuilder, FormGroup} from '@angular/forms';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {LocalStorageService} from '../../../services/local-storage.service';
import {CompanyAccountService} from '../../../services/company-account.service';
import {Router} from '@angular/router';
import {ADMIN_INFO_OBJECT} from '../../../services/local-storage.namespace';
import {IndexService} from '../../../services/index.service';
import {Validators} from '@angular/forms';
import {MerchantStoreService} from "../../../services/merchant-store.service";

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

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

  amountsEarlyWarningFrom: FormGroup;
  amountsEarlyWarningModal = false;

  constructor(private modal: NzModalService,
              private message: NzMessageService,
              private store: LocalStorageService,
              private indexService: IndexService,
              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({
      companyId: [null],
      merId: [null],
      gasName: [null],
      prestoreType: [null],
      amountsEarlyWarningStatus: [null],
    });

    this.amountsEarlyWarningFrom = this.form.group({
      storeId: [null, [Validators.required]],
      earlyWarning: [null, [Validators.required]],
    });

    this.requestData(1);
  }

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

  showEarlyWarningModal(gasObject: object) {
    this.amountsEarlyWarningModal = true;
    this.amountsEarlyWarningFrom.patchValue({ storeId: gasObject['storeId'], earlyWarning: gasObject['amountsEarlyWarning']});
  }

  submitEarlyWarning() {
    for (const i in this.amountsEarlyWarningFrom.controls) {
      this.amountsEarlyWarningFrom.controls[i].markAsDirty();
      this.amountsEarlyWarningFrom.controls[i].updateValueAndValidity();
    }
    if (this.amountsEarlyWarningFrom.status == null || this.amountsEarlyWarningFrom.status !== 'VALID') {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '请填写所有必填项',
      });
      return;
    }

    this.indexService.setAmountsEarlyWarning(this.amountsEarlyWarningFrom.value, data => {
      if (data['return_code'] === '000000') {
        this.modal.success({
          nzTitle: '提示',
          nzContent: '设置成功',
        });
        this.requestData(this.whereObject['pageNum']);
        this.closeEarlyWarningModal();
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg'],
        });
      }
    });
  }

  closeEarlyWarningModal() {
    this.amountsEarlyWarningModal = false;
  }
}
