import { Component, OnInit } from '@angular/core';
import {LocalStorageService} from "../../../services/local-storage.service";
import {OilCardService} from "../../../services/oil-card.service";
import {OrganizationService} from "../../../services/organization.service";
import {NavigationComponent} from "../../navigation/navigation.component";
import {CompanyAccountService} from "../../../services/company-account.service";

import {ADMIN_INFO_OBJECT} from "../../../services/local-storage.namespace";
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {FormBuilder, FormGroup} from "@angular/forms";
import {ActivatedRoute} from "@angular/router";
@Component({
  selector: 'app-oil-card-op-record-list',
  templateUrl: './oil-card-op-record-list.component.html',
  styleUrls: ['./oil-card-op-record-list.component.scss']
})
export class OilCardOpRecordListComponent implements OnInit {
  roleType;
  adminFlag;
  loadingObject = {
    spinning: false,
    msg: '加载中'
  };

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

  cardNo: string;

  constructor(private modal: NzModalService,
              private message: NzMessageService,
              private store: LocalStorageService,
              private oilCardService: OilCardService,
              private organizationService: OrganizationService,
              private navigationComponent: NavigationComponent,
              private companyAccountService: CompanyAccountService,
              private activatedRoute: ActivatedRoute,
              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({
      type: [null],
      sourceType: [null],
      createTime: [null],
      createTimeS: [null],
      createTimeE: [null],
    });
    this.activatedRoute.queryParams.subscribe(queryParams => {
      if (queryParams['cardNo'] != null) {
        this.cardNo = queryParams['cardNo'];
        this.requestData(1);
      }
    });
  }

  /**
   * 请求数据
   */
  requestData(pageNum) {
    this.tableLoading = true;
    this.whereObject['cardNo'] = this.cardNo;
    this.whereObject['pageNum'] = pageNum;
    this.whereObject['pageSize'] = 10;
    this.oilCardService.getOilCardContactRecordList(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;
    if (this.searchForm.value.createTime != null) {
      this.searchForm.value.createTimeS = new Date(this.searchForm.value.createTime[0]).getTime();
      this.searchForm.value.createTimeE = new Date(this.searchForm.value.createTime[1]).getTime();
    } else {
      this.searchForm.value.createTimeS = null;
      this.searchForm.value.createTimeE = null;
    }
    this.requestData(1);
  }

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