<template>
  <div class="content">
    <div class="form-div">
      <div class="row">
        <div class="item">
          <div class="label">合作社名称：</div>
          <div class="item-value" style="flex:2">
            <a-input :value="props.showInfoData.hezuoshemc" disabled/>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="item">
          <div class="label">社会信用代码：</div>
          <div class="item-value" style="flex:2">
            <a-input  v-model:value="props.showInfoData.shehuixydm" :disabled="props.isRead"/>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="item">
          <div class="label">地址：</div>
          <div class="item-value" style="flex:2">
            <a-input v-model:value="address" :disabled="props.isRead"/>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="item">
          <div class="label">农机数量：</div>
          <div class="item-value" style="flex:2">
            <a-input v-model:value="props.showInfoData.nongjishuliang" :disabled="props.isRead"/>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="item">
          <div class="label">2024年服务面积(亩)：</div>
          <div class="item-value" style="flex:2">
            <a-input v-model:value="props.showInfoData.nj_2024nianfuwu" :disabled="props.isRead"/>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="item">
          <div class="label">年服务面积(亩)：</div>
          <div class="item-value" style="flex:2">
            <a-input v-model:value="props.showInfoData.nianfuwu" :disabled="props.isRead"/>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="item">
          <div class="label">服务项目：</div>
          <div class="item-value" style="flex:2">
            <a-input v-model:value="fuwuxiangmuString" :disabled="props.isRead"/>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="item">
          <div class="label">服务区域范围：</div>
          <div class="item-value" style="flex:2">
            <a-input v-model:value="props.showInfoData.fuwufanwei" :disabled="props.isRead"/>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="item">
          <div class="label">联系人：</div>
          <div class="item-value" style="flex:2">
            <a-input v-model:value="props.showInfoData.lainxiren" :disabled="props.isRead"/>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="item">
          <div class="label">联系方式：</div>
          <div class="item-value" style="flex:2">
            <a-input v-model:value="props.showInfoData.lianxirendh" :disabled="props.isRead"/>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="item" style=" width: 100%;">
          <div class="label">图片：</div>
          <div style="flex:2;display:flex;flex-wrap: wrap">
            <a-image-preview-group>
              <div class="video-item" v-for="(item, index) in imageList" :key="index" style="display: inline-block;position:relative;">
                <div v-if="!props.isRead" class="video-delete-btn" @click="handlerDelete(index)">
                  <CloseOutlined />
                </div>
                <a-image :src="`${VITE_GLOB_INFO_IMAGE_URL}/${item}`" style="width:100px;height:100px;" />
              </div>
            </a-image-preview-group>
            <Upload
              v-if="!props.isRead"
              list-type="picture-card"
              :showUploadList="false"
              :custom-request="customRequest"
              @change="handleChange"
              style="width: 100px !important;"
            >
              <div>
                <plus-outlined />
                <div style="margin-top: 8px">Upload</div>
              </div>
            </Upload>
          </div>
        </div>
      </div>
      <div class="row" v-if="false">
        <div class="item" style=" width: 100%;">
          <div class="label">视频：</div>
          <div style="flex:2;display:flex;flex-wrap: wrap">
            <a-image-preview-group>
              <div class="video-item" v-for="(item, index) in props.showInfoData.videos" :key="index" style="display: inline-block;position:relative;">
                <div v-if="!props.isRead" class="video-delete-btn" @click="handlerDelete(index)">
                  <CloseOutlined />
                </div>
                <img src="/video.png" style="width:100px;height:100px;margin-right: 10px;margin-bottom: 10px;"  @click="handlePreview(item)" alt="">
              </div>
            </a-image-preview-group>
            <Upload
              v-if="!props.isRead"
              list-type="picture-card"
              :showUploadList="false"
              :custom-request="customVideoRequest"
              @change="handleChange"
              style="width: 100px !important;"
            >
              <div>
                <plus-outlined />
                <div style="margin-top: 8px">Upload</div>
              </div>
            </Upload>
          </div>
        </div>
      </div>
    </div>
    <div class="map-div">
      <MapboxMap :isRead="props.isRead" :id="props.showInfoData.id" :geoms="geomList" :showInfoFlyToPoint="props.showInfoFlyToPoint" @handlerDrawComplete="handlerDrawComplete"/>
    </div>
  </div>
  <Modal :open="previewOpen" :title="previewTitle" :footer="null" @cancel="handleCancel">
    <video :src="previewImage" class="video-player" controls muted autoplay></video>
  </Modal>
</template>

<script setup lang="ts">
import { defineAsyncComponent, defineProps, onMounted, ref } from "vue"
const MapboxMap = defineAsyncComponent(() => import('@/components/MapboxMaps/MapComponent.vue'));
import { PlusOutlined, CloseOutlined, ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { Upload, message, Modal } from 'ant-design-vue';
import { getAppEnvConfig } from '@/utils/env';
import axios from "axios";

const { VITE_GLOB_INFO_IMAGE_URL } = getAppEnvConfig();

const props = defineProps(['showInfoData','isRead','showInfoFlyToPoint'])
const previewOpen = ref<boolean>(false);
const previewImage = ref<string>('');
const previewTitle = ref<string>('');
const geomList = ref<string[]>([])
const imageList = ref<string[]>([])
const address = ref()
const fuwuxiangmuString = ref()

onMounted(() => {
  // geomList.value = props.showInfoData.geoms
  if(props.showInfoData.zhaopian){
    let zhaopianList = props.showInfoData.zhaopian.split(',')
    zhaopianList.forEach(item => {
      if(item){
        imageList.value.push(item)
      }
    })
  }
  if(props.showInfoData.area){
    const areaList = JSON.parse(props.showInfoData.area)
    address.value = areaList[0].address
    areaList.forEach(item => {
      geomList.value.push(item.geom)
    })
  }
  if(props.showInfoData.fuwuxiangmu){
    fuwuxiangmuString.value = JSON.parse(props.showInfoData.fuwuxiangmu).join('、')
  }
})

const handlerDelete = (index) => {
  props.showInfoData.images.splice(index, 1);
}
const customRequest = (file) => {
  console.log('handleCustomRequest',file)
  const { VITE_GLOB_INFO_IMAGE_URL } = getAppEnvConfig();
  const formData = new FormData()
  formData.append('files', file.file)
  axios({
    method: 'post',
    url: `${VITE_GLOB_INFO_IMAGE_URL}/api/Platform/Upload`,
    params: { project: 'xxcj'},
    data: formData,
    headers: {
      'Content-type': 'multipart/form-data',
    },
  }).then(res => {
    let data = res.data.result[0]
    props.showInfoData.images.push({
      fileName: data.fileName,
      fileSize: data.fileSize,
      path: data.filePath,
      type: 0,
      lng: '',
      lat: '',
      angle: 0,
      tag: ''
    })
  })
}
const customVideoRequest = (file) => {
  console.log('handleCustomRequest',file)
  const { VITE_GLOB_INFO_IMAGE_URL } = getAppEnvConfig();
  const formData = new FormData()
  formData.append('files', file.file)
  axios({
    method: 'post',
    url: `${VITE_GLOB_INFO_IMAGE_URL}/api/Platform/Upload`,
    params: { project: 'xxcj'},
    data: formData,
    headers: {
      'Content-type': 'multipart/form-data',
    },
  }).then(res => {
    let data = res.data.result[0]
    props.showInfoData.videos.push({
      fileName: data.fileName,
      fileSize: data.fileSize,
      path: data.filePath,
      type: 0,
      lng: '',
      lat: '',
      angle: 0,
      tag: ''
    })
  })
}
const handleChange = (info) => {
  if (info.file.status !== 'uploading') {
  }
  if (info.file.status === 'done') {
  } else if (info.file.status === 'error') {
  }
};
const handlerDrawComplete = (arr) => {
  console.log('geoms',arr)
  props.showInfoData.geoms = arr
}
const handlePreview = async (file) => {
  previewImage.value = `${VITE_GLOB_INFO_IMAGE_URL}/${file.path}`;
  previewOpen.value = true;
  previewTitle.value =
    file.fileName;
};
const handleCancel = () => {
  previewOpen.value = false;
  previewTitle.value = '';
};
</script>

<style lang="scss" scoped>
:deep(.ant-image){
  margin-right: 10px;
  margin-bottom: 10px;
}
.content{
  width: 100%;
  height: 100%;
  display: flex;
  .form-div{
    width: 50%;
    padding: 50px;
    overflow: auto;
    scrollbar-width: none; 
    ::-webkit-scrollbar {
      width: 0;
      height: 0;
    }
  }
  .map-div{
    width: 50%;
    height: 100%;
  }
}
.row{
  display: flex;
  margin-bottom: 15px;
  .item{
    display: flex;
    flex:1;
    .label{
      flex: 1;
      padding: 0px 15px;
      display: flex;
      align-items: center;
      justify-content: end;
    }
    .item-value{
      padding: 0px 15px;
      flex:1;
    }
  }
}
.video-delete-btn {
  position: absolute;
  top: 0px;
  right: 10px;
  width: 30px;
  height: 30px;
  border-bottom-left-radius: 80%;
  background: rgb(0 0 0 / 20%);
  color: #fff;
  line-height: 30px;
  text-align: center;
  z-index: 999;
}
.video-player{
  width:500px;
  margin:10px;
}
</style>
