<template>
  <div class="floor-list">
    <div class="container">
      <floor-bar :floors="floors"></floor-bar>
      <!--<a href="/store/33069557578d44e69bd91ad12d28a8d4" target="_blank"><img src="/images/all/banner-cuxiao.png" alt=""></a>-->
      <div class="banner">
        <ul>
          <li><a href="/store/33069557578d44e69bd91ad12d28a8d4" target="_blank"><img src="/images/all/banner-cuxiao01.png" alt=""></a></li>
          <li>
            <div class="banner-cuxiao">
              <table>
                <caption><span>最新求购</span><img src="/images/all/banner-cuxiao03.jpg" alt=""></caption>
                <thead>
                <tr>
                  <th>发布时间</th>
                  <th>买家名称</th>
                  <th>型号</th>
                  <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="purchaseMan in purchaseManList.content.slice(timerIndex, timerIndex + 5)">
                  <td>{{purchaseMan.date | date}}</td>
                  <td>
                    <div v-if="purchaseMan.inquiry.enterprise && purchaseMan.inquiry.enterprise.enName">{{purchaseMan.inquiry.enterprise.enName | enterpriseFilter}}</div>
                    <div v-else>{{purchaseMan.userName | userNameFilter}}</div>
                  </td>
                  <td :title="purchaseMan.cmpCode">{{purchaseMan.cmpCode}}</td>
                  <td><nuxt-link to="applyPurchase">我要报价</nuxt-link></td>
                </tr>
                </tbody>
              </table>
              <nuxt-link to="applyPurchase" class="purchase">我要求购</nuxt-link>
            </div>
          </li>
        </ul>
      </div>
      <floor :floor="defaultFloors[0]" :isDefault="true" v-if="!isEmpty"></floor>
      <floor :floor="defaultFloors[1]" :isDefault="true" v-if="!isEmpty"></floor>
      <floor v-for="(floor, index) in floors.data" :floor="floor" :isDefault="false" :key="index"></floor>
    </div>
    <span v-if="expandFloors && false">floor</span>
  </div>
</template>
<script>
  import Floor from './Floor.vue'
  import FloorBar from './FloorBar.vue'
  export default {
    name: 'floor-list',
    components: {
      Floor,
      FloorBar
    },
    data () {
      return {
        defaultFloors: [
          {
            items: [
              {
                backGroundColor: '',
                body: '',
                hrefUrl: '/store/33069557578d44e69bd91ad12d28a8d4',
                pictureUrl: '/images/floor/banner1.jpg',
                size: 'large'
              }
            ]
          },
          {
            items: [
              {
                backGroundColor: '',
                body: '',
                hrefUrl: '/store/33069557578d44e69bd91ad12d28a8d4',
                pictureUrl: '/images/floor/banner2.jpg',
                size: 'large'
              }
            ]
          }
        ],
        timerIndex: 0
      }
    },
    mounted () {
      setInterval(() => {
        this.timerIndex ++
        if (this.timerIndex > 45) {
          this.timerIndex = 0
        }
      }, 3000)
    },
    filters: {
      date: function (date) {
        const now = new Date()
        const day = (date - now.getTime()) - 1000 * 60 * 60 * 24
        return day < 0 ? '今天' : day + '天前'
      },
      enterpriseFilter (str) {
        return str.length > 4 ? str.substring(0, 2) + '**' + str.substring(str.length - 2, str.length) : str
      },
      userNameFilter (str) {
        return str.substring(0, 1) + '**'
      }
    },
    computed: {
      floors () {
        return this.$store.state.floor.list
      },
      isProd () {
        return this.$store.state.option.url === 'http://www.usoftmall.com'
      },
      expandFloors () {
        let data = this.$store.state.floor.list_expand.data
        let _this = this
        for (let i = 0; i < data.length; i++) {
          let obj = {
            backGroundColor: '',
            body: '',
            hrefUrl: '',
            name: '',
            pictureUrl: '',
            size: '',
            price: '',
            currency: 'RMB'
          }
          if (data[i]) {
            obj.name = data[i].code
            obj.body = data[i].brandNameEn + '<br/>' + (data[i].kindNameCn || '其他')
            obj.hrefUrl = '/store/productDetail/' + data[i].batchCode
            obj.pictureUrl = '/images/floor/' + (this.isProd ? data[i].code : '2SD2704KT146') + '.png'
            obj.size = i % 3 === 0 ? 'medium' : 'small'
            obj.currency = data[i].currencyName
            obj.price = _this.getMinPrice(data[i].prices, data[i].currencyName)
            _this.defaultFloors[i < 6 ? 0 : 1].items.push(obj)
          }
        }
        return data
      },
      isEmpty () {
        let data = this.$store.state.floor.list_expand.data
        if (!data.length) {
          return true
        } else {
          for (let i = 0; i < data.length; i++) {
            if (!(data[0] && data[0] !== null)) {
              return true
            }
          }
        }
        return false
      },
      purchaseManList () {
        return this.$store.state.applyPurchase.purchaseManList.purchaseManList.data
      }
    },
    methods: {
      getMinPrice: function (prices, currency) {
        for (let i = 0; i < prices.length; i++) {
          if (i === prices.length - 1) {
            return currency === 'RMB' ? prices[i].rMBPrice : prices[i].uSDPrice
          }
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  @import '~assets/scss/variables';
  /*add*/
  .floor-list .container{
    padding: 0;
  }
  .floor-list {
    margin-bottom: $xlg-pad;
  }
  .floor-list .container > a > img {
    margin-top: 30px;
  }
  @keyframes myfirst {
    0% {
      transform: translate(0px, 0px);
    }
    50% {
      transform: translate(0px, -3px);
    }
    100% {
      transform: translate(0px, 0px);
    }
  }
  .banner{
    width:1190px;
    height: 253px;
    margin-top: 20px;
    li{
      float: left;
      padding-left: 14px;
      position: relative;
      &:first-child{
        padding-left: 0;
      }
    }
    .banner-cuxiao {
      width: 660px;
      height: 253px;
      background: url('/images/all/banner-cuxiao02.png') no-repeat;
      table {
        width: 436px;
        table-layout: fixed;
        caption {
          color: #f57a2e;
          font-size: 20px;
          font-weight: bold;
          padding: 10px 30px;
          img{
            position: relative;
            top: -7px;
            animation: myfirst 1s infinite;
          }
        }
        thead tr th {
          height: 30px;
          color: #fff;
          font-size: 14px;
          background-color: #f57a2e;
        }
       tr th, tr td {
          height: 30px;
         text-align: center;
          &:first-child {
            padding: 6px 7px 6px 16px;
          }
         &:nth-child(2) {
           padding-right: 7px;
         }
         &:nth-child(3) {
           text-align: center;
           padding: 6px 10px 6px 10px;
         }
         &:nth-child(4) {
           text-align: center;
           padding: 6px 10px 6px 10px;
         }
          a {
            width: 64px;
            height: 22px;
            line-height: 22px;
            text-align: center;
            padding: 3px 5px;
            color: #fd3904;
            font-size: 12px;
            border-radius: 2px;
            border: 1px solid #fd3904;
            &:hover {
              border: 1px solid #fd3904;
              background-color: #fd3904;
              color: #fff;
              -moz-box-shadow: 0 3px 5px #f57a2e; /* 老的 Firefox */
              box-shadow: 0 3px 10px #f57a2e;
            }
          }
        }
        tr td{
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          &:first-child {
            color: #f57a2e;
          }
        }
      }
      .purchase {
        position: absolute;
        left: 515px;
        top: 184px;
        width: 100px;
        height: 28px;
        line-height: 28px;
        background-color: #fff;
        color: #f57a2e;
        border-radius: 25px;
        text-align: center;
        font-weight: bold;
        &:hover{
          background-color: #FB6102;
          color: #fff;
          box-shadow: 0px 3px 10px #fd863d;
        }
      }
    }
  }
</style>
