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

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

  _$expand = false;

  selectedRows: any[] = [];
  totalCallNo = 0;
  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;
    }
  }

  approval(): void {}

  add(): void {}

  rejectAction(item: any[]) {
    const modal = this.nzModalService.create({
      nzTitle: '驳回',
      nzContent: this.rejectModal,
      nzFooter: [
        {
          label: '拒绝',
          type: 'default',
          onClick: () => {
            modal.destroy();
          }
        },
        {
          label: '通过',
          type: 'primary',
          onClick: () => {
            modal.destroy();
          }
        }
      ]
    });
    modal.afterClose.subscribe(res => {
      this.st.load();
    });
  }

  showReason(item: any) {
    const modal = this.nzModalService.create({
      nzTitle: '查看原因',
      nzContent: '运单数据异常，暂时无法开票，请联系客服400-xxxx-xxxx',
      nzFooter: [
        {
          label: '关闭',
          type: 'primary',
          onClick: () => {
            modal.destroy();
          }
        }
      ]
    });
  }

  batchRequested() {
    if (this.selectedRows?.length <= 0) {
      this.service.msgSrv.warning('请选择订单');
      return;
    }
    const modal = this.nzModalService.create({
      nzTitle: '开票',
      nzContent: '确认对所有申请单进行批量开票？',
      nzFooter: [
        {
          type: 'default',
          label: '手工处理',
          onClick: () => {
            modal.destroy();
          }
        },
        {
          type: 'primary',
          label: '自动开票',
          onClick: () => {
            modal.destroy();
          }
        }
      ]
    });
  }

  requestedInvoiceAction(item: any) {
    const modal = this.nzModalService.create({
      nzTitle: '开票',
      nzContent: RequestedInvoiceModalComponent,
      nzWidth: 1200,
      nzFooter: [
        {
          type: 'default',
          label: '移除',
          onClick: (com) => {
            com?.removeOrder(com.data)
          }
        },
        {
          type: 'default',
          label: '手工处理',
          onClick: () => {
            modal.destroy();
          }
        },
        {
          type: 'primary',
          label: '自动开票',
          onClick: () => {
            modal.destroy();
          }
        }
      ]
    });
  }

  /**
   * 重置表单
   */
  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
          }
        },
        orderSn: {
          type: 'string',
          title: '申请编号',
          ui: {
            placeholder: '请输入'
          }
        },
        orderSn2: {
          type: 'string',
          title: '订单号',
          ui: {
            placeholder: '请输入'
          }
        },
        orderSn3: {
          type: 'string',
          title: '费用号',
          ui: {
            placeholder: '请输入'
          }
        },
        receiveName2: {
          type: 'string',
          title: '购买方',
          enum: [
            { label: '全部', value: '全部' },
            { label: '企业认证审核', value: '企业认证审核' },
            { label: '企业管理员审核', value: '企业管理员审核' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        receiveName3: {
          type: 'string',
          title: '网络货运人',
          enum: [{ label: '全部', value: '全部' }],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        receiveName23: {
          type: 'string',
          title: '其他需求',
          enum: [
            { label: '全部', value: '全部' },
            { label: '有', value: '有' },
            { label: '无', value: '无' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        receiveName: {
          type: 'string',
          title: '处理进度',
          enum: [
            { label: '全部', value: '全部' },
            { label: '待审核', value: '待审核' },
            { label: '处理中', value: '处理中' },
            { label: '已完成', value: '已完成' },
            { label: '已拒绝', value: '已拒绝' },
            { label: '已撤销', value: '已撤销' }
          ],
          ui: {
            widget: 'select',
            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
        },
        re2ceiveName: {
          type: 'string',
          title: '销货清单',
          enum: [
            { label: '全部', value: '全部' },
            { label: '需要', value: '需要' },
            { label: '不需要', value: '不需要' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox' },
      { title: '申请编号', index: 'no', width: 150, format: item => `VP202110012313</br><label class="text-primary">待受理</label>` },
      { title: '网络货运人', index: 'callNo', width: 120 },
      { title: '购买方', index: 'callNo', width: 90 },
      { title: '订单数', index: 'callNo', width: 90 },
      { title: '申请金额', index: 'callNo', width: 100 },
      { title: '运输费', index: 'callNo', width: 90 },
      { title: '附加费', index: 'callNo', width: 90 },
      { title: '已开票金额', index: 'callNo', width: 120 },
      { title: '开户行', index: 'callNo', width: 90 },
      { title: '银行账户', index: 'callNo', width: 100 },
      { title: '注册地址', index: 'callNo', width: 100 },
      { title: '注册电话', index: 'callNo', width: 100 },
      { title: '服务名称', index: 'callNo', width: 100 },
      { title: '销货清单', index: 'callNo', width: 100 },
      { title: '其他要求', index: 'callNo', width: 100 },
      { title: '申请人', index: 'callNo', width: 90 },
      { title: '申请时间', index: 'updatedAt', type: 'date', width: 150 },
      {
        title: '操作',
        width: 150,
        fixed: 'right',
        buttons: [
          {
            text: '开票',
            click: item => this.requestedInvoiceAction(item)
          },
          {
            text: '驳回',
            click: item => this.rejectAction([item])
          },
          {
            text: '查看原因',
            click: item => this.showReason(item)
          },
          {
            text: '订单明细',
            click: item => this.router.navigate(['/ticket/invoice-requested/detail/1'])
          },
          {
            text: '下载对账单'
            // click: item => this.rejectAction(item)
          }
        ]
      }
    ];
  }
}
