import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import format from 'date-fns/format';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { OrderManagementService } from 'src/app/routes/order-management/services/order-management.service';

@Component({
  selector: 'app-datatable-view-track',
  templateUrl: './view-track.component.html'
})
export class DatatableReportingvViewTrackComponent implements OnInit {
  mapList: any[] = []; //地图点位数据组
  addressItems: any[] = []; //打点地址数据组
  trajectory = 'car';
  pois: any[] = [];
  id = '';
  constructor(public service: OrderManagementService, private modalRef: NzModalRef, public router: Router) {}

  ngOnInit(): void {
    if (this.trajectory === 'car') {
      this.getTrajectory();
    } else if (this.trajectory === 'driver') {
      this.getDriverTrajectory();
    }
  }

  selectTab(e: any) {}

  close(): void {
    this.modalRef.destroy();
  }

  // 车辆轨迹
  getTrajectory() {
    this.service.request(this.service.$api_get_getTrajectory, { id: this.id }).subscribe(res => {
      if (res) {
        const points = res.trackArray;
        let list: any[] = [];
        points?.forEach((item: any) => {
          list.push({
            name: item.hgt,
            lnglat: [Number((Number(item.lon) / 600000).toFixed(6)), Number((Number(item.lat) / 600000).toFixed(6))]
          });
        });
        this.mapList = list;
        this.addressItems = [...res.parkArray];
        if (this.addressItems && this.addressItems.length > 0) {
          this.addressItems.forEach(item => {
            item.parkBte = this.getLocalTime(item.parkBte);
          });
        }
      }
    });
  }

  // 获取司机轨迹
  getDriverTrajectory() {
    this.service.request(this.service.$api_get_getAppDriverPosition, { id: this.id }).subscribe(res => {
      if (res) {
        const points = res.tracks;
        let list: any[] = [];
        points?.forEach((item: any) => {
          list.push({
            name: item.hgt,
            lnglat: [Number((Number(item.lon) / 600000).toFixed(6)), Number((Number(item.lat) / 600000).toFixed(6))]
          });
        });
        this.mapList = list;
        const addressItems = [...res.tracks];
        if (addressItems) {
          addressItems.forEach(item => {
            // item.parkBte = item.gtm;
            item.parkBte = this.getLocalTime(item.gtm);
            item.parkAdr = item.appAdress;
          });
          this.addressItems = [...addressItems];
        } else {
          this.addressItems = [];
        }
      }
    });
  }
  getLocalTime(time: any) {
    return format(new Date(parseInt(time)), 'yyyy-MM-dd HH:mm:ss');
  }
}
