import {Component, OnInit} from '@angular/core';
import {BreadcrumbComponent} from "../../common/breadcrumb/breadcrumb.component";
import {NzSpinComponent, NzSpinModule} from "ng-zorro-antd/spin";
import {NzFormDirective, NzFormModule} from "ng-zorro-antd/form";
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
import {NzInputDirective} from "ng-zorro-antd/input";
import {ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup} from "@angular/forms";
import {NzTabComponent, NzTabSetComponent} from "ng-zorro-antd/tabs";
import {NzBadgeComponent} from "ng-zorro-antd/badge";
import {DatePipe, NgForOf, NgIf} from "@angular/common";
import {
  NzCellFixedDirective,
  NzTableComponent,
  NzTableModule,
  NzTdAddOnComponent,
  NzThSelectionComponent
} from "ng-zorro-antd/table";
import {ActivityMoneyPipe, AllocationStatus} from "../../../pipes/activity-money/activity-money.pipe";
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {NzButtonComponent} from "ng-zorro-antd/button";
import {NzModalModule, NzModalService} from "ng-zorro-antd/modal";
import {NzMessageService} from "ng-zorro-antd/message";
import {ActivityMoneyService} from "../../../services/activity-money.service";
import {environment} from "../../../../environments/environment";
import {RouterLink} from "@angular/router";
import {NzDividerComponent} from "ng-zorro-antd/divider";
import {BrowserStorageService} from "../../../utils/localStorage.service";
import {NzRadioComponent, NzRadioGroupComponent} from "ng-zorro-antd/radio";
import {NzDatePickerModule} from "ng-zorro-antd/date-picker";
import {NzTypographyComponent} from "ng-zorro-antd/typography";

@Component({
  selector: 'app-prize-pool',
  standalone: true,
  imports: [
    BreadcrumbComponent,
    NzSpinComponent,
    NzFormDirective,
    NzRowDirective,
    NzColDirective,
    NzInputDirective,
    ReactiveFormsModule,
    NzTabSetComponent,
    NzTabComponent,
    NzBadgeComponent,
    NgForOf,
    NzTableComponent,
    DatePipe,
    NzThSelectionComponent,
    NzSelectComponent,
    NzTdAddOnComponent,
    NzOptionComponent,
    NzButtonComponent,
    NzCellFixedDirective,
    NgIf,
    ActivityMoneyPipe,
    RouterLink,
    NzDividerComponent,
    NzModalModule,
    NzTableModule,
    NzFormModule,
    NzSpinModule,
    NzRadioGroupComponent,
    NzRadioComponent,
    NzDatePickerModule,
    NzTypographyComponent
  ],
  templateUrl: './prize-pool.component.html',
  styleUrl: './prize-pool.component.less'
})
export class PrizePoolComponent implements OnInit {
  FILE_URL = environment.imageUrl;
  loading = false;
  dataObject: any = {};
  tableLoading = true;
  searchForm: UntypedFormGroup;
  pageNum: number;
  whereObject: any = {};


  constructor(private storage: BrowserStorageService,
              private modal: NzModalService,
              private message: NzMessageService,
              private activityMoneyService:ActivityMoneyService,
              private form: UntypedFormBuilder) { }

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

  /**
   * 请求数据
   */
  requestData(pageNum) {
    this.tableLoading = true;
    this.whereObject['pageNum'] = pageNum;
    this.whereObject['pageSize'] = 10;
    this.activityMoneyService.queryRewardList(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) {
    this.whereObject = whereObject;
    this.requestData(1);
  }

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

  /* 发放奖池金额 */
  disbursementAmount(batchIdentifier){
    let params ={
      batchIdentifier:batchIdentifier
    }
    this.modal.confirm({
      nzTitle: '提示',
      nzContent: '确定发放奖池金额吗?',
      nzOnOk: () => this.grantReward(params)
    });
  }
  grantReward(params){
    this.activityMoneyService.grantReward(params, data => {
      if (data['return_code'] === '000000') {
        this.message.create("success", data['return_data']);
        this.requestData(1);
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }
}
