import { Component, OnInit, ViewChild } from '@angular/core';
import { STColumn, STComponent } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFSchemaEnum, SFSelectWidgetSchema, SFUISchema } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { NzModalService } from 'ng-zorro-antd/modal';
import { map } from 'rxjs/operators';
import { OrderManagementService } from '../../services/order-management.service';
import { UpdateFreightComponent } from '../../modal/bulk/update-freight/update-freight.component';
import { ConfirReceiptComponent } from '../../modal/bulk/confir-receipt/confir-receipt.component';
import { of } from 'rxjs';
import { ShipperBaseService } from '@shared';
import { Router } from '@angular/router';
import { OneCarOrderAppealComponent } from '../../modal/audit/appeal/appeal.component';

@Component({
  selector: 'app-order-management-abnormal-warning',
  templateUrl: './abnormal-warning.component.html',
  styleUrls: ['./abnormal-warning.component.less']
})
export class OrderManagementAbnormalWarningComponent implements OnInit {
  ui: SFUISchema = {};
  uiView: SFUISchema = {};
  schema: SFSchema = {};
  schemaView: SFSchema = {};
  changeId: any;       // 主页面查看运费变更记录id - 用于运费变更记录
  changeViewId: any;  // 查看运费变更记录id - 用于查看
  auditId: any;
  auditIdR: any;
  auditMany = false;
  isVisibleView = false;
  isVisibleEvaluate = false;
  isVisible = false;
  isVisibleRE = false;
  _$expand = false;
  @ViewChild('st') private readonly st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  @ViewChild('sfView', { static: false }) sfView!: SFComponent;
  @ViewChild('stFloat') private readonly stFloat!: STComponent;
  @ViewChild('stFloatView') private readonly stFloatView!: STComponent;
  columns: STColumn[] = [];
  columnsFloat: STColumn[] = [];
  columnsFloatView: STColumn[] = [];
  ViewCause: any;  // 变更运费查看数据
  constructor(
    public service: OrderManagementService,
    private modal: NzModalService,
    public shipperservice: ShipperBaseService,
    private router: Router
  ) { }

  /**
   * 查询参数
   */
  get reqParams() {
    const a: any = {};
    const params: any = Object.assign({}, this.sf?.value || {});
    delete params._$expand;
    return {
      ...a,
      ...params,
      warningTime: {
        start: this.sf?.value?.warningTime?.[0] || '',
        end: this.sf?.value?.warningTime?.[1] || ''
      }
    };
  }
  get changeParams() {
    return {
      id: this.changeId
    };
  }
  get selectedRows() {
    return this.st?.list.filter(item => item.checked) || [];
  }
  get changeViewParams() {
    return {
      id: this.changeViewId
    };
  }
  search() {
    this.st?.load(1);
  }

  ngOnInit(): void {
    this.initSF();
    this.initST();
  }

  /**
   * 初始化查询表单
   */
  initSF() {
    this.schema = {
      properties: {
        _$expand: { type: 'boolean', ui: { hidden: true } },
        billCode: {
          type: 'string',
          title: '订单号',
          ui: {
          }
        },
        wayBillCode: {
          type: 'string',
          title: '运单号',
          ui: {
          }
        },
        serviceType: {
          title: '服务类型',
          type: 'string',
          default: '',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'service:type' },
            containsAllLabel: true,
          } as SFSelectWidgetSchema
        },
        resourceType: {
          title: '货源类型',
          type: 'string',
          default: '',
          ui: {
            visibleIf: {
              _$expand: (value: boolean) => value
            },
            widget: 'dict-select',
            params: { dictKey: 'goodresource:type' },
            containsAllLabel: true,
          } as SFSelectWidgetSchema
        },
        shipperId: {
          type: 'string',
          title: '货主',
          ui: {
            widget: 'select',
            serverSearch: true,
            searchDebounceTime: 300,
            searchLoadingText: '搜索中...',
            allowClear: true,
            visibleIf: {
              _$expand: (value: boolean) => value
            },
            onSearch: (q: any) => {
              let str =q.replace(/^\s+|\s+$/g,"");
              if (str) {
                return this.service
                  .request(this.service.$api_enterpriceList, { enterpriseName: str })
                  .pipe(map((res: any) => (res as any[]).map(i => ({ label: i.enterpriseName, value: i.id } as SFSchemaEnum))))
                  .toPromise();
              } else {
                return of([]);
              }
            },
          } as SFSelectWidgetSchema
        },
        loadingPlace: {
          type: 'string',
          title: '装货地',
          ui: {
            visibleIf: {
              _$expand: (value: boolean) => value
            }
          }
        },
        dischargePlace: {
          type: 'string',
          title: '卸货地',
          ui: {
            visibleIf: {
              _$expand: (value: boolean) => value
            }
          }
        },
        driverName: {
          title: '承运司机',
          type: 'string',
          ui: {
            visibleIf: {
              _$expand: (value: boolean) => value
            }
          }
        },
        carNo: {
          title: '车牌号',
          type: 'string',
          ui: {
            visibleIf: {
              _$expand: (value: boolean) => value
            }
          }
        },
        warningTime: {
          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 } } };
  }

  /**
   * 初始化数据列表
   */
  initST() {
    this.columns = [
      {
        title: '订单号',
        width: '180px',
        fixed: 'left',
        className: 'text-left',
        index: 'billCode'
      },
      {
        title: '运单号',
        width: '180px',
        fixed: 'left',
        className: 'text-left',
        index: 'wayCode'
      },
      { title: '服务类型', index: 'serviceTypeLabel', width: '220px', className: 'text-left',format: (item) => {
        return item?.resourceTypeLabel + item?.serviceTypeLabel
      } },
      { title: '货主', index: 'shipperName', width: '220px', className: 'text-left' },
      { title: '装货地', index: 'loadingAddressArr', width: '220px', className: 'text-left' },
      { title: '卸货地', index: 'unloadingAddressArr', width: '220px', className: 'text-left' },
      { title: '司机',  render: 'driverName', width: '180px', className: 'text-left' },
      { title: '车牌号', index: 'carNo', width: '180px', className: 'text-left' },
      { title: '预警类型', index: 'warningTypeLabel', width: '180px', className: 'text-left' },
      {
        title: '预警时间',
        className: 'text-left',
        width: '180px',
        index: 'warningTime'
      },
      {
        title: '位置描述',
        className: 'text-left',
        width: '250px',
        render: 'longitude'
      },
      {
        title: '提醒内容',
        className: 'text-left',
        width: '250px',
        index: 'warningContent',
      },
    
    ];
  }

  /**
   * 查询字段个数
   */
  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.exportStart({ ...this.reqParams, pageSize: -1 }, this.service.$api_abnormalWarning_asyncExport);
    }
}
