<template>
  <div class="page" v-loading.body.fullscreen="fullscreenLoading" element-loading-background="rgba(255, 255, 255, 0)">
    <van-sticky class="sticky">
      <div class="header">
        <div class="back">
          <backIcon/>
        </div>
        <!-- 存款 -->
        {{ $t('home.l13') }}
      </div>
      <div class="box" v-if="orderInfo.id">
        <div class="left">
          <van-image
            width="37"
            height="37"
            round
            fit="cover"
            :src="`/img/user/${orderInfo.to.server ? orderInfo.to.server : 'usd'}.png?14`"
          />
          <div>
            <!-- 美元钱包账户 -->
            <div class="name">{{ serverName[orderInfo.to.server] || $t('account.l1') }}</div>
            <div>{{ $t('common.l33') }} {{ $numToThousand((accountInfo1.balance)) }} {{ serverUtil[accountInfo1.server] || 'USD' }}</div>
          </div>
        </div>
      </div>
    </van-sticky>
    <div class="section" v-if="orderInfo.id">
      <div class="method-box">
        <div class="title-box flex">
          <div class="title">
            <!-- 存款方式 -->
            {{ $t('fund.l49') }}
          </div>
          <div class="title">{{ payName[orderInfo.pay_type] }}</div>
        </div>
      </div>
    </div>
    <digitalInfo v-if="orderInfo.pay_type == 3" :info="orderInfo" />
    <div class="section" v-if="orderInfo.pay_type == 1 || orderInfo.pay_type == 2">
      <bankInfo :info="orderInfo" />
    </div>
    <div class="section" v-if="orderInfo.id">
      <div class="box2">
        <div class="title">
          <!-- 到账金额 -->
          {{ $t('account.l46') }}
        </div>
        <div class="input-box flex">
          <div class="amount">{{ $handleDigits2(arriveAmount) }}</div>
          <div>{{ serverUtil[orderInfo.to.server] || 'USD' }}</div>
        </div>
        <div class="fee flex">
          <!-- 手续费 -->
          <div>{{ $t('exchange.l20') }}:</div>
          <div class="val">{{ $handleDigits(orderInfo.fee) }} USD</div>
        </div>
      </div>
    </div>
    <div class="section" v-if="orderInfo.id">
      <digitalForm ref="form" v-if="orderInfo.pay_type == 3" />
      <bankForm ref="form" v-if="orderInfo.pay_type == 1 || orderInfo.pay_type == 2" />
    </div>
    <div class="section" v-if="orderInfo.id">
      <digitalMsg/>
    </div>
    <div class="fixed flex" v-if="orderInfo.id">
      <div class="left">
        <div class="number">{{ $handleDigits(payAmount) }}</div>
        <div>{{ payCurrencySplit(orderInfo.pay_currency) }}</div>
      </div>
      <div class="btn" @click="submit">
        <!-- 确认支付 -->
        {{ $t('fund.l74') }}
      </div>
    </div>
  </div>
</template>
<script>
import bus from '@/utils/bus'
import { mapGetters } from "vuex";
import digitalInfo from "./components/digitalInfo.vue";
import bankInfo from "./components/bankInfo.vue";
import digitalForm from "./components/digitalForm.vue";
import bankForm from "./components/bankForm.vue";
import digitalMsg from "./components/digitalMsg.vue";
import { submitDepositVoucher, getPayRecordList } from '@/api/assets'
export default {
  components: {
    digitalInfo,
    bankInfo,
    digitalForm,
    bankForm,
    digitalMsg,
  },
  data () {
    return {
      id: '',
      ordernum: '',
      orderInfo: {
        to: {},
        from: {}
      },
    }
  },
  computed: {
    ...mapGetters(['cashInfo', 'accountList']),
    payName() {
      return {
        // '1': '银行汇款',
        // '2': '国际电汇',
        // '3': '数字货币',
        // '101': '第三方支付',
        // '102': '第三方支付',
        '1': this.$t('account.l31'),
        '2': this.$t('account.l32'),
        '3': this.$t('account.l33'),
        '101': this.$t('account.l30'),
        '102': this.$t('account.l30'),
      }
    },
    arriveAmount() {
      return this.$bigDecimal.multiply(this.orderInfo.amount, this.orderInfo.arrive_rate)
    },
    payAmount() {
      return this.$bigDecimal.multiply(this.$bigDecimal.add(this.orderInfo.amount, this.orderInfo.fee), this.orderInfo.pay_rate)
    },
    accountInfo() {
      var info = {}
      this.accountList.forEach(item => {
        if (item.server == this.orderInfo.to.server) {
          info = item
        }
      })
      return info
    },
    accountInfo1() {
      return this.orderInfo.to.server ? this.accountInfo : this.cashInfo
    }
  },
  created () {
    bus.$emit('updateAssets', ['getCash', 'getAccountList'])
    this.id = this.$route.params.id
    this.ordernum = this.$route.params.ordernum
    this.getPayRecordList()
  },
  methods: {
    getPayRecordList() {
      this.fullscreenLoading = true
      getPayRecordList({
        ordernum: this.ordernum
      }).then(res => {
        var list = res.list || []
        if (list[0]) {
          var info = list[0]
          info.from = info.from ? JSON.parse(info.from) : {}
          info.to = info.to ? JSON.parse(info.to) : {}
          this.orderInfo = info
        }
      }).catch(() => {}).finally(() => {
        this.fullscreenLoading = false
      })
    },
    submit() {
      if (this.orderInfo.pay_type == 101 && this.orderInfo.from.url) {
        location.href = this.orderInfo.from.url
        return
      }
      var msg = this.$refs['form'].submit()
      if (!msg) {
        return
      }
      var data = {
        id: this.orderInfo.id,
        voucher: msg
      }
      this.fullscreenLoading = true
      submitDepositVoucher(data).then(res => {
        this.$notify({
          // message: '操作成功',
          message: this.$t('common.l52'),
          type: 'success'
        });
        this.$router.replace('/deposit-success')
      }).catch(() => {}).finally(() => {
        this.fullscreenLoading = false;
      })
    },
    payCurrencySplit(value) {
      if (this.orderInfo.pay_type == 3) {
        return value.split('-')[0]
      }
      return value
    }
  }
}
</script>
<style lang="less" scoped>
.page {
  min-height: 100vh;
  padding-bottom: 100px;
}
.sticky {
  background-color: var(--bg-);
}
.header {
  position: relative;
  text-align: center;
  padding-top: 15px;
  padding-bottom: 17px;
  font-size: 18px;
  font-weight: 600;
  background-color: var(--bg-);
  .back {
    position: absolute;
    left: 9px;
    top: 11px;
  }
}
.box {
  background-color: var(--bg-);
  padding: 7px 18px 10px;
  .left {
    display: flex;
    align-items: center;
    column-gap: 5px;
    font-size: 12px;
    color: #828282;
    padding: 14px 10px;
    border-radius: 6px;
    border: 1px solid #E6E6E6;
  }
  .name {
    font-size: 16px;
    color: #000;
    margin-bottom: 6px;
  }
}
.section {
  background-color: var(--bg-);
  margin-bottom: 10px;
  padding: 0 18px;
  .van-icon-arrow {
    font-size: 15px;
    color: #C3C3C3;
  }
  .title {
    font-size: 16px;
    color: #000;
  }
}
.method-box {
  padding-bottom: 8px;
  .title-box {
    padding: 0 11px;
    justify-content: space-between;
    height: 54px;
    border-radius: 6px;
    border: 1px solid #E6E6E6;
  }
}
.box2 {
  border-radius: 6px;
  border: 1px solid #E6E6E6;
  padding: 18px 11px 11px;
  .input-box {
    padding: 29px 0 14px;
    font-size: 20px;
    color: #232323;
    justify-content: space-between;
    border-bottom: 1px solid #EBEBEB;
    margin-bottom: 12px;
    font-weight: 600;
  }
  .fee {
    font-size: 12px;
    color: #8B8B8B;
    justify-content: space-between;
    .val {
      color: #232323;
    }
  }
  .amount {
    font-weight: 400;
  }
}
.fixed {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 3;
  background-color: var(--bg-);
  padding: 19px 18px;
  justify-content: space-between;
  .left {
    flex: 1;
    min-width: 10%;
    max-width: 100%;
    display: flex;
    align-items: baseline;
    color: #A7A7A7;
    column-gap: 8px;
    padding-right: 8px;
    .number {
      color: #000;
      font-size: 26px;
      font-weight: 600;
    }
  }
  .btn {
    line-height: 42px;
    border-radius: 6px;
    background-color: #000;
    color: #fff;
    text-align: center;
    cursor: pointer;
    padding: 0 13px;
    font-size: 16px;
  }
}
</style>