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

@Component({
  selector: 'app-invoiced-list',
  templateUrl: './invoiced-list.component.html',
  styleUrls: ['./invoiced-list.component.less']
})
export class InvoicedListComponent implements OnInit {
  url = `/rule?_allow_anonymous=true`;
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  @ViewChild('logosticsModal', { static: false })
  logosticsModal!: any;
  @ViewChild('logosticsLogsModal', { static: false })
  logosticsLogsModal!: any;
  columns: STColumn[] = [
    { title: '', index: 'key', type: 'checkbox' },
    { title: '发票号码', index: 'no' },
    { title: '申请编号', index: 'callNo' },
    { title: '托运人', index: 'callNo' },
    { title: '无车承运人', index: 'callNo' },
    { title: '运单数', index: 'callNo' },
    { title: '开票金额', index: 'callNo' },
    { title: '税价合计', index: 'callNo' },
    { title: '开票日期', index: 'updatedAt', type: 'date' },
    { title: '快递信息', render: 'logostics' },
    {
      title: '操作',
      buttons: [
        {
          text: '作废发票',
          click: item => this.deletedInvoice(item)
        },
        {
          text: '预览发票',
          click: item => this.routeTo(item)
        },
        {
          text: '填写物流',
          click: item => this.editInvoice(item)
        }
      ]
    }
  ];
  searchSchema: SFSchema = {
    properties: {
      expand: {
        type: 'boolean',
        ui: {
          hidden: true
        }
      },
      orderSn: {
        type: 'string',
        title: '发票号码',
        ui: {
          autocomplete: 'off'
        }
      },
      orderSn2: {
        type: 'string',
        title: '申请编号',
        ui: {
          autocomplete: 'off'
        }
      },
      createTime: {
        title: '开票日期',
        type: 'string',
        ui: {
          widget: 'date',
          mode: 'range',
          format: 'yyyy-MM-dd'
        } as SFDateWidgetSchema
      },
      receiveName: {
        type: 'string',
        title: '托运人',
        enum: [{ label: '全部', value: '全部' }],
        ui: {
          widget: 'select',
          placeholder: '请选择',
          change: (i: any) => {
            this.sf.value.receiveName = i;
            this.sf?.setValue('/receiveName', i);
          },
          visibleIf: {
            expand: (value: boolean) => value
          }
        }
      },
      receiveName2: {
        type: 'string',
        title: '无车承运人',
        enum: [{ label: '全部', value: '全部' }],
        ui: {
          widget: 'select',
          placeholder: '请选择',
          change: (i: any) => {
            this.sf.value.receiveName2 = i;
            this.sf?.setValue('/receiveName2', i);
          },
          visibleIf: {
            expand: (value: boolean) => value
          }
        }
      }
    }
  };

  reqParams = {};

  _$expand = false;

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

  ngOnInit(): void {}

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        this.totalCallNo = this.selectedRows.reduce((total, cv) => total + cv.callNo, 0);
        break;
      case 'filter':
        this.st.load();
        break;
    }
  }

  approval(): void {}

  add(): void {}

  routeTo(item: any) {
    this.router.navigate(['/ticket/invoice-list-detail/1']);
  }

  delete(item: any) {
    this.st.removeRow(item);
  }

  editInvoice(item: any) {
    const modal = this.nzModalService.create({
      nzTitle: '填写物流/修改物流',
      nzContent: this.logosticsModal,
      nzOnOk: () => {}
    });
    modal.afterClose.subscribe(res => {
      this.st.load();
    });
  }

  deletedInvoice(item: any) {
    this.nzModalService.warning({
      nzTitle: '确定将所选发票作废？',
      nzCancelText: '取消',
      nzOnOk: () => {}
    });
  }

  showlogosticsLogs(item: any) {
    this.nzModalService.create({
      nzTitle: '查看物流',
      nzWidth: 600,
      nzContent: this.logosticsLogsModal,
      nzFooter: []
    });
  }

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

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