import { Component, OnInit } from '@angular/core';
import {environment} from '../../../../environments/environment';
import {FormBuilder, FormGroup} from '@angular/forms';
import {UserService} from '../../../services/user.service';
import {IconService} from '../../../services/icon.service';
import {NzMessageService} from 'ng-zorro-antd';
import {CommonsService} from '../../../services/commons.service';

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

  searchForm: FormGroup; // 搜索框
  requestData = []; // 列表数据
  total: number; // 页码
  pageNum = 1; // 页码
  pageSize = 10; // 条码
  loading = true;
  income = 0; // 收入
  expend = 0; // 支出

  constructor(
      private form: FormBuilder,
      private user: UserService,
      private iconService: IconService,
      private message: NzMessageService,
      private common: CommonsService
  ) {
  }

  ngOnInit(): void {
    this.init();
  }

  public init(): void {
    this.searchForm = this.form.group({
      goldType: [null],
      resType: [null],
      createTime: [null],
    });
    this.getRequest(true, this.searchForm.value);
  }

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

    this.loading = false;
    if (reset) {
      this.pageNum = 1;
    }
    if (whereObject['createTime'] != null && whereObject['createTime'].length > 0) {
      whereObject['createTimeS'] = new Date(whereObject['createTime'][0]).getTime();
      whereObject['createTimeE'] = new Date(whereObject['createTime'][1]).getTime();
    }
    whereObject['pageNum'] = this.pageNum;
    whereObject['pageSize'] = this.pageSize;
    this.user.getGoldRecList(whereObject, 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']);
      }
    });
    this.user.queryGold(whereObject , data => {
      this.income = 0;
      this.expend = 0;
      if (data['return_code'] === '000000') {
        for (const datum of data['return_data']) {
          if (datum['gold_type'] === 1) {
            this.income = datum['sum'];
          }
          if (datum['gold_type'] === 2) {
            this.expend = datum['sum'];
          }
        }
      }
    });
  }

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



}
