import { Component, OnInit } from '@angular/core';
import {ActivateService} from "../../../services/activate.service";
import {IconService} from "../../../services/icon.service";
import { Params, ActivatedRoute } from '@angular/router';
import {FormBuilder, FormGroup} from '@angular/forms';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
  selector: 'app-activate-lottery-record',
  templateUrl: './activate-lottery-record.component.html',
  styleUrls: ['./activate-lottery-record.component.scss']
})
export class ActivateLotteryRecordComponent implements OnInit {

  FILE_URL;
  dataObject: any = {};
  tableLoading = true;
  searchForm: FormGroup;
  pageNum: number;
  whereObject: any = {};
  activityId;
  numData;
  isVisible = false;
  id;
  num;
  constructor(
      private form: FormBuilder,
      private activate: ActivateService,
      private iconService: IconService,
      private activatedRoute: ActivatedRoute,
      private message: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.searchForm = this.form.group({
      userPhone: [null],
      awardName: [null],
    });

    this.activatedRoute.queryParams.subscribe((queryParams: Params) => {
      if (queryParams['activityId']) {
        this.activityId = queryParams['activityId'];
        this.requestData(1);
        this.checkNumberPrizes(queryParams['activityId']);
      }
    });
  }

  checkNumberPrizes(id: number) {
    this.activate.checkNumberPrizes(id , data => {
      this.numData = data['return_data'];
    });
  }

  /**
   * 请求数据
   */
  requestData(pageNum) {
    this.tableLoading = true;
    this.whereObject['activityId'] = this.activityId;
    this.whereObject['pageNum'] = pageNum;
    this.whereObject['pageSize'] = 10;
    this.activate.getWinLotteryList(this.whereObject, data => {
      if (data['return_code'] === '000000') {
        this.dataObject = data['return_data'];
      } else {
        this.message.warning('获取失败！');
      }
      this.tableLoading = false;
    });
  }

  /**
   * 搜索
   * @param whereObject
   */
  search(whereObject: object) {
    this.whereObject = whereObject;
    this.requestData(1);
  }

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

  getAdd(id: number) {
    this.id = id;
    this.isVisible = true;

  }

  handleOk(): void {
    if (this.num == null) {
      this.message.error('请输入新增数量！');
      return;
    }
    let post = {
      activityAwardId: this.id ,
      num: this.num
    }
    this.activate.numberOfNewRewards(post , data => {
      if (data['return_code'] === '000000') {
        this.isVisible = false;
        this.message.success('成功');
      } else {
        this.message.create('error', '编辑失败');
      }
    });

  }

  handleCancel(): void {
    this.isVisible = false;
  }

}
