import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { STComponent, STColumn } from '@delon/abc/st';
import { FreightAccountService } from '../../../services/freight-account.service';

@Component({
  selector: 'app-cost-management-detail',
  templateUrl: './cost-management-detail.component.html',
  styleUrls: ['./cost-management-detail.component.less']
})
export class CostManagementDetailComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  columns: { [key: string]: STColumn[] } = this.initST();
  data = [];
  textStatus: boolean = false;
  costInfo: any = {};
  constructor(public service: FreightAccountService, private route: ActivatedRoute) {
    const id = route.snapshot.params.id;
    this.loadDetail(id);
  }

  ngOnInit(): void {
    if (this.route.snapshot?.queryParams?.status === '应收') {
      this.textStatus = false;
    } else {
      this.textStatus = true;
    }
  }

  loadDetail(id: any) {
    this.service.request(this.service.$api_get_cost_detail, { id }).subscribe(res => {
      if (res) {
        this.costInfo = res;
      }
    });
  }

  goBack() {
    history.go(-1);
  }

  private initST(): { [key: string]: STColumn[] } {
    return {
      cost: [
        { title: '序号', render: 'no', width: 70, className: 'text-left' },
        { title: '费用明细号', index: 'feeHId', className: 'text-left', width: 200 },
        { title: '订单号', index: 'billHCode', className: 'text-left', width: 200 },
        { title: '订单日期', render: 'billTime', className: 'text-left', width: 200 },
        { title: '运单号', index: 'waybillHCode', className: 'text-left', width: 200 },
        { title: '运单日期', index: 'waybillDate', className: 'text-left', width: 200, format: _ => this.costInfo.billTime },
        { title: '结算客户', render: 'cnoName', className: 'text-left', width: 200 },
        { title: '订单费用类型', index: 'billLTypeLabel', className: 'text-left', width: 200 },
        { title: '费用科目', index: 'feeSubName', className: 'text-left', width: 200 },
        {
          title: '税率',
          index: 'arvatrate',
          className: 'text-right',
          width: 200,
          format: item => `${item.arvatrate ? ((item.arvatrate as number) * 100).toFixed(2) : 0}%`
        },
        { title: '费用金额', render: 'hrvatmoney', className: 'text-right', width: 200 },
        { title: '收/付款金额', render: 'armoney', className: 'text-right', width: 200 },
        { title: '开/收票金额', render: 'arkpmoney', className: 'text-right', width: 200 }
      ],
      requested: [
        { title: '序号', render: 'no', width: 70, className: 'text-left' },
        { title: '费用明细号', index: 'feeLId', className: 'text-left', width: 200 },
        { title: '发票申请', index: 'vatinvcode', className: 'text-left', width: 200 },
        { title: '发票类型', index: 'invoicetypeLabel', className: 'text-left', width: 200 },
        { title: '发票号', index: 'invoiceno', className: 'text-left', width: 200 },
        { title: '发票日期', index: 'invoicedate', className: 'text-left', width: 200 },
        { title: '发票状态', index: 'stsLabel', className: 'text-left', width: 200 },
        { title: '应收金额', render: 'armoney', className: 'text-left', width: 200 },
        { title: '开票金额', render: 'vatmoney', className: 'text-left', width: 200 }
      ],
      collection: [
        { title: '序号', render: 'no', width: 70 },
        { title: '费用明细号', index: 'feeLId', className: 'text-left', width: 200 },
        { title: '收款单号', index: 'ahxHId', className: 'text-left', width: 200 },
        { title: '收款日期', index: 'feedate', className: 'text-left', width: 200 },
        { title: '收款状态', render: 'status', className: 'text-left', width: 150 },
        { title: '应收金额', render: 'ahxmoney', className: 'text-left', width: 120 },
        { title: '收款金额', render: 'ahxmoney', className: 'text-left', width: 120 }
      ],
      ticket: [
        { title: '序号', render: 'no', width: 70 },
        { title: '费用明细号', index: 'callNo' },
        { title: '进项发票号', index: 'callNo' },
        { title: '发票类型', index: 'callNo' },
        { title: '发票日期', index: 'callNo' },
        { title: '认证日期', index: 'callNo' },
        { title: '发票状态', index: 'callNo' },
        { title: '应付金额', index: 'callNo' },
        { title: '收票金额', index: 'callNo' }
      ],
      payment: [
        { title: '序号', render: 'no', width: 70 },
        { title: '费用明细号', index: 'feeHCode' },
        { title: '付款单号', index: 'phxHId' },
        { title: '要求日期', index: 'billTime' },
        { title: '确认日期', index: 'feedate' },
        // { title: '付款状态', index: 'callNo' },
        { title: '应付金额', render: 'phxmoney' },
        { title: '付款金额', render: 'phxmoney' }
      ]
    };
  }
}
