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 } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { TicketService } from '../../services/ticket.service';

@Component({
  selector: 'app-invoice-detail',
  templateUrl: './invoice-detail.component.html',
  styleUrls: ['./invoice-detail.component.less']
})
export class InvoiceDetailComponent implements OnInit {
  @ViewChild('orderST', { static: true })
  orderST!: STComponent;
  @ViewChild('orderSf', { static: false })
  orderSf!: SFComponent;
  orderColumns: STColumn[] = this.initOrderST();
  orderSchema: SFSchema = this.initOrderSF();

  @ViewChild('costST', { static: true })
  costST!: STComponent;
  @ViewChild('costSf', { static: false })
  costSf!: SFComponent;
  costColumns: STColumn[] = this.initCostST();
  costSchema: SFSchema = this.initCostSF();

  @ViewChild('invoiceST', { static: true })
  invoiceST!: STComponent;
  invoiceColumns: STColumn[] = this.initInvoiceST();

  isCanEdit = false;
  isEdit = false;

  constructor(public service: TicketService, private route: ActivatedRoute) {
    this.isCanEdit = !!route.snapshot.queryParams.type;
  }

  ngOnInit(): void {}

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

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

  saveInvoices() {
    console.log(this.invoiceST._data);

    this.isEdit = false;
  }

  /**
   * 重置表单
   */
  resetSF(tab: number) {
    switch (tab) {
      case 1:
        this.orderSf.reset();
        break;
      case 2:
        this.costSf.reset();
        break;

      default:
        break;
    }
  }

  private initOrderSF(): SFSchema {
    return {
      properties: {
        orderSn: {
          type: 'string',
          title: '订单号',
          ui: {
            autocomplete: 'off'
          }
        },
        receiveName: {
          type: 'string',
          title: '订单类型',
          enum: [{ label: '全部', value: '' }],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: ''
        },
        receiveNa22me: {
          type: 'string',
          title: '所属项',
          enum: [{ label: '全部', value: '' }],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: ''
        }
      }
    };
  }

  private initOrderST(): STColumn[] {
    return [
      { title: '订单号', index: 'no' },
      { title: '订单完成日期', index: 'callNo', type: 'date' },
      { title: '所属项目', index: 'callNo' },
      { title: '订单类型', index: 'callNo' },
      { title: '装货地', index: 'callNo' },
      { title: '卸货地', index: 'callNo' },
      { title: '货物信息', index: 'callNo' },
      { title: '承运司机', index: 'callNo', format: item => `特朗普&#10; 13789040523 &#10;粤GT8419` },
      { title: '申请金额', index: 'callNo' },
      { title: '运输费', index: 'callNo' },
      { title: '附加费', index: 'callNo' },
      { title: '开票金额', index: 'callNo' }
    ];
  }

  private initCostSF(): SFSchema {
    return {
      properties: {
        orderSn: {
          type: 'string',
          title: '订单号',
          ui: {
            autocomplete: 'off'
          }
        },
        orderS2n: {
          type: 'string',
          title: '费用号',
          ui: {
            autocomplete: 'off'
          }
        }
      }
    };
  }

  private initCostST(): STColumn[] {
    return [
      { title: '费用号', index: 'no' },
      { title: '订单号', index: 'callNo' },
      { title: '订单日期', index: 'callNo', type: 'date' },
      { title: '计费日期', index: 'callNo', type: 'date' },
      { title: '税率', index: 'callNo' },
      { title: '申请金额', index: 'callNo' },
      { title: '开票金额', index: 'callNo' }
    ];
  }

  private initInvoiceST(): STColumn[] {
    return [
      { title: '服务名称', render: 'owner' },
      { title: '规格型号', render: 'callNo' },
      { title: '单位', render: 'progress' },
      { title: '数量', render: 'status' },
      { title: '金额', index: 'callNo' },
      { title: '税率', index: 'callNo' },
      { title: '税额', index: 'callNo' }
    ];
  }
}
