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, SFSelectWidgetSchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { TicketService } from '../../services/ticket.service';
import { RequestedInvoiceModalComponent } from '../invoice-requested/requested-invoice-modal/requested-invoice-modal.component';
import { PushInvoiceComponent } from './push-invoice/push-invoice.component';

@Component({
  selector: 'app-cancellation-invoice',
  templateUrl: './cancellation-invoice.component.html',
  styleUrls: ['./cancellation-invoice.component.less']
})
export class CancellationInvoiceComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  @ViewChild('requestedModal', { static: false })
  requestedModal!: any;
  columns: STColumn[] = this.initST();
  searchSchema: SFSchema = this.initSF();
  resourceStatus = ''
  _$expand = false;

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

  ngOnInit(): void {}

  get reqParams()  {
    // if (this.sf) {
    //   Object.assign(requestOptions.body, {
    //     ...this.sf.value,
    //     sts: this?.resourceStatus,
    //     createTime: {
    //       start: this.sf.value.createTime?.[0] || null,
    //       end: this.sf.value.createTime?.[1] || null
    //     }
    //   });
    // }
    const a:any = {};
    if(this.resourceStatus) {
      a.sts = this.resourceStatus 
    }
    return {
      ...a,
      ...this.sf?.value,
    };
  };
  selectChange(e: any) {
    console.log(e)
    this.resourceStatus = e;
    this.initST();
    setTimeout(() => {
      this.st.load();
    }, 500);
  }
  approval(): void {}

  add(): void {}

  /**
   * 手工开票
   * @param item
   */
  requestedAction(item: any) {
    const modal = this.nzModalService.create({
      nzTitle: '发票确认',
      nzContent: this.requestedModal,
      nzOnOk: () => {}
    });
  }

  /**
   * 批量推送发票
   * @param item
   */
  batchPush(item?: any) {
    if (this.selectedRows?.length <= 0 && !item) {
      this.service.msgSrv.warning('请选择开票申请');
      return;
    }
    this.nzModalService.warning({
      nzTitle: '确定将所选待处理开票申请推送开票？',
      nzContent: '推送开票后发票信息不可修改，待系统开票完成后会自动返回开票结果',
      nzOnOk: () => {}
    });
  }

  /**
   * 撤回
   * @param item
   * @returns
   */
  batchWithdraw(item?: any) {
    if (this.selectedRows?.length <= 0 && !item) {
      this.service.msgSrv.warning('请选择开票申请');
      return;
    }
    this.nzModalService.warning({
      nzTitle: '确定将所选待确认开票申请撤回？',
      nzContent: '提交税控后发票信息不可修改，待税控开票完成后返回开票结果',
      nzOnOk: () => {}
    });
  }

  /**
   * 移除
   * @param item
   * @returns
   */
  batchRemove(item?: any) {
    if (this.selectedRows?.length <= 0 && !item) {
      this.service.msgSrv.warning('请选择开票申请');
      return;
    }
    this.nzModalService.warning({
      nzTitle: '确定将所选待确认开票申请撤回？',
      nzContent: '提交税控后发票信息不可修改，待税控开票完成后返回开票结果',
      nzOnOk: () => {}
    });
  }

  /**
   * 推送发票
   * @param item 
   */
  pushInvoiceAction(item: any) {
    const modal = this.nzModalService.create({
      nzTitle: '推送开票',
      nzContent: PushInvoiceComponent,
      nzWidth: 1200,
    });
  }

  /**
   * 重置表单
   */
  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
          }
        },
        callNo: {
          type: 'string',
          title: '申请编号',
          ui: {
            placeholder: '请输入'
          }
        },
        receiveName2: {
          type: 'string',
          title: '购买人',
          enum: [{ label: '全部', value: '全部' }],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          }
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder(),
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        sts: {
          title: '结算依据',
          type: 'string',
          ui: {
            widget: 'dict-select',
            containsAllLable: true,
            params: { dictKey: 'vatinv:status' },
            containAllLable:true,
            visibleIf: {
              _$expand: (value: boolean) => value,
            },
          } as SFSelectWidgetSchema,
        },
        // or2derSn: {
        //   type: 'string',
        //   title: '订单号',
        //   ui: {
        //     placeholder: '请输入',
        //     visibleIf: {
        //       expand: (value: boolean) => value
        //     }
        //   }
        // },
        createTime: {
          title: '申请时间',
          type: 'string',
          ui: {
            widget: 'date',
            mode: 'range',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox' },
      { title: '分票编号', render: 'vatinvcode', width: 150 },
      { title: '申请编号', index: 'callNo', width: 120 },
      { title: '申请时间', index: 'createTime', type: 'date', width: 150 },
      { title: '网络货运人', index: 'ltdName', width: 120 },
      { title: '购买人', index: 'artoName', width: 90 },
      { title: '订单数', index: 'ordlines', width: 90 },
      { title: '价税合计', index: 'disvatmoney', width: 90 },
      { title: '金额', index: 'vatnotax', width: 100 },
      { title: '税率', index: 'billvatrate', width: 90 },
      { title: '税额', index: 'disvattax', width: 90 },
      { title: '服务名称', index: 'vatname', width: 100 },
      { title: '销货清单', index: 'isdetail', width: 90 },
      { title: '票面备注', render: 'vatremarks', width: 250 },
      { title: '其他要求', index: 'otherremarks', width: 100 },
      {
        title: '操作',
        width: 150,
        fixed: 'right',
        buttons: [
          {
            text: '查看明细',
            click: item => this.router.navigate(['ticket/cancellation-invoice/detail/' + item.id], { queryParams: { type: 1 } })
          },
          {
            text: '手工开票',
            click: item => this.requestedAction(item)
          },
          {
            text: '推送开票',
            click: item => this.pushInvoiceAction(item)
          },
          {
            text: '移除',
            click: item => this.batchRemove(item)
          },
          {
            text: '确认'
            // click: item => this.rejectAction(item)
          },
          {
            text: '撤回',
            click: item => this.batchWithdraw(item)
          }
        ]
      }
    ];
  }
}
