import { CurrencyPipe } from '@angular/common';
import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { DateHelperByDatePipe } from 'ng-zorro-antd/i18n';
import { NzModalService } from 'ng-zorro-antd/modal';
import { AddCollectionInvoiceModalComponent } from 'src/app/routes/ticket-management/components/input-invoice/add-collection-invoice-modal/add-collection-invoice-modal.component';
import { FreightAccountService } from '../../services/freight-account.service';

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

  _$expand = false;

  selectedRows: any[] = [];
  constructor(
    public service: FreightAccountService,
    private nzModalService: NzModalService,
    private router: Router,
    private dateHelperByDatePipe: DateHelperByDatePipe
  ) {}

  ngOnInit(): void {}

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

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  addInvoice() {
    if (this.selectedRows?.length <= 0) {
      this.service.msgSrv.warning('请选择申请记录');
      return;
    }
    const modal = this.nzModalService.create({
      nzTitle: '收票信息',
      nzContent: AddCollectionInvoiceModalComponent,
      nzComponentParams: { i: { userId: 0 } },
      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_export_fico_vch_page);
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        vc2code: {
          type: 'string',
          title: '汇总凭证号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        cno: {
          type: 'string',
          title: '客户',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          },
          default: ''
        },
        vctime: {
          title: '凭证时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        sourceCode: {
          type: 'string',
          title: '原始单号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        sourceType: {
          type: 'string',
          title: '原始单类型',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        createTime: {
          title: '创建时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        },
        remarks: {
          type: 'string',
          title: '凭证摘要',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        vctype: {
          type: 'string',
          title: '凭证类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'credential:type' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        s2ts: {
          type: 'string',
          title: '科目',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'refund:apply:status' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        drmoney: {
          type: 'string',
          title: '借方金额',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        crmoney: {
          type: 'string',
          title: '贷方金额',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        vcltdid: {
          type: 'string',
          title: '帐套',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'refund:apply:status' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        sts: {
          type: 'string',
          title: '凭证状态',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'credential:status' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        feecode: {
          type: 'string',
          title: 'NC凭证',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        vccode: {
          type: 'string',
          title: '凭证号',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        s22t2ss: {
          type: 'string',
          title: '导入NC',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'refund:apply:status' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        createt1im2e: {
          title: '统计时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox', width: 60, className: 'text-center', fixed: 'left' },
      { title: '汇总凭证号', index: 'vc2code', type: 'link', width: 200 },
      { title: '帐套', index: 'vcltdcode', width: 180 },
      { title: '凭证时间', index: 'createTime', type: 'date', width: 150 },
      {
        title: '统计区间',
        index: 'invdate',
        type: 'date',
        width: 200,
        format: item =>
          `${this.dateHelperByDatePipe.format(item.vctime2start, 'yyyy-MM-dd')} | ${this.dateHelperByDatePipe.format(
            item.vctime2end,
            'yyyy-MM-dd'
          )}`
      },
      { title: '凭证类型', index: 'vctype', width: 120 },
      { title: '序号', index: 'invmoney', width: 100, format: _ => '1' },
      { title: '摘要', index: 'remarks', width: 220 },
      { title: '币种', index: 'currency', width: 100 },
      {
        title: '借方金额',
        index: 'drmoney',
        width: 140,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.drmoney }) }
      },
      {
        title: '贷方金额',
        index: 'crmoney',
        width: 140,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.crmoney }) }
      },
      { title: 'NC凭证', index: 'importncnotes', width: 180 },
      { title: '凭证状态', index: 'stsLabel', width: 120 },
      { title: '创建时间', index: 'createTime', width: 180 },
      { title: '创建人', index: 'createUserName', width: 120 },
      {
        title: '操作',
        width: '130px',
        fixed: 'right',
        className: 'text-center',
        buttons: [
          {
            text: '浏览',
            click: (item: any) => this.router.navigate(['/financial-management/voucher-summary/detail/' + item.id])
          },
          {
            text: '列表',
            click: (item: any) => this.router.navigate(['/financial-management/voucher-summary/list/' + item.vc2code])
          }
        ]
      }
    ];
  }
}
