<template>
  <a-spin :spinning="loading">
    <div class="modal-content">
      <div class="subject" v-for="subject in fileList">
        <div class="subject-name">{{ subject.zname }}</div>
        <div class="show-file-div">
          <div class="item-file" v-for="(item, index) in subject.val">
            <a-checkbox
              style="margin-right: 10px;"
              :checked="selectFile.some(obj => obj === item.path)" 
              @change="changeSelect(selectFile.some(obj => obj === item.path)? 1: 2,item.path)">
            </a-checkbox>
            <Icon icon="material-symbols:attach-file" :size="20" style="cursor: pointer;" @click="hanlderPreViewFile(item.path)"/>
            {{ item.path }}
          </div>
        </div>
      </div>
    </div>
    <div class="button-div">
      <a-button type="primary" @click="save">保存</a-button>
    </div>
    <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>
  </a-spin>
  </template>
  
  <script setup lang="ts">
  import { defineProps, onMounted, ref, defineEmits } from "vue"
  import { rebackzhuantifujian, Copyfujian } from '@/api/tiankongdi/index'
  import Icon from '@/components/Icon/Icon.vue';
  import { message } from "ant-design-vue";
  import { getZhuantiName } from '@/utils/global'
  import FilePreview from '@/components/Upload/src/components/FilePreview.vue';
  
  const props = defineProps(['server'])
  const emits = defineEmits(['save'])
  const fileList = ref<string[]>([])
  const selectFile = ref<string[]>([])
  const loading = ref(false)
  const previewFileModalVisible = ref(false)
  const previewFileUrl = ref("")
  onMounted(() => {
    loading.value = true
    let caseid = sessionStorage.getItem('SelectOtherId')
    rebackzhuantifujian({CaseId:caseid,zhuanti:getZhuantiName()}).then(res => {
      console.log('res',res)
      fileList.value = res
    }).finally(() => {
      loading.value = false
    })
  })
  
  const changeSelect = (type,item) => {
    switch(type){
      case 1: 
      selectFile.value = selectFile.value.filter(useItem => useItem !== item);
        break;
      case 2:
      selectFile.value.push(item)
        break
    }
  }
  const save = () => {
    if(selectFile.value.length <= 0){
      message.warning('至少选择一个文件')
      return
    }
    loading.value = true
    Copyfujian(selectFile.value).then(res => {
      console.log(res)
      emits('save', res)
    }).finally(() => {
      loading.value = false
    })
  }
  const hanlderPreViewFile = (url)=>{
    previewFileUrl.value = `${props.server}/${url}`;
    previewFileModalVisible.value = true;
  }

  const handlerDownloadFle = ()=>{
    window.open(previewFileUrl.value, 'mozillaTab');
  }
  const handleCancelPreviewFile = ()=>{
    previewFileModalVisible.value = false;
  }
  </script>
  
  <style lang="scss" scoped>
  .modal-content{
    min-height: 500px;
    padding: 0px 20px;
    max-height: 678px;
    overflow: auto;
    scrollbar-width: none; 
    ::-webkit-scrollbar {
      width: 0;
      height: 0;
    }
    .subject{
      margin-bottom: 10px;
      .subject-name{
        height: 30px;
        background-color: #8b8b8b;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #fff;
        font-size: 20px;
        font-weight: 500;
        margin-bottom: 10px;
      }
      .show-file-div{
        width: 100%;
        .item-file{
          display: flex;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis; 
        }
        .item-file:hover{
          background-color: #f4f4f4;
        }
      }
    }
  
  }
  .button-div{
    display: flex;
    justify-content: end;
    padding: 20px;
  }
  </style>
  