import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { ShipperBaseService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { AddCollectionInvoiceModalComponent } from 'src/app/routes/ticket-management/components/input-invoice/add-collection-invoice-modal/add-collection-invoice-modal.component';
import { FreightAccountService } from '../../services/freight-account.service';

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

  _$expand = false;

  selectedRows: any[] = [];
  constructor(public service: FreightAccountService, private nzModalService: NzModalService, private router: Router) {}

  ngOnInit(): void {}

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf.value,
        createtime: {
          start: this.sf.value.createtime?.[0] || null,
          end: this.sf.value.createtime?.[1] || null
        },
        invdate: {
          start: this.sf.value.invdate?.[0] || null,
          end: this.sf.value.invdate?.[1] || null
        }
      });
    }
    return requestOptions;
  };

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  addInvoice() {
    if (this.selectedRows?.length <= 0) {
      this.service.msgSrv.warning('请选择申请记录');
      return;
    }
    const modal = this.nzModalService.create({
      nzTitle: '收票信息',
      nzContent: AddCollectionInvoiceModalComponent,
      nzComponentParams: { i: { userId: 0 } },
      nzFooter: null
    });
  }

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

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

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        inpinvcode: {
          type: 'string',
          title: '凭证号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        ltdid: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          },
          default: ''
        },
        invoiceno: {
          type: 'string',
          title: '发票号码',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        invtype: {
          type: 'string',
          title: '发票类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'refund:apply:status' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        hrto: {
          type: 'string',
          title: '销售方',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        createtime: {
          title: '创建时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        },
        sts: {
          type: 'string',
          title: '收票状态',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'refund:apply:status' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        invdate: {
          type: 'string',
          title: '发票日期',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        remarks: {
          type: 'string',
          title: '收票备注',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        billCode: {
          type: 'string',
          title: '订单号',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        feecode: {
          type: 'string',
          title: '费用号',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox' },
      { title: '收票单号', index: 'inpinvcode', type: 'link' },
      { title: '网络货运人', index: 'ltdid' },
      { title: '发票日期', index: 'invdate', type: 'date' },
      { title: '发票号', index: 'invoiceno' },
      { title: '发票金额', index: 'invmoney' },
      { title: '税额', index: 'invtax' },
      { title: '发票类型', index: 'invtype' },
      { title: '销售方', index: 'hrto' },
      { title: '创建时间', index: 'createtime', type: 'date' },
      { title: '创建人', index: 'createbyname' },
      { title: '收票状态', index: 'sts' },
      {
        title: '操作',
        buttons: [
          {
            text: '浏览',
            click: item => this.router.navigate(['/ticket/input-invoice/detail/' + item.id])
          },
          {
            text: '修改',
            click: item => this.router.navigate(['/ticket/input-invoice/edit/1'])
          }
        ]
      }
    ];
  }
}
