<template>
  <div class="component-detail mobile-content">
    <div class="base-detail">
      <div class="base-detail-item">
        <span>品&nbsp;&nbsp;&nbsp;&nbsp;牌：</span>
        <span>{{component.brand.nameCn || '-'}}</span>
      </div>
      <div class="base-detail-item">
        <span>物料名称：</span>
        <span>{{component.kind.nameCn || '-'}}</span>
      </div>
      <!--<div class="base-detail-item">-->
      <!--<span>规&nbsp;&nbsp;&nbsp;&nbsp;格：</span>-->
      <!--<span>{{component.spec || '-'}}</span>-->
      <!--</div>-->
      <div class="base-detail-item attach" @click="goAttach(component.attach)">
        <span v-if="component.attach">规格书：<img src="/images/mobile/@2x/productDetail/pdf.png" alt=""><span>查看</span></span>
        <span v-else>规格书：-</span>
      </div>
      <div class="base-detail-item product-description" v-if="component.description">
        <span class="description">产品描述：{{component.description || '-'}}</span>
      </div>
      <div class="base-detail-item product-description center" v-else>
        <img src="/images/mobile/@2x/brand/nostate-img.png"/>
        暂无描述
      </div>
      <i class="iconfont icon-shoucang" :style="isCollect?'color:#ff7800':'color: #ddd'" @click="collectComponent"></i>
    </div>
    <div class="product-switch-item">
      <span :class="activeType=='store'?'mobile-switch-btn active':'mobile-switch-btn'" @click="activeType='store'">商家</span>
      <span :class="activeType=='param'?'mobile-switch-btn active':'mobile-switch-btn'" @click="activeType='param'">参数</span>
    </div>
    <div class="product-params" v-if="activeType == 'param'">
      <div class="param-item" v-if="prop.value && prop.value!=''" v-for="prop in component.properties">
        <span class="prop-name">{{prop.property.labelCn}}</span>
        <span class="prop-value">{{prop.value}}</span>
      </div>
    </div>
    <div class="product-store" v-if="activeType == 'store'">
      <div v-for="(item, index) in searchLists">
        <div class="middle">
          <div class="storeName" @click="goProductDetail(item)">
            {{item.storeName || '-' | storeNameFilter}}
          </div>
          <div class="list">
            <div class="fl">
              <div class="name">品牌：</div>
              <div class="text">{{item.brandNameEn}}</div>
            </div>
          </div>
          <div class="list">
            <div class="fl" style="width: 100%">
              <div class="name">物料名称：</div>
              <div class="text" style="color: #3f84f6">{{item.kindNameCn || '-'}}</div>
            </div>

          </div>
          <div class="list">
            <div class="fl" style="width: 100%">
              <div class="name">型号：</div>
              <div class="text">{{item.code || '-'}}</div>
            </div>
          </div>
          <div class="list">
            <div class="fl" style="width: 100%">
              <div class="name">规格：</div>
              <div class="text">{{item.spec || '-'}}</div>
            </div>
          </div>

          <div class="list">
            <div class="fl" style="width: 100%">
              <div class="name">包装：</div>
              <div class="text">{{item.packaging || '无包装信息'}}</div>
              <div class="textinfo" v-if="item.breakUp">可拆卖</div>
            </div>
          </div>

          <div class="list">
            <div class="name">生产日期：</div>
            <div class="text" :title="item.produceDate">{{item.produceDate || '-'}}</div>
          </div>

          <div class="list">
            <div class="fl" style="width: 100%; ">
              <div class="name">交期(天)：</div>
              <div class="text" style="color: #ef5042" v-if="item.b2cMaxDelivery && (item.b2cMaxDelivery != item.b2cMinDelivery)" v-text="item.b2cMinDelivery + '-'+ item.b2cMaxDelivery"></div>
              <div class="text" style="color: #ef5042" v-if="item.b2cMaxDelivery && (item.b2cMaxDelivery == item.b2cMinDelivery)" v-text="item.b2cMinDelivery"></div>
              <div v-if="item.minBuyQty" style="display: inline-block;margin-left: 0.2rem"><span class="order-tag">订</span>{{item.minBuyQty}}起订</div>
              <div v-if="item.reserve" style="display: inline-block;margin-left: 0.2rem"><span class="order-tag reserve-tag">库</span>{{item.reserve}}</div>
            </div>
          </div>
          <div class="list">
            <div class="name left">价格梯度<p>(pcs):</p></div>
            <div class="table left">
              <ul>
                <li class="title">
                  <div>分段数量/PCS</div>
                  <div>分段单价</div>
                </li>
                <li v-for="price in item.prices">
                  <div>{{price.start}}+</div>
                  <div v-if="item.currencyName == 'RMB'">¥{{price.rMBPrice}}</div>
                  <div v-else>${{price.rMBPrice}}</div>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-if="(storeList.totalElements == 0 && activeType == 'store') || (component.properties && component.properties.length == 0 && activeType == 'param')" class="no-store">
      <img src="/images/mobile/@2x/car@2x.png" alt="">
      <div v-if="activeType == 'store'">抱歉，暂无商家出售此型号！</div>
      <div v-if="activeType == 'store'">您可前往<strong>www.usoftmall.com</strong>网页版进行<strong>“发布求购”</strong>或<strong>“产品上架”</strong>操作！</div>
      <div v-if="activeType == 'param'">抱歉，暂无参数信息！</div>
    </div>
    <remind-box :title="collectResult" :timeoutCount="timeoutCount"></remind-box>
    <loading v-show="isSearchingMore"></loading>
    <login-box @onLoginBoxClose="showLoginBox = false" v-if="showLoginBox" :url="url"></login-box>
    <pull-up :searchMore="fetching" :allPage="allPage" :page="params.page" @pullUpAction="getMoreStore"></pull-up>
  </div>
</template>
<script>
  import {RemindBox, Loading, LoginBox, PullUp} from '~components/mobile/common'
  export default {
    data () {
      return {
        activeType: 'store',
        collectResult: '收藏成功',
        timeoutCount: 0,
        storeIds: [],
        UmallExist: false,
        storeExist: false,
        params: {
          count: 10,
          page: 1,
          sorting: {'minPriceRMB': 'ASC'},
          filter: {
            uuid: this.$route.params.uuid,
            ignoreUMall: false,
            ignoreStore: false,
            storeIds: ''
          }
        },
        isSearchingMore: false,
        searchLists: [],
        showLoginBox: false,
        isScrollOverTab: false,
        url: ''
      }
    },
    components: {
      RemindBox,
      Loading,
      LoginBox,
      PullUp
    },
    mounted: function () {
      // let _this = this
      // _this.$nextTick(function () {
      //   window.addEventListener('scroll', function () {
      //     _this.scroll()
      //   }, false)
      // })
    },
    computed: {
      fetching () {
        return this.$store.state.componentInformation.information.fetching
      },
      component () {
        return this.$store.state.componentDetail.detail.data
      },
      storeList () {
        let storeList = this.$store.state.componentInformation.information.data
        let _self = this
        if (storeList.content) {
          storeList.content.forEach(function (item) {
            _self.storeIds.push(item.storeid)
          })
        }
        if (this.storeIds.length > 0) {
          if (this.storeIds.indexOf(this.storeId) === -1) {
            this.storeExist = true
          } else {
            this.storeIds.splice(this.storeIds.indexOf(this.storeId), 1)
            if (this.storeIds.length > 0) {
              this.storeExist = true
            }
            this.UmallExist = true
          }
        }
        this.searchLists = this.searchLists.concat(storeList.content)
        this.isSearchingMore = false
        return storeList
      },
      allPage () {
        return this.storeList.totalPages
      },
      colList () {
        return this.$store.state.product.common.collectList.data
      },
      isCollect () {
        let id = this.component.id
        let store = this.colList
        if (store) {
          for (let i = 0; i < store.length; i++) {
            if (store[i].componentid === id) {
              return true
            }
          }
        } else {
          return false
        }
      },
      user () {
        return this.$store.state.option.user
      }
    },
    filters: {
      currency: function (num) {
        if (typeof num === 'number') {
          if (num <= 0.000001) {
            num = 0.000001
          } else {
            if (num.toString().indexOf('.') === -1) {
              num += '.00'
            } else {
              let inputStr = num.toString()
              let arr = inputStr.split('.')
              let floatNum = arr[1]
              if (floatNum.length > 6) {
                num = inputStr.substring(0, arr[0].length + 7)
                if (Number(floatNum.charAt(6)) > 4) {
                  num = (Number(num) * 1000000 + 1) / 1000000
                }
              } else if (floatNum.length === 1) {
                num = num + '0'
              }
            }
          }
        }
        return num
      },
      storeNameFilter: function (str) {
        if (str === '') {
          return str
        }
        let len = 0
        let index = 0
        for (let i = 0; i < str.length; i++) {
          if (index === 0 && str.charAt(i).charCodeAt(0) > 255) {
            len = len + 2
          } else {
            len++
          }
          if (len > 22) {
            index = i
            break
          }
        }
        if (index > 0) {
          return str.substring(0, index) + '...'
        } else {
          return str
        }
      }
    },
    methods: {
      goProductDetail: function (com) {
        this.$router.push('/mobile/shop/' + com.storeid)
      },
      scroll: function () {
        let scrolled = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop
        if (Math.ceil(scrolled + window.screen.availHeight) >= document.body.scrollHeight && !this.isSearchingMore && this.params.page < this.allPage) {
          this.getMoreStore()
        }
        // let tbodyObj = document.getElementById('product-body')
        // let theadObj = document.getElementById('product-head')
        // if (theadObj) {
        //   this.isScrollOverTab = tbodyObj.getBoundingClientRect().top - theadObj.getBoundingClientRect().height - 5 <= theadObj.getBoundingClientRect().height
        // }
      },
      getMoreStore: function () {
        if (!this.isSearchingMore) {
          this.params.page++
          this.isSearchingMore = true
          this.$store.dispatch('loadComponentInformation', this.params)
        }
      },
      goAttach: function (url) {
        if (this.user.logged) {
          if (url && url !== '1') {
            window.open(url)
//            window.location.href = url
          } else {
            this.collectResult = '规格书地址错误'
            this.timeoutCount++
          }
        } else {
          this.url = this.$route.fullPath
          this.showLoginBox = true
        }
      },
      collectComponent: function () {
        if (this.user.logged) {
          if (!this.isCollect) {
            this.$http.post('/trade/collection/save', {componentid: this.component.id, kind: 2})
              .then(res => {
                if (res.data.code === 1) {
                  this.collectResult = '收藏成功'
                  this.$store.dispatch('product/saveStores')
                } else {
                  this.collectResult = res.data.data || '收藏失败'
                }
                this.timeoutCount++
              }, err => {
                this.collectResult = err.response.data || '收藏失败'
                this.timeoutCount++
                console.log(err)
              })
          } else {
            this.$http.post('/trade/collection/delete/cmpId', [this.component.id]).then(response => {
              this.collectResult = '取消成功'
              this.timeoutCount++
              this.$store.dispatch('product/saveStores')
            })
          }
        } else {
          this.url = this.$route.fullPath
          this.showLoginBox = true
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  .component-detail {
    font-size: .28rem;
    margin-bottom: 0.98rem;
    background: #f7f7f7;
    padding-top: .2rem;
    padding-bottom: 0.2rem;
    .base-detail {
      /*margin: 0 .27rem .2rem .27rem;*/
      padding: .18rem .36rem 0 .36rem;
      /*border-radius: .1rem;*/
      background: url('/images/mobile/@2x/productDetail/desc-bg2.jpg')no-repeat;
      background-size: cover;
      height: 4.11rem;
      position: relative;
      box-shadow: 0 0 5px #8a8a8a;
      .base-detail-item {
        margin-top: 0.3rem;
        position: relative;
        color: #fff;
        &:nth-child(1) {
          margin-top: 0;
        }
        &:nth-last-child(1) {
          color: #999;
        }
        &.attach {
          display: inline-block;
          img {
            background-color: #fff;
            width: .36rem;
            height: .4rem;
            position: relative;
            bottom: .05rem;
          }
          >span >span {
            margin-left: .1rem;
            color: #fff;
          }
        }
        &:last-child {
          margin-bottom: 0;
        }
        &.product-description {
          height: 1.58rem;
          margin-top: .6rem;
          img {
            width: 0.91rem;
            height: 0.91rem;
            margin-right: 0.1rem;
          }
          &.center {
            text-align: center;
            padding-top: 0.1rem;
            line-height: 0.91rem;
          }
          font-size: 0.28rem;
          color: #666;
        }
        .description {
          line-height: .4rem;
          max-height: 1.2rem;
          word-break: break-all;
          overflow : hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 3;
          -webkit-box-orient: vertical;
          color: #999;
        }
      }
      >i {
        position: absolute;
        font-size: .4rem;
        background: #fff;
        width: .6rem;
        height: .6rem;
        line-height: .6rem;
        border-radius: 100%;
        box-shadow: 0 0 .05rem #aaa;
        right: .28rem;
        top: .55rem;
        text-align: center;
      }
    }
    .product-switch-item {
      margin-top: 0.15rem;
      text-align: center;
      background: #fff;
      border-bottom: 1px solid #d8d8d8;
      .mobile-switch-btn {
        color: #333;
        display: inline-block;
        height: .64rem;
        line-height: .64rem;
        font-size: .34rem;
        width: 1.4rem;
        &:first-child {
          margin-right: 1.78rem;
        }
        &.active {
          color: #fc5708;
          border-bottom: .04rem solid #fc5708;
        }
      }
    }
    .product-params {
      line-height: .28rem;
      margin-top: .2rem;
      .param-item {
        padding: .19rem .4rem;
        border-bottom: 0.04rem solid #eee;
        &:nth-child(1) {
          border-top: 0.04rem solid #eee;
        }
        &:nth-child(even) {
          background: #f9f9f9;
        }
        &:nth-child(odd) {
          background: #fff;
        }
        .prop-name {
          width: 3.72rem;
          display: inline-block;
          text-overflow: ellipsis;
          overflow: hidden;
          white-space: nowrap;
        }
        .prop-value {
          text-overflow: ellipsis;
          overflow: hidden;
          white-space: nowrap;
          display: inline-block;
          width: 2.69rem;
          float: right;
          text-align: right;
        }
      }
    }
    .middle {
      .order-tag {
        display: inline-block;
        font-size: .18rem;
        color: #fff;
        font-weight: bold;
        background: #ee1717;
        height: .27rem;
        width: .27rem;
        line-height: .27rem;
        text-align: center;
        border-radius: .05rem;
        position: relative;
        top: -.05rem;
        margin-right: .05rem;
        &.reserve-tag {
          background: #07bb1c;
        }
      }
      text-align: left;
      padding: 0 0 0.24rem;
      background: #fff;
      margin: 0.24rem 0.24rem 0;
      border-radius: 5px;
      border: 1px solid #e3e5e8;
      .storeName{
        color: #3f84f6;
        font-size: 0.28rem;
        line-height: 0.6rem;
        border-bottom: 1px solid #d3d3d3;
        margin-bottom: 0.18rem;
        padding-left: 0.24rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        font-weight: bold;
      }
      .pms {
        color: #f57710;
        border: 1px solid #f57710;
        border-radius: 0.4rem;
        background: #fff;
        font-size: 0.24rem;
        height: 0.4rem;
        line-height: 0.4rem;
        width: 0.8rem;
        text-align: center;
      }
      .list {
        padding: 0 0.24rem;
        .left {
          float: left;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .textinfo {
          font-size: 0.18rem;
          margin-left: 0.1rem;
          display: inline-block;
          background: #3f84f6;
          color: #fff;
          font-weight: bold;
          border-radius: 3px;
          width: 0.8rem;
          height: 0.32rem;
          line-height: 0.32rem;
          text-align: center
        }
        .button {
          font-size: 0.3rem;
          color: #1a58dd;
          width: 0.92rem;
          height: 0.43rem;
          line-height: 0.43rem;
          text-align: center;
          border-radius: 5px;
          border:1px solid #1a58dd;
          display: inline-block;
          margin-right: 0.2rem;
        }
        margin-bottom: 0.18rem;
        &::after{
          clear: both;
          display: block;
          content: ' ';
          visibility: hidden;
          zoom: 1;
        }
        .fl {
          width: 4.4rem;
          float: left;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .fr {
          text-align: left;
          width: 2.6rem;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        &.list-long {
          .fl {
            width: 100% !important;
          }
        }
        .name {
          color: #333;
          font-size: 0.28rem;
          display: inline-block;
        }
        .text {
          display: inline-block;
          color: #333;
          font-size: 0.28rem
        }
        .table {
          width: 5rem;
          margin-bottom: 0;
          margin-top: 0;
          margin-left: 0.1rem;
          li {
            height: 0.43rem;
            line-height: 0.43rem;
            border-left: .01rem solid #c5c5c5;
            font-size: .28rem;
            &::after {
              clear: both;
              display: block;
              content: ' ';
              visibility: hidden;
              zoom: 1;
            }
            div {
              text-align: center;
              width: 50%;
              float: left;
              border-right: .01rem solid #c5c5c5;
              border-bottom: .01rem solid #c5c5c5;
            }
            &:nth-child(odd) {
              background: #ddd;
              color: #333;
              font-size: 0.28rem;
            }
            &:nth-child(even) {
              background: #fcfcfc;
              color: #333;
              font-size: 0.28rem;
            }
            &:nth-last-of-type(1){
              color: #f31919;
            }
            &.title {
              font-size: 0.28rem;
              color: #333;
            }
          }
        }
      }
    }
    .no-store {
      background: #fff;
      padding-top: 1rem;
      img {
        display: block;
        text-align: center;
        margin: 0 auto;
        margin-bottom: .45rem;
        width: 3.31rem;
        height: 2.13rem;
      }
      div {
        width: 5.27rem;
        margin: 0 auto;
        text-align: center;
        line-height: .4rem;
        color: #999;
        .link-url {
          color: #01a44e;
        }
      }
    }
  }

</style>
