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

@Component({
  selector: 'app-contract-management-contract-list',
  templateUrl: './contract-list.component.html',
  styleUrls: ['./contract-list.component.less']
})
export class ContractManagementContractListComponent 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;
  schema: SFSchema = {};
  columns: STColumn[] = [];
  ui: SFUISchema = {};
  _$expand = false;

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

  ngOnInit(): void {
    this.initST()
    this.initSF()
  }
  /**
   * 初始化数据列表
   */
   initST() {
    this.columns = [
      { title: '', type: 'checkbox', width: '50px', className: 'text-center' },
      {
        title: '合同编号',
        width: '100px',
        className: 'text-center',
        render: 'goodsId'
      },
      {
        title: '合同类型',
        width: '100px',
        className: 'text-center',
      },
      { title: '托运人', index: 'externalSn', width: '120px', className: 'text-center' },
      { title: '承运人', index: 'linkUrl', width: '120px', className: 'text-center' },
      {
        title: '业务单号',
        className: 'text-center',
        width: '120px',
      },
      {
        title: '装货地',
        className: 'text-center',
        width: '120px',
      }, 
      {
        title: '卸货地',
        className: 'text-center',
        width: '120px',
      },
      {
        title: '货物名称',
        className: 'text-center',
        width: '120px',
      },
      {
        title: '重量/体积',
        className: 'text-center',
        width: '120px',
      },
      {
        title: '金额',
        className: 'text-center',
        width: '120px',
      },
      {
        title: '签署日期',
        className: 'text-center',
        width: '120px',
        render: 'feiong'
      },
      {
        title: '交易时间',
        className: 'text-center',
        width: '120px',
      },
      // {
      //   title: '操作',
      //   fixed: 'right',
      //   width: '200px',
      //   className: 'text-left',
      //   buttons: [
      //     {
      //       text: '查看评价',
      //       // click: (_record) => this.viewEvaluate(_record),
      //     },
      //   ],
      // },
    ];
  }
    /**
 * 初始化查询表单
 */
     initSF() {
      this.schema = {
        properties: {
          _$expand: { type: 'boolean', ui: { hidden: true } },
          no: {
            type: 'string',
            title: '用户名',
          },
          no2: {
            type: 'string',
            title: '托运人'
          },
          no1: {
            type: 'string',
            title: '承运人'
          },
          createTime: {
            title: '签署日期',
            type: 'string',
            ui: {
              widget: 'date',
              mode: 'range',
              format: 'yyyy-MM-dd',
              visibleIf: {
                _$expand: (value: boolean) => value,
              },
            } as SFDateWidgetSchema,
          },
          appId: {
            type: 'string',
            title: '合同类型',
            ui: {
              widget: 'select',
              placeholder: '请选择',
              visibleIf: {
                _$expand: (value: boolean) => value,
              },
              allowClear: true,
              // asyncData: () => this.getCatalogueMember(),
            },
          },
        },
        type: 'object',
      };
      this.ui = { '*': { spanLabelFixed: 110, grid: { span: 8, gutter: 4 } } };
    }
      /**
   * 查询字段个数
   */
  get queryFieldCount(): number {
    return Object.keys(this.schema?.properties || {}).length;
  }
  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(): void {
    this._$expand = !this._$expand;
    this.sf?.setValue('/_$expand', this._$expand);
  }
}
