<template>
  <div class="section3">
    <van-row class="row" v-if="bidVolPer || askVolPer">
      <van-col class="flex" span="12">
        <div>
          <!-- 买盘 -->
          {{ $t('product.l46') }}
        </div>
        <div class="vol voll" v-if="bidVolPer">{{ bidVolPer }}%</div>
      </van-col>
      <van-col class="flex col" span="12">
        <div class="flex">
          <div>
            <!-- 卖盘 -->
            {{ $t('product.l47') }}
          </div>
          <div class="vol volr" v-if="askVolPer">{{ askVolPer }}%</div>
        </div>
        <div class="check">{{ number }}</div>
      </van-col>
    </van-row>
    <div class="bar-box flex" v-if="bidVolPer || askVolPer">
      <div class="left" :style="{ width: bidVolPer + '%' }" v-if="bidVolPer"></div>
      <div class="right" :style="{ width: askVolPer + '%' }" v-if="askVolPer"></div>
    </div>
    <van-row class="vanrow">
      <van-col span="12">
        <div class="left flex" :class="{index0: index == 0}" v-for="(item, index) in bids" :key="'a' + index">
          <div class="flex">
            <div class="index">{{ index + 1 }}</div>
            <div class="price">{{ $handleDigits(item.price, symbolInfo.digits) }}</div>
          </div>
          <div>{{ Number(item.volume) }}</div>
        </div>
      </van-col>
      <van-col span="12">
        <div class="right flex" :class="{index0: index == 0}" v-for="(item, index) in asks" :key="index">
          <div class="flex">
            <div class="index">{{ index + 1 }}</div>
            <div class="price">{{ $handleDigits(item.price, symbolInfo.digits) }}</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() {
      var vol = 0
      this.asks.forEach(item => {
        if (Number(item.volume) > vol) {
          vol = Number(item.volume)
        }
      })
      return vol || 1
    },
    maxBidVol() {
      var 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: 11px 10px 50px;
}
.row {
  margin-bottom: 15px;
  .vol {
    font-size: 12px;
    padding-left: 4px;
  }
  .voll {
    color: #F6465D;
  }
  .volr {
    color: #2EBD85;
  }
  .col {
    justify-content: space-between;
  }
  .check {
    background-color: #F6F6F6;
    font-size: 12px;
    color: #B6B6B6;
    line-height: 14px;
    width: 14px;
    text-align: center;
  }
}
.bar-box {
  margin-bottom: 4px;
  overflow: hidden;
  position: relative;
  height: 4px;
  .left, .right {
    height: 4px;
    padding-left: 1px;
  }
  .left {
    background:linear-gradient(-45deg, transparent 2.5px,#F6465D 0);
  }
  .right {
    background:linear-gradient(135deg, transparent 2.5px,#2EBD85 0);
  }
}
.vanrow {
  font-size: 12px;
  position: relative;
  .index {
    text-align: center;
    min-width: 12px;
    max-width: 12px;
    line-height: 12px;
    font-size: 10px;
    color: #fff;
    border-radius: 1px;
  }
  .left,.right {
    justify-content: space-between;
    padding: 10px 10px;
    >.flex {
      column-gap: 6px;
    }
  }
  .left {
    border-right: 1px solid #fff;
    background-color: rgba(226, 74, 89, 0.05);
    &.index0 {
      background-color: #FBEDED;
    }
    .price {
      color: #F6465D;
    }
    .index {
      background-color: #F6465D;
    }
  }
  .right {
    background-color: rgba(77, 153, 123, 0.05);
    &.index0 {
      background-color: #EDF5F1;
    }
    .price {
      color: #2EBD85;
    }
    .index {
      background-color: #2EBD85;
    }
  }
}
.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>