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-etc-invoiced-requested',
  templateUrl: './etc-invoiced-requested.component.html',
  styleUrls: ['./etc-invoiced-requested.component.less']
})
export class ETCInvoicedRequestedComponent implements OnInit {
  url = `/rule?_allow_anonymous=true`;
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  @ViewChild('auditModal', { static: false })
  auditModal!: 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: 'updatedAt', type: 'date' },
    { title: '提货时间', index: 'updatedAt', type: 'date' },
    { title: '卸货时间', index: 'updatedAt', type: 'date' },
    { title: '签收时间', index: 'updatedAt', type: 'date' },
  ];
  searchSchema: SFSchema = {
    properties: {
      expand: {
        type: 'boolean',
        ui: {
          hidden: true
        }
      },
      orderSn: {
        type: 'string',
        title: '运单号',
        ui: {
          autocomplete: 'off',
          placeholder: '请输入'
        }
      },
      orderSn1: {
        type: 'string',
        title: '司机',
        ui: {
          autocomplete: 'off',
          placeholder: '姓名/手机'
        }
      },
      orderSn2: {
        type: 'string',
        title: '车牌号',
        ui: {
          autocomplete: 'off',
          placeholder: '请输入'
        }
      },
      orderS3: {
        type: 'string',
        title: '卸货地',
        ui: {
          autocomplete: 'off',
          placeholder: '请选择',
          visibleIf: {
            expand: (value: boolean) => value
          }
        }
      },
      orderSn4: {
        type: 'string',
        title: '装货地',
        ui: {
          autocomplete: 'off',
          placeholder: '请选择',
          visibleIf: {
            expand: (value: boolean) => value
          }
        }
      },
      orderSn5: {
        type: 'string',
        title: '托运人',
        enum: [
          { label: '全部', value: '全部' },
          { label: '企业认证审核', value: '企业认证审核' },
          { label: '企业管理员审核', value: '企业管理员审核' }
        ],
        ui: {
          widget: 'select',
          placeholder: '请选择',
          change: (i: any) => {
            this.sf.value.receiveName = i;
            this.sf?.setValue('/receiveName', i);
          },
          visibleIf: {
            expand: (value: boolean) => value
          }
        }
      },
      receiveName: {
        type: 'string',
        title: '无车承运人',
        enum: [
          { label: '全部', value: '全部' },
          { label: '企业认证审核', value: '企业认证审核' },
          { label: '企业管理员审核', value: '企业管理员审核' }
        ],
        ui: {
          widget: 'select',
          placeholder: '请选择',
          change: (i: any) => {
            this.sf.value.receiveName = i;
            this.sf?.setValue('/receiveName', i);
          },
          visibleIf: {
            expand: (value: boolean) => value
          }
        }
      }
    }
  };

  reqParams = {};

  _$expand = false;

  selectedRows: any[] = [];
  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!;
        break;
      case 'filter':
        this.st.load();
        break;
    }
  }

  approval(): void {}

  add(): void {}

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

  auditAction(item: any) {
    const modal = this.nzModalService.create({
      nzTitle: '审核',
      nzContent: this.auditModal,
      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();
          }
        }
      ]
    });
  }

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

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