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

@Component({
  selector: 'app-push-invoice',
  templateUrl: './push-invoice.component.html',
  styleUrls: ['./push-invoice.component.less'],
  providers: [CurrencyPipe]
})
export class PushInvoiceComponent implements OnInit {
  @ViewChild('st', { static: false })
  st!: STComponent;
  columns!: STColumn[];

  info: any = {};
  id!: number;

  constructor(public service: TicketService, private nzModalService: NzModalService, private currencyPipe: CurrencyPipe) {}

  ngOnInit(): void {
    this.loadHeader(this.id);
    setTimeout(() => {
      this.columns = this.initST();
    }, 100);
  }

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

  loadHeader(id: number) {
    this.service.request(this.service.$api_ficoVatinv_header, { id }).subscribe(res => {
      console.log(res);

      if (res) {
        this.info = res;
      }
    });
  }

  private initST(): STColumn[] {
    return [
      { title: '服务名称', index: 'vatname', width: 100 },
      { title: '规格型号', index: 'vatmodel', width: 150 },
      { title: '单位', index: 'vatunit', width: 90, className: 'text-center' },
      { title: '数量', index: 'vatqty', width: 90, className: 'text-right' },
      { title: '金额', index: 'vatnotax', width: 90, type: 'currency', format: item => `${this.currencyPipe.transform(item.vatnotax)}` },
      {
        title: '税率',
        index: 'vatrate',
        width: 90,
        className: 'text-right',
        format: item => `${item.vatrate ? ((item.vatrate as number) * 100).toFixed(2) : 0}%`
      },
      { title: '税额', index: 'vattax', width: 90, type: 'currency', format: item => `${this.currencyPipe.transform(item.vattax)}` }
    ];
  }
}
