import { Component, OnInit, ViewChild } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { STColumn, STComponent, STRequestOptions, STData } from '@delon/abc/st';
import { SFUISchema, SFSchema, SFComponent } from '@delon/form';
import { ShipperBaseService, DynamicSettingModalComponent } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { AccountDetailComponent } from 'src/app/shared/components/account-detail/account-detail.component';
import { UsermanageService } from '../../../services/usercenter.service';

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

  constructor(public service: UsermanageService, private modal: NzModalService, public shipperservice: ShipperBaseService) {}

  ngOnInit() {
    this.initST();
  }

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

  settingAction(item?: any) {
    this.modal.create({
      nzTitle: '配置',
      nzContent: DynamicSettingModalComponent,
      nzWidth: 900,
      nzComponentParams: {
        extendType: '2',
        businessId: item.id
      },
      nzFooter: null
    });
  }

  exportList() {
    const params = {};
    if (this.sf) {
      Object.assign(params, {
        ...this.sf.value
      });
    }
    this.service.downloadFile(this.service.$api_export_enterprise, params);
  }

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

  private initSF(): SFSchema {
    return {
      properties: {
        enterpriseName: {
          title: '企业名称',
          type: 'string',
          ui: {
            placeholder: '请输入',
            showRequired: false
          }
        },
        networkTransporter: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.shipperservice.getNetworkFreightForwarder()
          }
        }
      }
    };
  }

  initST(): STColumn[] {
    return [
      { title: '企业名称', className: 'text-center', index: 'enterpriseName', width: 160 },
      { title: '网络货运人', className: 'text-center', index: 'netTranName', width: 160 },
      {
        title: '合同单费率',
        className: 'text-right',
        index: 'contractSurchargeRatio',
        width: 140,
        format: item => `${item.contractSurchargeRatio}%`
      },
      {
        title: '货源单费率',
        className: 'text-right',
        index: 'goodsSurchargeRatio',
        width: 140,
        format: item => `${item.goodsSurchargeRatio}%`
      },
      {
        title: '合同单业务量（元）',
        index: 'contractQuota',
        width: 100,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.contractQuota }) }
      },
      {
        title: '货源单业务量（元）',
        index: 'goodsQuota',
        width: 100,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.goodsQuota }) }
      },
      {
        title: '操作',
        width: '110px',
        className: 'text-center',
        buttons: [
          {
            text: '配置',
            click: item => this.settingAction(item)
          }
        ]
      }
    ];
  }
}
