import { NzModalSubject } from 'ng-zorro-antd'; 
 | 
import { Component, OnInit } from '@angular/core'; 
 | 
import { SensorsService } from '@business/services/http/sensors.service'; 
 | 
import { PageBean,  Grid } from '@business/entity/grid'; 
 | 
import { Sensor } from '@business/entity/data'; 
 | 
import { Subject } from 'rxjs/Subject'; 
 | 
import { count } from 'rxjs/operator/count'; 
 | 
import { debounce } from 'rxjs/operators'; 
 | 
  
 | 
@Component({ 
 | 
  selector: 'app-version-sensor-config', 
 | 
  templateUrl: './version-sensor-config.component.html', 
 | 
  styles: [] 
 | 
}) 
 | 
export class VersionSensorConfigComponent implements OnInit { 
 | 
  deviceVersionId: number; 
 | 
  selectedSensorIds: number[]; 
 | 
  originalOrder: { [key: string]: number} = {}; 
 | 
  isSaving = false; 
 | 
  grid: Grid<Sensor> = new Grid(null); 
 | 
  public searchValue = ''; 
 | 
  private initPage() { 
 | 
    const sensor: Sensor = { 
 | 
      name: { 
 | 
        text: '名称', 
 | 
        width: '200px' 
 | 
      }, 
 | 
      sensorKey: { 
 | 
        text: '键值', 
 | 
        width: '60px' 
 | 
      }, 
 | 
      lower: { 
 | 
        text: '下限值', 
 | 
        width: '90px' 
 | 
      }, 
 | 
      upper: { 
 | 
        text: '上限值', 
 | 
        width: '90px' 
 | 
      }, 
 | 
      unit: { 
 | 
        text: '单位', 
 | 
        width: '100px' 
 | 
      }, 
 | 
      description: { 
 | 
        text: '描述' 
 | 
      } 
 | 
    }; 
 | 
    this.grid.title = '传感器'; 
 | 
    this.grid.setColumns(sensor); 
 | 
    this.grid.pageSize = 0; 
 | 
    this.subjectStream.debounceTime(500).distinctUntilChanged().subscribe( 
 | 
      name => { 
 | 
          if (!name || !name.trim()) { 
 | 
            this.gridDataImage = this.grid.data; 
 | 
          } else { 
 | 
            this.gridDataImage =  this.grid.data.filter( 
 | 
              item => { 
 | 
                    name = name.trim().toLowerCase(); 
 | 
                    if (!!item.name.toLowerCase().match(name)) { 
 | 
                      return true; 
 | 
                    } else { 
 | 
                      return false; 
 | 
                    } 
 | 
              } 
 | 
            ); 
 | 
          } 
 | 
      } 
 | 
    ); 
 | 
  } 
 | 
  constructor( 
 | 
    private subject: NzModalSubject, 
 | 
    private sensorsService: SensorsService, 
 | 
  ) { } 
 | 
  
 | 
  ngOnInit() { 
 | 
    this.initPage(); 
 | 
    this.load(); 
 | 
  } 
 | 
  public gridDataImage = []; 
 | 
  load() { 
 | 
    // 延时加载避免ExpressionChangedAfterItHasBeenCheckedError 
 | 
    setTimeout(() => { 
 | 
      this.grid.loading = true; 
 | 
    }, 1); 
 | 
    this.sensorsService.getPagingList(this.grid, null).subscribe( 
 | 
      (res: PageBean) => { 
 | 
        this.grid.loading = true; 
 | 
        if (res != null && res.data != null) { 
 | 
          this.grid.initData(res); 
 | 
          this.gridDataImage = this.grid.data; 
 | 
          this.grid.data.map( 
 | 
            (row: any) => { 
 | 
               row['checked'] = this.selectedSensorIds.filter( 
 | 
                (id: number) => { 
 | 
                  return row.id === id; 
 | 
                } 
 | 
              ).length > 0; 
 | 
            } 
 | 
          ); 
 | 
          this.grid.refreshStatus(); 
 | 
          setTimeout(() => { 
 | 
            this.grid.loading = false; 
 | 
          }, 1); 
 | 
          this.grid.data.forEach((item, index) => { 
 | 
             this.originalOrder[item.id] = index; 
 | 
          }); 
 | 
        } 
 | 
      } 
 | 
    ); 
 | 
  } 
 | 
  close() { 
 | 
    this.subject.destroy(); 
 | 
  } 
 | 
  save($event, value, valid) { 
 | 
    $event.preventDefault(); 
 | 
    this.subject.next( this ); 
 | 
  } 
 | 
  public get selectedCount(): number { 
 | 
     let selectedCount = 0; 
 | 
     this.grid.data.forEach( 
 | 
       item => { 
 | 
         if (item['checked']) { 
 | 
          selectedCount++; 
 | 
         } 
 | 
       } 
 | 
     ); 
 | 
     return selectedCount; 
 | 
  } 
 | 
  sort(field: string, value: string) { 
 | 
       const data = this.grid.data.sort( 
 | 
           (a, b) => { 
 | 
              let aWeight = 0; 
 | 
              let bWeight = 0; 
 | 
              // debugger; 
 | 
             if (!!value) { 
 | 
               if (value === 'ascend') { 
 | 
                aWeight = a['checked'] ? 1 : 0; 
 | 
                bWeight = b['checked'] ? 1 : 0; 
 | 
               } else { 
 | 
                aWeight = a['checked'] ? 0 : 1; 
 | 
                bWeight = b['checked'] ? 0 : 1; 
 | 
               } 
 | 
             } else { 
 | 
                const idMap = this.originalOrder; 
 | 
                aWeight = idMap[a.id]; 
 | 
                bWeight = idMap[b.id]; 
 | 
             } 
 | 
             return aWeight - bWeight; 
 | 
           } 
 | 
       ); 
 | 
       this.grid.data = data; 
 | 
       this.gridDataImage = []; 
 | 
       setTimeout(() => { 
 | 
         this.gridDataImage = data; 
 | 
       }, 1); 
 | 
      //  this.subjectStream.next(null); 
 | 
      //  setTimeout(() => { 
 | 
      //   this.grid.data = data; 
 | 
      //   this.grid.refreshStatus(); 
 | 
      //  }, 1); 
 | 
    } 
 | 
    private subjectStream: Subject<string> = new Subject<string> (); 
 | 
    public setSearchValue(name) { 
 | 
        this.searchValue = name; 
 | 
        this.subjectStream.next(name); 
 | 
    } 
 | 
} 
 |