<template>
  <div class="components" v-loading.body.fullscreen="fullscreenLoading" element-loading-background="rgba(255, 255, 255, 0)">
    <div class="place" v-if="!symbol">
      <div class="search-box flex" @click="searchShow = true">
        <img class="img" src="/img/exchange/search.png" alt="极度证券">
        <div>
          <!-- 请输入代码 -->
          {{ $t('exchange.l40') }}
        </div>
      </div>
    </div>
    <div class="section" v-if="symbol">
      <div class="box flex" @click="searchShow = true">
        <div>
          <div class="name">
            {{ symbolInfo.title }}
          </div>
          <div class="code">{{ symbol }}</div>
        </div>
        <div class="flex right" v-if="symbolInfo.symbol">
          <chart :count="40" :info="symbolInfo" id="easychart" width="48px" height="27px" />
          <div>{{ $handleDigits(productDataObj[symbol].ask, symbolInfo.digits) }}</div>
          <div>{{ productDataObj[symbol].spread > 0 ? '+' : ''  }}{{ productDataObj[symbol].percentage }}%</div>
        </div>
      </div>
      <div class="de-box">
        <div class="de-top flex">
          <div class="left" v-if="bidVolPer">{{ bidVolPer }}%</div>
          <div class="line-box flex">
            <div class="line1" :style="{ width: bidVolPer + '%' }" v-if="bidVolPer"></div>
            <div class="line2" :style="{ width: askVolPer + '%' }" v-if="askVolPer"></div>
          </div>
          <div class="right" v-if="askVolPer">{{ askVolPer }}%</div>
        </div>
        <div class="de-bottom flex">
          <div class="dep">
            <div class="left flex" 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>
          </div>
          <div class="dep">
            <div class="right flex" 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>
          </div>
        </div>
      </div>
    </div>
    <div class="section" v-if="symbolInfo.symbol">
      <easyCfd v-if="symbolInfo.market == 'CFD'" :symbol="symbol" :accountInfo="accountInfo" :symbolCopy="symbolCopy" />
      <easyStock v-else :symbol="symbol" :accountInfo="accountInfo" :symbolCopy="symbolCopy" />
    </div>
    <van-popup v-model="searchShow" position="right" :style="{ width: '100%' }">
      <search :market="accountInfo.market || symbolInfo.market" v-if="searchShow" type="1" @success="searchBack" @cancel="searchShow = false"/>
    </van-popup>
  </div>
</template>
<script>
import chart from "@/components/eChart/index.vue";
import search from "./search.vue";
import easyCfd from "./open/easyCfd.vue";
import easyStock from "./open/easyStock.vue";
import { getPriceDepth } from "@/api/product";
import bus from '@/utils/bus'
export default {
  props: ['symbol', 'accountInfo', 'symbolCopy'],
  components: {
    search,
    chart,
    easyCfd,
    easyStock
  },
  data () {
    return {
      searchShow: false,
      tooltipShow: false,
      asks: [],
      bids: [],
      askSign: false,
      bidSign: false,
    }
  },
  inject: ['getproductDataObj'],
  computed: {
    productDataObj() {
      return this.getproductDataObj()
    },
    symbolInfo() {
      return this.productDataObj[this.symbol] || {}
    },
    // sumMoney() {
    //   if (this.type == 1 && !Number(this.price)) {
    //     return '--'
    //   }
    //   return Number(this.$bigDecimal.multiply(this.type == 1 ? this.price : this.productDataObj[this.symbol].ask, this.$bigDecimal.multiply(this.volume, this.symbolInfo.contract_size))).toFixed(2)
    // },
    // sumMoney1() {
    //   if (this.type == 1 && !Number(this.price)) {
    //     return '--'
    //   }
    //   return Number(this.$bigDecimal.multiply(this.type == 1 ? this.price : this.productDataObj[this.symbol].bid, this.$bigDecimal.multiply(this.volume, this.symbolInfo.contract_size))).toFixed(2)
    // },
    // availableBuy() {
    //   if (!Number(this.price)) {
    //     return '0.00'
    //   }
    //   return this.$bigDecimal.multiply(this.accountInfo.margin_free, 50)
    // },
    // availableSell() {
    //   if (!Number(this.price)) {
    //     return '0.00'
    //   }
    //   return this.$bigDecimal.multiply(this.accountInfo.margin_free, 50)
    // },
    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))
    },
    // buyDisable() {
    //   if (!this.isLogin || !Number(this.price) || !Number(this.volume)) {
    //     return false
    //   }
    //   return Number(this.volume) > Number(this.availableBuy)
    // },
    // sellDisable() {
    //   if (!this.isLogin || !Number(this.price) || !Number(this.volume)) {
    //     return false
    //   }
    //   return Number(this.volume) > Number(this.availableBuy)
    // },
  },
  watch: {
    symbol(val) {
      if (val) {
        this.getPriceDepth()
      }
    },
  },
  beforeDestroy() {
    bus.$off('updateDepth', this.updateDepth)
  },
  created () {
    bus.$on('updateDepth', this.updateDepth)
    if (this.symbol) {
      this.getPriceDepth()
    }
  },
  methods: {
    searchBack(val) {
      this.$router.replace(`/exchange/${val}/buy`)
      this.searchShow = false
    },
    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, 1);
        this.bids = (res.bids || []).sort((a, b) => Number(b.price) - Number(a.price)).filter(item => Number(item.price)).slice(0, 1);
        // 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, 1)
      this.bids = (msg.bids || []).sort((a, b) => Number(b.price) - Number(a.price)).filter(item => Number(item.price)).slice(0, 1)
      // 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
        }]
      }
    },
  }
}
</script>
<style lang="less" scoped>
.place {
  padding: 15px 16px 0;
  background-color: var(--bg-);
  color: #C3C3C3;
  .search-box {
    cursor: pointer;
    border-radius: 4px;
    border: 1px solid #D9D9D9;
    height: 38px;
    padding: 0 13px;
  }
  .img {
    width: 20px;
  }
}
.section {
  margin-bottom: 6px;
  background-color: var(--bg-);
  padding: 0 16px;
}
.box {
  padding: 12px 0;
  color: var(--color-1-);
  justify-content: space-between;
  .name {
    font-size: 15px;
    font-weight: 500;
    margin-bottom: 10px;
  }
  .code {
    color: #AEAEAE;
    font-size: 12px;
  }
  .right {
    column-gap: 26px;
    color: #F24040;
  }
}
.de-box {
  padding: 0 0 10px;
  .de-top {
    column-gap: 4px;
    justify-content: space-between;
    font-size: 12px;
    margin-bottom: 3px;
    .line-box {
      flex: 1;
      column-gap: 2px;
    }
    .left {
      color: var(--red-);
    }
    .right {
      color: var(--green-);
    }
    .line1, .line2 {
      height: 4px;
      border-radius: 2px;
    }
    .line1 {
      background: var(--red-);
    }
    .line2 {
      background: var(--green-);
    }
  }
  .de-bottom {
    border-radius: 4px;
    overflow: hidden;
    .dep {
      width: 50%;
      font-size: 12px;
      color: #262626;
    }
    .left {
      background-color: rgba(252, 81, 87, 0.13);
      height: 26px;
      padding-left: 3px;
      padding-right: 6px;
      justify-content: space-between;
      .index {
        background-color: var(--red-);
      }
      .price {
        color: var(--red-);
      }
    }
    .right {
      background-color: rgba(149, 198, 156, 0.13);
      height: 26px;
      padding-left: 3px;
      padding-right: 6px;
      justify-content: space-between;
      .index {
        background-color: var(--green-);
      }
      .price {
        color: var(--green-);
      }
    }
    .index {
      width: 11px;
      line-height: 11px;
      border-radius: 2px;
      font-size: 10px;
      margin-right: 4px;
      color: #fff;
      text-align: center;
    }
  }
}
</style>