<template>
  <div class="curb-spot-city">
    <div class="show-list">
      <a-spin :spinning="spinning">
        <MapList
          :infoScreenData="infoScreenData"
          :cityType="cityType"
          :pageNo="pageNo"
          :pageSize="pageSize"
          :total="total"
          :infoDataList="infoDataList"
          :municipalAreaOptions="municipalAreaOptions"
          :countiesAreaOptions="countiesAreaOptions"
          @infoDataListSort="infoDataListSort"
          @changeInfoPage="changeInfoPage"
          @mapListScreenChange="mapListScreenChange"
          @changeTask="changeTask"
          @collectChange="collectChange"
          @changeSpin="changeSpin"
        />
      </a-spin>
    </div>
    <div class="map-box-div">
      <MapboxMap
        :mapConfig="mapConfig"
        @handlerDrawComplete="handlerDrawComplete"
        @mapOnLoad="onMapboxLoad"
        ref="MapboxComponent"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
  // import AuthImage from '@/components/Upload/src/components/AuthImage.vue'
  import { ref, onMounted, onUnmounted, watch, defineAsyncComponent } from 'vue';
  import mapboxgl, { Map } from 'mapbox-gl';
  import { MapboxConfig, MapboxDefaultStyle } from '@/components/MapboxMaps/src/config.ts';
  import MapList from './MapList/index.vue';
  import { getLoadDroneCaseInfoDetail } from '@/api/tiankongdi/index.ts';
  import { getGeom, getConfig } from '@/api/sys/layerManagement';
  import { useMessage } from '@/hooks/web/useMessage';

  const { createMessage } = useMessage();

  const MapboxMap = defineAsyncComponent(() => import('@/components/MapboxMaps/MapComponent.vue'));
  const MapboxComponent = ref();
  const mapConfig = ref({ isShowMap: false });

  function onMapboxLoad(): void {
    getConfig({ code: 'mapsetting' }).then((res) => {
      mapConfig.value = JSON.parse(res.codeValue);
    });
  }


  const spinning = ref(false);

  const year = ref<number>();
  const batch = ref();
  const infoDataList = ref<any>([]);
  const infoScreenData = ref({
    year: '',
    batch: '',
    mapNo: '',
    countyId: '',
    streetId: '',
    mapType: '',
    illegalType: '',
    measure: '',
    mapStatus: '',
    markType: '',
    sort: '',
    order: '',
    type: '', // 收藏
  });
  const pageNo = ref(1);
  const pageSize = ref(10);
  const total = ref(0);
  const municipalAreaOptions = ref([{ label: '全部', value: '' }]);
  const countiesAreaOptions = ref([{ label: '全部', value: '' }]);
  const cityType = ref();
  const mapListScreenChange = (value, type) => {
    switch (type) {
      case 'year':
        infoScreenData.value.year = value;
        break;
      case 'batch':
        infoScreenData.value.batch = value;
        break;
      case 'mapNo':
        infoScreenData.value.mapNo = value;
        break;
      case 'countyId':
        infoScreenData.value.countyId = value;
        break;
      case 'streetId':
        infoScreenData.value.streetId = value;
        break;
      case 'mapType':
        infoScreenData.value.mapType = value;
        if (value !== 1) {
          infoScreenData.value.illegalType = '';
          infoScreenData.value.measure = '';
        }
        break;
      case 'illegalType':
        infoScreenData.value.illegalType = value;
        if (value !== 0) {
          infoScreenData.value.measure = '';
        }
        break;
      case 'measure':
        infoScreenData.value.measure = value;
        break;
      case 'mapStatus':
        infoScreenData.value.mapStatus = value;
        break;
      case 'markType':
        infoScreenData.value.markType = value;
        break;
    }
  };

  const getParams = () => {
    let result = {};
    let params = {
      year: infoScreenData.value.year,
      // batch: batch.value,
      countyid: infoScreenData.value.countyId,
      streetid: infoScreenData.value.streetId,
      geomid: infoScreenData.value.mapNo,
      is_illegal: infoScreenData.value.mapType,
      weifaleixing: infoScreenData.value.illegalType,
      measure_name: infoScreenData.value.measure,
      handle_status_id: infoScreenData.value.mapStatus,
      is_build_complete: infoScreenData.value.markType,
      areaid: cityType.value.areaid,
      level: cityType.value.level,
      page: pageNo.value,
      limit: pageSize.value,
      sort: infoScreenData.value.sort,
      order: infoScreenData.value.order,
      type: infoScreenData.value.type,
    };
    Object.keys(params).forEach((key) => {
      if (params[key] !== '' && params[key] !== null) {
        result[key] = params[key];
      }
    });
    return result;
  };
  watch(
    () => year.value,
    (newVal) => {
      infoScreenData.value.year = newVal;
    },
  );
  watch(
    () => batch.value,
    (newVal) => {
      infoScreenData.value.batch = newVal;
    },
  );
  let map: Map;
  onMounted(() => {
    mapboxgl.accessToken = MapboxConfig.ACCESS_TOKEN;
    // map = initMap();
  });
  onUnmounted(() => {
    map && map.remove();
  });

  const changeInfoPage = (page, limit) => {
    pageNo.value = page;
    pageSize.value = limit;
  };
  const collectChange = (value) => {
    pageNo.value = 1;
    infoScreenData.value.type = value;
  };

  function changeTask(val) {
    let getGeomPrams = {
      TableName: 'drone_shp_data ',
      FieldName: 'gid',
      FieldValue: val?.split(','),
      page: 1,
      limit: 999,
      key: null,
    };
    if (val) {
      getGeom(getGeomPrams).then((res) => {
        let geoms = [];
        if (res) {
          if (res.items?.length > 0) {
            res.items.forEach((item, index) => {
              let geom = {
                key: item.gid,
                mapgeom: item.geometry,
              };
              geoms.push(geom);
            });
          }
          // MapboxComponent.value.handlerDraw(status,mapgemoList.value, false);
          MapboxComponent.value.handlerDraw('Details', geoms, false);
        } else {
          createMessage.error('当前数据没有图斑！');
        }
      });
    } else {
      createMessage.error('当前数据没有图斑！');
    }
  }
  const infoDataListSort = (type, order) => {
    switch (order) {
      case 0:
        infoScreenData.value.sort = '';
        infoScreenData.value.order = '';
        break;
      case 1:
        infoScreenData.value.sort = type;
        infoScreenData.value.order = 'asc';
        break;
      case 2:
        infoScreenData.value.sort = type;
        infoScreenData.value.order = 'desc';
        break;
    }
    getLoadDroneCaseInfoDetail(getParams()).then((res) => {
      total.value = res.total;
      infoDataList.value = res.items;
      pageNo.value = 1;
    });
  };
  const changeSpin = (val) => {
    spinning.value = val;
  };
</script>

<style lang="scss" scoped>
  .curb-spot-city {
    height: 100%;
    display: flex;
    .show-list {
      width: 590px;
      :deep(.ant-spin-nested-loading) {
        height: 100%;
      }
      :deep(.ant-spin-container) {
        height: 100%;
      }
    }
    .map-box-div {
      width: calc(100% - 590px);
    }
  }
</style>
