<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">
        <div class="label">
          <!-- 代码 -->
          {{ $t('exchange.l39') }}
        </div>
        <div class="right flex" @click="searchShow = true">
          <div>{{ symbol }}</div>
          <i class="el-icon-arrow-right"></i>
        </div>
      </div>
      <div class="box flex" v-if="symbolInfo.symbol">
        <div class="label" v-if="language == 'zh-CN'">
          {{ symbolInfo.title }}
        </div>
        <div class="label" v-else-if="language == 'zh-TW'">
          {{ symbolInfo.name_cht || symbolInfo.title }}
        </div>
        <div class="label" v-else>
          {{ symbolInfo.name_en || (symbolInfo.market == 'CFD' ? symbolInfo.symbol : symbolInfo.title) }}
        </div>
        <div class="flex right">
          <div class="flex" :class="{'down': productDataObj[symbol].spread < 0}">
            <div>{{ $handleDigits(productDataObj[symbol].ask, symbolInfo.digits) }}</div>
            <div>{{ productDataObj[symbol].percentage > 0 ? '+' : ''  }}{{ $handleDigits(productDataObj[symbol].spread, symbolInfo.digits) }}</div>
            <div>{{ productDataObj[symbol].percentage > 0 ? '+' : ''  }}{{ productDataObj[symbol].percentage }}%</div>
          </div>
          <i class="el-icon-arrow-down" :class="{'active': showChart == 2}" @click="toggleChart"></i>
        </div>
      </div>
      <div v-if="symbolInfo.symbol && showChart == 2">
        <quotationk :symbol="symbol" :symbolInfo="symbolInfo" height="299px" />
      </div>
    </div>
    <div class="section" v-if="symbolInfo.symbol">
      <div class="box2 flex">
        <div class="label">
          <!-- 类型 -->
          {{ $t('exchange.l41') }}
          <i class="el-icon-warning-outline"></i>
        </div>
        <div class="right flex" @click="typeShow = true">
          <div class="type">
            <!-- 限价单   市价单-->
            {{ type == 1 ? $t('exchange.l42') : $t('exchange.l43') }}
          </div>
          <i class="el-icon-arrow-right"></i>
        </div>
      </div>
      <div class="box2 flex" v-if="type == 1">
        <div class="label">
          <!-- 价格 -->
          {{ $t('market.l4') }}
        </div>
        <div class="right flex">
          <div class=" flex">
            <van-field v-model="price" type="number" />
          </div>
          <div class="icon-box flex">
            <i class="el-icon-minus" @click="addPrice(1)"></i>
            <i class="el-icon-plus" @click="addPrice(2)"></i>
          </div>
        </div>
      </div>
      <div class="box2 flex">
        <div class="label">
          <!-- 手数 -->
          {{ $t('exchange.l16') }}
          <i class="el-icon-warning-outline"></i>
        </div>
        <div class="right flex">
          <div class="flex">
            <van-field v-model="volume" type="number" />
          </div>
          <div class="icon-box flex">
            <i class="el-icon-minus" @click="addVolume(1)"></i>
            <i class="el-icon-plus" @click="addVolume(2)"></i>
          </div>
        </div>
      </div>
      <div class="box4 flex">
        <div class="left flex">
          <div>
            <div class="label mr">
              <!-- 数量 -->
              {{ $t('common.l25') }}
            </div>
            <div class="label mr">
              <!-- 当前杠杆 -->
              {{ $t('exchange.l44') }}
            </div>
          </div>
          <div>
            <div class="mr">{{ $bigDecimal.multiply(volume, symbolInfo.contract_size) }}</div>
            <div class="mr">{{ (symbolInfo.contract_size1 || symbolInfo.margin_divider).toFixed(0) }}</div>
          </div>
        </div>
        <div class="left flex">
          <div>
            <div class="label mr">
              <!-- 交易金额 -->
              {{ $t('exchange.l45') }}
            </div>
            <div class="label mr">
              <!-- 可用资金 -->
              {{ $t('exchange.l46') }}
            </div>
          </div>
          <div>
            <div class="mr">{{ sumMoney }}</div>
            <div class="mr" v-if="accountInfo.account">{{ $numToThousand($handleDigits(accountInfo.margin_free)) }}</div>
            <div class="mr" v-else>0.00</div>
          </div>
        </div>
      </div>
      <div class="box2 flex">
        <div class="label">
          <!-- 止损 -->
          {{ $t('exchange.l4') }}
          <i class="el-icon-warning-outline"></i>
        </div>
        <div class="right flex">
          <div class="flex">
            <van-field v-model="sl" type="number" />
          </div>
          <div class="icon-box flex">
            <i class="el-icon-minus" @click="addsl(1)"></i>
            <i class="el-icon-plus" @click="addsl(2)"></i>
          </div>
        </div>
      </div>
      <div class="box2 flex">
        <div class="label">
          <!-- 止盈 -->
          {{ $t('exchange.l5') }}
          <i class="el-icon-warning-outline"></i>
        </div>
        <div class="right flex">
          <div class="flex">
            <van-field v-model="tp" type="number" />
          </div>
          <div class="icon-box flex">
            <i class="el-icon-minus" @click="addtp(1)"></i>
            <i class="el-icon-plus" @click="addtp(2)"></i>
          </div>
        </div>
      </div>
      <div class="box2 flex" v-if="type == 1">
        <div class="label">
          <!-- 期限 -->
          {{ $t('exchange.l47') }}
          <i class="el-icon-warning-outline"></i>
        </div>
        <div class="right flex" @click="termShow = true">
          <div class="type">
            <!-- 当日有效   撤单前有效-->
            {{ term == 1 ? $t('exchange.l48') : $t('exchange.l49') }}
          </div>
          <i class="el-icon-arrow-right"></i>
        </div>
      </div>
      <div class="box3">
        <div class="btn-box flex">
          <div class="btn buy" @click="submitClick(0)">
            <!-- 买入 -->
            {{ $t('exchange.l50') }}
          </div>
          <div class="btn sell" @click="submitClick(1)">
            <!-- 卖出 -->
            {{ $t('exchange.l51') }}
          </div>
        </div>
      </div>
    </div>
    <van-popup v-model="typeShow" round position="bottom">
      <priceType :type="type" @success="selectTypeBack"/>
    </van-popup>
    <van-popup v-model="termShow" round position="bottom">
      <termType :term="term" @success="selectTermBack"/>
    </van-popup>
    <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 quotationk from "./quotationk.vue";
import priceType from "./priceType.vue";
import termType from "./termType.vue";
import search from "./search.vue";
import { openPosition, openEntrust } from "@/api/exchange";
import { getStorage, setStorage } from '@/utils/localStorage'
import bus from '@/utils/bus'
export default {
  props: ['symbol', 'symbolInfo', 'accountInfo'],
  components: {
    priceType,
    termType,
    search,
    quotationk
  },
  data () {
    return {
      price: '',
      volume: '1.00',
      tp: '0',
      sl: '0',
      type: 2,
      term: 1,
      showChart: getStorage('showChart') || 2,
      typeShow: false,
      searchShow: false,
      termShow: false,
      tooltipShow: false
    }
  },
  inject: ['getproductDataObj'],
  computed: {
    productDataObj() {
      return this.getproductDataObj()
    },
    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)
    },
    maxBuy() {
      let a = this.$bigDecimal.multiply(this.type == 1 ? (Number(this.price) || 1) : this.productDataObj[this.symbol].ask, this.$bigDecimal.divide(1 * this.symbolInfo.contract_size, this.symbolInfo.margin_divider))
      return Number(this.$bigDecimal.divide(this.$bigDecimal.multiply(this.accountInfo.margin_free, this.symbolInfo.contract_size || 1), a)).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)
    },
    // buyDisable() {
    //   if (!this.isLogin || !Number(this.price) || !Number(this.volume)) {
    //     return false
    //   }
    //   return Number(this.sumMoney) > Number(this.availableBuy)
    // },
    // sellDisable() {
    //   if (!this.isLogin || !Number(this.price) || !Number(this.volume)) {
    //     return false
    //   }
    //   return Number(this.sumMoney) > Number(this.availableBuy)
    // }
  },
  watch: {
    'symbolInfo.market'() {
      if (this.appParams.marketVolume && this.symbolInfo.market) {
        this.volume = Number(this.appParams.marketVolume[this.symbolInfo.market]).toFixed(2)
      }
    },
    'appParams.marketVolume': {
      handler() {
        if (this.appParams.marketVolume && this.symbolInfo.market) {
          this.volume = Number(this.appParams.marketVolume[this.symbolInfo.market]).toFixed(2)
        }
      },
      deep: true
    }
  },
  created () {
    if (this.appParams.marketVolume && this.symbolInfo.market) {
      this.volume = Number(this.appParams.marketVolume[this.symbolInfo.market]).toFixed(2)
    }
  },
  methods: {
    submitClick(cmd) {
      if (!this.isLogin) {
        this.$router.push('/login?rejectUrl=' + this.$route.path)
        return
      }
      // if (cmd === 0 && this.buyDisable) {
      //   return
      // }
      // if (cmd === 1 && this.sellDisable) {
      //   return
      // }
      if (this.type == 1) {
        if (!Number(this.price)) {
          this.$notify({
            // message: '请输入价格',
            message: this.$t('exchange.l52'),
            type: 'danger'
          })
          return
        }
      }
      if (!Number(this.volume)) {
        this.$notify({
          // message: '请输入数量',
          message: this.$t('exchange.l53'),
          type: 'danger'
        })
        return
      }
      if (this.type == 2) {
        this.openPosition(cmd)
      }
      if (this.type == 1) {
        let cmd1 = 0
        let price = Number(this.price)
        if (cmd == 0) {
          if (price > this.productDataObj[this.symbol].ask) {
            cmd1 = 4
          } else {
            cmd1 = 2
          }
        } else {
          if (price > this.productDataObj[this.symbol].bid) {
            cmd1 = 3
          } else {
            cmd1 = 5
          }
        }
        this.openEntrust(cmd1)
      }
    },
    openPosition(cmd) {
      this.fullscreenLoading = true
      openPosition({
        account: this.accountInfo.account,
        symbol: this.symbol,
        cmd: cmd,
        volume: this.volume,
        server: this.accountInfo.server,
        tp: Number(this.tp) === 0 ? '' : Number(this.tp),
        sl: Number(this.sl) === 0 ? '' : Number(this.sl),
      }).then(() => {
        this.$notify({
          // message: '下单成功',
          message: this.$t('exchange.l54'),
          type: 'success'
        })
        // bus.$emit('updateAssets', ['getAccountList'])
        // bus.$emit('updateOrder')
      }).finally(() => {
        this.fullscreenLoading = false
      }).catch((err) => {
        if (err.code && err.code == 134) {
          this.$emit('deposit')
        }
      })
    },
    openEntrust(cmd) {
      this.fullscreenLoading = true
      openEntrust({
        account: this.accountInfo.account,
        symbol: this.symbol,
        cmd: cmd,
        price: this.price,
        expiration: this.term,
        volume: this.volume,
        server: this.accountInfo.server,
        tp: Number(this.tp) === 0 ? '' : Number(this.tp),
        sl: Number(this.sl) === 0 ? '' : Number(this.sl),
      }).then(() => {
        this.$notify({
          // message: '下单成功',
          message: this.$t('exchange.l54'),
          type: 'success'
        })
        // bus.$emit('updateOrder')
      }).finally(() => {
        this.fullscreenLoading = false
      }).catch(() => {

      })
    },
    addPrice(type) {
      if (type == 1) {
        if (!Number(this.price)) {
          return
        }
        this.price = Number(this.$bigDecimal.subtract(Number(this.price) || 0, this.symbolInfo.point)) + ''
      }
      if (type == 2) {
        this.price = Number(this.$bigDecimal.add(Number(this.price) || 0, this.symbolInfo.point)) + ''
      }
    },
    addsl(type) {
      if (type == 1) {
        if (!Number(this.sl)) {
          return
        }
        this.sl = Number(this.$bigDecimal.subtract(Number(this.sl) || 0, this.symbolInfo.point)) + ''
      }
      if (type == 2) {
        this.sl = Number(this.$bigDecimal.add(Number(this.sl) || 0, this.symbolInfo.point)) + ''
      }
    },
    addtp(type) {
      if (type == 1) {
        if (!Number(this.tp)) {
          return
        }
        this.tp = Number(this.$bigDecimal.subtract(Number(this.tp) || 0, this.symbolInfo.point)) + ''
      }
      if (type == 2) {
        this.tp = Number(this.$bigDecimal.add(Number(this.tp) || 0, this.symbolInfo.point)) + ''
      }
    },
    addVolume(type) {
      if (type == 1) {
        if (!Number(this.volume) || Number(this.volume) <= 0.01) {
          return
        }
        this.volume = Number(this.$bigDecimal.subtract(Number(this.volume) || 0, 0.01)) + ''
      }
      if (type == 2) {
        this.volume = Number(this.$bigDecimal.add(Number(this.volume) || 0, 0.01)) + ''
      }
    },
    changePrice() {
      this.price = this.$handleDigits(this.symbolInfo.ask, this.symbolInfo.digits) + ''
    },
    selectTypeBack(type) {
      this.typeShow = false
      if (this.type == type) {
        return
      }
      this.tp = '0'
      this.sl = '0'
      this.type = type
      this.price = this.$handleDigits(this.symbolInfo.ask, this.symbolInfo.digits) + ''
    },
    selectTermBack(term) {
      this.term = term
      this.termShow = false
    },
    searchBack(val) {
      this.$router.replace(`/exchange/${val}/buy`)
      this.searchShow = false
    },
    toggleChart() {
      this.showChart = this.showChart == 1 ? 2 : 1
      setStorage('showChart', this.showChart)
    }
  }
}
</script>
<style lang="less" scoped>
.place {
  padding: 15px 18px 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: 8px;
  background-color: var(--bg-);
}
.box {
  padding: 12px 18px;
  column-gap: 7px;
  border-bottom: 1px solid #F0F0F0;
  .label {
    width: 110px;
    color: #7C7C7C;
    font-size: 13px;
  }
  .right {
    flex: 1;
    justify-content: space-between;
    min-width: 10%;
    max-width: 100%;
    color: var(--color-1-);
    cursor: pointer;
    .flex {
      color: #F24040;
      column-gap: 11px;
      &.down {
        color: var(--green-);
      }
    }
  }
  .place {
    color: #9F9F9F;
  }
  .el-icon-arrow-down {
    cursor: pointer;
    transform: rotateZ(180deg);
    transition: transform 0.5s;
    &.active {
      transform: rotateZ(0);
    }
  }
  .el-icon-arrow-right, .el-icon-arrow-down {
    font-size: 16px;
    color: #AAAAAA;
  }
}
.box2 {
  padding: 0 18px;
  column-gap: 7px;
  border-bottom: 1px solid #F0F0F0;
  font-size: 13px;
  height: 39px;
  .label {
    width: 110px;
    color: #7C7C7C;
  }
  .right {
    flex: 1;
    justify-content: space-between;
    min-width: 10%;
    max-width: 100%;
    color: var(--color-1-);
  }
  .el-icon-arrow-right {
    font-size: 18px;
    color: #AAAAAA;
  }
  .icon-box {
    column-gap: 13px;
    color: #313232;
  }
  .el-icon-plus, .el-icon-minus {
    font-size: 18px;
    color: #313232;
  }
  .icon {
    width: 18px;
  }
  .van-field {
    padding: 0 15px 0 0;
  }
}
.box4 {
  justify-content: flex-end;
  padding: 12px 17px;
  font-size: 12px;
  color: #313232;
  column-gap: 48px;
  border-bottom: 1px solid #F0F0F0;
  .label {
    color: #9F9F9F;
  }
  .left {
    column-gap: 8px;
  }
  .mr + .mr {
    margin-top: 10px;
  }
}
.box3 {
  padding: 18px 16px 16px;
  .btn-box {
    flex: 1;
    column-gap: 11px;
    position: relative;
  }
  .btn {
    flex: 1;
    cursor: pointer;
    font-size: 16px;
    color: #fff;
    line-height: 41px;
    text-align: center;
    border-radius: 21px;
    font-weight: 600;
  }
  .buy {
    background-color: #FC5157;
    &.dis {
      cursor: not-allowed;
      opacity: 0.5;
    }
  }
  .sell {
    background-color: #62D585;
    &.dis {
      cursor: not-allowed;
      opacity: 0.5;
    }
  }
  .icon1 {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 24px;
  }
  .img-box {
    column-gap: 3px;
  }
  .icon {
    width: 22px;
  }
}
</style>