<template>
  <div>
    <div class="demo-image__preview">
      <el-image-viewer
        hide-on-click-modal
        @close="
          () => {
            showViewer = false;
          }
        "
        :initial-index="urlindex"
        v-if="showViewer"
        :url-list="initFileList"
        fit="cover"
      />
    </div>
    <div class="imgList my-2 grid gap-4 grid-cols-4" :class="'grid-cols-'+ itenNumber">
      <!-- flex justify-start flex-wrap -->
      <div
        class="maskContainer previewBox "
        :style="{ width: width, height: height }"
        v-for="(item, index) in list"
        :key="index"
      >
        <div class="demo-image__preview h-full w-full">
          <el-image :src="urlFilter(item.filesUrl||item.mapUrl)" class="originalImg" fit="cover">
            <template #error>
              <div class="image-slot el-image__error">
                <el-icon :size="30"><VideoCameraFilled /></el-icon>
              </div>
            </template>
          </el-image>
        </div>
        <div class="mask">
          <div  class="zoomInImg" v-if="TypeFilter(item.filesUrl) == '.mp4'" @click="showVideoView(item.filesUrl)">
            <el-icon color="#fff"><VideoPlay /></el-icon>
          </div>
          <div v-else class="zoomInImg" @click="showImgView(item.filesUrl||item.mapUrl)">
            <el-icon color="#fff"><zoom-in /></el-icon>
          </div>
          <div
            v-if="!edit"
            class="downloadImg"
            @click="handleItem('edit', item)"
          >
            <el-icon color="#fff"><Edit /></el-icon>
          </div>
          <div
            v-if="!deleteShow"
            class="downloadImg"
            @click="handleItem('delete', item)"
          >
            <el-icon color="red"><Delete /></el-icon>
          </div>
          <!-- <div v-if="!close" class="close" @click="handleItem('close', item)">
            <el-icon color="red"><CircleCloseFilled /></el-icon>
          </div> -->
        </div>
      </div>
    </div>
    <el-dialog v-model="visible.show"  title="查看视频" width="500" @close="close">
      <video :src="visible.src" v-if="visible.show" ref="videoPlayer" style="width: 100%; height: 300px;" controls class="video-js">
        <source :src="visible.src"  type="video/mp4">
      </video>
  </el-dialog>
  </div>
</template>

<script setup>
import { ref, defineEmits, watchEffect, computed } from "vue";
import { router } from "@/router";
import { ElMessage, ElMessageBox } from "element-plus";
import fileImg from "@/assets/svg/file.svg";
import { delCaseFile, } from "@/store/caseFile";
import { caseOverviewDel, } from "@/store/case";
import { number } from "echarts";
const caseId = computed(() => {
  const caseId = router.currentRoute.value.params.caseId;
  if (caseId) {
    return Number(caseId);
  }
});
const emits = defineEmits(['handleItem']);
const props = defineProps({
  width: {
    type: String,
    default: "66px",
    required: false,
  },
  height: {
    type: String,
    default: "66px",
    required: false,
  },
  edit: {
    type: Boolean,
    default: false,
    required: false,
  },
  delete: {
    type: Boolean,
    default: false,
    required: false,
  },
  deleteShow: {
    type: Boolean,
    default: false,
    required: false,
  },
  close: {
    type: Boolean,
    default: false,
    required: false,
  },
  itenNumber: {
    type: number,
    default: 4,
  },
  list: {
    type: Array,
    default() {
      return [];
   }
  }
});
const visible = ref({
  show: false,
  src: "",
});
const deleteVal = ref(props.deleteShow);
const videoPlayer = ref(null);
const currSize = ref({ width: 0, height: 0 });
const fileList = ref([".doc", ".docx", ".pdf", ".xls", ".xlsx"]);
const initFileList = ref([
  "https://fuss10.elemecdn.com/a/3f/3302e58f9a181d2509f3dc0fa68b0jpeg.jpeg",
  "https://fuss10.elemecdn.com/1/34/19aa98b1fcb2781c4fba33d850549jpeg.jpeg",
  "https://fuss10.elemecdn.com/0/6f/e35ff375812e6b0020b6b4e8f9583jpeg.jpeg",
  // "https://fuss10.elemecdn.com/9/bb/e27858e973f5d7d3904835f46abbdjpeg.jpeg",
  // "https://fuss10.elemecdn.com/d/e6/c4d93a3805b3ce3f323f7974e6f78jpeg.jpeg",
  // "https://fuss10.elemecdn.com/3/28/bbf893f792f03a54408b3b7a7ebf0jpeg.jpeg",
  // "https://fuss10.elemecdn.com/2/11/6535bcfb26e4c79b48ddde44f4b6fjpeg.jpeg",
]);
const urlFilter = (url) => {
  if (!url) return "";
  const fileType = url.substring(url.lastIndexOf(".")).toLowerCase();
  return fileList.value.includes(fileType) ? fileImg : url ;
 }
 
const TypeFilter = (url) => {
  if (!url) return "";
  const fileType = url.substring(url.lastIndexOf(".")).toLowerCase();
  return fileType;
 }
 const verifySuffix = (imageName) => {
  const suffix = `(bmp|jpg|png|tif|gif|pcx|tga|exif|fpx|svg|psd|cdr|pcd|dxf|ufo|eps|ai|raw|WMF|webp|jpeg)`
  const regular = new RegExp(`.*\.${suffix}`)
  return regular.test(imageName)
}
watchEffect(() => {
  if (props.list.length) {
    let newlist = props.list.map(item => item.filesUrl || item.fileUrl || item.mapUrl);
    initFileList.value = newlist.filter(item => {
      return verifySuffix(item)
    });
  }
});

const urlindex = ref("");
const showViewer = ref(false);
const previewList = ref([]);

const showVideoView = (src) => {
  visible.value.show = true;
  visible.value.src = src;
}
  const showImgView = (src) => {
  console.log(src, "showImgView", initFileList.value);
  const fileType = src.substring(src.lastIndexOf(".")).toLowerCase();
  urlindex.value = initFileList.value.findIndex((item) => item === src);
  console.log(fileType, fileList.value.includes(fileType), "urlFilter");
  if(fileList.value.includes(fileType)){
    window.open(window.location.origin + src);
    return;
  }
  // url.value = src;
  showViewer.value = true;
  // previewList.value = [src];
};
const handleItem = (type, item) => {
  if(type == 'delete'){
    del(item)
    return
  }
  emits("handleItem", type, item);
};
const close = () => {
  visible.value.show = false;
  videoPlayer.value && videoPlayer.value.pause();

};
const del = async (file) => {
  console.log(file, "file");
  ElMessageBox.confirm("确定删除？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    if(props.delete){//删除绘图
      await caseOverviewDel(file.id);
    }else{
      await delCaseFile({ caseId: caseId.value, filesId: file.filesId });
    }

    emits("handleItem", 'delete', file);
    ElMessage({
      type: "success",
      message: "删除成功",
    });
  });
};
</script>

<style lang="scss" scoped>
.maskContainer {
  position: relative;
  line-height: 0;
  width: 66px;
  height: 66px;
  border-radius: 4px;
  padding: 0px;
  // border: 1px solid #d9d9d9;
  &:hover {
    .mask {
      opacity: 1;
    }

    img {
      transform: scale(1.1);
    }
  }

  .mask {
    transition: all 0.5s;
    opacity: 0;
    position: absolute;
    border-radius: 4px;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba($color: #000000, $alpha: 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    div {
      margin: 3px;
    }
    .close {
      position: absolute;
      right: 0;
      top: 0;
    }
    .svgBox {
      height: 40px;
      width: 40px;
      background: rgb(#000, 0.3);
      border-radius: 50%;
      margin: 10px;
      padding: 10px;
      cursor: pointer;
    }

    svg {
      // color: #fff;
      width: 14px;
      height: 14px;
    }
  }
  .originalImg {
    height: 100%;
    width: 100%;
    border-radius: 4px;
  }
}
</style>