import { CurrencyPipe } from '@angular/common';
import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STChange, STRequestOptions } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { CurrencyService } from '@delon/util';
import { CurrencyCNYPipe } from '@delon/util/pipes/currency/cny.pipe';
import { ShipperBaseService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { parse } from 'path';
import { SystemService } from 'src/app/routes/sys-setting/services/system.service';
import { AccountDetailComponent } from 'src/app/shared/components/account-detail/account-detail.component';
import { FreightAccountService } from '../../services/freight-account.service';

@Component({
  selector: 'app-freight-account',
  templateUrl: './freight-account.component.html',
  styleUrls: ['../../../commom/less/box.less', '../../../commom/less/expend-but.less']
})
export class FreightAccountComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  searchSchema: SFSchema = this.initSF();
  columns: STColumn[] = this.initST();

  selectedRows: any[] = [];

  _$expand = false;

  constructor(public service: FreightAccountService, private router: Router, private modal: NzModalService) {}

  ngOnInit(): void {}

  beforeReq = (requestOptions: STRequestOptions) => {
    Object.assign(requestOptions.body, { accountType: 1 });
    if (this.sf) {
      Object.assign(requestOptions.body, { ...this.sf.value });
      if (this.sf.value.createTime) {
        Object.assign(requestOptions.body, {
          createTime: {
            start: this.sf.value.createTime?.[0] || '',
            end: this.sf.value.createTime?.[1] || ''
          }
        });
      }
    }
    return requestOptions;
  };

  showAccountDetail(item: any) {
    this.modal.create({
      nzTitle: '账户明细',
      nzContent: AccountDetailComponent,
      nzNoAnimation: true,
      nzWidth: 600,
      nzComponentParams: {
        params: { accountType: 1, roleId: item.roleId },
        url: '/api/fcc/accountBalance/getShipperAccountBalanceDetailByOperator'
      },
      nzFooter: null
    });
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
  }

  /**
   * 伸缩查询条件
   */
  expandToggle() {
    this._$expand = !this._$expand;
    this.sf?.setValue('/expand', this._$expand);
  }

  exportList() {
    this.service.exportStart({ ...this.sf.value, pageSize: -1 }, this.service.$api_get_exportShipperAccountBalanceByOperator);
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        tenantName: {
          type: 'string',
          title: '企业名称',
          ui: { placeholder: '请输入' }
        },
        userName: {
          type: 'string',
          title: '联系人',
          ui: { placeholder: '请输入' }
        },
        phone: {
          type: 'string',
          title: '联系人电话',
          ui: {
            placeholder: '请输入'
          }
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            },
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          }
        },
        bankType: {
          type: 'string',
          title: '银行类型',
          enum: [
            { label: '全部', value: null },
            { label: '平安银行', value: '1' },
            { label: '浦发银行', value: '2' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: null
        },
        virtualAccount: {
          type: 'string',
          title: '虚拟账户',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        createTime: {
          title: '创建时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '企业名称', width: 170, index: 'tenantName' },
      { title: '联系人', width: 120, index: 'name' },
      { title: '联系人电话', width: 140, index: 'phone' },
      { title: '网络货运人', width: 170, index: 'ltdName' },
      { title: '银行类型', width: 120, index: 'bankTypeLabel' },
      { title: '虚拟账户', width: 140, index: 'virtualAccount' },
      {
        title: '可用余额',
        index: 'availableBalance',
        width: 150,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.availableBalance }) }
      },
      {
        title: '冻结余额',
        index: 'freezeBalance',
        width: 150,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.freezeBalance }) }
      },
      {
        title: '账户总余额',
        render: 'description',
        width: 140,
        className: 'text-right'
      },
      { title: '创建时间', index: 'createTime', type: 'date', width: 150 },
      {
        title: '状态',
        index: 'stateDeletedLabel',
        width: 80
      },
      {
        title: '操作',
        width: 120,
        className: 'text-center',
        fixed: 'right',
        buttons: [
          {
            text: '查看明细',
            click: item =>
              this.router.navigate(['/financial-management/freight-account/detail/' + item.id], {
                queryParams: {
                  tenantName: item.tenantName,
                  ltdName: item.ltdName,
                  bankType: item.bankType,
                  availableBalance: item.availableBalance,
                  projectId: item.projectId,
                  enterpriseId: item.enterpriseId,
                  roleId: item.roleId,
                  ltdId: item.ltdId
                }
              })
          }
        ]
      }
    ];
  }

  parseFloat(num: any) {
    return parseFloat(num);
  }
}
