<template>

  <div class="event-list">
  
    <div class="title">
      <div class="name">事件检测列表</div>
      <!-- <Icon @click="onClose" class="icon" icon="ep:close" />  -->
    </div>
  
    <div class="search">
  
      <div class="inp-item">
        <div class="inp-name">车牌号：</div>
        <div class="inp-content">
          <el-input v-model="params.eventCarPlate" placeholder="请输入..." @change='getEvents(false)' />
        </div>
      </div>
  
      <el-button @click="goReset" class="reset">重置</el-button>
  
    </div>
  
    <div class="list">
  
      <div class="row header">
        <div class="col-1">
          <div class="inner">
            <Filter v-bind="{ options: useeventtypes, defaultText: '事件类型' }" v-model="params.type" @okSelect='getEvents(false)' />
          </div>
        </div>
        <div class="col-2">发生时间</div>
        <div class="col-3">
          <div class="inner">
            <Filter v-bind="{ options: useDirections, defaultText: '方向' }" v-model="params.direct" @okSelect='getEvents(false)' />
          </div>
        </div>
      </div>
  
      <div
        v-for="(item, x) in eventList"
        :key="item.code"
        :class="['row', { 'stripe-row': x % 2 === 0 }]"
        @click="goPointer(item)"
      >
        <div :class="['col-1', { act: locatId === item.id }]">
          <div class="inner">
            <div :class="['event-type',  typeCompute(item).classStr]">
              {{ typeCompute(item).nameStr }}
            </div>
          </div>
        </div>
        <div class="col-2">
          <div class="inner">{{ item.eventStartTime.slice(5, -3) }}</div>
        </div>
        <div class="col-3">
          <div class="inner">{{ directMap[item.tunnelId] }}</div>
        </div>
      </div>

    </div>

    <div class="pages">
      
      <div class="sum">共 {{ params.total }} 条</div>

      <el-pagination
        hide-on-single-page
        v-model:currentPage="params.pageNo"
        :page-size="params.pageSize"
        layout="prev, pager, next"
        :total="params.total"
        :pager-count="3"
        class="car-pages"
        @change="getEvents"
      />

    </div>
  
  </div>
  
  </template>
  
  <script lang='ts' setup>
  
  import Filter from '../Coms/Filter.vue'
  import { getEventList } from '@/api/event'
  import { remainCars } from '@/api/home'
  import { EVENT_TYPES, ASYNC_DIRECTIONS } from '@/config/dict'
  import { bus, trackCar, locatId } from '@/views/3DModel/state'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

  const emits = defineEmits(['list-close', 'mode-event'])

  const viewAct = defineModel()
  const eventList = ref([])
  
  const params = reactive({
    keyword: '', // 关键词
    eventStatusSearch: 0,
    direct: '', // 方向
    type: '', // 事件类型
    direction: [], // 方向
    eventType: [], // 事件类型
    pageNo: 1,
    pageSize: 10,
    total: 0
  })
  
  
  const useDirections = computed(() => {
    return ASYNC_DIRECTIONS().map(({ name, seKey }) => {
      return { name, type: seKey }
    })
  })
   const useeventtypes = computed(() => {
    return EVENT_TYPES.map(({ name, key }) => {
      return { name, type: key }
    })
  })
  const EVENT_MAP = Object.fromEntries( getIntDictOptions(DICT_TYPE.EVENT_TYPES).map(item => {
    return [item.value, item.label]
  }))
  
  const directMap = computed(() => {
    return Object.fromEntries(ASYNC_DIRECTIONS().map(item => {
      return [item.seKey, item.name]
    }))
  })
  
  // 获取事件列表
  const getEvents = async (x?: number) => {

    params.pageNo = x || 1

    const directMap = {
      xl_tunnel_up: '上行',
      xl_tunnel_down: '下行'
    }
    if (params.direct) {
      params.direction = [directMap[params.direct]]
    } else {
      params.direction = []
    }

    if (params.type) {
      params.eventType = [params.type]
    } else {
      params.eventType = []
    }
  
    const res = await getEventList(params)
    eventList.value = res.list
    params.total = res.total

  
  }


  // 状态计算
  const typeCompute = (row) => {

    const { eventCarPlate, eventType } = row

    if(+eventType === 6) {
      return {
        classStr: 'type-1',
        nameStr: EVENT_MAP[eventType]
      }
    }

    if(eventCarPlate.length > 1) {
      return {
        classStr: 'type-2',
        nameStr: EVENT_MAP[eventType] + '(多车)'
      }
    }

    return {
      classStr: 'type-3',
      nameStr: EVENT_MAP[eventType] + '(单车)'
    }

  }
  
  // 关闭列表
  const onClose = () => {
    emits('list-close')
  }
  
  // 事件定位
  const goPointer = async (evt) => {

    const { milestone, tunnelId } = evt

    const direct = ['xl_tunnel_up', 'xl_tunnel_down'].findIndex(item => item === tunnelId)
    
    if (!milestone) {
      return ElMessage.warning('数据错误！')
    }

    viewAct.value = 3
    await nextTick()
    bus.emit('event-locate', evt)
  
  }
  
  // 条件重置
  const goReset = () => {
    params.direct = ''
    params.type = ''
    params.eventCarPlate = ''
    params.vehicleType = ''
    params.pageNo = 1
    getEvents()

  }
  
  onMounted(() => {
    getEvents()
  })

  </script>
  
  <style lang='scss' scoped>
  
  .event-list {
    position: absolute;
    right: 0;
    top: 0;
    width: 360px;
    background: rgba(14, 42, 85, 0.8);
    padding: 15px;
    z-index: 9;
    .title {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .name {
        font-size: 16px;
        font-weight: 600;
        color: white;
      }
      .icon {
        color: white;
        cursor: pointer;
      }
    }
    .search {
      display: flex;
      justify-content: space-between;
      margin: 15px 0;
      .inp-item {
        flex-grow: 1;
        display: flex;
        align-items: center;
        color: white;
        font-size: 13px;
        .inp-content {
          flex-grow: 1;
          :deep(.el-input__wrappe) {
            width: 100% !important;
          }
        }
      }
      .reset {
        margin-left: 10px;
        font-size: 13px;
      }
    }
    .list {
      height: calc(100vh - 100px);
      .row {
        display: flex;
        min-height: 40px;
        color: white;
        font-size: 13px;
        justify-content: space-between;
        &.stripe-row {
          background: rgba(69, 116, 182, 0.2);
          border-radius: 4px;
        }
  
        &:not(.header) {
          cursor: pointer;
        }
  
        .col-1,
        .col-2,
        .col-3{
          line-height: 18px;
          position: relative;
          padding: 12px 10px;
          box-sizing: border-box;
          flex-shrink: 0;
          display: flex;
          flex-direction: column;
          justify-content: center;
        }
  
        .col-1{
          .event-type {
            display: inline-block;
            padding: 2px 6px;
            border-radius: 4px;
            border: 1px solid rgba(255, 255, 255, 0.5);
            box-sizing: border-box;
            color: white;
            &.type-2{
              background: #F56C6C;
            }
            
            &.type-1 {
              background: #FA8C16;
            }
            &.type-3 {
              background: #507AFC;
            }
          }
          &.act::before {
            content: "";
            width: 6px;
            height: 6px;
            border-radius: 6px;
            background: #00DDFF;
            position: absolute;
            left: -2px;
            top: 50%;
            margin-top: -3px;
          }
        }
      }
    }

    .pages {
      margin-top: 20px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      color: white;
      .sum {
        // margin-right: 10px;
        font-size: 14px;
      }
    }
  
  }
  
  </style>

<style lang="scss">

.el-pagination {
  &.car-pages {
    --el-pagination-bg-color: rgba(32, 58, 96, 0.6);
    --el-pagination-button-color: white;
    --el-pagination-button-disabled-bg-color: rgba(32, 58, 96, 0.4);
    --el-pagination-button-disabled-color: rgba(255, 255, 255, 0.5);
    .number {
      margin: 0 4px;
      &.is-active {
        background: #007DFA;
        color: white;
      }
    }
  }
}

</style>