<template>
  <div v-if="!(project == '' || project == 'index')" 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>
    <el-popover v-model="showCanUseApp" open-delay="200" placement="left-end" width="365" trigger="hover" popper-class="sw-page-pop" @hide="close">
      <div class="pl15">
        <div class="text-right"><i class="el-dialog__close el-icon el-icon-close mr10 cursor" @click="close"></i></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: []
    };
  },
  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;
    }
  },
  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;
    }
  }
}
.app-block {
  padding-left: 20px;
}
.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;
      }
      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;
}
</style>