<template>
  <div v-if="visible" class="switch-page">
    <el-popover placement="left-end" open-delay="200" width="143" trigger="hover" popper-class="sw-page-pop">
      <div>
        <ul class="toggle">
          <li v-show="!project.includes('gift')">
            <router-link to="/gift-manage/real-gift"><img :src="require(`@/assets/images/svg/w-real-gifts.svg`)" width="16" />礼品</router-link>
          </li>
          <li v-show="!project.includes('card')">
            <router-link to="/gic-card/list"><img :src="require(`@/assets/images/svg/w-gic-card.svg`)" width="16" />卡券</router-link>
          </li>
        </ul>
      </div>
      <div class="s-icon mb10" slot="reference">
        <img src="../../assets/images/svg/more.svg" width="30" height="30" />
      </div>
    </el-popover>
    <transition name="slide-fade">
      <div v-if="showCanUseApp" class="page-pop">
        <div class="pl15" style="position:relative">
          <div class="text-right" style="position:absolute;right:0"><i class="el-dialog__close el-icon el-icon-close mr10 cursor" @click="close"></i></div>
          <div class="tip-title">提示</div>
          <div class="tips">当前福利支持在以下应用使用</div>
          <ul class="app-list">
            <li v-for="(item, idx) in appList" :key="idx" @click="goLink(item)">
              <div>
                <img :src="item.iconUrl" draggable="false" />
                <p :title="item.appName" class="ellipsis-100">{{ item.appName }}</p>
              </div>
            </li>
            <div v-if="appList.length == 0" class="empty_block"></div>
          </ul>
        </div>
      </div>
    </transition>
    <el-popover open-delay="200" placement="left-end" width="365" trigger="hover" popper-class="sw-page-pop">
      <div class="pl15">
        <!-- <div class="text-right"><i class="el-dialog__close el-icon el-icon-close mr10 cursor" @click="close"></i></div> -->
        <div class="tips">当前福利支持在以下应用使用</div>
        <ul class="app-list">
          <li v-for="(item, idx) in appList" :key="idx" @click="goLink(item)">
            <div>
              <img :src="item.iconUrl" draggable="false" />
              <p :title="item.appName" class="ellipsis-100">{{ item.appName }}</p>
            </div>
          </li>
          <div v-if="appList.length == 0" class="empty_block"></div>
        </ul>
      </div>
      <div class="s-icon" slot="reference">
        <img src="../../assets/images/svg/app.svg" width="30" height="30" />
      </div>
    </el-popover>
  </div>
</template>

<script>
import fetch from '@/api/common.js';
const { getWelfareAppList } = fetch;

export default {
  name: 'SwitchPage',
  data() {
    return {
      showCanUseApp: false,
      appList: [],
      // show: false,
    };
  },
  mounted() {
    this.getAppList();
  },
  methods: {
    close() {
      this.showCanUseApp = false;
      this.$store.commit('updateShowCornerCanUseApp', false);
    },
    getAppList() {
      getWelfareAppList().then(res => {
        this.appList = res.result || [];
      });
    },
    async goLink(item) {
      localStorage.setItem('appApplicationId', item.applicationId);
      window.open(item.authorizeUrl, '_blank', 'noopener');
    },
  },
  computed: {
    project() {
      return this.$store.state.project;
    },
    showCornerCanUseApp() {
      return this.$store.state.showCornerCanUseApp;
    },
    visible() {
      let routes = [ '/gic-card/list', '/card-package/list', '/gift-manage/real-gift', '/gift-manage/virtual-gift' ];
      // 因为会挡住分页控件，ui考虑之后决定砍掉本控件
      routes = [];
      return routes.some(i => i.indexOf(this.$route.path) > -1);
    }
  },
  watch: {
    showCornerCanUseApp(val) {
      if(val) {
        this.showCanUseApp = true;
      }
    },
  }
};
</script>

<style lang="scss" scoped>
.switch-page {
  position: fixed;
  right: 40px;
  bottom: 90px;
  width: 50px;
  height: 110px;
  z-index: 900;
  .s-icon {
    padding-top: 9px;
    width: 50px;
    height: 50px;
    background: #fff;
    border-radius: 4px;
    border: 1px solid #DCDFE6;
    cursor: pointer;
    text-align: center;
    transition: all 0.2s;
    &:hover {
      background: #F7F8FA;
    }
  }
}
.tip-title {
  font-size: 18px;
  font-weight: 400;
  color: #303133;
  line-height: 25px;
}
.app-block {
  padding-left: 20px;
}
.tips {
  margin-top: 20px;
  margin-bottom: 8px;
}
.app-list {
  margin-top: 12px;
  li {
    width: 162px;
    height: 48px;
    background: #FFFFFF;
    display: inline-block;
    margin-right: 10px;
    margin-bottom: 10px;
    height: 48px;
    position: relative;
    cursor: pointer;
    &:nth-child(2n) {
      margin-right: 0px;
    }
    div {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      // box-shadow: 0px 4px 12px 0px rgba(220, 223, 230, 0.6);
      border: 1px solid #DCDFE6;
      padding-left: 56px;
      padding-right: 12px;
      line-height: 48px;
      transition: all 0.2s;
      &:hover {
        margin-top: -5px;
        box-shadow: 0px 4px 12px 0px rgba(220, 223, 230, 0.6);
      }
      img {
        width: 32px;
        height: 32px;
        position: absolute;
        top: 8px;
        left: 12px;
      }
    }
  }
}
.toggle {
  li {
    img {
      margin-right: 7px;
    }
    a {
      line-height: 34px;
      display: flex;
      align-content: center;
      padding: 0 5px 0 15px;
      width: 100%;
      color: $gray01;
      &:hover {
        background: $gray-disable;
      }
    }
  }
}
</style>
<style lang="scss">
.sw-page-pop {
  padding: 6px 0 !important;
  color: #303133;
}
.page-pop {
  transform-origin: calc(100% - 25px) calc(100% - 25px);
  width: 546px;
  right: 0;
  bottom: 0;
  position: absolute;
  border-radius: 2px;
  right: 0;
  bottom: 0;
  background: #FFF;
  min-width: 150px;
  border: 1px solid #EBEEF5;
  padding: 12px;
  z-index: 2000;
  color: #303133;
  line-height: 1.4;
  text-align: justify;
  font-size: 14px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  word-break: break-all;
  padding: 6px 0 !important;
  padding-top: 16px !important;
  li {
    margin-right: 10px !important;
  }
  .app-list:nth-child(3n) {
    margin-right: 0px !important;
  }
}
.slide-fade-enter-active {
  transition: all .4s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}
.slide-fade-leave-active {
  transition: all .4s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}
.slide-fade-enter, .slide-fade-leave-to {
  transform: scale(0.1);
  opacity: 0.8;
}
</style>
