import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STComponent, STColumn, STChange, STRequestOptions } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema, SFSelectWidgetSchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { TicketService } from '../../services/ticket.service';

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

  _$expand = false;

  constructor(public service: TicketService, private nzModalService: NzModalService, private router: Router, private ar: ActivatedRoute) {}

  ngOnInit(): void {}

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value
      });
    }
    return requestOptions;
  };

  routeTo(item: any) {
    // return;
    this.router.navigateByUrl(`/order-management/vehicle/vehicle-detail/${item.billId}`);
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
  }

  /**
   * 伸缩查询条件
   */
  expandToggle() {
    this._$expand = !this._$expand;
    this.sf?.setValue('/expand', this._$expand);
  }

  export() {
    this.service.exportStart({ ...this.sf?.value, pageSize: -1 }, this.service.$api_export_InvoicedBillInfoPage);
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        billCode: {
          type: 'string',
          title: '订单号',
          ui: {
            placeholder: '请输入'
          }
        },
        serviceType: {
          title: '服务类型',
          type: 'string',
          enum: [
            {
              label: '全部',
              value: ''
            },
            {
              label: '整车抢单',
              value: 1
            },
            {
              label: '整车指派',
              value: 2
            },
            {
              label: '大宗抢单',
              value: 3
            },
            {
              label: '大宗指派',
              value: 4
            },
            {
              label: '结算单',
              value: 5
            }
          ],
          ui: {
            widget: 'select'
          }
        },
        shipperAppUserName: {
          type: 'string',
          title: '货主名称',
          ui: {
            placeholder: '请输入'
          }
        },
        vatappcode: {
          type: 'string',
          title: '申请编号',
          ui: {
            autocomplete: 'off',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        vatinvcode: {
          type: 'string',
          title: '分票编号',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        vatappdate: {
          title: '申请时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to',
            type: 'date',
            format: 'yyyy-MM-dd HH:mm:ss',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        },
        sts: {
          type: 'string',
          title: '开票状态',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'etc:invoicing:status' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        invoiceno: {
          type: 'string',
          title: '发票号码',
          ui: {
            placeholder: '请输入',
            autocomplete: 'off',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        invoicedate: {
          title: '开票日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to',
            type: 'date',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        },
        enterpriseInfoId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            visibleIf: {
              expand: (value: boolean) => value
            },
            asyncData: () => this.service.getNetworkFreightForwarder()
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '订单号', index: 'billCode', type: 'link', className: 'text-center', width: 180, click: _record => this.routeTo(_record) },
      { title: '运单号', index: 'wayBillCode', width: 180, className: 'text-center' },
      { title: '银行类型', index: 'bankTypeLabel', render: 'bankType', width: 100, className: 'text-center' },
      { title: '网络货运人', index: 'enterpriseInfoName', width: 220, className: 'text-center' },
      {
        title: '开票金额', // TODO
        index: 'vatmoney',
        width: 120,
        type: 'widget',
        className: 'text-center',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.vatmoney }) }
      },
      {
        title: '总费用',
        index: 'totalAmount',
        width: 120,
        type: 'widget',
        className: 'text-center',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.totalAmount }) }
      },
      {
        title: '运输费',
        index: 'price',
        width: 120,
        type: 'widget',
        className: 'text-center',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.price }) }
      },
      {
        title: '技术服务费',
        index: 'insurancePremium',
        width: 120,
        type: 'widget',
        className: 'text-center',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.insurancePremium }) }
      },
      {
        title: '附加费',
        index: 'surcharge',
        width: 120,
        type: 'widget',
        className: 'text-center',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.surcharge }) }
      },
      {
        title: '附加费率',
        className: 'text-center',
        index: 'paymentMethodRate',
        width: 130,
        format: record => `${record.paymentMethodRate}%`
      },
      { title: '货主名称', index: 'shipperAppUserName', width: '180px', className: 'text-center' },
      { title: '所属项目', index: 'enterpriseProjectName', width: '180px', className: 'text-center' },
      {
        title: '服务类型',
        render: 'serviceType',
        width: '120px',
        className: 'text-center'
      },
      { title: '装货地', index: 'consignor', width: '180px', className: 'text-center' },
      { title: '卸货地', index: 'consignee', width: '180px', className: 'text-center' },
      { title: '货物信息', index: 'goodsName', className: 'text-center', width: '140px' },
      { title: '车牌号', index: 'carNo', className: 'text-center', width: '120px' },
      {
        title: '承运司机',
        index: 'driverName',
        className: 'text-center',
        width: '120px'
      },
      {
        title: '车队长',
        className: 'text-center',
        width: '120px',
        index: 'carCaptainName'
      },
      { title: '业务员', width: '100px', index: 'salesmanName', className: 'text-center' },
      // { title: '录单时间', index: 'recordTime', type: 'date', className: 'text-center', width: '150px' }, //TODO
      { title: '装货时间', index: 'loadTime', type: 'date', width: '150px', className: 'text-center' },
      { title: '卸货时间', index: 'unloadTime', type: 'date', width: '150px', className: 'text-center' },
      { title: '订单完成时间', index: 'orderReceivingTime', type: 'date', width: 150, className: 'text-center' },
      { title: '支付完成时间', index: 'overallPaymentTime', type: 'date', width: 150, className: 'text-center' },
      { title: '开票状态', index: 'sts', render: 'sts', className: 'text-center', width: 120 },
      { title: '申请开票时间', index: 'vatappdate', type: 'date', className: 'text-center', width: 180 },
      { title: '申请开票编号', index: 'vatappcode', className: 'text-center', width: 190 },
      { title: '分票编号', index: 'vatinvcode', width: '200px', className: 'text-center' },
      { title: '发票号码', index: 'invoiceno', width: 130, className: 'text-center' },
      { title: '发票代码', index: 'invoiceno2', width: 130, className: 'text-center' },
      { title: '开票日期', index: 'invoicedate', type: 'date', width: 150, className: 'text-center' },
      // { title: '作废日期', index: 'invalidTime', type: 'date', width: 150 }, // TODO
      {
        title: 'ETC开票金额',
        index: 'etcInvoiceMoney',
        className: 'text-center',
        width: 120,
        type: 'widget',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.etcInvoiceMoney }) }
      }
    ];
  }
}
