import { Component, OnInit } from '@angular/core';
import {environment} from '../../../../environments/environment';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {RechargeService} from '../../../services/recharge.service';
import {DiscountService} from '../../../services/discount.service';
import {CouponService} from '../../../services/coupon.service';
import {LocalStorageService} from '../../../services/local-storage.service';
import {IconService} from '../../../services/icon.service';
import {NzMessageService} from 'ng-zorro-antd';
import {ADMIN_INFO_OBJECT} from '../../../services/local-storage.namespace';
import {NzModalService} from "ng-zorro-antd";
import {AgentService} from "../../../services/agent.service";

@Component({
  selector: 'app-discount-use-condition-list',
  templateUrl: './discount-use-condition-list.component.html',
  styleUrls: ['./discount-use-condition-list.component.scss']
})
export class DiscountUseConditionListComponent implements OnInit {


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

  discountArray = [];

  constructor(private modal: NzModalService,
              private message: NzMessageService,
              private agentService: AgentService,
              private discountService: DiscountService,
              private store: LocalStorageService,
              private form: FormBuilder) { }

  ngOnInit(): void {
    this.searchForm = this.form.group({
      discountId: [null],
      status: [null],
      storeName: [null],
      receiveTime: [[]],
      useTime: [[]],
      idS: [null],
      idE: [null],
    });
    this.requestAgentDiscount();
    this.requestData(1);
  }

  /**
   * 请求数据
   */
  requestData(pageNum) {
    this.tableLoading = true;
    this.whereObject['pageNum'] = pageNum;
    this.whereObject['pageSize'] = 10;
    this.discountService.getDiscountUseConditionList(this.whereObject, data => {
      this.dataObject = data['return_data'];
      this.tableLoading = false;
    });
  }

  /**
   * 查询代理商优惠券数据
   */
  requestAgentDiscount() {
    const param = {
      agentId: this.store.get(ADMIN_INFO_OBJECT)['highAgent'].id,
      pageNum: 1,
      pageSize: 9999,
    };
    this.agentService.getDiscountAgentList(param, data => {
      if (data['return_code'] === '000000') {
        this.discountArray = data['return_data'].list;
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }

  /**
   * 搜索
   * @param whereObject
   */
  search(whereObject: object) {
    if (whereObject['receiveTime'] != null && whereObject['receiveTime'].length > 0) {
      whereObject['receiveTimeS'] = new Date(whereObject['receiveTime'][0]).getTime();
      whereObject['receiveTimeE'] = new Date(whereObject['receiveTime'][1]).getTime();
    } else {
      whereObject['receiveTimeS'] = null;
      whereObject['receiveTimeE'] = null;
    }

    if (whereObject['useTime'] != null && whereObject['useTime'].length > 0) {
      whereObject['useTimeS'] = new Date(whereObject['useTime'][0]).getTime();
      whereObject['useTimeE'] = new Date(whereObject['useTime'][1]).getTime();
    } else {
      whereObject['useTimeS'] = null;
      whereObject['useTimeE'] = null;
    }
    this.whereObject = whereObject;
    this.requestData(1);
  }

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

}
