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

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

  detail: any = {};

  selectedRows: any[] = [];
  totalCallNo = 0;
  _$expand = false;
  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 });
    }
    return requestOptions;
  };

  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;
    }
  }

  openRequestedModal() {
    const modal = this.nzModalService.create({
      nzTitle: '开票',
      nzContent: RequestedDetailComponent,
      nzWidth: 800,
      nzFooter: [
        {
          type: 'default',
          label: '手工处理',
          onClick: () => {
            modal.destroy();
          }
        },
        {
          type: 'primary',
          label: '自动开票',
          onClick: () => {
            modal.destroy();
          }
        }
      ]
    });
  }

  /**
   * 移除订单
   * @returns
   */
  removeOrder() {
    if (this.selectedRows?.length <= 0) {
      this.service.msgSrv.warning('请选择订单');
      return;
    }
    this.nzModalService.warning({
      nzTitle: '确定从当前批次中移除所选订单？',
      nzContent: '移除后相关订单可以重新提交开票申请',
      nzOnOk: () => {
        console.log(this.selectedRows);
      }
    });
  }

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

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
  }

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

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        orderSn: {
          type: 'string',
          title: '订单号',
          ui: {
            placeholder: '请输入'
          }
        },
        receiveName: {
          type: 'string',
          title: '开票状态',
          enum: [
            { label: '全部', value: '' },
            { label: '待受理', value: '待受理' },
            { label: '待开票', value: '待开票' },
            { label: '开票中', value: '开票中' },
            { label: '已开票', value: '已开票' },
            { label: '已撤销', value: '已撤销' },
            { label: '已拒绝', value: '已拒绝' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: ''
        },
        orderSn2: {
          type: 'string',
          title: '发票号码',
          ui: {
            placeholder: '请输入'
          }
        },
        receiveName2: {
          type: 'string',
          title: '订单类型',
          enum: [{ label: '全部', value: '' }],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        orderS2n2: {
          type: 'string',
          title: '运司机',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        orderS22n2: {
          type: 'string',
          title: '车牌号',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        receiveN2ame2: {
          type: 'string',
          title: '所属项目',
          enum: [{ label: '全部', value: '' }],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox' },
      { title: '订单号', index: 'no', width: 150 },
      { title: '订单完成日期', index: 'updatedAt', type: 'date', width: 150 },
      { title: '开票状态', index: 'callNo', width: 100 },
      { title: '所属项目', index: 'callNo', width: 100 },
      { title: '订单类型', index: 'callNo', width: 100 },
      { title: '装货地', index: 'callNo', width: 90 },
      { title: '卸货地', index: 'callNo', width: 90 },
      { title: '货物信息', index: 'callNo', width: 100 },
      { title: '承运司机', index: 'callNo', width: 140, format: item => `特朗普</br>13789040523</br>粤GT8419` },
      { title: '申请金额', index: 'callNo', width: 100 },
      { title: '运输费', index: 'callNo', width: 90 },
      { title: '附加费', index: 'callNo', width: 90 },
      { title: '开票金额', index: 'callNo', width: 100 },
      { title: '税率', index: 'callNo', width: 90 },
      { title: '发票号码', index: 'callNo', width: 100 },
      { title: '开票日期', index: 'updatedAt', type: 'date', width: 150 }
    ];
  }
}
