<template>
  <div class="com" v-loading.body.fullscreen="fullscreenLoading" element-loading-background="rgba(255, 255, 255, 0)">
    <div class="sticky">
      <div class="header">
        <div class="back">
          <backIcon back="cancel" @cancel="cancel"/>
        </div>
        <!-- 收款人货币 -->
        {{ title || $t('fund.l7') }}
      </div>
      <div class="search-box">
        <div class="input-box flex">
          <img class="search" src="/img/deposit/search.png" alt="极度证券">
          <el-input v-model="value" :placeholder="$t('common.l23')"></el-input>
        </div>
      </div>
    </div>
    <div class="list">
      <!-- <div class="title">
        最近选择
      </div>
      <div class="item flex">
        <div>CNY</div>
        <van-icon v-if="currency == 'CNY'" name="passed" />
      </div> -->
      <van-index-bar :index-list="Object.keys(list1)">
        <template v-for="(item, index) in list1">
          <div :key="index">
            <van-index-anchor :index="index"></van-index-anchor>
            <div class="item flex" v-for="li in item" :key="li.id" @click="selectPay(li)">
              <div>{{ li.symbol }}</div>
              <van-icon v-if="currency == li.symbol" name="passed" />
            </div>
          </div>
        </template>
      </van-index-bar>
    </div>
  </div>
</template>
<script>
import { getCurrencyList } from "@/api/product";
export default {
  props: {
    currency: {
      default: ''
    },
    type: {
      default: ''
    },
    pay: {
      default: ''
    },
    title: {
      default: ''
    }
  },
  data () {
    return {
      value: '',
      list: {}
    }
  },
  computed: {
    list1() {
      if (!this.value.trim()) {
        return this.list
      }
      var list = {}
      Object.keys(this.list).forEach(item => {
        this.list[item].forEach(i => {
          if ((i.symbol).toLowerCase().indexOf(this.value.trim().toLowerCase()) !== -1 || i.title.indexOf(this.value.trim().toLowerCase()) !== -1) {
            if (list[item]) {
              list[item].push({
                symbol: i.symbol,
                id: i.id,
                title: i.title
              })
            } else {
              list[item] = [{
                symbol: i.symbol,
                id: i.id,
                title: i.title
              }]
            }
          }
        })
      })
      return list
    }
  },
  created () {
    this.getCurrencyList()
  },
  methods: {
    getCurrencyList() {
      this.fullscreenLoading = true
      getCurrencyList({
        type: this.type,
        pay: this.pay
      }).then(res => {
        var list = res.list || []
        list = list.sort((a, b) => (a.symbol+'').localeCompare(b.symbol+''))
        var listIndex = {}
        list.forEach(item => {
          var char = (item.symbol + '')['0'].toUpperCase()
          if (listIndex[char]) {
            listIndex[char].push({
              symbol: item.symbol,
              id: item.id,
              title: item.title
            })
          } else {
            listIndex[char] = [{
              symbol: item.symbol,
              id: item.id,
              title: item.title
            }]
          }
          this.list = listIndex
        })
      }).catch(() => {}).finally(() => {
        this.fullscreenLoading = false
      })
    },
    selectPay(item) {
      this.$emit('success', item)
    },
    cancel() {
      this.$emit('cancel')
    }
  }
}
</script>
<style lang="less" scoped>
.com {
  min-height: 100vh;
  background-color: var(--bg2-);
}
.sticky {
  position: sticky;
  top: 0;
  z-index: 10;
  background-color: var(--bg-);
  margin-bottom: 8px;
}
.header {
  position: relative;
  text-align: center;
  padding-top: 15px;
  padding-bottom: 17px;
  font-size: 18px;
  font-weight: 600;
  .back {
    position: absolute;
    left: 9px;
    top: 11px;
  }
}
.search-box {
  padding: 12px 17px 16px;
}
.input-box {
  background: #FFFFFF;
  box-shadow: 0px 2px 10px 0px rgba(212,212,212,0.5);
  border-radius: 18px;
  padding-left: 14px;
  padding-right: 30px;
  flex: 1;
  min-width: 10%;
  max-width: 100%;
  .search {
    width: 18px;
  }
  .el-input {
    flex: 1;
    min-width: 10%;
    max-width: 100%;
  }
}
.list {
  background-color: var(--bg-);
  padding-top: 10px;
  .title {
    padding: 22px 18px 12px;
    font-size: 18px;
    font-weight: 500;
    color: var(--color-1-);
  }
}
.van-index-bar {
  height: calc(100vh - 137px);
  overflow-y: auto;
}
.item {
  height: 44px;
  justify-content: space-between;
  cursor: pointer;
  padding: 0 18px;
  font-size: 18px;
  background-color: var(--bg-);
  color: var(--color-1-);
  .van-icon-passed {
    color: var(--color-);
    font-size: 20px;
  }
}
::v-deep {
  .input-box {
    .el-input__inner {
      border: none;
      height: 34px;
      line-height: 34px;
      padding-left: 4px;
    }
  }
  .van-index-anchor {
    padding: 0 18px;
    background-color: #F6FAFE;
    line-height: 34px;
    font-size: 16px;
    color: #95A0AC;
  }
  .van-index-bar__sidebar {
    right: 17px;
    z-index: 12;
  }
  .van-index-bar__index {
    line-height: 21px;
    padding: 0;
    width: 21px;
    text-align: center;
    font-size: 14px;
    color: #95A0AC;
    border-radius: 50%;
    &.van-index-bar__index--active {
      background-color: var(--color-);
      color: #fff;
    }
  }
}
</style>