<template>
  <div class="body">
    <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 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 :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>
    <!-- 图片放大镜 -->
    <common-dialog
      v-if="Magnifier"
      @closePopup="Magnifier = false"
      showMask
      @ClickingBackground="Magnifier = false"
    >
      <div class="display-area">
        <img :src="pcUrl" />
      </div>
    </common-dialog>
  </div>
</template>

<script lang="ts">
import { $ajax } from "@/service";
import {
  fmtTimeStamp,
  GO_URLS,
  syCommonDialog as CommonDialog,
} from "cqk-sy-ui";
import { defineComponent } from "vue";

export default defineComponent({
  //   引入组件
  components: {
    CommonDialog,
  },
  props: ["displayData"],
  data() {
    return {
      Magnifier: false, // 图片放大器组件
      List: [] as any[], // 展示用数据组
      pcUrl: "",
      jsons: [] as any[], // JSON 类型时的展示数据
    };
  },
  computed: {},
  watch: {
    displayData: {
      handler(newval, oldVal) {
        if (newval.length > 0) {
          this.List = JSON.parse(newval) as any;
          for (let index = 0; index < this.List.length; index++) {
            const element = this.List[index];
            if (element.label === "ext") {
              this.List.splice(index, 1);
            }
          }

          this.replacementData(this.List);
        } else {
          this.List = [];
        }
      },
      immediate: true,
    },
  },
  methods: {
    // 获取增量的数据
    // 时间转换
    isImgSrc(str: string) {
      return !!str.match(/^(http|https):\/\//);
    },
    fmtTimeStamp2: function (stampStr: string) {
      if (stampStr) {
        return fmtTimeStamp("YYYY-MM-DD", parseInt(stampStr));
      } else {
        return " ";
      }
    },
    // 显示图片放大器
    displayPicture(item: string) {
      this.pcUrl = item;
      this.Magnifier = true;
    },
    // 替换图片
    async requestPictures(string: any) {
      const res = await $ajax({
        type: "post",
        params: { hashes: [string] },
        url: GO_URLS.getFiles,
      });
      if (res) {
        return res.data.files[0].url;
      } else {
        return "";
      }
    },
    // 数据处理
    async replacementData(data: string | any[]) {
      for (let index = 0; index < data.length; index++) {
        const element = data[index];
        // 有下级，跳过
        if (element.type === 3) {
          this.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 this.requestPictures(img);
            // if (index2 === element.data.length - 1) this.$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 this.requestVideos(value);
            // if (index2 === element.data.length - 1) this.$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 this.requestFdf(value);
            // if (index2 === element.data.length - 1) this.$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 this.requestFdf(value);
            // if (index2 === element.data.length - 1) this.$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;
          // this.$forceUpdate();
        } else if (element.type === 13) {
          if (Array.isArray(element.data)) {
            element.data = element.data.filter(
              (item: { value: string }) => item.value !== ""
            );
          }
        }
      }
    },
    async 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 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文件
    openPDF(item: { url: string | URL | undefined }) {
      window.open(item.url);
    },
  },
});
</script>
<style scoped lang="scss">
* {
  margin: 0;
  padding: 0;
}
li {
  list-style: none;
  margin-bottom: 30px;
}
.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: 16px;
    font-weight: 400;
    color: rgba(41, 41, 41, 1);
    padding-left: 16px;
    line-height: 44px;
    background: rgba(249, 249, 249, 1);
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.05);
  }
  .info {
    padding-left: 20px;
    margin-top: 16px;
    margin-bottom: 24px;
    line-height: 1;
    display: flex;
    .info-title {
      font-size: 14px;
      font-weight: 400;
      width: 110px;
      color: rgba(188, 188, 188, 1);
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }
    .info-content--pdf {
      cursor: pointer;
      color: rgba(63, 121, 254, 1);
    }
    .info-content {
      font-size: 14px;
      font-weight: 400;
      flex-grow: 1;
      overflow: hidden;
      max-width: calc(100% - 110px);
      color: rgba(53, 53, 53, 1);
      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>
