<template>
  <div class="com" :style="{background: list.length === 0 ? '#fff' : ''}" v-loading.body.fullscreen="fullscreenLoading" element-loading-background="rgba(255, 255, 255, 0)">
    <div class="header">
      <div class="back">
        <backIcon back="cancel" @cancel="cancel"/>
      </div>
      <!-- 选择收款账户 -->
      {{ $t('fund.l103') }}
      <van-icon class="add" name="plus" size="20px" @click="$router.push('/add-pay')"/>
    </div>
    <div class="list" v-if="list.length">
      <div class="cell" v-if="list3.length">
        <!-- 数字货币 -->
        <div class="key">{{ $t('account.l33') }}</div>
        <div class="item" v-for="item in list3" :class="{active: info.id == item.id}" :key="item.id" @click="selectPay(item)">
          <img class="icon" src="/img/pay/icon5.svg" alt="极度证券">
          <div class="right">
            <!-- 数字货币 -->
            <div class="name">{{ $t('account.l33') }}: {{ item.value.currency }}</div>
            <div class="value">{{ $t('fund.l13') }}: {{ item.value.address }}</div>
          </div>
          <img class="delete" @click.stop="deleteClick(item)" src="/img/icon/delete1.png" alt="极度证券">
        </div>
      </div>
      <div class="cell" v-if="list1.length">
        <!-- 本地银行汇款 -->
        <div class="key">{{ $t('fund.l106') }}</div>
        <div class="item" v-for="item in list1" :class="{active: info.id == item.id}" :key="item.id" @click="selectPay(item)">
          <img class="icon" src="/img/pay/icon4.svg" alt="极度证券">
          <div class="right">
            <!-- 收款人 -->
            <div class="name">{{ $t('fund.l125') }}: {{ item.value.name }}({{ item.currency }})</div>
            <!-- 收款账号 -->
            <div class="value">{{ $t('fund.l126') }}: {{ item.value.bankName }}({{ item.value.account }})</div>
          </div>
          <img class="delete" @click.stop="deleteClick(item)" src="/img/icon/delete1.png" alt="极度证券">
        </div>
      </div>
      <div class="cell" v-if="list2.length">
        <!-- 国际电汇 -->
        <div class="key">{{ $t('account.l32') }}</div>
        <div class="item" v-for="item in list2" :class="{active: info.id == item.id}" :key="item.id" @click="selectPay(item)">
          <img class="icon" src="/img/pay/icon6.svg" alt="极度证券">
          <div class="right">
            <div class="name">{{ $t('fund.l125') }}: {{ item.value.name }}({{ item.currency }})</div>
            <!-- 收款账号 -->
            <div class="value">{{ $t('fund.l126') }}: {{ item.value.bankName }}({{ item.value.account }})</div>
            <div class="value">SWIFT: {{ item.value.swift }}</div>
          </div>
          <img class="delete" @click.stop="deleteClick(item)" src="/img/icon/delete1.png" alt="极度证券">
        </div>
      </div>
    </div>
    <!-- 您暂无收款账户信息,请立即添加 -->
    <NoData :title="$t('fund.l116')" v-if="list.length === 0" pTop="204px"/>
    <div class="btn-box" v-if="list.length === 0">
      <div class="btn" @click="$router.push('/add-pay')">
        <!-- 添加 -->
        {{ $t('fund.l104') }}
      </div>
    </div>
    <van-popup get-container="body" v-model="deletePayPop" round position="bottom">
      <deletePay @cancel="deletePayPop = false" @success="deleteBack"/>
    </van-popup>
  </div>
</template>
<script>
import { getCollectionAccountList, deleteCollectionAccount } from "@/api/user";
import deletePay from "./deletePay.vue";
export default {
  props: ['info'],
  components: {
    deletePay
  },
  data () {
    return {
      list: [],
      deletePayPop: false,
      deleteInfo: {}
    }
  },
  computed: {
    list1() {
      return this.list.filter(item => item.type == 1)
    },
    list2() {
      return this.list.filter(item => item.type == 2)
    },
    list3() {
      return this.list.filter(item => item.type == 3)
    }
  },
  created () {
    this.getCollectionAccountList()
  },
  methods: {
    getCollectionAccountList() {
      this.fullscreenLoading = true
      getCollectionAccountList({
      }).then(res => {
        var list = res.list || []
        list.forEach(item => {
          item.value = JSON.parse(item.value)
        })
        this.list = list
      }).finally(() => {
        this.fullscreenLoading = false
      }).catch(() => {})
    },
    selectPay(item) {
      this.$emit('success', item)
    },
    cancel() {
      this.$emit('cancel')
    },
    deleteClick(item) {
      this.deleteInfo = item
      this.deletePayPop = true
    },
    deleteBack() {
      this.fullscreenLoading = true
      deleteCollectionAccount({
        id: this.deleteInfo.id
      }).then(() => {
        this.$notify({
          // message: '删除成功',
          message: this.$t('common.l27'),
          type: 'success'
        });
        this.deletePayPop = false
        this.getCollectionAccountList()
        if (this.info.id == this.deleteInfo.id) {
          this.$emit('reset')
        }
      }).catch(() => {
        this.fullscreenLoading = false
      })
    }
  }
}
</script>
<style lang="less" scoped>
.com {
  min-height: 100vh;
  background-color: #fff;
}
.header {
  position: sticky;
  top: 0;
  z-index: 2;
  text-align: center;
  padding-top: 15px;
  padding-bottom: 17px;
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 10px;
  background-color: var(--bg-);
  .back {
    position: absolute;
    left: 9px;
    top: 11px;
  }
  .add {
    position: absolute;
    right: 17px;
    top: 15px;
    cursor: pointer;
    font-weight: 600;
  }
}
.list {
  padding: 0 17px;
}
.cell {
  padding-bottom: 17px;
  .key {
    font-size: 16px;
  }
}
.item {
  padding: 9px 7px;
  margin-top: 10px;
  background-color: var(--bg-);
  position: relative;
  border-radius: 6px;
  border: 1px solid #E3E3E3;
  display: flex;
  align-items: flex-start;
  &.active {
    border-color: #000;
  }
  .icon {
    width: 46px;
  }
  .right {
    padding-top: 5px;
    flex: 1;
    min-width: 10%;
    max-width: 100%;
    padding-left: 11px;
    word-break: break-all;
  }
  .name {
    font-size: 14px;
    line-height: 16px;
  }
  .value {
    font-size: 12px;
    margin-top: 9px;
    color: #979797;
  }
  .delete {
    position: absolute;
    top: 5px;
    right: 4px;
    cursor: pointer;
    width: 14px;
  }
}
.btn-box {
  padding: 31px 17px 20px;
  .btn {
    cursor: pointer;
    background: #000000;
    border-radius: 6px;
    line-height: 44px;
    text-align: center;
    font-size: 16px;
    font-weight: 600;
    color: #FEFFFF;
  }
}
</style>