import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { STColumn, STComponent } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema } from '@delon/form';
import { FreightAccountService } from '../../../services/freight-account.service';

@Component({
  selector: 'app-expenses-receivable',
  templateUrl: './expenses-receivable.component.html',
  styleUrls: ['./expenses-receivable.component.less']
})
export class ExpensesReceivableComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  schema!: SFSchema;
  columns: STColumn[] = this.initST();

  id = null;
  data: any[] = [
    {
      id: 1,
      description1: '',
      description2: '',
      description3: '',
      num: null,
      description5: ''
    }
  ];

  constructor(public service: FreightAccountService, private route: ActivatedRoute) {
    this.id = route.snapshot.queryParams.id;
    if (this.id) {
      this.schema = this.initSF({ page2: '天津怡亚通物流科技有限公司', pag2e21: '茅台集团' });
    } else {
      this.schema = this.initSF();
    }
  }

  ngOnInit(): void {}

  addRow() {
    this.data.push({
      id: this.data.length + 1,
      description1: '',
      description2: '',
      description3: '',
      num: null,
      description5: ''
    });
    this.st.reload();
  }

  goBack() {
    history.go(-1);
  }

  private initSF(data?: any): SFSchema {
    return {
      properties: {
        page2: {
          type: 'string',
          title: '网络货运人',
          enum: [],
          ui: {
            widget: data ? 'text' : 'select',
            placeholder: '请选择'
          },
          default: data?.page2 || ''
        },
        pag2e21: {
          title: '应收对象',
          type: 'string',
          enum: [],
          ui: {
            widget: data ? 'text' : 'select',
            placeholder: '请选择'
          },
          default: data?.pag2e21 || ''
        },
        dee: {
          title: '',
          type: 'string',
          ui: {
            widget: 'text'
          },
          default: ' '
        },
        createTi2me: {
          title: '费用日期',
          type: 'string',
          ui: {
            widget: 'date',
            mode: 'range',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema,
          default: data?.createTi2me || ''
        },
        pa2ge2: {
          type: 'string',
          title: '开票方式',
          enum: [],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: data?.pa2ge2 || ''
        },
        page: {
          type: 'string',
          title: '应收备注',
          ui: {
            placeholder: '请输入'
          },
          default: data?.page || ''
        }
      },
      required: ['page2', 'pag2e21', 'createTi2me', 'pa2ge2']
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '结算客户', render: 'description1', width: 150 },
      { title: '费用科目', render: 'description2', width: 120 },
      {
        title: '税率',
        render: 'description3',
        width: 120,
        format: item => `${item.description3 ? ((item.description3 as number) * 100).toFixed(2) : 0}%`
      },
      { title: '应收金额', render: 'num', width: 120 },
      { title: '备注', render: 'description5', width: 150 },
      {
        title: '操作',
        fixed: 'right',
        className: 'text-center',
        width: 90,
        buttons: [
          {
            text: '删除',
            click: item => {
              this.st.removeRow(item);
              this.data = this.data.filter(i => i.id !== item.id);
            }
          }
        ]
      }
    ];
  }
}
