import {Component, OnInit} from '@angular/core';
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
import {NzFormDirective, NzFormItemComponent, NzFormLabelComponent, NzFormModule} from "ng-zorro-antd/form";
import {NzInputNumberComponent} from "ng-zorro-antd/input-number";
import {NzInputDirective} from "ng-zorro-antd/input";
import {NzButtonComponent} from "ng-zorro-antd/button";
import {BreadcrumbComponent} from "../../common/breadcrumb/breadcrumb.component";
import {ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup, Validators} from "@angular/forms";
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {NzCellFixedDirective, NzTableComponent, NzTableModule, NzThMeasureDirective} from "ng-zorro-antd/table";
import {AgentStatusPipe} from "../../../pipes/agent-status.pipe";
import {DatePipe, NgForOf, NgIf} from "@angular/common";
import {NzDropDownDirective, NzDropdownMenuComponent} from "ng-zorro-antd/dropdown";
import {NzMenuDirective, NzMenuItemComponent} from "ng-zorro-antd/menu";
import {NzIconDirective} from "ng-zorro-antd/icon";
import {RouterLink} from "@angular/router";
import {NzModalComponent, NzModalModule, NzModalService} from "ng-zorro-antd/modal";
import {NzMessageService} from "ng-zorro-antd/message";
import {CompanyService} from "../../../services/company.service";
import {CommonsService} from "../../../services/commons.service";
import {TradeTakeConfigService} from "../../../services/trade-take-config.service";
import {NzSpinModule} from "ng-zorro-antd/spin";
import {NzRadioComponent, NzRadioGroupComponent} from "ng-zorro-antd/radio";
import {NzDatePickerModule} from "ng-zorro-antd/date-picker";

@Component({
  selector: 'app-company-list',
  standalone: true,
  imports: [
    NzColDirective,
    NzFormItemComponent,
    NzInputNumberComponent,
    NzInputDirective,
    NzButtonComponent,
    BreadcrumbComponent,
    NzFormDirective,
    NzRowDirective,
    ReactiveFormsModule,
    NzOptionComponent,
    NzSelectComponent,
    NzTableComponent,
    NzThMeasureDirective,
    AgentStatusPipe,
    DatePipe,
    NzDropdownMenuComponent,
    NzMenuDirective,
    NzMenuItemComponent,
    NzCellFixedDirective,
    NzIconDirective,
    RouterLink,
    NzDropDownDirective,
    NzModalComponent,
    NzFormLabelComponent,
    NgIf,
    NgForOf,
    NzModalModule,
    NzTableModule,
    NzFormModule,
    NzSpinModule,
    NzRadioGroupComponent,
    NzRadioComponent,
    NzDatePickerModule
  ],
  templateUrl: './company-list.component.html',
  styleUrl: './company-list.component.less'
})
export class CompanyListComponent implements OnInit {
  dataObject: any = {};
  tableLoading = true;
  searchForm: UntypedFormGroup;
  tradeTakeConfigForm: UntypedFormGroup;
  pageNum: number;
  whereObject: any = {};
  tradeTakeConfigMadel = false;
  platformTypeArray = [];

  constructor(private modal: NzModalService,
              private message: NzMessageService,
              private companyService: CompanyService,
              private commonsService: CommonsService,
              private tradeTakeConfigService: TradeTakeConfigService,
              private form: UntypedFormBuilder) { }

  ngOnInit(): void {
    this.searchForm = this.form.group({
      name: [null],
    });
    this.commonsService.getDictionary('PLATFORM_TYPE', data => {
      this.platformTypeArray = data['return_data'];
    });

    this.tradeTakeConfigForm = this.form.group({
      companyId: [null, [Validators.required]],
      platformType: ['4', [Validators.required]],
      receiverName: [null, [Validators.required]],
      receiverAccount: [null, [Validators.required]],
      receiverRatio: [0, [Validators.required]]
    });
    this.requestData(1);
  }

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


  /**
   * 展示交易分账配置
   */
  showTradeTakeConfigMadel(companyId: number) {
    this.tradeTakeConfigService.getTradeTakeConfig(companyId, this.tradeTakeConfigForm.controls['platformType'].value, 1 , 2, null, data => {
      if (data['return_code'] === '000000') {
        if (data['return_data'] != null) {
          data['return_data']['platformType'] = String(data['return_data']['platformType']);
          data['return_data']['receiverRatio'] =  data['return_data']['receiverRatio'] * 100;
          this.tradeTakeConfigForm.patchValue(data['return_data']);
        } else {
          this.tradeTakeConfigForm.controls['companyId'].setValue(companyId);
        }
      }
    });
    this.tradeTakeConfigMadel = true;
  }

  /**
   * 关闭交易分账配置
   */
  closeTradeTakeConfigMadel() {
    this.tradeTakeConfigForm.reset();
    this.tradeTakeConfigForm.controls['platformType'].setValue('4');
    this.tradeTakeConfigForm.controls['receiverRatio'].setValue(0);
    this.tradeTakeConfigMadel = false;
  }

  /**
   * 提交配置
   */
  submitTradeTakeConfig() {
    for (const i in this.tradeTakeConfigForm.controls) {
      this.tradeTakeConfigForm.controls[i].markAsDirty();
      this.tradeTakeConfigForm.controls[i].updateValueAndValidity();
    }
    if (this.tradeTakeConfigForm.status == null || this.tradeTakeConfigForm.status !== 'VALID') {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '请规范填写所有的必填项信息',
      });
      return;
    }
    this.tradeTakeConfigForm.value['receiverRatio'] = this.tradeTakeConfigForm.controls['receiverRatio']['value'] / 100;
    this.tradeTakeConfigService.editParentCompanyConfig(this.tradeTakeConfigForm.value, data => {
      if (data['return_code'] === '000000') {
        this.modal.success({
          nzTitle: '提示',
          nzContent: '保存成功',
          nzOnOk: () => this.closeTradeTakeConfigMadel()
        });
      } else {
        this.modal.error({
          nzTitle: '提示',
          nzContent: data['return_msg'],
        });
      }
    });

  }
}
