<template>
  <view class="chart-ranking">
    <template v-if="!$isEmpty(rankList)">
      <view class="rank-list rank-header">
        <view class="column">
          排名
        </view>
        <view class="column">
          姓名
        </view>
        <view class="column">
          {{ typeLabel }}
        </view>
        <view v-if="showRate" class="column">
          目标完成率
        </view>
      </view>

      <scroll-view
        scroll-y
        :style="{
          height: wrapperHeight + 'px'
        }"
        class="scroll-rank">
        <view class="rank-list">
          <view class="column">
            <view
              v-for="(item, index) in rankList"
              :key="index"
              class="column-item sort">
              <image
                v-if="index < 3"
                :src="$static(`images/rank/${item.sort}.png`)"
                alt=""
                class="icon-img" />
              <text
                v-else class="rank">
                {{ item.sort }}
              </text>
            </view>
          </view>
          <view class="column">
            <view
              v-for="(item, index) in rankList"
              :key="index"
              class="column-item user">
              <wk-avatar
                :name="item.realname"
                :avatar="item.img"
                :size="12"
                class="avatar" />
              <text class="username">
                {{ item.realname }}
              </text>
            </view>
          </view>
          <view v-if="showRate" class="column">
            <view
              v-for="(item, index) in rankList"
              :key="index"
              class="column-item money">
              <template v-if="!$isEmpty(item.money)">
                ￥{{ item.money }}
              </template>
            </view>
          </view>
          <view v-else class="column">
            <view
              v-for="(item, index) in rankList"
              :key="index"
              class="column-item count">
              <template v-if="!$isEmpty(item.count)">
                {{ item.count }}
              </template>
            </view>
          </view>

          <view v-if="showRate" class="column">
            <view
              v-for="(item, index) in rankList"
              :key="index"
              class="column-item rate">
              <template v-if="!$isEmpty(item.rate)&&item.rate!=='暂无目标'">
                {{ item.rate }}%
              </template>
            </view>
          </view>
        </view>
      </scroll-view>
    </template>

    <view
      v-else
      :style="{backgroundImage: bgUrl('images/no_data.png')}"
      class="no-data">
      暂无数据
    </view>


    <view
      v-if="!$isEmpty(selfData)"
      class="rank-list self">
      <view class="column">
        <view
          class="column-item sort">
          <image
            v-if="selfData.sort <= 3"
            :src="$static(`images/rank/${selfData.sort}.png`)"
            alt=""
            class="icon-img" />
          <text
            v-else class="rank">
            {{ selfData.sort }}
          </text>
        </view>
      </view>
      <view class="column">
        <view class="column-item user">
          <wk-avatar
            :name="selfData.realname"
            :avatar="selfData.img"
            :size="12"
            class="avatar" />
          <text class="username">
            {{ selfData.realname }}
          </text>
        </view>
      </view>

      <template v-if="showRate">
        <view class="column">
          <view class="column-item money">
            <template v-if="!$isEmpty(selfData.money)">
              ￥{{ selfData.money }}
            </template>
          </view>
        </view>
      </template>
      <template v-else>
        <view class="column">
          <view class="column-item count">
            <template v-if="!$isEmpty(selfData.count)">
              {{ selfData.count }}
            </template>
          </view>
        </view>
      </template>

      <view v-if="showRate" class="column">
        <view class="column-item rate">
          <template v-if="!$isEmpty(selfData.rate)&&selfData.rate!=='暂无目标'">
            {{ selfData.rate }}%
          </template>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  // import { RankingList } from 'API/crm/work'
  import {
    biRankingReceivablesAPI,
    biRankingContractAPI,
    biRankingContractCountAPI,
    biRankingCustomerCountAPI,
    biRankingContactsCountAPI,
    biRankingRecordCountAPI
  } from 'API/crm/work'
  import { mapGetters } from 'vuex'
  export default {
    name: 'ChartRanking',
    props: {
      params: {
        type: Object,
        default: () => {}
      }
    },
    data() {
      return {
        dropOptions: [
          { label: '合同金额', value: 1, request: biRankingContractAPI },
          { label: '回款金额', value: 2, request: biRankingReceivablesAPI },
          { label: '合同数', value: 3, request: biRankingContractCountAPI },
          { label: '新增客户数', value: 4, request: biRankingCustomerCountAPI },
          { label: '新增联系人', value: 5, request: biRankingContactsCountAPI },
          { label: '新增跟进记录数', value: 8, request: biRankingRecordCountAPI }
        ],
        dropVal: 2,
        rankList: [],

        selfData: {}
      }
    },
    computed: {
      ...mapGetters({
        userInfo: 'user/userInfo'
      }),
      typeLabel() {
        const findRes = this.dropOptions.find(o => o.value === this.dropVal)
        return findRes ? findRes.label : ''
      },
      wrapperHeight() {
        if (this.rankList.length <= 3) {
          return uni.upx2px(300)
        }
        if (this.rankList.length <= 7) {
          return uni.upx2px(100 * this.rankList.length)
        }
        return uni.upx2px(700)
      },
      showRate() {
        return [1, 2].includes(this.dropVal)
      },
      optionInfo() {
        if (
          this.dropVal == 1 ||
          this.dropVal == 2 ||
          this.dropVal == 7
        ) {
          return {
            key: 'money',
            unit: '元'
          }
        } else if (
          this.dropVal == 8
        ) {
          return {
            key: 'count',
            unit: '条'
          }
        }
        return {
          key: 'count',
          unit: '个'
        }
      },
    },
    watch: {
      params: {
        handler() {
          this.getRankData()
        },
        deep: true,
        immediate: true
      }
    },
    methods: {
      bgUrl(val) {
        return `url(${this.$static(val)})`
      },
      getData(val) {
        console.log('toggle: ', val)
        this.dropVal = val
        this.getRankData()
      },

      getRankData() {
        const request = this.dropOptions.find(item => item.value === this.dropVal).request
        request({
        ...this.params
        }).then(res => {
          const list = res || []
          const ranking = list.slice(0, 10)
          const myData = list.find(o => o.userId === this.userInfo.userId)
          this.rankList = ranking.map((item, index) => {
            item.sort = index + 1
            item.value = item[this.optionInfo.key]
            return item
          })
          if (myData) {
            this.selfData = {
              ...myData,
              value: myData[this.optionInfo.key]
            }
          } else {
            this.selfData = {
              ...this.userInfo,
              sort: '--',
              value: '--',
              rate: '--'
            }
          }
        }).catch()
      }
    }
  }
</script>

<style scoped lang="scss">
.no-data {
  width: 100%;
  height: 336rpx;
  text-align: center;
  font-size: 26rpx;
  color: #BBBBBB;
  background-position: center 50rpx;
  background-repeat: no-repeat;
  background-size: 25%;
  padding-top: 250rpx;
}

.rank-list {
  width: 100%;
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;

  .column {
    &:nth-child(1) {
      width: 80rpx;
    }
    &:nth-child(2) {
      flex: 1;
    }
    &:nth-child(3) {
      width: 220rpx;
    }
    &:nth-child(4) {
      width: 130rpx;
    }

    .column-item {
      font-size: $wk-font-medium;
      height: 100rpx;
      &.sort {
        font-size: 32rpx;
        color: #666;
        @include left;
        .icon-img {
          width: 46rpx;
          height: 40rpx;
        }
      }

      &.count {
        font-size: 32rpx;
        color: #666;
        @include left;
      }

      &.user {
        @include left;
        .avatar {
          width: 66rpx;
          height: 66rpx;
        }
        .username {
          max-width: 180rpx;
          font-size: $wk-font-medium;
          margin-left: 20rpx;
          @include ellipsis;
        }
      }

      &.money {
        font-size: $wk-font-medium;
        @include left;
        // @include ellipsis;
      }
      &.rate {
        font-size: $wk-font-medium;
        @include right;
      }
    }
  }
}

.scroll-rank {
  overflow: hidden;
}

.rank-header {
  padding-top: 15rpx;
  .column {
    height: 36rpx;
    line-height: 36rpx;
    font-size: $wk-font-mini;
    color: $light;
  }
}

.self {
  width: 100%;
  padding: 0;
  border-top: 1rpx solid $border-color;
}
</style>
