import { Component, OnInit, ViewChild } from '@angular/core';
import { STColumn, STComponent } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFSelectWidgetSchema, SFUISchema } from '@delon/form';
import { map } from 'rxjs/operators';
import { ShipperBaseService } from '@shared';
import {InterfaceDockingService} from "../../services/interface-docking.service";

@Component({
  selector: 'app-interface-docking-waybill',
  templateUrl: './waybill.component.html',
  styleUrls: ['./waybill.component.less']
})
export class interfaceDockingWaybillComponent implements OnInit {
  ui: SFUISchema = {};
  uiView: SFUISchema = {};
  schema: SFSchema = {};
  schemaView: SFSchema = {};
  auditMany = false;
  isVisibleView = false;
  isVisibleEvaluate = false;
  isVisible = false;
  _$expand = false;
  @ViewChild('st') private readonly st!: STComponent;
  @ViewChild('stFloat') private readonly stFloat!: STComponent;
  @ViewChild('stFloatView') private readonly stFloatView!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  @ViewChild('sfFre', { static: false }) sfFre!: SFComponent;
  @ViewChild('sfView', { static: false }) sfView!: SFComponent;
  columns: STColumn[] = [];
  resourceStatus: any;
  tabs = {
    totalCount: 0,
    billStatus1: 0,
    billStatus2: 0,
    billStatus3: 0,
    billStatus4: 0,
    billStatus5: 0,
    billStatus6: 0,
    billStatus7: 0,
    billStatus8: 0,
    billStatus9: 0,
    billStatus10: 0,
    billStatus11: 0,
    billStatus12: 0,
    billStatus13: 0,
    billStatus14: 0
  };
  constructor(
    public service: InterfaceDockingService,
    public shipperservice: ShipperBaseService,
  ) { }

  /**
   * 查询参数
   */
  get reqParams() {
    const a: any = {};
    if (this.resourceStatus) {
      a.billStatus = this.resourceStatus;
    }
    const params: any = Object.assign({}, this.sf?.value || {});
    delete params._$expand;
    return {
      ...a,
      ...params,
      createTime: {
        start: this.sf?.value?.createTime?.[0] || '',
        end: this.sf?.value?.createTime?.[1] || ''
      },
      planDeliverTime: {
        start: this.sf?.value?.planDeliverTime?.[0] || '',
        end: this.sf?.value?.planDeliverTime?.[1] || ''
      },
    };
  }
  get selectedRows() {
    return this.st?.list.filter(item => item.checked) || [];
  }
  search() {
    this.st?.load(1);
    this.getGoodsSourceStatistical();
  }
  getGoodsSourceStatistical() {
    this.tabs = {
      totalCount: 0,
      billStatus1: 0,
      billStatus2: 0,
      billStatus3: 0,
      billStatus4: 0,
      billStatus5: 0,
      billStatus6: 0,
      billStatus7: 0,
      billStatus8: 0,
      billStatus9: 0,
      billStatus10: 0,
      billStatus11: 0,
      billStatus12: 0,
      billStatus13: 0,
      billStatus14: 0
    };
    const params: any = Object.assign({}, this.reqParams || {});
    delete params.status;
    this.service.request(this.service.$api_listStatisticalStatus, params).subscribe((res: any) => {
      if (res) {
        let totalCount = 0;
        res.forEach((element: any) => {
          if (element.statusLabel === '新建') {
            this.tabs.billStatus1 = element.quantity;
          } else if (element.statusLabel === '运单已发送') {
            this.tabs.billStatus2 = element.quantity;
          } else if (element.statusLabel === '司机信息已获取') {
            this.tabs.billStatus3 = element.quantity;
          } else if (element.statusLabel === '司机信息已反馈') {
            this.tabs.billStatus4 = element.quantity;
          } else if (element.statusLabel === '签收单号已接收') {
            this.tabs.billStatus5 = element.quantity;
          } else if (element.statusLabel === '签收单号已反馈') {
            this.tabs.billStatus6 = element.quantity;
          } else if (element.statusLabel === '报道已接收') {
            this.tabs.billStatus7 = element.quantity;
          } else if (element.statusLabel === '报道已反馈') {
            this.tabs.billStatus8 = element.quantity;
          } else if (element.statusLabel === '发运已接收') {
            this.tabs.billStatus9 = element.quantity;
          } else if (element.statusLabel === '发运已反馈') {
            this.tabs.billStatus10 = element.quantity;
          } else if (element.statusLabel === '签收已接收') {
            this.tabs.billStatus11 = element.quantity;
          } else if (element.statusLabel === '签收已反馈') {
            this.tabs.billStatus12 = element.quantity;
          } else if (element.statusLabel === 'POD已获取') {
            this.tabs.billStatus13 = element.quantity;
          } else if (element.statusLabel === 'POD已反馈') {
            this.tabs.billStatus14 = element.quantity;
          }
          totalCount += element.quantity;
        });
        this.tabs.totalCount = totalCount;
      }
    });
  }
  selectChange(e: number) {
    this.resourceStatus = e;
    this.initST();
    setTimeout(() => {
      this.st.load();
    }, 500);
  }
  ngOnInit(): void {
    this.getGoodsSourceStatistical();
    this.initSF();
    this.initST();
  }

  /**
   * 初始化查询表单
   */
  initSF() {
    this.schema = {
      properties: {
        _$expand: { type: 'boolean', ui: { hidden: true } },
        blNo: {
          type: 'string',
          title: '运单号'
        },
        status: {
          title: '运单状态',
          type: 'string',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'api:bill:status' },
            containsAllLabel: true,
          } as SFSelectWidgetSchema
        },
        source: {
          title: '运单来源',
          type: 'string',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'api:bill:source' },
            containsAllLabel: true,
          } as SFSelectWidgetSchema
        },
        planDeliverTime: {
          title: '计划发货时间',
          type: 'string',
          ui: {
            widget: 'date',
            mode: 'range',
            format: 'yyyy-MM-dd',
            allowClear: true,
            visibleIf: {
              _$expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        },
        createTime: {
          title: '创建时间',
          type: 'string',
          ui: {
            widget: 'date',
            mode: 'range',
            format: 'yyyy-MM-dd',
            allowClear: true,
            visibleIf: {
              _$expand: (value: boolean) => value
            }
          } as SFDateWidgetSchema
        },
      },
      type: 'object'
    };
    this.ui = { '*': { spanLabelFixed: 110, grid: { span: 8, gutter: 4 } } };
  }
  // 获取城市列表
  getRegionCode(regionCode: any) {
    console.log(regionCode);
    return this.service
      .request(this.service.$api_get_enterprise_project, { id: regionCode })
      .pipe(
        map(res =>
          res.map((item: any) => ({
            label: item.projectName,
            value: item.id
          }))
        )
      )
      .subscribe(res => {
        this.sf.getProperty('/enterpriseProjectId')!.schema.enum = res;
        this.sf.getProperty('/enterpriseProjectId')!.widget.reset(res);
      });
  }
  /**
   * 初始化数据列表
   */
  initST() {
    this.columns = [
      { title: '', type: 'checkbox', fixed: 'left', width: '50px', className: 'text-center' },
      {
        title: '运单号',
        width: '180px',
        fixed: 'left',
        className: 'text-left',
        render: 'blNo',
        index: 'blNo'
      },
      {
        title: '运单状态',
        width: '250px',
        className: 'text-left',
        index: 'statusLabel'
      },
      { title: '运单来源', index: 'sourceLabel', width: '220px', className: 'text-left' },
      { title: '计划发货时间', index: 'planDeliverTime', width: '220px', className: 'text-left' },
      { title: '创建时间', index: 'createTime', width: '220px', className: 'text-left' },
      { title: '件数', index: 'quantity', width: '220px', className: 'text-left' },
      { title: '重量', index: 'weight', width: '180px', className: 'text-left' },
      { title: '体积', index: 'volume', width: '180px', className: 'text-left' },
    ];
  }
  /**
   * 查询字段个数
   */
  get queryFieldCount(): number {
    return Object.keys(this.schema?.properties || {}).length;
  }
  /**
   * 伸缩查询条件
   */
  expandToggle(): void {
    this._$expand = !this._$expand;
    this.sf?.setValue('/_$expand', this._$expand);
  }
  tabChange(item: any) { }
  /**
   * 重置表单
   */
  resetSF(): void {
    this.sf.reset();
    this._$expand = false;
  }

  exprot() {
    this.service.downloadFile(this.service.$api_get_asyncExport, { ...this.reqParams, pageSize: -1 });
  }

}
