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

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

  selectedRows: any[] = [];

  _$expand = false;

  constructor(public service: FreightAccountService, private nzModalService: NzModalService, private router: Router) {}

  ngOnInit(): void {}

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, { ...this.sf.value });
    }
    return requestOptions;
  };

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

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

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

  exportList() {
    this.service.downloadFile(this.service.$mock_url, { ...this.sf.value, pageIndex: this.st.pi, pageSize: this.st.ps });
  }

  routeTo(url: string, params?: any) {
    this.router.navigate([url], { queryParams: params });
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        receiveName: {
          type: 'string',
          title: '费用单号',
          ui: { placeholder: '请输入' }
        },
        phone: {
          type: 'string',
          title: '订单号',
          ui: { placeholder: '请输入' }
        },
        page: {
          type: 'string',
          title: '运单号',
          ui: {
            placeholder: '请输入'
          }
        },
        createTi2me: {
          title: '费用日期',
          type: 'string',
          ui: {
            widget: 'date',
            mode: 'range',
            format: 'yyyy-MM-dd',
            visibleIf: {
              expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        },
        pa2ge2: {
          type: 'string',
          title: '费用类型',
          enum: [{ label: '全部', value: '全部' }],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        pa22ge2: {
          type: 'string',
          title: '费用科目',
          enum: [{ label: '全部', value: '全部' }],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        page2: {
          type: 'string',
          title: '网络货运人',
          enum: [{ label: '全部', value: '全部' }],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        page21: {
          type: 'string',
          title: '应付对象',
          enum: [{ label: '全部', value: '全部' }],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        pag2e21: {
          type: 'string',
          title: '应收对象',
          enum: [{ 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
        },
        pag2e221: {
          type: 'string',
          title: '应付核销',
          enum: [{ label: '全部', value: '全部' }],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        pag2e121: {
          type: 'string',
          title: '结算客户',
          enum: [{ label: '全部', value: '全部' }],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '费用单号', index: 'description', width: 120 },
      { title: '网络货运人', index: 'description', width: 120 },
      { title: '订单号', index: 'description', width: 120 },
      { title: '运单号', index: 'description', width: 120 },
      { title: '费用日期', index: 'description', width: 150 },
      { title: '费用类型', index: 'description', width: 100 },
      { title: '应付核销', index: 'description', width: 100 },
      { title: '结算客户', index: 'description', width: 100 },
      { title: '应收对象', index: 'description', width: 100 },
      { title: '应付对象', render: 'description1', width: 100 },
      { title: '应收金额', render: 'description1', width: 100 },
      { title: '应付金额', render: 'description1', width: 100 },
      { title: '收/付款金额', render: 'description1', width: 120 },
      { title: '开/收票金额', render: 'description1', width: 120 },
      { title: '创建时间', index: 'updatedAt', type: 'date', width: 150 },
      { title: '状态', render: 'description1', width: 90 },
      {
        title: '操作',
        fixed: 'right',
        className: 'text-center',
        width: 90,
        buttons: [
          {
            text: '浏览',
            click: item => this.routeTo('/financial-management/cost-management/detail/1')
          },
          {
            text: '审核',
            click: item => this.auditAction(item)
          },
          {
            text: '修改',
            click: item => this.router.navigate(['/financial-management/cost-management/expenses-receivable/1'], { queryParams: { id: 1 } })
          }
        ]
      }
    ];
  }
}
