import { NzModalSubject } from 'ng-zorro-antd'; 
 | 
import { Sensor } from '@business/entity/data'; 
 | 
import { Component, OnInit } from '@angular/core'; 
 | 
import { FormGroup, FormBuilder, Validators } from '@angular/forms'; 
 | 
import { SensorsService } from '@business/services/http/sensors.service'; 
 | 
  
 | 
@Component({ 
 | 
  selector: 'app-sensor-edit', 
 | 
  templateUrl: './sensor-edit.component.html', 
 | 
  styles: [] 
 | 
}) 
 | 
export class SensorEditComponent implements OnInit { 
 | 
  data: Sensor; 
 | 
  isSaving = false; 
 | 
  validateForm: FormGroup; 
 | 
  constructor(     
 | 
    private subject: NzModalSubject, 
 | 
    private formBuilder: FormBuilder, 
 | 
    private organizationService: SensorsService 
 | 
    ) { } 
 | 
  
 | 
  ngOnInit() { 
 | 
     const data = this.data; 
 | 
     const validates: Sensor  = { 
 | 
          name: [data.name, [Validators.required] ], 
 | 
          sensorKey: [data.sensorKey, [Validators.required] ], 
 | 
          lower: [data.lower ], 
 | 
          upper: [data.upper ], 
 | 
          unit: [data.unit ], 
 | 
          description: [data.description ] 
 | 
     }; 
 | 
     this.validateForm = this.formBuilder.group( 
 | 
         validates 
 | 
     ); 
 | 
  } 
 | 
  close() { 
 | 
       this.subject.destroy(); 
 | 
  } 
 | 
  save($event, value, valid) { 
 | 
      $event.preventDefault(); 
 | 
      if (valid) { 
 | 
        for (const i in this.validateForm.controls) { 
 | 
          this.validateForm.controls[ i ].disable(); 
 | 
        }  
 | 
        this.isSaving = true; 
 | 
        Object.keys(value).forEach( (key: string) => { 
 | 
              // '_'为前缀的为自定义属性 
 | 
              if (!key.startsWith('_') && value[key] != null) { 
 | 
                   this.data[key] = value[key]; 
 | 
              } 
 | 
        } ); 
 | 
        this.subject.next( this ); 
 | 
      }else { 
 | 
        this.validate();  
 | 
      } 
 | 
  } 
 | 
  validate() { 
 | 
     for (const i in this.validateForm.controls) { 
 | 
       this.validateForm.controls[ i ].markAsDirty(); 
 | 
     } 
 | 
  } 
 | 
   
 | 
  
 | 
} 
 |