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 {CommonsService} from '../../../services/commons.service';
import {Router} from '@angular/router';
import {ADMIN_INFO_OBJECT} from '../../../services/local-storage.namespace';
import {OilPriceOfficialService} from '../../../services/oil-price/oil-price-official.service';

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

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

  oilNoArray = [];

  constructor(private modal: NzModalService,
              private message: NzMessageService,
              private store: LocalStorageService,
              private oilPriceOfficialService: OilPriceOfficialService,
              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({
      regionName: [null],
      oilNo: [null],
      oilType: [null],
    });

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

    this.requestData(1);
  }

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

}
