<template>
  <div class="detail-container">
    <div class="map-container" v-if="!props.hiddenInfoMap">
      <MapboxMap
        :caseno="props.showInfoData.case_no"
        :countyname="props.showInfoData.countyname"
        :imageList="imageList"
        :geomsList="geomsList"
        :mapConfig="mapConfig"
        @handlerDrawComplete="handlerDrawComplete"
        @mapOnLoad="onMapboxLoad"
        ref="MapboxComponent"
      />
    </div>
    <div class="info-container" id="info-container">
      <a-tabs v-model:activeKey="activeKey">
        <a-tab-pane key="1" tab="项目信息">
          <a-descriptions
            :column="2"
            bordered
            :contentStyle="{
              'text-align': 'center',
              'min-width': '250px',
              'word-break': 'break-all',
            }"
          >
            <a-descriptions-item label="项目编号">{{
              props.showInfoData.xiangmu_no
            }}</a-descriptions-item>
            <a-descriptions-item label="项目名称">{{
              props.showInfoData.xiangmu_name
            }}</a-descriptions-item>
            <a-descriptions-item label="乡镇">{{
              props.showInfoData.streetname
            }}</a-descriptions-item>
            <a-descriptions-item label="村庄">{{
              props.showInfoData.communityname
            }}</a-descriptions-item>
            <a-descriptions-item label="权利人">{{
              props.showInfoData.quanliren
            }}</a-descriptions-item>
            <a-descriptions-item label="行政区划">{{
              props.showInfoData.xingzhengquhua
            }}</a-descriptions-item>
            <a-descriptions-item label="备案编号">{{
              props.showInfoData.beian_no
            }}</a-descriptions-item>
            <a-descriptions-item label="项目开始时间">{{
              props.showInfoData.start_time
            }}</a-descriptions-item>
            <a-descriptions-item label="项目结束时间">{{
              props.showInfoData.end_time
            }}</a-descriptions-item>
            <a-descriptions-item label="项目当前用途">{{
              props.showInfoData.xiangmu_yt
            }}</a-descriptions-item>
            <a-descriptions-item label="设施农业申请用地面积(公顷)">{{
              props.showInfoData.shenqing_area
            }}</a-descriptions-item>
            <a-descriptions-item label="生产设施用地(公顷)">{{
              props.showInfoData.shengchan_area
            }}</a-descriptions-item>
            <a-descriptions-item label="辅助设施用地(公顷)">{{
              props.showInfoData.fuzhu_area
            }}</a-descriptions-item>
            <a-descriptions-item label="下发时间">{{
              props.showInfoData.xiafatime
            }}</a-descriptions-item>
          </a-descriptions>
        </a-tab-pane>
        <a-tab-pane key="2" tab="开工核查">
          <a-descriptions
            :column="2"
            bordered
            :contentStyle="{
              'text-align': 'center',
              'min-width': '250px',
              'word-break': 'break-all',
            }"
          >
            <a-descriptions-item label="是否存在问题">{{
              props.showInfoData.iswenti_kg
            }}</a-descriptions-item>
            <a-descriptions-item label="问题类型">{{
              props.showInfoData.wentileixing_kg
            }}</a-descriptions-item>
            <a-descriptions-item label="其他情况说明">{{
              props.showInfoData.qtqksm_kg
            }}</a-descriptions-item>
            <a-descriptions-item label="整改方式">{{
              props.showInfoData.zgfs_kg
            }}</a-descriptions-item>
            <a-descriptions-item label="整改期限">{{
              props.showInfoData.zgqx_kg
            }}</a-descriptions-item>
            <a-descriptions-item label="现场外景照片">{{
              props.showInfoData.wjzp_kg
            }}</a-descriptions-item>
            <a-descriptions-item label="现场内景照片">{{
              props.showInfoData.njzp_kg
            }}</a-descriptions-item>
            <a-descriptions-item label="附件">{{
              props.showInfoData.fujian_kg
            }}</a-descriptions-item>
            <a-descriptions-item label="核查人员">{{
              props.showInfoData.hechauser_kg
            }}</a-descriptions-item>
            <a-descriptions-item label="核查事件">{{
              props.showInfoData.hechatime_kg
            }}</a-descriptions-item>
          </a-descriptions>
        </a-tab-pane>
        <a-tab-pane key="3" tab="整改情况">
          <a-descriptions
            :column="2"
            bordered
            :contentStyle="{
              'text-align': 'center',
              'min-width': '250px',
              'word-break': 'break-all',
            }"
          >
          <a-descriptions-item label="整改方式">{{
            props.showInfoData.zgfs_jzzg
          }}</a-descriptions-item>
          <a-descriptions-item label="现场外景照片">{{
            props.showInfoData.wjzp_jzzg
          }}</a-descriptions-item>
          <a-descriptions-item label="现场内景照片">{{
            props.showInfoData.njzp_jzzg
          }}</a-descriptions-item>
          <a-descriptions-item label="附件">{{
            props.showInfoData.fujian_jzzg
          }}</a-descriptions-item>
          <a-descriptions-item label="办理人员">{{
            props.showInfoData.hechauser_jzzg
          }}</a-descriptions-item>
          <a-descriptions-item label="办理时间">{{
            props.showInfoData.hechatime_jzzg
          }}</a-descriptions-item>
          </a-descriptions>
        </a-tab-pane>
        <a-tab-pane key="4" tab="建中核查">
          <a-descriptions
            :column="2"
            bordered
            :contentStyle="{
              'text-align': 'center',
              'min-width': '250px',
              'word-break': 'break-all',
            }"
          >
          <a-descriptions-item label="是否存在问题">{{
            props.showInfoData.iswenti_wg
          }}</a-descriptions-item>
          <a-descriptions-item label="问题类型">{{
            props.showInfoData.iswenti_wg
          }}</a-descriptions-item>
          <a-descriptions-item label="其他情况说明">{{
            props.showInfoData.qtqksm_wg
          }}</a-descriptions-item>
          <a-descriptions-item label="整改方式">{{
            props.showInfoData.zgfs_wg
          }}</a-descriptions-item>
          <a-descriptions-item label="整改期限">{{
            props.showInfoData.zgqx_wg
          }}</a-descriptions-item>
          <a-descriptions-item label="现场外景照片">{{
            props.showInfoData.wjzp_wg
          }}</a-descriptions-item>
          <a-descriptions-item label="现场内景照片">{{
            props.showInfoData.njzp_wg
          }}</a-descriptions-item>
          <a-descriptions-item label="附件">{{
            props.showInfoData.fujian_wg
          }}</a-descriptions-item>
          <a-descriptions-item label="核查人员">{{
            props.showInfoData.hechauser_wg
          }}</a-descriptions-item>
          <a-descriptions-item label="核查时间">{{
            props.showInfoData.hechatime_wg
          }}</a-descriptions-item>
          </a-descriptions>
        </a-tab-pane>
        <a-tab-pane key="5" tab="整改情况">
          <a-descriptions
            :column="2"
            bordered
            :contentStyle="{
              'text-align': 'center',
              'min-width': '250px',
              'word-break': 'break-all',
            }"
          >
            <a-descriptions-item label="整改方式">{{
              props.showInfoData.zgfs_wgzg
            }}</a-descriptions-item>
            <a-descriptions-item label="现场外景照片">{{
              props.showInfoData.wjzp_wgzg
            }}</a-descriptions-item>
            <a-descriptions-item label="现场内景照片">{{
              props.showInfoData.njzp_wgzg
            }}</a-descriptions-item>
            <a-descriptions-item label="附件">{{
              props.showInfoData.fujian_wgzg
            }}</a-descriptions-item>
            <a-descriptions-item label="办理人员">{{
              props.showInfoData.hechauser_wgzg
            }}</a-descriptions-item>
            <a-descriptions-item label="办理时间">{{
              props.showInfoData.hechatime_wgzg
            }}</a-descriptions-item>
          </a-descriptions>
        </a-tab-pane>
        <a-tab-pane key="6" tab="完工核查">
          <a-descriptions
            :column="2"
            bordered
            :contentStyle="{
              'text-align': 'center',
              'min-width': '250px',
              'word-break': 'break-all',
            }"
          >
            <a-descriptions-item label="是否存在问题">{{
              props.showInfoData.iswenti_wg
            }}</a-descriptions-item>
            <a-descriptions-item label="问题类型">{{
              props.showInfoData.wentileixing_wg
            }}</a-descriptions-item>
            <a-descriptions-item label="其他情况说明">{{
              props.showInfoData.qtqksm_wg
            }}</a-descriptions-item>
            <a-descriptions-item label="整改方式">{{
              props.showInfoData.zgfs_wg
            }}</a-descriptions-item>
            <a-descriptions-item label="整改期限">{{
              props.showInfoData.zgqx_wg
            }}</a-descriptions-item>
            <a-descriptions-item label="现场外景照片">{{
              props.showInfoData.wjzp_wg
            }}</a-descriptions-item>
            <a-descriptions-item label="现场内景照片">{{
              props.showInfoData.njzp_wg
            }}</a-descriptions-item>
            <a-descriptions-item label="附件">{{
              props.showInfoData.fujian_wg
            }}</a-descriptions-item>
            <a-descriptions-item label="核查人员">{{
              props.showInfoData.hechauser_wg
            }}</a-descriptions-item>
            <a-descriptions-item label="核查时间">{{
              props.showInfoData.hechatime_wg
            }}</a-descriptions-item>
          </a-descriptions>
        </a-tab-pane>
        <a-tab-pane key="7" tab="整改情况">
          <a-descriptions
            :column="2"
            bordered
            :contentStyle="{
              'text-align': 'center',
              'min-width': '250px',
              'word-break': 'break-all',
            }"
          >
            <a-descriptions-item label="整改方式">{{
              props.showInfoData.zgfs_wgzg
            }}</a-descriptions-item>
            <a-descriptions-item label="现场外景照片">{{
              props.showInfoData.wjzp_wgzg
            }}</a-descriptions-item>
            <a-descriptions-item label="现场内景照片">{{
              props.showInfoData.njzp_wgzg
            }}</a-descriptions-item>
            <a-descriptions-item label="附件">{{
              props.showInfoData.fujian_wgzg
            }}</a-descriptions-item>
            <a-descriptions-item label="办理人员">{{
              props.showInfoData.hechauser_wgzg
            }}</a-descriptions-item>
            <a-descriptions-item label="办理时间">{{
              props.showInfoData.hechatime_wgzg
            }}</a-descriptions-item>
          </a-descriptions>
        </a-tab-pane>
        <a-tab-pane key="8" tab="日常监管">
          <a-descriptions
            :column="2"
            bordered
            :contentStyle="{
              'text-align': 'center',
              'min-width': '250px',
              'word-break': 'break-all',
            }"
          >
            <a-descriptions-item label="是否存在问题">{{
              props.showInfoData.iswenti_rc
            }}</a-descriptions-item>
            <a-descriptions-item label="问题类型">{{
              props.showInfoData.wentileixing_rc
            }}</a-descriptions-item>
            <a-descriptions-item label="其他情况说明">{{
              props.showInfoData.qtqksm_rc
            }}</a-descriptions-item>
            <a-descriptions-item label="整改方式">{{
              props.showInfoData.zgfs_rc
            }}</a-descriptions-item>
            <a-descriptions-item label="整改期限">{{
              props.showInfoData.zgqx_rc
            }}</a-descriptions-item>
            <a-descriptions-item label="现场外景照片">{{
              props.showInfoData.wjzp_rc
            }}</a-descriptions-item>
            <a-descriptions-item label="现场内景照片">{{
              props.showInfoData.njzp_rc
            }}</a-descriptions-item>
            <a-descriptions-item label="附件">{{
              props.showInfoData.fujian_rc
            }}</a-descriptions-item>
            <a-descriptions-item label="核查人员">{{
              props.showInfoData.hechauser_rc
            }}</a-descriptions-item>
            <a-descriptions-item label="核查时间">{{
              props.showInfoData.hechatime_rc
            }}</a-descriptions-item>
          </a-descriptions>
        </a-tab-pane>
        <a-tab-pane key="9" tab="项目整改">
          <a-descriptions
            :column="2"
            bordered
            :contentStyle="{
              'text-align': 'center',
              'min-width': '250px',
              'word-break': 'break-all',
            }"
          >
            <a-descriptions-item label="整改方式">{{
              props.showInfoData.zgfs_rczg
            }}</a-descriptions-item>
            <a-descriptions-item label="现场外景照片">{{
              props.showInfoData.wjzp_rczg
            }}</a-descriptions-item>
            <a-descriptions-item label="现场内景照片">{{
              props.showInfoData.njzp_rczg
            }}</a-descriptions-item>
            <a-descriptions-item label="附件">{{
              props.showInfoData.fujian_rczg
            }}</a-descriptions-item>
            <a-descriptions-item label="办理人员">{{
              props.showInfoData.hechauser_rczg
            }}</a-descriptions-item>
            <a-descriptions-item label="办理时间">{{
              props.showInfoData.hechatime_rczg
            }}</a-descriptions-item>
          </a-descriptions>
        </a-tab-pane>
        <a-tab-pane key="10" tab="现场复核">
          <a-descriptions
            :column="2"
            bordered
            :contentStyle="{
              'text-align': 'center',
              'min-width': '250px',
              'word-break': 'break-all',
            }"
          >
            <a-descriptions-item label="是否存在问题">{{
              props.showInfoData.iswenti_rcfh
            }}</a-descriptions-item>
            <a-descriptions-item label="现场外景照片">{{
              props.showInfoData.iswenti_rcfh
            }}</a-descriptions-item>
            <a-descriptions-item label="现场内景照片">{{
              props.showInfoData.njzp_rcfh
            }}</a-descriptions-item>
            <a-descriptions-item label="附件">{{
              props.showInfoData.fujian_rcfh
            }}</a-descriptions-item>
            <a-descriptions-item label="复核人员">{{
              props.showInfoData.hechauser_rcfh
            }}</a-descriptions-item>
            <a-descriptions-item label="复核时间">{{
              props.showInfoData.hechatime_rcfh
            }}</a-descriptions-item>
          </a-descriptions>
        </a-tab-pane>
      </a-tabs>
    </div>

      <!-- File Preview && Download Start -->
      <a-modal
        v-model:open="previewFileModalVisible"
        style="width: 100vw"
        title="文件预览"
        wrap-class-name="full-modal"
      >
        <FilePreview v-if="previewFileModalVisible" :fileUrl="previewFileUrl"></FilePreview>
        <template #footer>
          <a-button key="cancel" @click="handleCancelPreviewFile">取消</a-button>
          <a-button key="confirm" type="primary" @click="handlerDownloadFle">下载</a-button>
        </template>
      </a-modal>
    <!--File Preview && Download End -->
  </div>
</template>

<script setup lang="ts">
  import { defineProps, ref, computed, onBeforeMount, watch } from 'vue';
  import MapboxMap from '@/components/MapboxMaps/MapComponent.vue';
  import { getConfig } from '@/api/sys/layerManagement';
  import { getGeom } from '@/api/sys/layerManagement';
  import { getLoadCaseImgList } from '@/api/tiankongdi';
  import { useMessage } from '@/hooks/web/useMessage';
  import axios from 'axios';
  const { createMessage } = useMessage();
  import Icon from '@/components/Icon/Icon.vue';
  import { getAppEnvConfig } from '@/utils/env';
  const { VITE_GLOB_INFO_IMAGE_URL } = getAppEnvConfig();

  import {
    mapTypeOptions,
    illegalTypeOptions,
    measureOptions,
    mapStatusOptions,
    markTypeOptions,
    illegalTypeList,
    resultOptions,
    qitaUseTOOptions,
  } from '@/utils/global';

  const MapboxComponent = ref();
  const mapConfig = ref({});
  const props = defineProps(['showInfoData','hiddenInfoMap']);
  const activeKey = ref('1');
  const geomsList = ref();
  const imageList = ref([]);

  console.log('props',props.showInfoData)
  watch(
    () => props.showInfoData,
    () => {
      getConfig({ code: 'mapsetting' }).then((res) => {
        mapConfig.value = JSON.parse(res.codeValue);
      });
      changeTask();
    },
  );
  async function getCaseImgList() {
    imageList.value = await getLoadCaseImgList({ caseid: props.showInfoData.id,category:'违法用地' });
    MapboxComponent.value.handlerLoadPictureAzimuth(imageList.value);

    // 匹配去除无效图片
    // let zhengshiImageList = [];
    // imageList.value?.forEach((item,index)=>{
    //   let obj = anjianzhaopianList.value?.find((it,idx)=>{
    //     return item.filePath == it;
    //   })
    //   if(obj){
    //     zhengshiImageList.push(imageList.value[index]);
    //   }
    // })
  }

  function handlerDealFileName(path) {
    const regex = /([^/\\]+)(?=\.[^/\\]*$|$)/;
    const matchStr = path.match(regex);
    if (matchStr?.length) {
      return matchStr[0];
    }
  }

  function handlerPreviewImage(index, url) {
    const regex = /([^/\\]+)(?=\.[^/\\]*$|$)/;
    const match = url.match(regex);
    if (match) {
      MapboxComponent.value.handlerCurrentImageChange(match[1]);
    }
  }

  const isInitImageLisener = ref<Boolean>(false);
  // 监听预览图片地址变化进行箭头切换
  function handlerImageChange(e): void {
    isInitImageLisener.value = false;
    if (e && !isInitImageLisener.value) {
      setTimeout(function () {
        const targetNode = document.getElementsByClassName('ant-image-preview-img');
        targetNode?.forEach((node,index)=>{
          let imageObserver = new MutationObserver((mutationsList)=>{
            for(const mutation of mutationsList){
              if (node.getAttribute(mutation.attributeName).match('http')){
                handlerPreviewImage(0, node.getAttribute(mutation.attributeName));
              }
            }
          })
          const config = { attributes: true };
          imageObserver.observe(node, config);
          isInitImageLisener.value = true;
        })
      }, 250);
    }
  }

  onBeforeMount(() => {});

  const anjianzhaopianList = computed(() => {
    getCaseImgList();
    return props.showInfoData.anjianzhaopian ? props.showInfoData.anjianzhaopian.split(',') : [];
  });
  const casepicList = computed(() => {
    return props.showInfoData.casepic ? props.showInfoData.casepic.split(',') : [];
  });
  const chaichufugenghoupicList = computed(() => {
    return props.showInfoData.chaichufugenghoupic
      ? props.showInfoData.chaichufugenghoupic.split(',')
      : [];
  });
  const bubanzhaopianList = computed(() => {
    return props.showInfoData.bubanzhaopian ? props.showInfoData.bubanzhaopian.split(',') : [];
  });
  const fujianList = computed(() => {
    return props.showInfoData.fujian ? props.showInfoData.fujian.split(',') : [];
  });
  const hefafujianList = computed(() => {
    return props.showInfoData.hefafujian ? props.showInfoData.hefafujian.split(',') : [];
  });
  const qitafujianList = computed(() => {
    return props.showInfoData.qitafujian ? props.showInfoData.qitafujian.split(',') : [];
  });
  const zhenggaifujianList = computed(() => {
    return props.showInfoData.zhenggaifujian ? props.showInfoData.zhenggaifujian.split(',') : [];
  });
  const yanshoubiaoList = computed(() => {
    return props.showInfoData.yanshoubiao ? props.showInfoData.yanshoubiao.split(',') : [];
  });
  const wrjffzhaopianList = computed(() => {
    return props.showInfoData.wrjffzhaopian ? props.showInfoData.wrjffzhaopian.split(',') : [];
  });
  const xchczhaopianList = computed(() => {
    return props.showInfoData.xchczhaopian ? props.showInfoData.xchczhaopian.split(',') : [];
  });
  function onMapboxLoad() {
    changeTask();
  }
  const getLabel = (type, value) => {
    let result: any[] = [];
    let label = '';
    switch (type) {
      case 'is_illegal':
        result = mapTypeOptions;
        break;
      case 'weifaleixing':
        result = illegalTypeList;
        break;
      case 'measure_name':
        result = measureOptions;
        break;
      case 'result_name':
        result = resultOptions;
        break;
      case 'qita_use_to':
        result = qitaUseTOOptions;
        break;
    }
    result.forEach((item) => {
      if (item.value == value) {
        label = item.label;
      }
    });
    return label;
  };
  async function changeTask() {
    let getGeomPrams = {
      TableName: 'drone_shp_data ',
      FieldName: 'gid',
      FieldValue: props.showInfoData.geomid?.split(','),
      page: 1,
      limit: 999,
      key: null,
    };
    if (props.showInfoData.geomid) {
      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);
              geomsList.value = geoms;
            });
          }
          // MapboxComponent.value.handlerDraw(status,mapgemoList.value, false);
          MapboxComponent.value.handlerDraw('Details', geoms, false);
        } else {
          geomsList.value = null;
          createMessage.error('当前数据没有图斑！');
        }
      });
    } else {
      createMessage.error('当前数据没有图斑！');
    }
  }

  // const hanlderPreViewFile = (url) => {
  //   if (url.indexOf('.pdf') !== -1) {
  //     axios({
  //       method: 'get',
  //       url: `${VITE_GLOB_INFO_IMAGE_URL}/${url}`,
  //       responseType: 'blob',
  //       headers: {
  //         'Content-Disposition': 'inline',
  //       },
  //     })
  //       .then((response) => {
  //         let blob = new Blob([response.data], { type: 'application/pdf' });
  //         let url = window.URL.createObjectURL(blob);
  //         window.open(url);
  //       })
  //       .catch((error) => {
  //         console.error(error);
  //       });
  //   } else {
  //     window.open(`${VITE_GLOB_INFO_IMAGE_URL}/${url}`, '_blank');
  //   }
  // };

  ///////// File Preview && Download  ////////

  import FilePreview from '@/components/Upload/src/components/FilePreview.vue';

  const previewFileModalVisible = ref(false);
  const previewFileUrl = ref("");

  const hanlderPreViewFile = (url)=>{
    previewFileUrl.value = `${VITE_GLOB_INFO_IMAGE_URL}/${url}`;
    previewFileModalVisible.value = true;
  }

  const handlerDownloadFle = ()=>{
    window.open(previewFileUrl.value, 'mozillaTab');
  }

  const handleCancelPreviewFile = ()=>{
    previewFileModalVisible.value = false;
  }
  ///////
  
  const getContainer = () => {
    return document.getElementById('info-container');
  };
  const dataProcessing = (value) => {
    if (!value) {
      return '0';
    }
    if (value.indexOf('.') == -1) {
      return value;
    } else {
      if (value.split('.')[1].length <= 2) {
        return value;
      }
      let resultString = value.replace('㎡', '');
      return Number(resultString).toFixed(2);
    }
  };
  const showImage = (url) => {
    if (url.indexOf('.png') !== -1 || url.indexOf('.jpg') !== -1 || url.indexOf('.jpeg') !== -1) {
      return true;
    } else {
      return false;
    }
  };
</script>

<style lang="scss" scoped>
  .image-div {
    min-width: 340px;
    max-height: 220px;
    overflow: auto;
  }
  .detail-container {
    width: 100%;
    height: calc(100vh - 120px);
    display: flex;
    padding: 0px 20px;
  }
  .detail-container::after {
    content: '';
    display: block;
    clear: both;
    height: 0;
    visibility: none;
  }
  .map-container {
    float: left;
    width: 45vw;
    height: calc(100vh - 100px);
    margin-right: 20px;
  }
  :deep(.ant-image) {
    margin-right: 10px;
    margin-bottom: 10px;
  }
  :deep(.ant-image-preview-switch-left) {
    position: absolute;
  }
  :deep(.ant-image-preview-switch-right) {
    position: absolute;
  }
  .info-container {
    // float: left;
    position: relative;
    flex: 1;
    :deep(.ant-image-preview-wrap) {
      position: absolute;
    }
    :deep(.ant-image-preview-mask) {
      position: absolute;
    }
    :deep(.ant-image-preview-operations-wrapper) {
      height: 100%;
      position: absolute;
      .ant-image-preview-operations {
        position: absolute;
        top: 0;
        width: 100%;
        .ant-image-preview-operations-operation {
          // flex:1;
        }
      }
      .ant-image-preview-operations-operation:nth-last-child(1) {
        display: none;
      }
      .ant-image-preview-operations-operation:nth-last-child(2) {
        display: none;
      }
    }
  }
  ::v-deep .ant-tabs .ant-tabs-content-holder {
    overflow: auto;
    height: 80vh;
    overflow: auto;
    padding-right: 10px;
  }
</style>
