<template>
  <div class="container" id="title-fragment">
    <div class="container">
      <div class="row" style="margin-bottom: 20px;">
        <!-- 商标展示 -->
        <div class="logo two-word" v-if="storeInfo.type == 'ORIGINAL_FACTORY'">原厂</div>
        <div class="logo two-word" v-else-if="storeInfo.type == 'CONSIGNMENT'">寄售</div>
        <div class="logo" v-else-if="storeInfo.type == 'AGENCY'">代理商</div>
        <div class="logo" v-else-if="storeInfo.type == 'DISTRIBUTION'">经销商</div>
        <div class="shop-logo">
          <img class="logo-img" :src="storeInfo.logoUrl || '/images/store/common/default.png'" />
         <!-- <p id="shop-title" v-if="storeInfo.storeShortName" v-text="storeInfo.storeShortName">店铺名称</p>-->
          <p class="shop-name" v-if="storeInfo.storeName">{{storeInfo.storeName | nameFilter}}</p>
          <p class="shop-name" v-else></p>
          <div class="enter-store">
            <a v-if="isFocus == 'false' || typeof isFocus == 'object'" @click="focus(storeInfo.id, storeInfo.storeName)">关注店铺</a>
            <a v-if="isFocus == 'true'" class="focus">已关注</a>
            <a @click="goLink()">联系卖家</a>
          </div>
          <div class="share">
            <span @click="setShowShare(!showShare, $event)">手机版链接&nbsp;<img src="/images/store/home/qrccode-small.png">&nbsp;&nbsp;<img :class="{'active': showCate}" src="/images/store/home/arrow-down.png" alt=""></span>
            <div v-show="showShare">
              <i class="icon-guanbi1 iconfont" @click="setShowShare(false, $event)"></i>
              <h1>分享链接</h1>
              <p>随时随地使用手机查看店铺现货</p>
              <canvas id="qrccode-canvas"></canvas>
              <!--<input v-show="showShare" :value="url" readonly>-->
              <span v-if="showShare" id="copyLink"  :data-clipboard-text="url">复制链接</span>
            </div>
          </div>
        </div>
        <!-- 店名展示 -->
        <div class="shop-banner">
          <img :src="storeInfo.bannerUrl || '/images/store/default/shop_banner.png'">
         <!-- <div id="shop-title" v-if="storeInfo.storeShortName" v-text="storeInfo.storeShortName">店铺名称</div>-->
        </div>
      </div>
      <el-dialog
        :visible.sync="dialogVisible"
        size="tiny"
      >
        <h3 class="header-text">关注成功！</h3>
        <div class="focus modal-body">
          <button type="button" @click="dialogVisible = false" class="btn" style="margin-left:25px;">关&nbsp;&nbsp;闭</button>
          <button type="button" @click="dialogVisible = false" class="focus-btn btn btn btn-info" style="margin-left:35px;">
            <a href="/user#/storeFocus" target="_blank">查看我的店铺关注</a>
          </button>
        </div>
      </el-dialog>
      <link-saler-box
        :tel="tel"
        v-if="showLinkBox"
        @cancelAction="showLinkBox = false">
      </link-saler-box>
    </div>
  </div>
</template>
<script>
import Clipboard from 'clipboard'
import LinkSalerBox from '~components/common/LinkSalerBox.vue'
let QRCode = require('qrcode')

export default {
  name: 'store-banner',
  data () {
    return {
      dialogVisible: false,
      showShare: false,
      clipboard: {},
      showLinkBox: false,
      showCate: false,
      tel: ''
    }
  },
  mounted () {
    let _this = this
    _this.url = window.location.href
    _this.clipboard = new Clipboard('#copyLink')
    _this.clipboard.on('success', e => {
      _this.clipboard.destroy()
      _this.$message.success('已复制到剪切板')
    })
    _this.clipboard.on('error', e => {
      _this.$message.error('浏览器不支持自动复制，请手动复制')
      _this.clipboard.destroy()
    })
    document.addEventListener('click', function () {
//      _this.showShare = false
//      _this.showCate = false
    })
  },
  components: {
    LinkSalerBox
  },
  methods: {
    loadQRcode: function () {
      let canvas = document.getElementById('qrccode-canvas')
      QRCode.toCanvas(canvas, this.url, (error) => {
        console.log(error || 'QRcode success')
      })
    },
    setShowShare: function (flag, event) {
      event.stopPropagation()
      this.loadQRcode()
      this.showShare = flag
      this.showCate = flag
    },
    focus (id, name) {
      if (!this.user.logged) {
        this.$http.get('/login/page', {params: {returnUrl: window.location.href}}).then(response => {
          if (response.data) {
            window.location.href = response.data.content + '&baseUrl=' + encodeURIComponent(window.location.protocol + '//' + window.location.host + response.data.baseUrl)
          }
        })
      } else {
        this.dialogVisible = true
        this.$store.dispatch('shop/StoreFocus', {storeName: name, storeid: id})
        this.isFocus = true
      }
    },
    goLink: function () {
      this.baseUtils.goLinkUser(this, this.storeInfo.enUU)
    }
//    goWebChat: function () {
//      if (!this.user.logged) {
//        this.$http.get('/login/page').then(response => {
//          if (response.data) {
//            this.$router.push('/auth/login')
//          }
//        })
//      } else {
//        // 获得窗口的垂直位置
//        let iTop = (window.screen.availHeight - 30 - 780) / 2
//        // 获得窗口的水平位置
//        let iLeft = (window.screen.availWidth - 10 - 1030) / 2
//        if (this.tab.close) {
//          this.tab.close()
//        }
//        let newTab = window.open('', '即时对话框', 'height=750, width=1000, top=' + iTop + ', left=' + iLeft + ', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no')
//        newTab.close()
//        newTab = window.open('', '即时对话框', 'height=750, width=1000, top=' + iTop + ', left=' + iLeft + ', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no')
//        this.$store.dispatch('chat/setChatTab', {tab: newTab})
//        this.$http.get('/basic/enterprise/' + this.storeInfo.enUU + '/info')
//          .then(response => {
//            let obj = {}
//            obj.userPhone = this.user.data.userTel
//            obj.userType = 'ENTERPRISE'
//            this.user.data.enterprises.forEach(function (item, index) {
//              if (item.current) {
//                obj.enUU = item.uu
//                obj.enterprise = {enUU: item.uu, name: item.enName}
//              }
//            })
//            obj.otherEnUU = response.data.uu
//            obj.otherUserType = 'STORE'
//            obj.otherEnterprise = {enUU: response.data.uu, name: response.data.enName}
//            obj.type = 'CHAT'
//            if (!(/^1\d{10}$/).test(response.data.enTel)) {
//              this.$http.get('/basic/enterprise/' + response.data.uu + '/admin').then(response => {
//                console.log(response)
//                obj.toPhone = response.data.userTel
//                console.log(obj)
//                this.openWebChat(newTab, obj)
//              }, err => {
//                console.log(err)
//                this.$message.error('暂无卖家管理员手机号！')
//              })
//            } else {
//              obj.toPhone = response.data.enTel
//              console.log(obj)
//              this.openWebChat(newTab, obj)
//            }
//          }, err => {
//            console.log(err)
//          })
//      }
//    },
//    openWebChat: function (newTab, obj) {
//      this.$http.post('https://im.ubtob.com/api/chat/infos?condition=chat_info', obj)
//        .then(response => {
//          if (response.data.success) {
//            newTab.location.href = 'https://im.ubtob.com/chat/visit?gid=' + response.data.content
//          }
//        })
//    }
  },
  filters: {
    nameFilter: function (title) {
      if (title === '') {
        return title
      }
      let len = 0
      let index = 0
      for (let i = 0; i < title.length; i++) {
        if (index === 0 && title.charAt(i).charCodeAt(0) > 255) {
          len = len + 2
        } else {
          len++
        }
        if (len > 24) {
          index = i
          break
        }
      }
      if (index > 0) {
        return title.substring(0, index) + '...'
      } else {
        return title
      }
    },
    date: function (input) {
      const d = new Date(input)
      const year = d.getFullYear()
      const monthTemp = d.getMonth() + 1
      const month = monthTemp < 10 ? '0' + monthTemp : '' + monthTemp
      const hour = d.getHours() < 10 ? '0' + d.getHours() : '' + d.getHours() + ' '
      const minutes = d.getMinutes() < 10 ? '0' + d.getMinutes() : '' + d.getMinutes() + ' '
      const day = d.getDate() < 10 ? '0' + d.getDate() : '' + d.getDate() + ' '
      return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes
    }
  },
  computed: {
    storeInfo () {
      return this.$store.state.shop.storeInfo.store.data
    },
    isConsignment () {
      return this.storeInfo.type === 'CONSIGNMENT'
    },
    tab () {
      return this.$store.state.chat.tab.tab.data
    },
    user () {
      return this.$store.state.option.user
    },
    isFocus () {
      return this.$store.state.shop.storeInfo.focusList.data
    },
    url: {
      get: function () {
        return window.location.protocol + '//' + window.location.host + '/mobile/share/storeShare/' + this.storeInfo.uuid
      },
      set: function () {
      }
    }
  }
}
</script>
<style lang="scss">
  .header-text {
    text-align: center;
    font-size: 20px;
    color: #008B00;
    margin-top: 0;
  }
  .el-dialog__wrapper .el-dialog--tiny {
    width: 320px !important;
  }
  .el-dialog__body {
    padding: 14px !important;
  }
  .focus button.focus-btn a{
    color: #fff;
    width: 100%;
    height: 100%;
    display: inline-block;
  }
  .focus button.focus-btn{
    width: 138px;
    height: 36px;
    line-height: 36px;
    padding: 0;
  }
  #title-fragment {
		padding: 0;
	}
	#title-fragment .container {
		width: 1190px;
		padding-left: 0px;
		padding-right: 0px;
	}
	#title-fragment .container >.row {
		margin-left: 0px;
		margin-right: 0px;
    position: relative;
    background: #fff;
	}
  .logo{
    width: 28px;
    padding-right: 3px;
    height: 60px;
    text-align: center;
    line-height: 15px;
    position: absolute;
    top: 13px;
    left: 0px;
    color: #fff;
    margin-top: -20px;
    background: url('/images/store/home/logo-type.png') no-repeat;
    z-index: 100;
    padding-top: 3px;
  }
  .two-word{
    padding-top: 10px;
  }
	#title-fragment .shop-logo {
		width: 220px;
		height: 220px;
		border: 1px solid #e8e8e8;
		float: left;
		text-align: center;
		line-height: 210px;
    position: relative;
    .shop-name{
      height: 40px;
      line-height: 40px;
      margin-top: -105px;
      width: 100%;
    }
    .enter-store {
      width: 100%;
      height: 30px;
      line-height: 30px;
      text-align: left;
      margin-top: -10px;
      padding-left: 20px;
      a {
        width: 70px;
        height: 24px;
        display: inline-block;
        line-height: 22px;
        text-align: center;
        font-size: 13px;
        margin-left: 12px;
        border-radius: 3px;
        &:first-child {
          background: #bd0505;
          border: 1px solid #bd0505;
          color: #fff;
        }
        &.focus{
          background-color: #c1c1c1;
          border: 1px solid #c1c1c1;
        }
        &:last-child {
          background: #3c7cf7;
          border: 1px solid #3c7cf7;
          color: #fff;
        }
      }
    }
    .share {
      display: inline-block;
      position: relative;
      > span {
        position: relative;
        top: -81px;
        color: #4290f7;
        cursor: pointer;
        .active {
          transform:rotate(180deg);
          -ms-transform:rotate(180deg); /* Internet Explorer */
          -moz-transform:rotate(180deg); /* Firefox */
          -webkit-transform:rotate(180deg); /* Safari 和 Chrome */
          -o-transform:rotate(180deg); /* Opera */
        }
      }
      > div {
        position: absolute;
        top: 38px;
        left: -47px;
        width: 211px;
        height: 321px;
        background: #fff;
        box-shadow: 1px 1px 4px 0 #ccbebe;
        z-index: 1300000;
        text-align: center;
        h1, p{
          height: 30px;
          width: 100%;
        }
        h1{
          margin-top: -80px;
        }
        p{
          margin-top: -10px;
        }
        canvas{
          margin-top: 75px;
        }
        /*input {
          display: inline-block;
          width: 140px;
          overflow: hidden;
          height: 35px;
          vertical-align: middle;
        }*/
        span {
           display: inline-block;
           color: #fff;
           background: #4290f7;
           height: 36px;
           line-height: 36px;
           width: 70px;
           font-style: normal;
           vertical-align: middle;
           cursor: pointer;
           margin-top: -380px;
         }
        i {
          position: absolute;
          right: 10px;
          top: -89px;
          cursor: pointer;
          font-size: 14px;
          font-weight: bold;
        }
      }
    }
	}
	#title-fragment .shop-logo .logo-img {
		width: 173px;
		height: 86px;
    margin-top: -102px;
	}

	#title-fragment .shop-banner {
		float: left;
		position: relative;
		width: 970px;
		height: 220px;
		border: 1px solid #e8e8e8;
		border-left: none;
		overflow: hidden;
	}

	#title-fragment .shop-banner img {
		width: 970px;
		height: 220px;
	}

	#title-fragment .shop-banner #shop-title {
		position: absolute;
		top: 50px;
		left: 60px;
		font-size: 30px;
		color: rgb(255,255,255);
	}
	.container{
		width: 1190px;
		padding: 0;
	}
</style>
