// 巡检的model
import 'dart:math';
import 'dart:convert' as convert;
import 'package:dio/dio.dart';
import 'package:flustars/flustars.dart';

class InspectionModel {
  String id; //id
  String name; // 巡检标签
  String startTime; //开始事件
  String endTime; // 结束时间
  String date; // 日期
  double startLng; // 开始经度
  double startLat; // 开始纬度
  double endLng; // 结束经度
  double endLat; // 结束维度
  // List<dynamic> startPosition; // 开始位置
  // List<dynamic> endPosition; // 结束位置
  List routes; // 路线
  String user; // 巡检人
  
  InspectionModel(id, name, startTime,endTime,startLng,startLat,endLng,endLat,routes,tasks,user){
    // 生成随机主键
    if(id==null){
      var rng = new Random();
      this.id = DateUtil.getNowDateMs().toString()+rng.nextInt(999).toRadixString(3);
    }else{
      this.id = id;
    }
    this.name = name;
    this.startTime = startTime;
    this.endTime = endTime;
    this.startLng = startLng;
    this.startLat = startLat;
    this.endLng = endLng;
    this.endLat = endLat;
    // this.startPosition = startPosition;
    // this.endPosition = endPosition;
    this.routes = routes;
    this.date = DateUtil.formatDateStr(startTime,format:'yyyy年MM月dd日');
    this.user = user;

  }
  
  InspectionModel.fromJson(Map<String, dynamic> json){
    startTime= json['startTime'];
    endTime = json['endTime'];
    startLng = json['startLng'];
    startLat = json['startLat'];
    endLng = json['endLng'];
    endLat = json['endLat'];
    routes = convert.jsonDecode(json['routes']);
    id = json['id'];
    name = json['name'];
    date = json['date'];
    user = json['user'];
  }

  Map<String, dynamic> toJson() => {
        'startTime': startTime,
        'endTime': endTime,
        'startLng': startLng,
        'startLat': startLat,
        'endLng': endLng,
        'endLat':endLat,
        'routes': convert.jsonEncode(routes),
        'id' : id,
        'name' : name,
        'user' : user,
        'date' : date
      };

  FormData toFormData(){
    Map<String, dynamic> jsonMap = toJson();
    print(jsonMap);
    return FormData.from(jsonMap);
  }
}
