import { Component, OnInit, ViewChild } from '@angular/core';
import { STComponent, STColumn, STRequestOptions, STChange } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema } from '@delon/form';
import { TicketService } from '../../../services/ticket.service';

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

  constructor(public service: TicketService) {}

  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.selectedData = e.checkbox!;
        break;
    }
  }

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

  private initSF(): SFSchema {
    return {
      properties: {
        enterpriseName: {
          title: '费用单号',
          type: 'string',
          ui: {
            placeholder: '请输入'
          }
        },
        enterprise2Name: {
          title: '订单号',
          type: 'string',
          ui: {
            placeholder: '请输入'
          }
        },
        contactName: {
          title: '费用日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        contac2tName: {
          title: '订单日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        mobile: {
          type: 'string',
          title: '结算客户',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getCloseAccount(),
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox', width: 60 },
      { title: '序号', render: 'no', width: 60 },
      { title: '费用号', index: 'enterpriseName', width: 120 },
      { title: '费用日期', index: 'contacter', width: 120 },
      { title: '订单号', index: 'mobile', width: 120 },
      { title: '订单日期', index: 'mobile', width: 120 },
      { title: '结算客户', index: 'mobile', width: 120 },
      { title: '费用科目', index: 'mobile', width: 120 },
      { title: '费用金额', index: 'mobile', width: 120 },
      { title: '已收金额', index: 'mobile', width: 120 },
      { title: '未收金额', index: 'mobile', width: 120 },
      { title: '收票金额', render: 'mo1bile', width: 150, fixed: 'right' }
    ];
  }
}
