<template>
  <el-row :gutter="10" v-loading="loading">
    <el-col :span="18">
      <area-map 
        ref="map"
        style="height: 500px"
        :area="area" 
        :area-sub-list="areaSubList" 
        :location-log-list="locationLogList" 
        :enable-edit="false" 
      />
    </el-col>
    <el-col :span="6">
      <el-date-picker
        v-model="queryParams.timeRange"
        value-format="yyyy-MM-dd HH:mm:ss"
        type="datetimerange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        size="mini"
        :clearable="false"
        @change="getLocationLogList"
        style="width: 100%;"
      />
      <div class="location-log-list">
        <div 
          @click="handleLogClick(log, index)"
          v-for="(log, index) in locationLogList" 
          :key="index"
          class="log-item"
        >
          <div class="log-time">{{ log.at }}</div>
          <div class="log-info">
            <span class="info-item">
              <i class="el-icon-location"></i>
              {{ log.longitude.toFixed(6) }}, {{ log.latitude.toFixed(6) }}
            </span>
            <span class="info-item">
              <i class="el-icon-odometer"></i>
              {{ log.voltage }}V
            </span>
            <span class="info-item">
              {{ log.sn }}
              <dict-tag :options="dict.type.device_status" :value="log.status" size="mini"/>
            </span>
          </div>
        </div>
        <el-empty v-if="!locationLogList || locationLogList.length === 0" description="暂无数据" />
      </div>
    </el-col>
  </el-row>
</template>

<script>
import AreaMap from '@/views/bst/areaSub/components/AreaMap.vue';
import { listAreaSubByAreaId } from '@/api/bst/areaSub';
import { listAllLocation } from '@/api/bst/locationLog';
import { getArea } from '@/api/bst/area';
import { parseTime } from '@/utils/ruoyi.js';

export default {
  name: 'DeviceLocation',
  dicts: ['device_status', 'device_lock_status', 'device_quality'],
  components: {
    AreaMap
  },
  props: {
    query: {
      type: Object,
      default: () => ({})
    },
    areaId: {
      type: String,
      default: null,
    }
  },
  data() {
    return {
      area: {},
      areaSubList: [],
      locationLogList: [],
      queryParams: {
        timeRange: [parseTime(new Date(), '{y}-{m}-{d} 00:00:00'), parseTime(new Date(), '{y}-{m}-{d} 23:59:59')]
      },  
      loading: false,
    }
  },
  created() {
    this.getArea();
    this.getAreaSubList();
    this.getLocationLogList();
  },
  methods: {
    // 获取运营区域
    getArea() {
      if (!this.areaId) {
        this.area = {};
        this.areaSubList = [];
        return;
      }
      getArea(this.areaId).then(res => {
        this.area = res.data;
        this.getAreaSubList();
      });
    },
    // 获取子区域列表
    getAreaSubList() {  
      listAreaSubByAreaId({ areaId: this.areaId }).then(res => {
        this.areaSubList = res.data;
      });
    },
    // 获取定位日志列表
    getLocationLogList() {
      Object.assign(this.queryParams, this.query);
      this.loading = true;  
      listAllLocation(this.queryParams).then(res => {
        this.locationLogList = res.data;
        if (this.$refs.map) {
          this.$nextTick(() => {
            this.$refs.map.initPlayback();
          });
        }
      }).finally(() => {
        this.loading = false;
      });
    },
    // 点击日志
    handleLogClick(log, index) {
      if (this.$refs.map) {
        this.$nextTick(() => {
          // 地图播放到指定日志
          this.$refs.map.handleSliderChange(index);
        });
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.location-log-list {
  margin-top: 10px;
  height: calc(500px - 40px);
  overflow-y: auto;
  border: 1px solid #EBEEF5;
  border-radius: 4px;

  .log-item {
    padding: 8px 12px;
    border-bottom: 1px solid #EBEEF5;
    cursor: pointer;
    transition: all 0.3s;

    &:hover {
      background-color: #F5F7FA;
    }

    .log-time {
      font-size: 12px;
      color: #909399;
      margin-bottom: 4px;
    }

    .log-info {
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
      font-size: 12px;

      .info-item {
        color: #606266;
        display: inline-flex;
        align-items: center;
        gap: 4px;

        i {
          font-size: 14px;
        }
      }
    }
  }

  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-thumb {
    background: #C0C4CC;
    border-radius: 3px;
  }

  &::-webkit-scrollbar-track {
    background: #F5F7FA;
  }
}
</style>