import { CurrencyPipe } from '@angular/common';
import { Component, OnInit, ViewChild } from '@angular/core';
import { STComponent, STColumn, STRequestOptions } from '@delon/abc/st';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { BaseService } from '../../services';

@Component({
  selector: 'app-account-detail',
  templateUrl: './account-detail.component.html',
  providers: [CurrencyPipe]
})
export class AccountDetailComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  columns: STColumn[] = [];

  url = '/api/fcc/accountBalance/getDriverAccountDetailByOperator';

  isCanCreate = false;
  params: any = {};
  constructor(public service: BaseService, public currencyPipe: CurrencyPipe, private modalHelp: NzModalRef) {
    this.initST();
  }

  ngOnInit(): void {}
  createAccount(item: any, type: '1' | '2') {
    if ((type === '1' && item.paAccount) || (type === '2' && item.pfAccount)) {
      return;
    }
    const params = {
      ltdId: item.ltdId || this.params.ltdId,
      roleId: item.roleId || this.params.roleId,
      projectId: item.projectId || this.params.projectId,
      enterpriseId: item.enterpriseId || this.params.roleId,
      ctfId: this.params.ctfId,
      clientName: this.params.clientName,
      roleName: this.params.clientName
    };
    if (this.params.accountType === 1) {
      this.service
        .request('/api/fcc/accountBalance/saveByShipper', {
          accountType: this.params.accountType,
          bankType: type,
          isProjectMain: 0,
          ...params
        })
        .subscribe(res => {
          if (res) {
            this.service.msgSrv.success(type === '1' ? res?.paMsg : res?.pfMsg);
            this.modalHelp.destroy();
          }
        });
    }
    if (this.params.accountType === 2) {
      this.service
        .request('/api/fcc/accountBalance/saveByDriver', {
          accountType: this.params.accountType,
          bankType: type,
          ...params
        })
        .subscribe(res => {
          if (res) {
            this.service.msgSrv.success(type === '1' ? res?.paMsg : res?.pfMsg);
            this.modalHelp.destroy();
          }
        });
    }
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    Object.assign(requestOptions.body, this.params);
    return requestOptions;
  };

  initST() {
    this.columns = [
      { title: '网络货运人', index: 'ltdName', className: 'text-center' },
      {
        title: '平安资金账户',
        render: 'paAccount',
        className: 'text-center',
        iif: _ => this.isCanCreate
      },
      {
        title: '浦发资金账户',
        render: 'pfAccount',
        className: 'text-center',
        iif: _ => this.isCanCreate
      },
      {
        title: '平安账户余额',
        render: 'paBalance',
        className: 'text-right',
        format: item => `${this.currencyPipe.transform(item.paBalance)}`,
        iif: _ => !this.isCanCreate
      },
      {
        title: '浦发账户余额',
        render: 'pfBalance',
        className: 'text-right',
        format: item => `${this.currencyPipe.transform(item.pfBalance)}`,
        iif: _ => !this.isCanCreate
      }
    ];
  }
}
