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

@Component({
  selector: 'app-input-invoice',
  templateUrl: './input-invoice.component.html',
  styleUrls: ['../../../commom/less/box.less', '../../../commom/less/expend-but.less']
})
export class InputInvoiceComponent 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: TicketService, 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] || '',
          end: this.sf.value.createtime?.[1] || ''
        },
        invdate: {
          start: this.sf.value.invdate?.[0] || '',
          end: this.sf.value.invdate?.[1] || ''
        }
      });
    }
    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: '发票类型',
          enum: [
            { value: '', label: '全部' },
            { value: '1', label: '运输专票' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        hrto: {
          type: 'string',
          title: '销售方',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getCRMCustomerId(),
            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: '收票状态',
          enum: [
            { value: '', label: '全部' },
            { value: '1', label: '新建' },
            { value: '2', label: '关闭' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        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', width: 60, className: 'text-center', fixed: 'left' },
      { title: '收票单号', index: 'inpinvcode', type: 'link', width: 190 },
      { title: '网络货运人', index: 'ltdName', width: 220 },
      { title: '发票日期', index: 'invdate', type: 'date', width: 150, className: 'text-center' },
      { title: '发票号', index: 'invoiceno', width: 130 },
      {
        title: '发票金额',
        index: 'invmoney',
        width: 100,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.invmoney }) }
      },
      {
        title: '税额',
        index: 'invtax',
        width: 100,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.invtax }) }
      },
      { title: '发票类型', index: 'invtype', width: 150, className: 'text-center' },
      { title: '销售方', index: 'hrtoName', width: 200 },
      { title: '创建时间', index: 'createtime', type: 'date', width: 150, className: 'text-center' },
      { title: '创建人', index: 'createbyname', width: 120 },
      { title: '收票状态', index: 'stsLabel', width: 120, className: 'text-center' },
      {
        title: '操作',
        fixed: 'right',
        className: 'text-center',
        width: 120,
        buttons: [
          {
            text: '浏览',
            click: item => this.router.navigate(['/ticket/input-invoice/detail/' + item.id])
          },
          {
            text: '修改',
            click: item => this.router.navigate(['/ticket/input-invoice/edit/1'])
          }
        ]
      }
    ];
  }
}
