<template>
  <div class="section3">
    <div class="title flex" v-if="bidVolPer || askVolPer">
      <div>
        <!-- 买卖盘 -->
        {{ $t('product.l45') }}
      </div>
      <div class="check">{{ number }}</div>
    </div>
    <div class="box flex" v-if="bidVolPer || askVolPer">
      <div>
        <!-- 买盘 -->
        {{ $t('product.l46') }}
      </div>
      <div>
        <!-- 卖盘 -->
        {{ $t('product.l47') }}
      </div>
    </div>
    <div class="bar-box flex" v-if="bidVolPer || askVolPer">
      <div class="left" :style="{ width: bidVolPer + '%' }" v-if="bidVolPer">{{ bidVolPer }}%</div>
      <div class="right" :style="{ width: askVolPer + '%' }" v-if="askVolPer">{{ askVolPer }}%</div>
    </div>
    <van-row class="vanrow" gutter="15">
      <div class="border"></div>
      <van-col span="12">
        <div class="left flex" v-for="(item, index) in bids" :key="'a' + index">
          <div class="index">{{ index + 1 }}</div>
          <div class="price">{{ $handleDigits(item.price, symbolInfo.digits) }}</div>
          <div class="line-box">
            <div class="line" :style="{width: (item.volume / maxBidVol * 100) + '%' }"></div>
          </div>
          <div>{{ Number(item.volume) }}</div>
        </div>
      </van-col>
      <van-col span="12">
        <div class="right flex" v-for="(item, index) in asks" :key="index">
          <div class="index">{{ index + 1 }}</div>
          <div class="price">{{ $handleDigits(item.price, symbolInfo.digits) }}</div>
          <div class="line-box">
            <div class="line" :style="{width: (item.volume / maxAskVol * 100) + '%' }"></div>
          </div>
          <div>{{ Number(item.volume) }}</div>
        </div>
      </van-col>
    </van-row>
    <div class="pie-box" v-if="false">
      <pieView id="pie" ref="pie" height="162px" />
    </div>
    <van-row gutter="8" v-if="false">
      <van-col span="12">
        <div class="box2">
          <div class="sub-title">买入</div>
          <div class="wrap left">
            <div class="col flex">
              <div class="flex">
                <div class="radius radius1"></div>
                <div class="label">特大</div>
              </div>
              <div>13.27亿</div>
            </div>
            <div class="col flex">
              <div class="flex">
                <div class="radius radius2"></div>
                <div class="label">大单</div>
              </div>
              <div>13.27亿</div>
            </div>
            <div class="col flex">
              <div class="flex">
                <div class="radius radius3"></div>
                <div class="label">中单</div>
              </div>
              <div>13.27亿</div>
            </div>
            <div class="col flex">
              <div class="flex">
                <div class="radius radius4"></div>
                <div class="label">小单</div>
              </div>
              <div>13.27亿</div>
            </div>
          </div>
        </div>
      </van-col>
      <van-col span="12">
        <div class="box2">
          <div class="sub-title">卖出</div>
          <div class="wrap right">
            <div class="col flex">
              <div class="flex">
                <div class="radius radius1"></div>
                <div class="label">特大</div>
              </div>
              <div>13.27亿</div>
            </div>
            <div class="col flex">
              <div class="flex">
                <div class="radius radius2"></div>
                <div class="label">大单</div>
              </div>
              <div>13.27亿</div>
            </div>
            <div class="col flex">
              <div class="flex">
                <div class="radius radius3"></div>
                <div class="label">中单</div>
              </div>
              <div>13.27亿</div>
            </div>
            <div class="col flex">
              <div class="flex">
                <div class="radius radius4"></div>
                <div class="label">小单</div>
              </div>
              <div>13.27亿</div>
            </div>
          </div>
        </div>
      </van-col>
    </van-row>
  </div>
</template>
<script>
import pieView from "@/components/charts/pie.vue";
import { getPriceDepth } from "@/api/product";
import bus from '@/utils/bus'
export default {
  props: ['symbol', 'symbolInfo'],
  components: {
    pieView
  },
  data () {
    return {
      asks: [],
      bids: [],
      number: 5,
      askSign: false,
      bidSign: false
    }
  },
  computed: {
    askVol() {
      return this.asks[0] ? Number(this.asks[0].volume) : 0
    },
    bidVol() {
      return this.bids[0] ? Number(this.bids[0].volume) : 0
    },
    askVolPer() {
      return this.allVol ? Number(this.$bigDecimal.divide(this.askVol, this.allVol) * 100).toFixed(2) : 0
    },
    bidVolPer() {
      return this.allVol ? Number(this.$bigDecimal.divide(this.bidVol, this.allVol) * 100).toFixed(2) : 0
    },
    allVol() {
      return Number(this.$bigDecimal.add(this.askVol, this.bidVol))
    },
    maxAskVol() {
      let vol = 0
      this.asks.forEach(item => {
        if (Number(item.volume) > vol) {
          vol = Number(item.volume)
        }
      })
      return vol || 1
    },
    maxBidVol() {
      let vol = 0
      this.bids.forEach(item => {
        if (Number(item.volume) > vol) {
          vol = Number(item.volume)
        }
      })
      return vol || 1
    }
  },
  beforeDestroy() {
    bus.$off('updateDepth', this.updateDepth)
  },
  created () {
    this.getPriceDepth()
    bus.$on('updateDepth', this.updateDepth)
  },
  mounted () {
    // this.test()
  },
  methods: {
    getPriceDepth() {
      getPriceDepth({
        symbol: this.symbol
      }).then(res => {
        this.asks = (res.asks || []).sort((a, b) => Number(a.price) - Number(b.price)).filter(item => Number(item.price)).slice(0, this.number)
        this.bids = (res.bids || []).sort((a, b) => Number(b.price) - Number(a.price)).filter(item => Number(item.price)).slice(0, this.number)
        // if (this.asks.length == 0 || this.bids.length == 0) {
        //   this.randomDetch()
        // } else {
        //   this.askSign = false
        //   this.bidSign = false
        // }
      }).catch(() => {})
    },
    updateDepth(msg) {
      if (msg.symbol != this.symbol) {
        return
      }
      this.asks = (msg.asks || []).sort((a, b) => Number(a.price) - Number(b.price)).filter(item => Number(item.price)).slice(0, this.number)
      this.bids = (msg.bids || []).sort((a, b) => Number(b.price) - Number(a.price)).filter(item => Number(item.price)).slice(0, this.number)
      // if (this.asks.length == 0 || this.bids.length == 0) {
      //   this.randomDetch()
      // } else {
      //   this.askSign = false
      //   this.bidSign = false
      // }
    },
    randomDetch() {
      this.asks = this.asks[0] || []
      this.bids = this.bids[0] || []
      if (!this.bids.length && Number(this.symbolInfo.bid)) {
        this.bidSign = true
        this.bids = [{
          price: this.symbolInfo.bid,
          volume: parseInt(Math.random() * 2700) + 300
        }]
      }
      if (!this.asks.length && Number(this.symbolInfo.ask)) {
        this.askSign = true
        this.asks = [{
          price: this.symbolInfo.ask,
          volume: parseInt(Math.random() * 2700) + 300
        }]
      }
    },
    test() {
      this.$refs['pie'].setOption([
        { value: 1048, name: 'Search Engine' },
        { value: 735, name: 'Direct' },
        { value: 580, name: 'Email' },
        { value: 484, name: 'Union Ads' },
        { value: 300, name: 'Video Ads' }
      ])
    },
  }
}
</script>
<style lang="less" scoped>
.section3 {
  background-color: var(--bg-);
  padding: 12px 18px 76px;
  .title {
    justify-content: space-between;
    font-size: 18px;
    color: var(--color-1-);
    font-weight: 600;
    margin-bottom: 17px;
  }
  .check {
    width: 20px;
    text-align: center;
    line-height: 18px;
    border: 1px solid #E9EBF0;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 400;
    color: #82858C;
  }
  .box {
    justify-content: space-between;
    font-size: 12px;
    font-weight: 600;
    margin-bottom: 6px;
  }
  .bar-box {
    height: 16px;
    font-size: 12px;
    margin-bottom: 7px;
    font-weight: 600;
    overflow: hidden;
    .left, .right {
      line-height: 16px;
      color: #fff;
      white-space: nowrap;
    }
    .left {
      background-color: #EA4A45;
      padding-left: 4px;
    }
    .right {
      background-color: #07AC3A;
      text-align: right;
      padding-right: 4px;
    }
  }
  .vanrow {
    font-size: 12px;
    padding-bottom: 24px;
    position: relative;
    .border {
      position: absolute;
      top: 7px;
      bottom: 7px;
      left: 50%;
      transform: translateX(-50%);
      width: 2px;
      background-color: #EFEFEF;
    }
    .flex {
      column-gap: 4px;
    }
    .line-box {
      flex: 1;
      height: 4px;
    }
    .line {
      height: 4px;
      max-width: 100%;
    }
    .left {
      &+.left {
        margin-top: 5px;
      }
      .price {
        color: #FF6F30;
      }
      .index {
        background-color: #EA4A45;
      }
      .line {
        background-color: #FFD1BD;
      }
    }
    .right {
      &+.right {
        margin-top: 5px;
      }
      .price {
        color: #07AC3A;
      }
      .index {
        background-color: #07AC3A;
      }
      .line {
        background-color: #07AC3A;
      }
    }
  }
  .index {
    text-align: center;
    min-width: 14px;
    max-width: 14px;
    line-height: 14px;
    font-size: 10px;
    color: #fff;
    border-radius: 2px;
  }
}
.box2 {
  font-size: 12px;
  .sub-title {
    font-weight: 600;
    margin-bottom: 7px;
  }
  .wrap {
    background-color: #FAFCFF;
    border-radius: 5px;
    padding: 12px 11px;
  }
  .col {
    justify-content: space-between;
    &+.col {
      margin-top: 12px;
    }
    >.flex {
      column-gap: 3px;
    }
    .radius {
      width: 10px;
      height: 10px;
      border-radius: 50%;
    }
    .label {
      color: #A2A5AB;
    }
  }
  .left {
    .radius1 {
      background-color: #FF6F30;
    }
    .radius2 {
      background-color: #FFA882;
    }
    .radius3 {
      background-color: #FFD1BD;
    }
    .radius4 {
      background-color: #FFEAE1;
    }
  }
  .right {
    .radius1 {
      background-color: #62D585;
    }
    .radius2 {
      background-color: #71EC9D;
    }
    .radius3 {
      background-color: #96F6CA;
    }
    .radius4 {
      background-color: #D5FDEA;
    }
  }
}
.pie-box {
  padding-bottom: 27px;
}
</style>