import { onMounted, defineComponent,onUnmounted } from 'vue';
import MessageCard from './MessageCard';
import './index.scss';
import {
  useCommonStore,
  useIncidentStore,
  useMarkerStore,
} from '@/store';
import { IncidentListResponse } from '@/api/incident';
import { useRouter } from 'vue-router';

export default defineComponent({
  name: 'HomePage',
  provide: {},
  setup() {
    const store = useIncidentStore();
    const commonStore = useCommonStore();
    const router = useRouter();
    const markerStore = useMarkerStore();
    var ss = null;

    onMounted(async () => {
      commonStore.getGlobalDict('zhdd_incident_level');
      commonStore.getGlobalDict('zhdd_incident_type');
      commonStore.getGlobalDict('zhdd_incident_source');
      commonStore.getGlobalDict('zhdd_car_type');

      await store.getIncidentList({
        // 事件等级1,2的事件
        level: 1,
      })

      ss = setInterval(async () => { 
         if ((store.incidents.data ?? []).length > 0) {
           var item = store.incidents.data![0];
           markerStore.currentIncident = item;
           item.id && (await store.getIncidentItem(item.id));
           router.push(`/incidentDetail?id=${item.id}`);
         }
      },1000)
      
      
    
      
    });

    onUnmounted(() => { 
      clearInterval(ss);
    });
    return () => (
      <div class="home-page-container">
        <MessageCard />
      </div>
    );
  },
});
