import {Component, OnInit} from '@angular/core';
import {BreadcrumbComponent} from "../../common/breadcrumb/breadcrumb.component";
import {NzSpinComponent, NzSpinModule} from "ng-zorro-antd/spin";
import {environment} from "../../../../environments/environment";
import {ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup} from "@angular/forms";
import {NzModalModule, NzModalService} from "ng-zorro-antd/modal";
import {NzMessageService} from "ng-zorro-antd/message";
import {ActivityMoneyService} from "../../../services/activity-money.service";
import {NzFormDirective, NzFormModule} from "ng-zorro-antd/form";
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
import {NzInputDirective} from "ng-zorro-antd/input";
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 {AllocationStatus} from "../../../pipes/activity-money/activity-money.pipe";
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {NzButtonComponent} from "ng-zorro-antd/button";
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-merchant-allocation',
  standalone: true,
  imports: [
    BreadcrumbComponent,
    NzSpinComponent,
    NzFormDirective,
    NzRowDirective,
    NzColDirective,
    NzInputDirective,
    ReactiveFormsModule,
    NzTabSetComponent,
    NzTabComponent,
    NzBadgeComponent,
    NgForOf,
    NzTableComponent,
    DatePipe,
    NzThSelectionComponent,
    AllocationStatus,
    NzSelectComponent,
    NzTdAddOnComponent,
    NzOptionComponent,
    NzButtonComponent,
    NzCellFixedDirective,
    NgIf,
    NzModalModule,
    NzTableModule,
    NzFormModule,
    NzSpinModule,
    NzRadioGroupComponent,
    NzRadioComponent,
    NzDatePickerModule,
    NzTypographyComponent
  ],
  templateUrl: './merchant-allocation.component.html',
  styleUrl: './merchant-allocation.component.less'
})
export class MerchantAllocationComponent implements OnInit {
  FILE_URL = environment.imageUrl;
  loading = false;
  dataObject: any = {};
  tableLoading = true;
  searchForm: UntypedFormGroup;
  pageNum: number;
  whereObject: any = {};


  loadingObject = {
    spinning: false,
    msg: '加载中'
  };




  selectedIndex:number = 0;
  checked = false;
  setOfCheckedId = new Set<number>();

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

  ngOnInit(): void {

    this.searchForm = this.form.group({
      merNo: [null],
      merName: [null],
      merAbbreviate: [null],
      status:[null],
    });
    /*  this.requestData(1); */
    if(this.selectedIndex==0){
      this.requestqueryNotPartMerList(1);//未参与
    }else{
      this.requestqueryPartMerList(1);//已参与
    }
  }

  /**
   * 请求未参与数据
   */
  requestqueryNotPartMerList(pageNum) {
    this.setOfCheckedId.clear();
    this.checked=false;

    this.tableLoading = true;
    this.whereObject['pageNum'] = pageNum;
    this.whereObject['pageSize'] = 10;
    this.activityMoneyService.queryNotPartMerList(this.whereObject, data => {
      if (data['return_code'] === '000000') {
        this.dataObject = data['return_data'];
      } else {
        this.message.error(data['return_msg']);
      }
      this.tableLoading = false;
    });
  }
  /**
   * 请求已参与数据
   */
  requestqueryPartMerList(pageNum) {
    this.tableLoading = true;
    this.whereObject['pageNum'] = pageNum;
    this.whereObject['pageSize'] = 10;
    this.activityMoneyService.queryPartMerList(this.whereObject, data => {
      if (data['return_code'] === '000000') {
        this.dataObject = data['return_data'];
      } else {
        this.message.error(data['return_msg']);
      }
      this.tableLoading = false;
    });
  }

  /**
   * 搜索
   * @param whereObject 条件
   */
  search(whereObject: object) {
    this.whereObject = whereObject;

    if(this.selectedIndex==0){
      this.requestqueryNotPartMerList(1);//未参与
    }else{
      this.requestqueryPartMerList(1);//已参与
    }

  }

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

  /**
   * 添加参与活动用户
   */
  attendConfirm(): void {
    const requestData = [...this.setOfCheckedId];
    this.modal.confirm({
      nzTitle: '提示',
      nzContent: '确定选中这些商户吗？',
      nzOkText: '是',
      nzCancelText: '否',
      nzOkType: 'primary',
      nzOnOk: () => this.addPartMer(requestData)
    });

  }

  addPartMer(merNoList){
    let arr = merNoList.map(item=>{
      return {merNo: item}
    })

    let params={
      "merNoArray": arr
    }

    this.activityMoneyService.addPartMer(params, data => {
      if (data['return_code'] === '000000') {
        this.message.create("success", data['return_data']);
        this.requestqueryNotPartMerList(1)
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }

  /* 禁用 */
  forbidden(merNo){
    let params = {
      "merNo":merNo
    }
    this.activityMoneyService.disable(params, data => {
      if (data['return_code'] === '000000') {
        this.message.create("success", data['return_data']);
        this.requestqueryPartMerList(1);//已参与
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }
  /* 启用 */
  enable(merNo){
    let params = {
      "merNo":merNo
    }
    this.activityMoneyService.enable(params, data => {
      if (data['return_code'] === '000000') {
        this.message.create("success", data['return_data']);
        this.requestqueryPartMerList(1);//已参与
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }

  /* 切换tab */
  nzSelectChange(e){
    this.whereObject={};
    this.resetForm();
    this.dataObject={};
    this.setOfCheckedId.clear();
    this.checked=false;

    if(this.selectedIndex==0){
      this.requestqueryNotPartMerList(1);//未参与
    }else{
      this.requestqueryPartMerList(1);//已参与
    }

  }


  /*  */
  updateCheckedSet(merNo: number, checked: boolean): void {
    if (checked) {
      this.setOfCheckedId.add(merNo);
    } else {
      this.setOfCheckedId.delete(merNo);
    }
  }
  refreshCheckedStatus(): void {
    const listOfEnabledData = this.dataObject.list;
    this.checked = listOfEnabledData.every(({ merNo }) => this.setOfCheckedId.has(merNo));
  }

  onItemChecked(merNo: number, checked: boolean): void {
    this.updateCheckedSet(merNo, checked);
    this.refreshCheckedStatus();
  }

  /*全选 */
  onAllChecked(checked: boolean): void {
    console.log(checked,"checked")
    this.dataObject.list.forEach(({ merNo }) => this.updateCheckedSet(merNo, checked));
    this.refreshCheckedStatus();
  }
  /*  */
}
