<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"
      />
    </div>
    <div class="imgList flex justify-start flex-wrap">
      <div
        class="maskContainer previewBox my-2 mr-4"
        :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)" 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)">
            <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">
      <video ref="videoPlayer" style="width: 100%; height: 300px;" 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";
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,
  },
  list: {
    type: Array,
    default() {
      return [];
   }
  }
});
const visible = ref({
  show: false,
  src: "",
});
const deleteVal = ref(props.deleteShow);
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();
  console.log(url, fileType, "urlFilter");
  return fileList.value.includes(fileType) ? fileImg : url ;
 }
 
const TypeFilter = (url) => {
  if (!url) return "";
  const fileType = url.substring(url.lastIndexOf(".")).toLowerCase();
  return fileType;
 }
watchEffect(() => {
  if (props.list.length) {
    console.log(props.list, "watchEffect");
    let newlist = props.list.map(item => item.filesUrl || item.fileUrl);
    initFileList.value = newlist;
  }
});

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 del = async (file) => {
  console.log(file, "file");
  ElMessageBox.confirm("确定删除？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    if(props.delete){

    }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;
  padding: 7px;
  border: 1px solid #d9d9d9;
  &:hover {
    .mask {
      opacity: 1;
    }

    img {
      transform: scale(1.1);
    }
  }

  .mask {
    transition: all 0.5s;
    opacity: 0;
    position: absolute;
    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%;
  }
}
</style>