<template>
  <el-row :gutter="10" v-loading="loading">
    <el-col :span="18">
      <area-map
        ref="map"
        style="height: 700px"
        :area="area"
        :area-sub-list="areaSubList"
        :location-log-list="locationLogList"
        :enable-edit="false"
      />
    </el-col>
    <el-col :span="6">
      <el-date-picker
        v-if="queryParams.timeRange != null && queryParams.timeRange[0] != null && queryParams.timeRange[1] != null"
        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">
        <el-collapse v-model="activeGroup" accordion>
          <el-collapse-item
            v-for="(group, groupIndex) in groupedLocationLogs"
            :key="groupIndex"
            :name="groupIndex"
          >
            <template slot="title">
              <div class="group-header">
                <span class="group-time">{{ group.startTime }} - {{ group.endTime }}</span>
                <dict-tag :options="dict.type.device_status" :value="group.status" size="mini"/>
                <span class="group-count">({{ group.logs.length }}条)</span>
              </div>
            </template>
            <div class="group-content">
              <div
                v-for="(log, logIndex) in group.logs"
                :key="logIndex"
                @click="handleLogClick(log, getLogIndex(groupIndex, logIndex))"
                class="log-item"
              >
                <div class="log-time">
                  <dict-tag :options="dict.type.device_status" :value="log.status" size="mini"/>
                  {{ log.at }}
                </div>
                <div class="log-info">
                  <span class="info-item">
                    <i class="el-icon-location"></i>
                    {{ log.longitude.toFixed(8) }}, {{ log.latitude.toFixed(8) }}
                  </span>
                  <span class="info-item">
                    <i class="el-icon-odometer"></i>
                    {{ log.voltage | fix2 }}V
                  </span>
                  <span class="info-item">{{ log.sn }}</span>
                </div>
              </div>
            </div>
          </el-collapse-item>
        </el-collapse>
        <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,
      activeGroup: '', // 当前激活的分组
    }
  },
  computed: {
    groupedLocationLogs() {
      if (!this.locationLogList || this.locationLogList.length === 0) {
        return [];
      }

      const groups = [];
      let currentGroup = {
        status: this.locationLogList[0].status,
        logs: [this.locationLogList[0]],
        startTime: this.locationLogList[0].at,
        endTime: this.locationLogList[0].at
      };

      for (let i = 1; i < this.locationLogList.length; i++) {
        const currentLog = this.locationLogList[i];
        if (currentLog.status === currentGroup.status) {
          currentGroup.logs.push(currentLog);
          currentGroup.endTime = currentLog.at;
        } else {
          groups.push(currentGroup);
          currentGroup = {
            status: currentLog.status,
            logs: [currentLog],
            startTime: currentLog.at,
            endTime: currentLog.at
          };
        }
      }
      groups.push(currentGroup);
      return groups;
    }
  },
  created() {
    this.getArea();
    this.getAreaSubList();
    this.getLocationLogList();
  },
  methods: {
    // 获取运营区域
    getArea() {
      if (this.areaId == null) {
        this.area = {};
        return;
      }
      getArea(this.areaId).then(res => {
        this.area = res.data;
      });
    },
    // 获取子区域列表
    getAreaSubList() {
      if (this.areaId == null) {
        this.areaSubList = [];
        return;
      }
      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);
        });
      }
    },
    getLogIndex(groupIndex, logIndex) {
      let totalIndex = 0;
      for (let i = 0; i < groupIndex; i++) {
        totalIndex += this.groupedLocationLogs[i].logs.length;
      }
      return totalIndex + logIndex;
    },
    getStatusType(status) {
      const statusMap = {
        0: 'info',    // 离线
        1: 'success', // 在线
        2: 'warning', // 骑行中
        3: 'danger'   // 故障
      };
      return statusMap[status] || 'info';
    }
  }
}
</script>

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

  .group-header {
    font-size: 12px;
    padding-left: 8px;

    .group-time {
      color: #606266;
      margin-right: 8px;
    }

    .group-count {
      margin-left: 8px;
      color: #909399;
    }
  }

  .log-item {
    padding: 8px 12px;
    cursor: pointer;
    transition: all 0.3s;

    &:not(:last-child) {
      border-bottom: 1px solid #EBEEF5;
    }

    &: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;
        }
      }
    }
  }

  .el-collapse {
    border-top: none;
    border-bottom: none;
  }

  .el-collapse-item__header {
    padding: 8px 12px;
    background-color: #F5F7FA;
    border-bottom: 1px solid #EBEEF5;
    font-size: 12px;

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

  .el-collapse-item__content {
    padding: 0;
  }

  .el-collapse-item__wrap {
    border-bottom: none;
  }

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

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

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