/*
 * @Author: your name
 * @Date: 2021-12-23 16:50:17
 * @LastEditTime: 2021-12-31 13:35:22
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \tms-obc-web\src\app\routes\ticket-management\components\invoice-requested\requested-invoice-modal\requested-invoice-modal.component.ts
 */
import { Component, OnInit, ViewChild } from '@angular/core';
import { STChange, STColumn, STComponent } from '@delon/abc/st';
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
import { TicketService } from '../../../services/ticket.service';

@Component({
  selector: 'app-requested-invoice-modal',
  templateUrl: './requested-invoice-modal.component.html',
  styleUrls: ['./requested-invoice-modal.component.less']
})
export class RequestedInvoiceModalComponent implements OnInit {
  @ViewChild('st', { static: false })
  st!: STComponent;
  columns: STColumn[] = this.initST();
  headerInfo: any;
  id: any;
  info = {};
  selectedRows: any[] = [];
  totalCallNo = 0;
  constructor(public service: TicketService, private nzModalService: NzModalService,private modal: NzModalRef,) {}

  ngOnInit(): void {
    this.initData()
  }
  initData() {
    this.service.request(this.service.$api_get_invoice_requested_header_detail, { id: this.id }).subscribe(res => {
      console.log(res);
      if (res) {
        this.headerInfo = res;
      }
    });
  }

  /**
   * 移除订单
   * @returns
   */
  removeOrder(item: any[]) {
    console.log(item)
    this.nzModalService.warning({
      nzTitle: '确定从当前批次中移除所有订单？',
      nzContent: '移除后相关订单可以重新提交开票申请',
      nzOnOk: () => {
        // const ids = this.selectedRows.map(order => order.billHId);
        // this.service.request(this.service.$api_remove_bill, ids).subscribe(res => {
        //   if (res) {
        //     this.service.msgSrv.success('移除成功');
        //     this.loadHeadInfo();
        //     this.st.reload();
        //   }
        // });
      }
    });
  }
  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        this.totalCallNo = this.selectedRows.reduce((total, cv) => total + cv.callNo, 0);
        break;
      case 'filter':
        this.st.load();
        break;
    }
  }
  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox' },
      { title: '订单号', index: 'no', width: 150 },
      { title: '订单完成日期', index: 'updatedAt', type: 'date', width: 150 },
      { title: '所属项目', index: 'callNo', width: 120 },
      { title: '订单类型', index: 'callNo', width: 90 },
      { title: '装货地', index: 'callNo', width: 90 },
      { title: '卸货地', index: 'callNo', width: 100 },
      { title: '货物信息', index: 'callNo', width: 90 },
      { title: '承运司机', index: 'callNo', width: 140, format: item => `特朗普</br>13789040523</br>粤GT8419` },
      { title: '总费用', index: 'callNo', width: 90 },
      { title: '运输费', index: 'callNo', width: 90 },
      { title: '附加费', index: 'callNo', width: 90 },
      {
        title: '操作',
        width: 80,
        fixed: 'right',
        className: 'text-center',
        buttons: [
          {
            text: '移除',
            click: item => this.removeOrder([item])
          }
        ]
      }
    ];
  }
  saveManage() {
    console.log(this.selectedRows)
    console.log('11111')
    const params = {
      ficoVatappBillVOList: this.selectedRows,
      id: this.id
    }
    this.service.request(this.service.$api_get_applyFicoVatinv, params).subscribe((res: any) => {
      console.log(res)
    })
    this.modal.destroy();
  }
}
