import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STColumn, STComponent } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFUISchema, Widget } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { AccountManagemantService } from '../../services/account-managemant.service';

@Component({
  selector: 'app-partner-account-management-recorded-detail',
  templateUrl: './recorded-detail.component.html',
})
export class PartnerAccountManagementRecordedDetailComponent implements OnInit {
  summaryObj: any = {
    waitRecordedAmount: 0,
    totalRebate: 0,
    taxPersonalSum: 0,
    recordedAmount: 0,
    ltdName: '',
    taxno: ''
  };

  footerSummary = {
    total: 0,
    income: 0,
    spending: 0
  }

  detailRecord: any = {};

  url = `/user`;
  schema: SFSchema = {};
  ui!: SFUISchema;
  @ViewChild('st') private readonly st!: STComponent;
  @ViewChild('sf') private readonly sf!: SFComponent;


  columns: STColumn[] = [];
  billDetailColumns: STColumn[] = [];
  showBillDetail = false;
  billDetailList = [];
  roleId = '';

  constructor(public service: AccountManagemantService, public router: Router, public ar: ActivatedRoute) {
    this.roleId = this.ar.snapshot.params.id;

  }


  get reqParams() {
    return { ...this.sf?.value, partnerId: this.roleId };
  }

  get billDetailReqParams() {
    return {};
  }
  ngOnInit(): void {
    this.initSF();
    this.initST();
    this.getInvoiceSummary();
  }

  initSF() {
    this.schema = {
      properties: {
        ltdName: {
          title: '网络货运人',
          type: 'string',
          ui: {
            placeholder: '请输入',
          },
        },

      }
    }
    this.ui = { '*': { spanLabelFixed: 120, grid: { span: 8, gutter: 4 } }, };
  }

  /**
* 初始化数据列表
*/
  initST() {
    this.columns = [
      { title: '网络货运人', index: 'ltdName', className: 'text-center', width: 200 },
      { title: '银行类型', render: 'bankTypeLabel', className: 'text-center', width: 150 },
      { title: '虚拟账户', render: 'fictitiousAccount', className: 'text-center', width: 200 },
      { title: '返佣总额（元）', index: 'totalRebate', className: 'text-center', width: 180, type: 'currency' },
      { title: '已入账金额（元）', index: 'recordedAmount', className: 'text-center', width: 180, type: 'currency' },
      { title: '代缴个税（元）', index: 'taxPersonalSum', className: 'text-center', width: 180, type: 'currency' },
      { title: '待入账金额（元）', index: 'waitRecordedAmount', className: 'text-right', width: 180, type: 'currency' },
      {
        title: '操作', className: 'text-center', width: 300,
        buttons: [
          {
            text: '查看入账记录',
            click: (_record) => this.viewBookedRecord(_record)
          },
          {
            text: '查看账单明细',
            click: (_record) => this.viewAccountDetail(_record)
          },
        ]
      },
    ];
  }

  initBillDetailST() {
    this.billDetailColumns = [
      { title: '账单月份', render: 'month', className: 'text-center', width: '40%' },
      { title: '返佣金额（元）', render: 'profitAmountSum', className: 'text-center', width: '40%' },
      {
        title: '操作', className: 'text-center', width: '20%', buttons: [
          {
            text: '订单明细',
            click: (_record) => window.open(location.origin + `#/partner/rebate/record?ltdId=${_record?.ltdId}`)
          }
        ]
      },
    ]
  }


  resetSF() {
    this.sf.reset();
    setTimeout(() => {
      this.st.reset();
    })
  }



  search() {
    this.st.load(1);
  }

  /**
   * 获取账单明细
   */
  getBillDetail(ltdId: string) {
    this.service.request(this.service.$api_get_bill_detail, { ltdId }).subscribe(res => {
      if (res) {
        this.billDetailList = res;
      }
    })
  }


  export() { }

  /**
 * 查看入账记录
 * @param record 当前行
 */
  viewBookedRecord(record: any) {
    window.open(location.origin + `#/partner/recorded/record?ltdId=${record?.ltdId}`);
  }

  /**
   * 查看账单明细
   * @param record 当前行
   */
  viewAccountDetail(record: any) {
    this.billDetailColumns = [];
    this.showBillDetail = true;
    this.initBillDetailST();
    this.detailRecord = record;
    this.getBillDetail(record?.ltdId);
  }

  getInvoiceSummary() {
    this.service.request(this.service.$api_get_invoice_summary, { partnerId: this.roleId }).subscribe(res => {
      if (res) {
        this.summaryObj = res;
      }
    })
  }

  handleCancel() {
    this.showBillDetail = false;
    this.detailRecord = {};

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

}
