<template>
  <div>
    <div class="mobile-modal" v-if="showStoreInfo || showShare">
      <div class="mobile-modal-box" v-if="showStoreInfo">
        <div class="mobile-modal-header">联系方式<i @click="showStoreInfo = false" class="icon-guanbi iconfont"></i></div>
        <div class="mobile-modal-content" v-if="showDefaultAddr">
          <div>商家地址：深圳市南山区英唐大厦6楼</div>
          <div class="content-line link-url">在线咨询</div>
          <div>致电：<span class="content-line link-url">0755-96586323</span></div>
        </div>
        <div class="mobile-modal-content" v-if="!showDefaultAddr">
        <div>商家地址：{{store.enterprise.enAddress || store.enterprise.address}}</div>
        <div class="content-line link-url">在线咨询</div>
        <div>致电：<span class="content-line link-url">{{store.enterprise.enTel}}</span></div>
        </div>
      </div>
      <div class="mobile-share-box" v-if="showShare">
        <div class="share-area">
          <ul>
            <li class="share-item" @click="shareWeChat">
              <img src="/images/mobile/@2x/share/weChat@2x.png" alt="">
              <span>微信</span>
            </li>
            <li class="share-item" @click="shareQQ">
              <img src="/images/mobile/@2x/share/QQ@2x.png" alt="">
              <span>QQ</span>
            </li>
            <li class="share-item" @click="shareWeibo">
              <img src="/images/mobile/@2x/share/sina@2x.png" alt="">
              <span>微博</span>
            </li>
            <li class="share-item" @click="shareMessage">
              <img src="/images/mobile/@2x/share/message@2x.png" alt="">
              <span>短信</span>
            </li>
            <li class="share-item" @click="shareEmail">
              <img src="/images/mobile/@2x/share/email@2x.png" alt="">
              <span>邮件</span>
              <a href="mailto:yangc@usoftchina.com" class="hide" id="share-mail"></a>
            </li>
            <li class="share-item" @click="flash">
              <img src="/images/mobile/@2x/share/flash@2x.png" alt="">
              <span>刷新</span>
            </li>
            <li class="share-item" @click="copyLink" id="copyLink"  :data-clipboard-text="url">
              <img src="/images/mobile/@2x/share/copylink@2x.png" alt="">
              <span>复制链接</span>
            </li>
          </ul>
        </div>
        <div class="cancel-share" @click="showShare=false">取消</div>
      </div>
    </div>
    <div class="mobile-header">
      <a class="iconfont icon-fanhui" @click="goLastPage">返回</a>
      <p>{{title}}</p>
      <i v-show="rightIcon=='share'" class="iconfont icon-fenxiang" @click="showShare = true"></i>
      <i v-show="rightIcon=='phone'" class="iconfont icon-dianhua" @click="showLink"></i>
    </div>
  </div>
</template>
<script>
  import Clipboard from 'clipboard'
  export default {
    data () {
      return {
        showStoreInfo: false,
        showShare: false,
        rightIcon: 'share',
        showDefaultAddr: true,
        url: '',
        clipboard: {}
      }
    },
    computed: {
      brandDetail () {
        return this.$store.state.brandDetail.detail.data
      },
      title () {
        return this.initHeader(this.$route.path)
      },
      store () {
        return this.$store.state.shop.storeInfo.store.data
      },
      component () {
        return this.$store.state.componentDetail.detail.data
      }
    },
    mounted () {
      let _this = this
      _this.url = window.location.href
      _this.clipboard = new Clipboard('#copyLink')
      _this.clipboard.on('success', e => {
        _this.clipboard.destroy()
        _this.showShare = false
      })
      _this.clipboard.on('error', e => {
        alert('浏览器不支持自动复制，请手动复制')
        _this.clipboard.destroy()
      })
    },
    methods: {
      goLastPage: function () {
        window.history.back(-1)
      },
      initHeader: function (val) {
        let title = '优软商城'
        if (val.startsWith('/mobile/brand/')) {
          if (this.brandDetail.nameCn) {
            if (this.brandDetail.nameCn !== this.brandDetail.nameEn) {
              title = this.brandDetail.nameEn + '(' + this.brandDetail.nameCn + ')'
            } else {
              title = this.brandDetail.nameCn
            }
          } else {
            if (this.component.brand.nameCn !== this.component.brand.nameEn) {
              title = this.component.brand.nameEn + '(' + this.component.brand.nameCn + ')'
            } else {
              title = this.component.brand.nameCn
            }
          }
          this.rightIcon = 'share'
        } else if (val.startsWith('/mobile/shop')) {
          title = '店铺列表'
          this.rightIcon = 'phone'
        } else if (val.startsWith('/mobile/merchantDescription/')) {
          title = '商家简介'
          this.rightIcon = 'phone'
        } else if (val.startsWith('/mobile/user')) {
          title = '我的收藏'
          this.rightIcon = 'phone'
        } else if (val.startsWith('/mobile/search')) {
          title = '搜索结果'
          this.rightIcon = 'share'
        } else {
          title = '优软商城'
          this.rightIcon = 'phone'
        }
        return title
      },
      showLink: function () {
        this.showStoreInfo = true
        if (this.title === '商家简介') {
          this.showDefaultAddr = false
        } else {
          this.showDefaultAddr = true
        }
      },
      shareWeibo: function () {
        let _shareUrl = 'http://v.t.sina.com.cn/share/share.php?&appkey=895033136'     // 真实的appkey，必选参数
        _shareUrl += '&url=' + encodeURIComponent(document.location)     // 参数url设置分享的内容链接|默认当前页location，可选参数
        _shareUrl += '&title=' + encodeURIComponent(document.title)    // 参数title设置分享的标题|默认当前页标题，可选参数
        _shareUrl += '&source=' + encodeURIComponent('')
        _shareUrl += '&sourceUrl=' + encodeURIComponent('')
        _shareUrl += '&content=' + 'utf-8'   // 参数content设置页面编码gb2312|utf-8，可选参数
        _shareUrl += '&pic=' + encodeURIComponent('')  // 参数pic设置图片链接|默认为空，可选参数
        window.open(_shareUrl)
        this.showShare = false
      },
      shareWeChat: function () {
      },
      shareQQ: function () {
        let url = encodeURIComponent(document.location)
        let title = encodeURIComponent(document.title)
        let source = encodeURIComponent('')
        let desc = '优软商城'
        let pics = 'http://dfs.ubtob.com/group1/M00/4F/C3/CgpkyFnxWjOAMy5DAAlh1PrLlc8684.png'
        window.open('http://connect.qq.com/widget/shareqq/index.html?url=' +
          url + '&title=' + title + '&source=' + source + '&desc=' + desc + '&pics=' + pics)
        this.showShare = false
      },
      shareMessage: function () {
      },
      shareEmail: function () {
        document.getElementById('share-mail').click()
      },
      flash: function () {
        window.location.reload()
      },
      copyLink: function () {
        let _this = this
        _this.url = window.location.href
        _this.clipboard = new Clipboard('#copyLink')
      }
    }
  }
</script>
<style lang="scss" scoped>
  @import '~assets/scss/mobileCommon';
  .mobile-header{
    width:100%;
    position: fixed;
    top: 0;
    z-index:10000;
    height:.88rem;
    display:flex;
    justify-content:space-around;
    align-items:center;
    border-bottom:.02rem solid #ccc;
    background: #3e82f5;
    padding:0 .2rem 0 .1rem;
    color:#fff;
  }
  .mobile-header p{
    flex:1;
    font-size:.36rem;
    text-align: center;
    margin-top:.2rem;
  }
  .mobile-header a{
    font-size:.28rem;
    color:#fff;
  }
  .mobile-header i{
    font-size:.4rem;
  }
  .hide {
    display: none;
  }
</style>
