<template>
  <div class="user-content">
    <div class="user-name">
      <img src="/images/component/default.png"/>
      <div class="user-info">
        <p>{{loadUserInfo.userName}}</p>
        <p>{{enterprise.enName}}</p>
      </div>
      <span @click="onclick()">{{listName}}<i class="iconfont icon-arrow-down"></i></span>
      <ul class="supdown" v-if="down">
        <li @click="onDown('1')" v-show="!isDevice || !isShop">全部收藏</li>
        <li @click="onDown('-1')" v-show="isDevice || !isShop">店铺关注</li>
        <li @click="onDown('0')" v-show="!isDevice || isShop">器件收藏</li>
      </ul>
    </div>
    <div class="collect-list-type" v-if="focusPage.content && focusPage.content.length > 0 && isShop">
      <p>店铺</p>
    </div>
    <div class="shop-list" v-if="isShop" v-for="item in focusPage.content" @click="goStoreDetail(item.storeInfo.uuid)">
      <h3>{{item.storeName}}</h3>
      <div class="list-item">
        <div class="item-img">
          <i :style="getBackground(item.storeInfo.type)"></i>
          <img :src="item.storeInfo.logoUrl || '/images/component/default.png'">
        </div>
        <div class="list-item-phone">
          <p>电话：<span>{{item.storeInfo.enterprise.enTel}}</span></p>
          <p>传真：<span>{{item.storeInfo.enterprise.enFax}}</span></p>
          <i class="iconfont icon-shoucang" @click="cancelFocus('store', item)"></i>
        </div>
      </div>
    </div>
    <div class="collect-list-type" v-if="collectSave.content && collectSave.content.length > 0 && isDevice">
      <p>器件</p>
    </div>
    <div class="detail-brand" v-for="(item, index) in collectSave.content" v-if="isDevice">
      <nuxt-link :to="'/mobile/brand/componentDetail/'+ item.componentinfo.uuid">
        <div class="brand-item">
          <p>型号：<span>{{item.componentinfo.code}}</span></p>
          <p>品牌：<span>{{item.componentinfo.brand.nameCn}}</span></p>
          <p>产品描述：<span>{{item.componentinfo.kind.nameCn}}</span></p>
          <i class="iconfont icon-shoucang" @click="cancelFocus('product', item)"></i>
        </div>
      </nuxt-link>
    </div>
    <div class="none-state" v-if="!(collectSave.content&&collectSave.content.length>0 || focusPage.content&&focusPage.content.length > 0)">
      <img src="/images/mobile/@2x/shoucangkongzhuangtai@2x.png">
      <nuxt-link to="/">返回首页</nuxt-link>
    </div>
  </div>
</template>

<script>
  export default {
    layout: 'main',
    data () {
      return {
        userName: '',
        down: false,
        count: '',
        page: '',
        type: '',
        listName: '全部收藏',
        isShop: true,
        isDevice: true
      }
    },
    fetch ({ store }) {
      return Promise.all([
        store.dispatch('product/saveStores', { count: 25, page: 1, type: 'component' }),
        store.dispatch('loadUserInfo'),
        store.dispatch('shop/StoreFocusPage', { count: 5, page: 1 })
      ])
    },
    methods: {
      onclick () {
        this.down = !this.down
      },
      onDown (type) {
        if (type === '-1') {
          this.listName = '店铺关注'
          this.isShop = true
          this.isDevice = false
          this.down = false
        }
        if (type === '0') {
          this.listName = '器件收藏'
          this.isDevice = true
          this.isShop = false
          this.down = false
        }
        if (type === '1') {
          this.listName = '全部收藏'
          this.isDevice = true
          this.isShop = true
          this.down = false
        }
      },
      cancelFocus: function (type, item) {
        if (type === 'store') {
          this.$http.post('/trade/storeFocus/delete/storeId', [item.storeid])
            .then(response => {
              window.location.reload()
            })
        } else {
          // /trade/collection/
          this.$http.delete('/trade/collection/' + item.id)
            .then(response => {
              window.location.reload()
            })
        }
      },
      getBackground: function (type) {
        let style = 'background: url('
        if (type === 'AGENCY') {
          style += '/images/mobile/@2x/shop/daili@2x.png'
        } else if (type === 'DISTRIBUTION') {
          style += '/images/mobile/@2x/shop/jingxiao@2x.png'
        } else if (type === 'ORIGINAL_FACTORY') {
          style += '/images/mobile/@2x/shop/yuanchang@2x.png'
        }
        style += ')no-repeat; background-size: .65rem .33rem;'
        return style
      },
      goStoreDetail: function (uuid) {
        this.$router.push('/mobile/shop/' + uuid)
      }
    },
    computed: {
      collectSave () {
        return this.$store.state.product.common.collectList.data
      },
      loadUserInfo () {
        return this.$store.state.option.user.data
      },
      enterprise () {
        let ens = this.loadUserInfo.enterprises
        if (ens && ens.length) {
          return ens.find(item => item.current) || {enName: this.user.data.userName + '（个人账户）'}
        } else {
          return {enName: this.user.data.userName + '（个人账户）'}
        }
      },
      focusPage () {
        return this.$store.state.shop.storeInfo.focusPage.data
      }
    }
  }
</script>

<style scoped lang="scss">
  .user-content{
    margin-bottom: .98rem;
    background: #dfe2e4;

    .none-state{
      text-align: center;
      padding:1.5rem 0;
      background: #fff;
      margin-top:.1rem;
      width:100%;
      img{
        margin:0 auto;
        width: 4.08rem;
        height: 4.13rem;
      }
      a {
        display: block;
        font-size: .28rem;
        color: #fff;
        width: 1.88rem;
        height: .54rem;
        line-height: .54rem;
        background: #418bf6;
        margin: .7rem auto 0;
        border-radius: .05rem;
      }
    }
    .user-name{
      padding:.14rem 0rem .09rem .34rem;
      background:#fff;
      display:flex;
      align-items: center;
      width:100%;
      position:relative;
      .supdown{
        position:absolute;
        top:1rem;
        right:.3rem;
        z-index:1000;
        width:1.7rem;
        background:#616264;
        border-radius:.1rem;
        li{
          font-size: .28rem;
          color:#ffffff;
          height: .32rem;
          line-height: .32rem;
          margin: .4rem 0;
          text-align: center;
        }
      }
      img{
        display: inline-block;
        width:1.25rem;
        height:1.25rem;
        border:.04rem solid #c5dbfc;
        border-radius: .05rem;
      }
      .user-info {
        margin-left:.25rem;
        display: inline-block;
        width: 3.92rem;
        p{
          font-size:.3rem;
          margin:0;
          font-weight: bold;
          display: block;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          &:nth-child(2) {
            font-weight: normal;
            margin-top: .3rem;
          }
        }
      }
      span{
        font-size:.28rem;
        color:#53a0f7;
      }
    }
    .shop-list {
      background:#fff;
      margin-top:.12rem;
      padding-bottom:.28rem;
      h3{
        font-size:.32rem;
        line-height: .8rem;
        margin:0;
        margin-left:.27rem;
        margin-bottom:.14rem;
      }
      .list-item{
        width:6.77rem;
        margin-left:.39rem;
        justify-content: space-around;
        display:inline-flex;
        .item-img{
          width:2.42rem;
          i{
            display:block;
            position:absolute;
            width:.65rem;
            height:.33rem;
            background: url(/images/mobile/@2x/shop/daili@2x.png)no-repeat;
            background-size: .65rem .33rem;
          }
          img{
            border:.02rem solid #418bf6;
            width:2.4rem;
            height:1.69rem;
          }
        }
        .list-item-phone{
          width:3.95rem;
          padding-top:.18rem;
          position:relative;
          p{
            font-size:.28rem;
            line-height: .45rem;
            margin:0;
          }
          i{
            display:block;
            position:absolute;
            top: 0;
            right: -.1rem;
            font-size:.4rem;
            color:#ff7800;
          }
        }
      }
      &:active {
        background: #e1e1e1;
      }
    }
    .detail-brand{
      background: #fff;
      width:100%;
      margin-top:.12rem;
      min-height:1.5rem;
      padding:.2rem 0;
      .brand-item{
        width:7rem;
        margin:0 auto;
        border-radius:.1rem;
        background: #fff;
        padding:.2rem;
        position:relative;
        &:active{
           background: #e1e1e1;
         }
        p{
          font-size:.28rem;
          line-height:.4rem;
          color:#333;
          margin:0;
          span{}
        }
        i{
          display:block;
          position:absolute;
          top:.2rem;
          right:.1rem;
          font-size:.4rem;
          color:#ff7800;
        }
      }
      div.active{
        background: #d4d;
      }
    }
    .collect-list-type {
      background: #fff;
      margin-top: .1rem;
      margin-bottom: -.11rem;
      p {
        font-size: .32rem;
        margin: 0 0 0 .13rem;
        width: .92rem;
        text-align: center;
        line-height: .5rem;
        border-bottom: .06rem solid #418bf6;
      }
    }
  }
</style>
