<template>
  <div class="detail-book">
    <div v-if="list.length > 0">
      <div class="book-page" v-for="(node, i) in list" :key="i"
        :class="{ active: i === activePage, flipped: i < activePage }">
        <div class="article-title" @click="handlePageChange(i, true)" v-if="node.imagesVo[0]">
          <div class="img" :style="`background-image: url('${node.imagesVo[0] &&node.imagesVo[0].url}')`"></div>
          <p>
            <span class="title">{{ node.title }}</span>
            <span class="seq">{{ node.unit }}</span>
          </p>
        </div>
        <div class="article-big-title" v-else @click="handlePageChange(i, true)">
          <p>
            <span class="seq">{{ node.unit }}</span>
            <span class="title">{{ node.title }}</span>
          </p>
        </div>
        <div class="book-cover" v-if="i === 0" @click="handlePageChange(i)">
          <div class="img" :style="`background-image: url('${img}')`">
          </div>
          <p>
            <span>{{ title }}</span>
          </p>
        </div>
        <div class="article-content" v-else @click="handlePageChange(i)">
          <div class="content-detail" v-html="list[i - 1].intro "></div>
        </div>
      </div>
      <div class="book-page">
        <div class="article-content">
          <div class="content-detail" v-html="list[list.length - 1].intro"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    props: {
      data: {
        type: Array,
        default: () => [],
      },
      title: {
        type: String,
        default: "",
      },
      img: {
        type: String,
        default: "",
      },
    },
    data() {
      return {
        activePage: 0,
      };
    },
    computed: {
      list() {
        let arr = this.data;

        return this.transformTree2List(arr);
      },
    },
    methods: {
      handlePageChange(index, flag) {
        if (flag) {
          this.activePage = index;
        } else {
          this.activePage = index + 1;
        }
      },
      transformTree2List(arr) {
        let list = [];
        let loopTree = (data) => {
          data.forEach((n) => {
            list.push(n);
            if (n.children && n.children.length > 0) {
              loopTree(n.children);
            }
          });
        };

        loopTree(arr);
        return list;
      },
    },
  };
</script>

<style lang="scss" scoped>
  .detail-book {
    position: relative;
    width: 100%;
    height: 100%;
    min-height: 800px;
    perspective: 1000px;

    >div {
      // position: absolute;
      position: absolute;
      // width: 800px;
      width: 48%;
      height: 600px;
      top: 50%;
      left: 50%;
      transform: translate(0%, -50%);
      transform-style: preserve-3d;
      /* &:first-child,
    &:last-child {
      box-shadow: 0px -5px 0 2px #8f712fdb;
    } */
    }

    .book-page {
      position: absolute;
      // width: 800px;
      width: 100%;
      height: 100%;
      transition: all 1.5s;
      transform-style: preserve-3d;
      transform-origin: 0 0;
      border-left: 2px solid #8f712fdb;
      box-shadow: 6px 6px 0 2px #8f712fdb;

      &.active {
        z-index: 1;
      }

      &.flipped {
        //   z-index: 1;
        transform: rotateY(-180deg);
      }

      >div {
        position: absolute;
        width: 100%;
        height: 100%;
        box-sizing: border-box;
        backface-visibility: hidden;
        background-color: #bfa277;
      }

      .book-cover {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 30px;
        width: 100%;
        height: 100%;

        >.img {
          width: 600px;
          height: 400px;
          background-size: cover;
          background-repeat: no-repeat;
          background-position: 50% 50%;
          margin: 50px auto;
        }

        >p {
          display: flex;

          >span {
            display: block;
            text-align: center;
            font-size: 22px;
            font-weight: 600;
            font-family: "楷体";
            writing-mode: tb-rl;

            &.title {
              color: #850e0e;
              font-size: 30px;
            }

            &.seq {
              padding-bottom: 100px;
              margin-left: 20px;
            }
          }
        }
      }

      .article-title {
        @extend .book-cover;
        transform: rotateY(180deg);

        >.img {
          border-radius: 10px;
        }
      }

      .article-big-title {
        display: flex;
        justify-content: center;
        align-items: center;
        transform: rotateY(180deg);

        >p {
          font-size: 36px;
          text-align: center;
          font-family: "华文行楷";

          >span {
            display: block;
            margin: 20px;

            &.title {
              color: #fff2b3;
            }
          }
        }
      }

      .article-content {
        width: 100%;
        height: 100%;
        padding: 50px 100px;
        line-height: 1.8;
        color: #333;
        font-weight: 600;
        font-family: "仿宋";
        text-indent: 32px;
      }
    }
  }
</style>