import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
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 {ActivatedRoute} from '@angular/router';
import {ADMIN_INFO_OBJECT} from '../../../services/local-storage.namespace';
import {environment} from '../../../../environments/environment';

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

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

  cardNo: string;
  orgArray = [];

  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({
      orgId: [null],
      cardNo: [null],
      goodsName: [null],
      status: [null],
      payTime: [null],
      payTimeS: [null],
      payTimeE: [null],
    });
    this.activatedRoute.queryParams.subscribe(queryParams => {
      if (queryParams['cardNo'] != null) {
        this.cardNo = queryParams['cardNo'];
      }
    });

    if (this.roleType === 5 && this.adminFlag === 1) {
      this.organizationService.getOrganizationList(this.store.get(ADMIN_INFO_OBJECT)['bsCompany']['id'], data => {
        this.orgArray = data['return_data'];
      });
    }
    this.requestData(1);
  }

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

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

  /**
   * 导出油卡订单
   */
  excelCardOrder() {
    this.oilCardService.exportCardOrder(this.whereObject, data => {
      if (data['return_code'] === '000000') {
        window.location.href = this.FILE_URL + 'temporary/' + data['return_data'];
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
    });
  }
}
