<template>
    <div class="overview">
      <!-- 顶部tag -->
      <div class="preview-tag">
        <div class="page-tag-btn tagPage" :class="{ active: viewState === 0 }" @click="function () { viewState = 0 }">
          <div class="page-tag-btn-tip">页面</div>
        </div>
        <div class="page-tag-btn tagElement" :class="{ active: viewState === 1 }" @click="function () { viewState = 1 }">
          <div class="page-tag-btn-tip">元素</div>
        </div>
      </div>
      <!-- 页面 -->
      <div class="page-preview" style="z-index: 1;">
        <ul class="list custom-scrollbar">
          <li class="cover">
            <img class="page_preview_tag" src="../../assets/images/page_preview_tag.png" alt="">
            <div class="page_preview_tag_title">封面</div>
            <div class="item-page">
              <span>1</span>
              <div class="page" :class="{ active: pages[0] === editingPage }"
                :style="{ width: 70 + 4 + 'px', height: (70 / canvasWidth) * canvasHeight + 4 + 'px' }"
                @click="setEditingPage(pages[0],0)">
                <Page :isOverView="true" class="content" :hideFoot="true"
                  :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px', transform: 'scale(' + 70 / canvasWidth +')',backgroundColor: bodyBackgroundColor }"
                  :elements="pages[0].elements" type="see" />
              </div>
            </div>
          </li>
          <li class="cover">
            <img class="page_preview_tag" src="../../assets/images/page_preview_tag.png" alt="">
            <div class="page_preview_tag_title">题目</div>
            <div class="item-page" v-for="(page,index) in pages" v-show="isShow(index)">
              <span>{{index + 1 }}</span>
              <div class="page" :class="{ active: page === editingPage }"
                :style="{ width: 70 + 4 + 'px', height: (70 / canvasWidth) * canvasHeight + 4 + 'px' }"
                @click="setEditingPage(page,index)">
                <Page :isOverView="true" class="content" :hideFoot="true"
                  :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px', transform: 'scale(' + 70 / canvasWidth +')',backgroundColor: bodyBackgroundColor }"
                  :elements="page.elements" type="see" />
              </div>
            </div>
            <div class="item-page-add" @click="addPage">添加题目页</div>
          </li>
          <li class="cover">
            <img class="page_preview_tag" src="../../assets/images/page_preview_tag.png" alt="">
            <div class="page_preview_tag_title">结论</div>
            <div class="item-page" v-for="(page,index) in pages" v-show="isShowResult(index)">
              <span>1</span>
              <div class="page" :class="{ active: page === editingPage }"
                :style="{ width: 70 + 4 + 'px', height: (70 / canvasWidth) * canvasHeight + 4 + 'px' }"
                @click="setEditingPage(page,index)">
                <Page :isOverView="true" class="content" :hideFoot="true"
                  :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px', transform: 'scale(' + 70 / canvasWidth +')',backgroundColor: bodyBackgroundColor }"
                  :elements="page.elements" type="see" />
              </div>
            </div>
            <div class="item-page-add" @click="addResultPage">添加结论页</div>
          </li>
        </ul>
      </div>
      <!-- 图层 -->
      <div class="list custom-scrollbar" style="z-index: 2;" v-show="viewState === 1"
        :class="{ dragging: dragState === 1 }">
        <ul>
          <li v-for="layer in layersNoBg">
            <div class="layer" :class="{ active: editingLayer === layer}" @click="setEditingLayer($event, layer)"
              @mousedown="moveLayer">
              <span class="thumb" :style="{ backgroundImage: 'url(' + layer.imgSrc + ')' }"></span>{{ layer.type }}
            </div>
          </li>
        </ul>
        <div v-for="layer in layersBg" class="layer" :class="{ active: editingLayer === layer}"
          @click="setEditingLayer($event, layer)">
          <span class="thumb" :style="{ backgroundImage: 'url(' + layer.imgSrc + ')' }"></span>{{ layer.type }}
        </div>
      </div>
      <!-- 题目 -->
      <div class="topic-list">
        <div class="topic-top" @click="showTopic = !showTopic">
          <span>题目列表</span>
          <img class="topic-top-arrow" :src="topicIcon" alt="">
        </div>
        <el-collapse-transition>
          <div v-show="showTopic">
            <el-checkbox-group v-model="checkList" class="topic-group">
              <el-checkbox label="1.复选框 A复选框 A复选框 A复选框 A复选框 A复选框 A复选框 A"></el-checkbox>
              <el-checkbox label="2.复选框 B"></el-checkbox>
              <el-checkbox label="3.复选框 C"></el-checkbox>
              <el-checkbox label="4.选中且禁用" disabled></el-checkbox>
            </el-checkbox-group>
          </div>
        </el-collapse-transition>
      </div>
    </div>
  </template>
  <script>
  import Page from "./../../components/Page";
  import AppConst from "../../util/appConst";
  export default {
    data() {
      return {
        viewState: 0,
        dragState: 0,
        http: AppConst.BACKEND_DOMAIN,
        canvasWidth: 750,
        canvasHeight: 1334,
        bodyBackgroundColor: "rgba(255,255,255,0)",
        questionPages: [],
        resultPageCount: 0,
        resultPages: [],
        checkList: ["选中且禁用"],
        showTopic: true,
        topicIcon: "../../static/img/up-arrow.png"
      };
    },
    computed: {
      vxEditor() {
        return this.$store.state["editor"];
      },
      pages() {
        this.bodyBackgroundColor =
          this.$store.state.editor.editorTheme.bodyBackgroundColor ||
          "rgba(255,255,255,0)";
        return this.vxEditor["editorTheme"]["pages"];
      },
      editingPage() {
        return this.vxEditor["editorPage"];
      },
      layers() {
        return this.editingPage["elements"];
      },
      layersNoBg() {
        return (
          this.layers && this.layers.filter(v => v["type"] !== "bg").reverse()
        );
      },
      layersBg() {
        return this.layers && this.layers.filter(v => v["type"] === "bg");
      },
      editingLayer() {
        return this.vxEditor["editorElement"];
      }
    },
    methods: {
      isShow: function(index) {
        var result = false;
        if (index > 0) {
          console.log(this.pages.length, index, this.resultPageCount);
          if (this.pages.length - index > this.resultPageCount) {
            result = true;
          }
        }
        return result;
      },
      isShowResult(index) {
        var result = false;
        console.log(this.pages.length, index, this.resultPageCount);
        if (
          this.pages.length - index <= this.resultPageCount &&
          this.resultPageCount > 0
        ) {
          result = true;
        }
        return result;
      },
      moveLayer(downEvent) {
        let height = 30;
        let timer = null;
        let layer = downEvent.target;
        let li = layer.parentNode;
        let parent = li.parentNode;
        let liLen = parent.childNodes.length;
        let startTop = li.offsetTop;
        let startIndex = Math.round(startTop / height);
        let targetIndex = null;
        let placeholder = document.createElement("li");
        placeholder.style = "height: " + height + "px; background-color: #d6d6d6";
        let move = moveEvent => {
          if (!timer) {
            // 被拖动的层
            let top = moveEvent.clientY - downEvent.clientY + startTop;
            layer.setAttribute("data-moving", true);
            layer.style.top = top + "px";
            this.dragState = 1;
            // 占位层
            let nowIndex = Math.round(top / height);
            nowIndex =
              nowIndex <= 0 ? 0 : nowIndex > liLen - 1 ? liLen - 1 : nowIndex;
            if (targetIndex !== nowIndex) {
              (targetIndex || targetIndex === 0) &&
                parent.removeChild(placeholder);
              targetIndex = nowIndex;
              parent.insertBefore(
                placeholder,
                parent.childNodes[nowIndex + (startIndex >= targetIndex ? 0 : 1)]
              );
            }
            // timer负责减少onmousemove对客户端的负担
            timer = setTimeout(() => {
              timer = null;
            }, 20);
          }
        };
        let up = upEvent => {
          if (layer.getAttribute("data-moving")) {
            layer.removeAttribute("data-moving");
            layer.style.top = "";
            parent.removeChild(placeholder);
            this.layersNoBg[startIndex]["zindex"] =
              this.layersNoBg[targetIndex]["zindex"] +
              (targetIndex > startIndex ? -0.5 : 0.5);
            this.updateLayersSort();
          }
          document.removeEventListener("mousemove", move);
          document.removeEventListener("mouseup", up);
          this.dragState = 0;
        };
        if (liLen > 1) {
          document.addEventListener("mousemove", move);
          document.addEventListener("mouseup", up);
        }
      },
      copyPage(page) {
        this.bodyBackgroundColor =
          this.$store.state.editor.editorTheme.bodyBackgroundColor ||
          "rgba(255,255,255,0)";
        this.$store.dispatch("copyPage", page);
      },
      delPage(page) {
        this.$store.dispatch("delPage", page);
      },
      addPage() {
        this.bodyBackgroundColor =
          this.$store.state.editor.editorTheme.bodyBackgroundColor ||
          "rgba(255,255,255,0)";
        this.$store.dispatch("addPage");
      },
      addResultPage() {
        this.bodyBackgroundColor =
          this.$store.state.editor.editorTheme.bodyBackgroundColor ||
          "rgba(255,255,255,0)";
        this.$store.dispatch("addPage");
        this.resultPageCount += 1;
      },
      setEditingPage(page) {
        this.bodyBackgroundColor =
          this.$store.state.editor.editorTheme.bodyBackgroundColor ||
          "rgba(255,255,255,0)";
        this.$store.dispatch("setEditorPage", page);
      },
      setEditingLayer(event, layer) {
        this.$store.dispatch("setEditorElement", layer);
      },
      updateLayersSort() {
        this.$store.dispatch("sortElementsByZindex");
      }
    },
    components: { Page },
    watch: {
      showTopic: function(val) {
        if (val) {
          this.topicIcon = "../../static/img/up-arrow.png";
        } else {
          this.topicIcon = "../../static/img/down-arrow.png";
        }
      }
    }
  };
  </script>
  <style lang="less" scoped>
  .overview {
    position: relative;
    background-color: rgb(196, 185, 185);
    height: 100%;
    .preview-tag {
      width: 44px;
      height: 100%;
      background: #fff;
      box-shadow: 5px 0px 10px 0px rgba(62, 67, 116, 0.1);
      position: absolute;
      top: 0px;
      left: 226px;
      z-index: 3;
      padding-top: 10px;
      .page-tag-btn {
        margin-top: 10px;
        margin-left: 5px;
        width: 34px;
        height: 34px;
      }
      .tagPage {
        background: url(../../assets/images/page-tag.png) center no-repeat;
        background-size: cover;
        &.active {
          background: url(../../assets/images/page-tag-sel.png) center no-repeat;
          background-size: cover;
        }
      }
      .tagPage:hover {
        background: url(../../assets/images/page-tag-hover.png) center no-repeat;
        background-size: cover;
      }
      .tagPage:hover div {
        display: block;
      }
      .tagElement {
        background: url(../../assets/images/element-tag.png) center no-repeat;
        background-size: cover;
        &.active {
          background: url(../../assets/images/element-tag-sel.png) center
            no-repeat;
          background-size: cover;
        }
      }
      .tagElement:hover {
        background: url(../../assets/images/element-tag-hover.png) center
          no-repeat;
        background-size: cover;
      }
      .tagElement:hover div {
        display: block;
      }
      .page-tag-btn-tip {
        width: 80px;
        height: 26px;
        position: relative;
        top: 5px;
        left: -80px;
        display: none;
        text-align: center;
        line-height: 26px;
        font-size: 14px;
        color: #fff;
        background: url(../../assets/images/page_tag_btn_tip.png) center no-repeat;
        background-size: cover;
      }
    }
    .panel {
      float: left;
      line-height: 40px;
      width: 50%;
      text-align: center;
      background-color: #d6d6d6;
      cursor: pointer;
      &.active {
        background-color: transparent;
      }
    }
    .list {
      background-color: #fff;
      position: absolute;
      top: 0px;
      bottom: 0px;
      width: 100%;
      overflow-y: auto;
      overflow-x: hidden;
      padding-top: 10px;
      padding-bottom: 20px;
      .cover {
        margin-left: 16px;
        width: 180px;
        background: rgba(230, 237, 255, 1);
        border-radius: 6px;
        padding-top: 10px;
        padding-bottom: 10px;
        position: relative;
        margin-top: 16px;
        .page_preview_tag {
          position: absolute;
          top: 6px;
          left: 0px;
          width: 49px;
          height: 22px;
        }
        .page_preview_tag_title {
          position: absolute;
          top: 6px;
          left: 0px;
          line-height: 22px;
          margin-left: 10px;
          font-size: 12px;
          color: rgba(255, 255, 255, 1);
        }
        .item-page {
          display: flex;
          flex-direction: row;
          justify-content: center;
          margin-top: 20px;
          margin-bottom: 20px;
          font-size: 14px;
          font-weight: 500;
          color: rgba(100, 107, 129, 1);
          .preview-page {
            margin-left: 10px;
            width: 70px;
            height: 114px;
            box-shadow: 0px 2px 4px 0px rgba(78, 93, 255, 1);
            border: 2px solid rgba(255, 255, 255, 1);
            background: #fff;
          }
        }
        .item-page-add {
          width: 70px;
          height: 114px;
          margin-left: 64px;
          margin-bottom: 30px;
          font-size: 12px;
          color: rgba(100, 107, 129, 1);
          line-height: 154px;
          text-align: center;
          background: url(../../assets/images/item-page-add.png) center no-repeat;
          background-size: cover;
        }
        .item-page-add:hover {
          color: rgba(51, 51, 51, 1);
          background: url(../../assets/images/item-page-add-hover.png) center
            no-repeat;
          background-size: cover;
        }
      }
    }
    .dragging:before {
      content: "";
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      z-index: 10;
    }
    .page {
      position: relative;
      margin-left: 10px;
      border: 2px solid rgba(255, 255, 255, 1);
      background: #fff;
      &.active {
        border-color: rgba(78, 93, 255, 1);
        box-shadow: 0px 2px 4px 0px rgba(78, 93, 255, 1);
        .icons {
          display: block;
        }
      }
      &:before {
        content: "";
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
        z-index: 2;
      }
      .content {
        transform-origin: left top;
        background-color: #fff;
        overflow: hidden;
        position: relative;
      }
      .icons {
        position: absolute;
        bottom: -1.5em;
        right: 0.5em;
        display: none;
        width: 100%;
        color: #fff;
        .icon {
          float: right;
          margin-left: 1em;
          opacity: 0.5;
          cursor: pointer;
          &:hover {
            opacity: 1;
          }
        }
      }
    }
    .layer {
      padding-left: 20px;
      height: 34px;
      line-height: 34px;
      cursor: pointer;
      color: rgba(51, 51, 51, 1);
      &[data-moving] {
        background-color: #d6d6d6;
        position: absolute;
        width: 100%;
      }
      &:hover {
        background: rgba(230, 237, 255, 1);
      }
      &.active {
        background: rgba(78, 93, 255, 1);
        color: #fff;
      }
      .thumb {
        display: inline-block;
        width: 15px;
        height: 15px;
        margin-right: 1em;
        background: white center no-repeat;
        background-size: cover;
      }
    }
    .add {
      border: none;
      position: absolute;
      bottom: 0;
      height: 50px;
      line-height: 50px;
      width: 100%;
      left: 0;
      background-color: #373f42;
      text-align: center;
      color: #fff;
      cursor: pointer;
    }
  
    .topic-list {
      position: absolute;
      top: 10px;
      left: 280px;
      width: 345px;
      background: rgba(255, 255, 255, 1);
      border-radius: 6px;
      z-index: 4;
      .topic-top {
        width: 100%;
        height: 34px;
        background: rgba(78, 93, 255, 1);
        color: #fff;
        border-radius: 6px 6px 0px 0px;
        line-height: 34px;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        padding-left: 10px;
        padding-right: 10px;
        .topic-top-arrow {
          width: 13px;
          height: 12px;
        }
      }
      .topic-group {
        display: flex;
        flex-direction: column;
        margin-left: 24px;
        .el-checkbox {
          padding-top: 10px;
          padding-bottom: 10px;
          border-bottom: 1px solid #e2e4ee;
          display: flex;
          position: relative;
          white-space: pre-line;
          align-items: center;
          font-size: 14px;
          font-weight: 400;
          color: rgba(51, 51, 51, 1);
          line-height: 19px;
        }
      }
    }
  }
  </style>
  