<template>
  <div class="vue-admin-beautiful-wrapper" :class="classObj">
    <div
      v-if="'horizontal' === layout"
      class="layout-container-horizontal"
      :class="{
        fixed: header === 'fixed',
        'no-tags-bar': tagsBar === 'false' || tagsBar === false,
      }"
    >
      <div :class="header === 'fixed' ? 'fixed-header' : ''">
        <top-bar></top-bar>
        <div
          v-if="tagsBar === 'true' || tagsBar === true"
          :class="{ 'tag-view-show': tagsBar }"
        >
          <div class="vab-main">
            <tags-bar></tags-bar>
          </div>
        </div>
      </div>
      <div class="vab-main main-padding">
        <app-main></app-main>
      </div>
    </div>
    <div
      v-else
      class="layout-container-vertical"
      :class="{
        fixed: header === 'fixed',
        'no-tags-bar': tagsBar === 'false' || tagsBar === false,
      }"
    >
      <div :class="header === 'fixed' ? 'fixed-header' : ''">
        <nav-bar
          :cur-selected-index="curSelectedIndex"
          @openModalFlag="openModalFlag"
        ></nav-bar>
      </div>
      <side-bar></side-bar>
      <div class="vab-main" :class="collapse ? 'is-collapse-main' : ''">
        <app-main></app-main>
      </div>
    </div>
    <back-to-top transition-name="fade"></back-to-top>
    <el-dialog
      custom-class="homeDialog"
      :title="''"
      :visible="modalFlag"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      width="70%"
    >
      <div class="modalContent">
        <div class="title">请选择您的筛查填报场景</div>
        <ul class="list">
          <li
            v-for="(item, index) in screeningList"
            :key="index"
            @click="selectedIndex = item.value"
          >
            <div class="screeningItem">
              <div class="circle">
                <img
                  v-if="selectedIndex !== item.value"
                  src="~@/assets/img/DataCenter/circle.png"
                  alt
                  srcset
                />
                <img
                  v-if="selectedIndex == item.value"
                  src="~@/assets/img/DataCenter/selected.png"
                  alt
                  srcset
                />
              </div>
              <div class="itemTitle">{{ item.title }}</div>
            </div>
            <div class="ItemIamge">
              <img :src="item.src" alt />
            </div>
          </li>
        </ul>
        <div class="submit">
          <el-button type="primary" class="btn" @click="setSelectedIndex"
            >保存</el-button
          >
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  AppMain,
  NavBar,
  SideBar,
  TagsBar,
  TopBar,
  BackToTop,
} from "./components"
import { mapGetters, mapActions } from "vuex"
import { tokenName } from "@/config/settings"
import Media from "./mixin/Media"

export default {
  name: "Layout",
  components: {
    TopBar,
    NavBar,
    SideBar,
    AppMain,
    TagsBar,
    BackToTop,
  },
  mixins: [Media],
  data() {
    return {
      modalFlag: false,
      screeningList: [
        {
          title: "社区筛查",
          value: "1",
          src: require("@/assets/img/DataCenter/shequ.png"),
        },
        {
          title: "医院筛查",
          value: "2",
          src: require("@/assets/img/DataCenter/yiyuan.png"),
        },
        {
          title: "体检筛查",
          value: "3",
          src: require("@/assets/img/DataCenter/tijian.png"),
        },
      ],
      selectedIndex: "1",
      curSelectedIndex: "",
    }
  },
  computed: {
    ...mapGetters({
      layout: "settings/layout",
      tagsBar: "settings/tagsBar",
      collapse: "settings/collapse",
      header: "settings/header",
      device: "settings/device",
      // selectedIndex: "table/selectedIndex",
    }),
    classObj() {
      return {
        mobile: this.device === "mobile",
      }
    },
  },
  created() {
    this.selectedIndex = localStorage.getItem("selectedIndex") || ""
    this.curSelectedIndex = String(this.selectedIndex) ? this.selectedIndex : ""
  },
  mounted() {
    if (!this.selectedIndex) {
      this.modalFlag = true
      this.selectedIndex = "1"
    } else {
      this.selectedIndex = this.selectedIndex
      this.curSelectedIndex = String(this.selectedIndex)
        ? this.selectedIndex
        : ""
    }
    // console.log(this.curSelectedIndex)

    this.$nextTick(() => {
      window.addEventListener(
        "storage",
        (e) => {
          if (e.key === tokenName || e.key === null) window.location.reload()
          if (e.key === tokenName && e.value === null) window.location.reload()
        },
        false
      )
    })
  },
  methods: {
    ...mapActions({
      handleFoldSideBar: "settings/foldSideBar",
    }),
    setSelectedIndex() {
      console.log(this.selectedIndex)
      this.curSelectedIndex = this.selectedIndex
      this.$store.commit("table/setSelectedIndex", this.selectedIndex)
      this.modalFlag = false
    },
    openModalFlag() {
      this.modalFlag = true
    },
  },
}
</script>

<style lang="scss" scoped>
@mixin fix-header {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  z-index: $base-z-index - 2;
  width: 100%;
  overflow: hidden;
}

.vue-admin-beautiful-wrapper {
  position: relative;
  width: 100%;
  height: 100%;

  .layout-container-horizontal {
    position: relative;

    &.fixed {
      padding-top: 96px;
    }

    &.fixed.no-tags-bar {
      // padding-top: 56px;
    }

    ::v-deep {
      .vab-main {
        width: 88%;
        margin: auto;
      }

      .fixed-header {
        @include fix-header;
      }

      .tag-view-show {
        background: $base-color-white;
        box-shadow: $base-box-shadow;
      }

      .nav-bar-container {
        .fold-unfold {
          display: none;
        }
      }

      .main-padding {
        margin-top: 15px;
        margin-bottom: 15px;

        .app-main-container {
          min-height: calc(100vh - 180px);
          background: $base-color-white;
        }
      }
    }
  }

  .layout-container-vertical {
    position: relative;
    .fixed-header {
      @include fix-header;
      box-shadow: $base-box-shadow;
      transition: $base-transition;
    }
    .mask {
      position: fixed;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      z-index: $base-z-index - 1;
      width: 100%;
      height: 100vh;
      overflow: hidden;
      background: #000;
      opacity: 0.5;
    }

    &.fixed {
      padding-top: 90px;
    }

    &.fixed.no-tags-bar {
      // padding-top: 56px;
    }

    .vab-main {
      position: relative;
      min-height: 100%;
      margin-left: $base-left-menu-width;
      background: #f6f8f9;
      transition: $base-transition;

      ::v-deep {
        .nav-bar-container {
          position: relative;
          box-sizing: border-box;
        }

        .tags-bar-container {
          box-sizing: border-box;
          padding-right: 5px;
          padding-left: 5px;
        }

        .app-main-container {
          width: calc(100% - 30px);
          margin: 4px auto;
          background: $base-color-white;
          border-radius: $base-border-radius;
        }
      }

      &.is-collapse-main {
        margin-left: $base-left-menu-width-min;

        ::v-deep {
          .fixed-header {
            left: $base-left-menu-width-min;
            width: calc(100% - 65px);
          }
        }
      }
    }
  }

  .side-bar-container {
    top: 90px;
    height: calc(100vh - 90px);
    border-radius: 0px 4px 4px 0px;
    box-shadow: none;
  }
}
::v-deep .homeDialog .el-dialog__header {
  display: none;
}
.modalContent {
  padding: 60px;
  .title {
    text-align: center;
    height: 30px;
    font-size: 24px;
    font-family: AlibabaPuHuiTiM;
    color: #333333;
    line-height: 30px;
    margin-bottom: 60px;
  }
  .list {
    display: flex;
    justify-content: space-between;
    li {
      cursor: pointer;
    }
    .screeningItem {
      display: flex;
      .circle {
        width: 26px;
        height: 26px;
        margin-right: 12px;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .itemTitle {
        height: 24px;
        font-size: 18px;
        font-family: AlibabaPuHuiTiM;
        color: #333333;
        font-weight: bold;
        line-height: 24px;
      }
    }
    .ItemIamge {
      width: 90%;
      // height: 180px;
      margin-top: 14px;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
  .submit {
    margin-top: 60px;
    text-align: center;
    .btn {
      width: 100px;
      height: 32px;
    }
  }
}
</style>
