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 {CouponService} from '../../../services/coupon.service';

@Component({
  selector: 'app-use-order',
  templateUrl: './use-order.component.html',
  styleUrls: ['./use-order.component.scss']
})
export class UseOrderComponent implements OnInit {
  FILE_URL = environment.imageUrl;

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

  constructor(private modal: NzModalService,
              private message: NzMessageService,
              private couponService: CouponService,
              private form: FormBuilder) { }

  ngOnInit(): void {
    this.searchForm = this.form.group({
      couponName: [null],
      status: [null],
      assignTimeArray: [null],
    });
    this.requestData(1);
  }

  /**
   * 请求数据
   */
  requestData(pageNum) {
    this.tableLoading = true;
    this.whereObject['status'] = 2;
    this.whereObject['pageNum'] = pageNum;
    this.whereObject['pageSize'] = 10;
    this.couponService.getGzSinopecAssignList(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) {
    if (whereObject['assignTimeArray'] != null && whereObject['assignTimeArray'].length > 0) {
      whereObject['assignTimeS'] = new Date(whereObject['assignTimeArray'][0]).getTime();
      whereObject['assignTimeE'] = new Date(whereObject['assignTimeArray'][1]).getTime();
    } else {
      whereObject['assignTimeS'] = null;
      whereObject['assignTimeE'] = null;
    }
    this.whereObject = whereObject;
    this.requestData(1);
  }

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

  /**
   * 导出数据
   */
  excelData() {
    this.couponService.importGzSinopecAssign(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']
        });
      }
    });
  }
}
