import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {NzMessageService} from 'ng-zorro-antd';
import {RechargeService} from '../../../services/recharge.service';
import {environment} from '../../../../environments/environment';


@Component({
  selector: 'app-user-statistical',
  templateUrl: './user-statistical.component.html',
  styleUrls: ['./user-statistical.component.scss']
})
export class UserStatisticalComponent implements OnInit {

  searchForm: FormGroup; // 搜索框
  FILE_URL = environment.imageUrl;
  requestData = []; // 列表数据
  total: number; // 页码
  pageNum = 1; // 页码
  pageSize = 10; // 条码
  loading = true;

  constructor(
      private recharge: RechargeService,
      private form: FormBuilder,
      private message: NzMessageService,
  ) { }

  ngOnInit(): void {
    this.searchForm = this.form.group({
      finishTime: [null],
    });
    this.getRequest(true, this.searchForm.value);
  }

  // 查询列表
  public getRequest(reset: boolean = false, whereObject: object) {

    if (whereObject['finishTime'] != null && whereObject['finishTime'].length !== 0) {
      whereObject['finishTimeS'] = whereObject['finishTime'][0].getTime();
      whereObject['finishTimeE'] = whereObject['finishTime'][1].getTime();
    }


    this.loading = false;
    if (reset) {
      this.pageNum = 1;
    }
    whereObject['pageNum'] = this.pageNum;
    whereObject['pageSize'] = this.pageSize;
    this.recharge.getUserCountList(whereObject, data => {
      console.log(data);
      if (data['return_code'] === '000000') {
        this.requestData = data['return_data'].list;
        this.total = data['return_data'].total;
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }

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

  // 下载模板
  downloadTemplate(whereObject) {
    if (whereObject['finishTime'] != null && whereObject['finishTime'].length !== 0) {
      whereObject['finishTimeS'] = whereObject['finishTime'][0].getTime();
      whereObject['finishTimeE'] = whereObject['finishTime'][1].getTime();
    }
    this.recharge.exportUserOrderListCount(whereObject, data => {
      if (data['return_code'] === '000000') {
        window.location.href = this.FILE_URL + data['return_data'];
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }

}
