<template>
  <div class="body p-10">
    <div class="flex items-center justify-between">
      <div class="titleOfProof">
        {{ name }}
      </div>
      <syButton
        v-if="showIncrease"
        style="width: 113px"
        mode="elementBtn"
        size="large"
        @click="$emit('increaseProof')"
        type="primary"
        >增量更新</syButton
      >
    </div>
    <div
      class="divide-line"
      style="margin-top: 26px; margin-bottom: 33px"
    ></div>
    <ul>
      <li v-for="(item, index) in List" :key="index">
        <!-- 一级标题 -->
        <h1 class="title">{{ item.label }}</h1>
        <!-- 普通展示模式 -->
        <div class="info" v-for="(item2, index2) in item.data" :key="index2">
          <!-- 二级>子级 -->
          <div class="info-title">{{ item2.label }}</div>
          <!-- 文本 -->
          <div class="info-content" v-if="item2.type === 0">
            {{ item2.data.value }}
          </div>
          <div class="info-content info-content--area" v-if="item2.type === 10">
            {{ item2.data.value }}
          </div>
          <!-- 选择器 -->
          <div class="info-content" v-if="item2.type === 5">
            {{ item2.data.value }}
          </div>
          <div class="info-content" v-if="item2.type === 11">
            <a :href="item2.data.value" target="black">
              {{ item2.data.value }}</a
            >
          </div>
          <!-- 显示时间 -->
          <div class="info-content" v-if="item2.type === 6">
            {{ fmtTimeStamp2(item2.data.value) }}
          </div>
          <!-- 显示视频 -->
          <div class="info-content" v-if="item2.type === 8">
            <div
              class="info-content--video"
              v-for="(item, index) in item2.data"
              :key="index"
            >
              <video
                v-if="item.url"
                :src="item.url"
                :controls="true"
                style="outline: none"
              ></video>
            </div>
          </div>
          <!-- 音频 -->
          <div class="info-content" v-if="item2.type === 9">
            <div
              class="info-content--video"
              v-for="(item, index) in item2.data"
              :key="index"
            >
              <audio
                v-if="item.url"
                :src="item.url"
                :controls="true"
                style="outline: none"
              ></audio>
            </div>
          </div>
          <!-- pdf文件 -->
          <div class="info-content info-content--pdf" v-if="item2.type === 2">
            <div
              class="show"
              v-for="(pdf1, pdfIndex1) in item2.data"
              :key="pdfIndex1"
            >
              <i class="iconfont iconpdf1 items-center justify-center"></i>
              <p @click="openPDF(pdf1)">{{ pdf1.name }}</p>
            </div>
          </div>
          <!-- 编辑器 -->
          <div
            class="info-content info-content--bor"
            v-if="item2.type == 7"
            v-html="item2.data.value"
          ></div>
          <!-- 图片 -->
          <div class="info-content" v-if="item2.type === 1">
            <div
              class="imgbox"
              v-for="(item3, index3) in item2.data"
              :key="index3"
            >
              <img
                class="object-cover"
                v-if="isImgSrc(item3.value)"
                :src="item3.value"
              />
              <img v-else />
              <div class="ibox" @click="displayPicture(item3.value)">
                <i class="iconfont iconfangda"></i>
              </div>
            </div>
          </div>
          <!-- 图片链接 -->
          <div class="info-content" v-if="item2.type === 13">
            <div
              class="imgbox"
              v-for="(item3, index) in item2.data"
              :key="index"
            >
              <img class="object-cover" :src="item3.value" />
              <div class="ibox" @click="displayPicture(item3.value)">
                <i class="iconfont iconfangda"></i>
              </div>
            </div>
          </div>
          <!-- 文本数组 -->
          <div class="info-content" v-if="item2.type === 14">
            <div
              class="textbox"
              v-for="(item3, index) in item2.data"
              :key="index"
            >
              <div class="value">{{ item3.value }}</div>
            </div>
          </div>
        </div>
      </li>
    </ul>
    <!-- 图片放大镜 -->
    <syCommonDialog
      v-if="Magnifier"
      @closePopup="Magnifier = false"
      @ClickingBackground="Magnifier = false"
      showMask
    >
      <div class="display-area">
        <img :src="pcUrl" />
      </div>
    </syCommonDialog>
  </div>
</template>

<script lang="ts" setup>
import { $ajax } from "@/service";
import { GO_URLS, fmtTimeStamp, syCommonDialog, syButton } from "cqk-sy-ui";
import { ref, watch } from "vue";

const props = defineProps<{
  displayData?: string | undefined;
  name: string;
  showIncrease?: boolean;
}>();

const Magnifier = ref(false); // 图片放大器组件
const List = ref<any[]>([]); // 展示用数据组
const pcUrl = ref("");
const jsons = ref<any[]>([]); // JSON 类型时的展示数据

defineEmits(["increaseProof"]);

watch(
  () => props.displayData,
  () => {
    if (
      props.displayData &&
      JSON.parse(props.displayData) &&
      JSON.parse(props.displayData).length > 0
    ) {
      List.value = JSON.parse(props.displayData);
      for (let index = 0; index < List.value.length; index++) {
        const element = List.value[index];
        if (element.label === "ext") {
          List.value.splice(index, 1);
        }
      }
      replacementData(List.value);
    } else {
      List.value = [];
    }
  },
  {
    immediate: true,
  }
);

async function replacementData(data: any[]) {
  for (let index = 0; index < data.length; index++) {
    const element = data[index];
    // 有下级，跳过
    if (element.type === 3) {
      replacementData(element.data);
    } else if (element.type === 1) {
      // 图片
      if (Array.isArray(element.data)) {
        element.data = element.data.filter(
          (item: { value: string }) => item.value !== ""
        );
      }
      for (let index2 = 0; index2 < element.data.length; index2++) {
        const img = element.data[index2].value;
        element.data[index2].value = await requestPictures(img);
        // if (index2 === element.data.length - 1) $forceUpdate();
      }
    } else if (element.type === 8) {
      //视频
      for (let index2 = 0; index2 < element.data.length; index2++) {
        const value = element.data[index2].value;
        element.data[index2] = await requestVideos(value);
        // if (index2 === element.data.length - 1) $forceUpdate();
      }
    } else if (element.type === 2) {
      for (let index2 = 0; index2 < element.data.length; index2++) {
        const value = element.data[index2].value;
        element.data[index2] = await requestFdf(value);
        // if (index2 === element.data.length - 1) $forceUpdate();
      }
    } else if (element.type === 9) {
      for (let index2 = 0; index2 < element.data.length; index2++) {
        const value = element.data[index2].value;

        element.data[index2] = await requestFdf(value);
        // if (index2 === element.data.length - 1) $forceUpdate();
      }
    } else if (element.type === 12) {
      let jsondata = JSON.parse(element.data.value || "{}");
      let list = [] as any[];
      for (let i in jsondata) {
        list.push({
          type: 0,
          label: i,
          key: "",
          data: {
            value: jsondata[i],
            format: "string",
            type: "text",
          },
        });
      }
      element.data = list;
    } else if (element.type === 13) {
      if (Array.isArray(element.data)) {
        element.data = element.data.filter(
          (item: { value: string }) => item.value !== ""
        );
      }
    }
  }
}

const isImgSrc = (str: string) => {
  return !!str.match(/^(http|https):\/\//);
};
const fmtTimeStamp2 = function (stampStr: string) {
  if (stampStr) {
    return fmtTimeStamp("YYYY-MM-DD", parseInt(stampStr));
  } else {
    return " ";
  }
};
// 显示图片放大器
function displayPicture(item: any) {
  pcUrl.value = item;
  Magnifier.value = true;
}
// 替换图片
async function requestPictures(str: string) {
  const res = await $ajax({
    type: "post",
    params: { hashes: [str] },
    url: GO_URLS.getFiles,
  });
  if (res) {
    return res.data.files[0].url;
  } else {
    return "";
  }
}
// 数据处理

async function requestVideos(value: any) {
  if (!value) {
    return false;
  }
  const res = await $ajax({
    type: "post",
    params: { hashes: [value] },
    url: GO_URLS.getFiles,
  });
  if (res) {
    return res.data.files[0];
  } else {
    return "";
  }
}
// pdf的替换
async function requestFdf(item: any) {
  const res = await $ajax({
    type: "post",
    params: { hashes: [item] },
    url: GO_URLS.getFiles,
  });
  if (res) {
    return res.data.files[0];
  } else {
    return "";
  }
}
// 打开pdf文件
function openPDF(item: any) {
  window.open(item.url);
}
</script>
<style scoped lang="scss">
.titleOfProof {
  font-size: 30px;
  font-weight: 600;
  line-height: 36px;
  color: var(--sy-black);
}
.divide-line {
  height: 1px;
  background-color: #dfdfdf;
}

li {
  list-style: none;
  margin-bottom: 41px;
}
.show {
  line-height: 1;
  cursor: pointer;
  padding-top: 20px;
  padding-bottom: 4px;

  i {
    display: inline-block;
    font-size: 16px;
    color: rgba(63, 121, 254, 1);
  }

  p {
    font-size: 12px;
    margin: 0;
    display: inline-block;
    padding: 0;
    padding-left: 4px;
    font-weight: 400;
    color: rgba(63, 121, 254, 1);
  }
}
.display-area {
  max-height: 80vh;
  overflow-y: auto;
  img {
    width: 100%;
  }
}
.body {
  padding-left: 46px;
  padding-right: 40px;
  .title {
    font-size: 18px;
    font-weight: 500;
    color: var(--sy-black);
    padding-left: 16px;
    line-height: 44px;
  }
  .info {
    padding-left: 20px;
    margin-top: 16px;
    margin-bottom: 24px;
    line-height: 1;
    display: flex;
    .info-title {
      font-size: 14px;
      font-weight: 500;
      color: var(--sy-gray);

      width: 110px;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }
    .info-content--pdf {
      cursor: pointer;
      color: rgba(63, 121, 254, 1);
    }
    .info-content {
      font-weight: 500;
      color: var(--sy-black);

      font-size: 14px;
      flex-grow: 1;
      overflow: hidden;
      max-width: calc(100% - 110px);
      a {
        color: rgba(63, 121, 254, 1);
      }
      .info-content--video {
        margin-bottom: 24px;
        video {
          width: 468px;
        }
      }
      .imgbox {
        box-sizing: content-box;
        width: 93px;
        height: 66px;
        border: 1px solid rgba(235, 235, 235, 1);
        float: left;
        position: relative;
        margin-bottom: 6px;
        margin-right: 6px;
        &:hover {
          .ibox {
            display: block;
          }
        }
        img {
          width: 100%;
          height: 100%;
        }
        .ibox {
          position: absolute;
          display: none;
          width: 28px;
          left: 50%;
          background: rgba(0, 0, 0, 0.3);
          top: 50%;
          border-radius: 50%;
          transform: translate(-50%, -50%);
          margin-bottom: 0;
          height: 28px;
          text-align: center;
          line-height: 28px;
          cursor: pointer;
          color: white;
        }
      }
      .jsonbox {
        display: flex;

        .key,
        .value {
          margin-bottom: 10px;
        }

        .key {
          width: 100px;
          margin-right: 20px;
        }
        .value {
          max-width: 200px;
        }
      }
      .textbox {
        .value {
          margin-bottom: 10px;
        }
      }
    }
    .info-content--area {
      word-wrap: break-word;
      white-space: pre-wrap;
    }

    .info-content--two {
      background: rgba(249, 249, 249, 1);
    }
    .info-content--bor {
      :deep(p) {
        word-wrap: break-word;
      }
    }
  }
}
</style>
