<template>
  <div class="wrapper">
    <div class="fixed">
      <div class="header">
        <div class="ai-area__wrapper" @click="showSelectOrg=true" :style="tabIndex == 1 ? 'width:100%;' : ''">
          <img src="./img/location-icon.png" alt="" class="location-icon">
          <span class="label" v-if="partyOrgName" style="color:#E6736E;">{{ partyOrgName }}</span>
          <span v-else style="color:#E6736E;">请选择</span>
          <u-icon name="arrow-down" color="#E6736E" size="24" style="margin-left: 4px" ></u-icon>
        </div>
        <div class="right" @click="showDate=true" v-if="tabIndex != 1">{{searchDate}}<u-icon name="arrow-down" color="#333" size="24" style="margin-left: 4px" ></u-icon></div>
      </div>
      <div class="tab-list">
        <div class="tab-item" v-for="(item, index) in tabList" :key="index" @click="tabClick(index)" :style="tabIndex == index ? 'color:#333;' : ''">{{ item }}
          <span class="active-line" v-if="tabIndex == index"></span>
        </div>
      </div>
    </div>
    <div class="list">
      <div class="item" v-for="(item, index) in list" :key="index">
        <div class="left">
          <div class="index">{{index+1}}</div>
          <img :src="item.avatarUrl" alt="" v-if="item.avatarUrl">
          <img src="./img/user-img.png" alt="" v-else>
          {{item.name}}
        </div>
        <div class="right">{{item.integral}}分</div>
      </div>
      <AiEmpty v-if="!list.length"/>
    </div>
    <u-picker mode="time" v-model="showDate" :params="dateParams" @confirm="selectDate"></u-picker>
    <u-select v-model="showSelectOrg" :list="partyOrgList"  @confirm="confirmSelect" value-name="id" label-name="name"></u-select>
  </div>
</template>
<script>
import {mapState} from 'vuex';

export default {
  name: "pointRankingList",
  appName: "积分排行",
  computed: {
    ...mapState(['user']),
  },
  data() {
    return {
      tabList: ['月度排行', '累计排行'],
      tabIndex: 0,
      searchDate: '',
      showDate: false,
      dateParams: {
        year: true,
        month: true,
        day: false,
        hour: false,
        minute: false,
        second: false
      },
      partyOrgId: '',
      partyOrgName: '',
      partyOrgList: [],
      showSelectOrg: false,
      current: 1,
      pages: 2,
      list: []
    };
  },
  onLoad() {
    uni.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: "#E76056",
    })
    this.partyOrgId = this.user.partyOrgId
    this.partyOrgName = this.user.partyOrgName
    var datetime = new Date();
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? '0' + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    this.searchDate = `${year}-${month}`
    this.getList()
    this.getPartyOrgList()
  },
  methods: {
    tabClick(index) {
      this.tabIndex = index
      this.getListInit()
    },
    selectDate(e) {
      this.searchDate = `${e.year}-${e.month}`
      this.getListInit()
    },
    getListInit() {
      this.current = 1
      this.pages = 2
      this.list = []
      this.getList()
    },
    getList() {
      if (this.current > this.pages) return;
      this.$instance.post(`/app/apppartyintegralinfo/listByOrg`, null, {
        params: {
          partyOrgId: this.partyOrgId,
          current: this.current,
          year: this.tabIndex == 1 ? '' : this.searchDate,
        }
      }).then((res) => {
        if (res.code == 0) {
          const list = this.current > 1 ? [...this.list, ...res.data.records] : res.data.records
          this.pages = res.data.pages
          this.list = list
        }
      });
    },
    getPartyOrgList() {
      this.$instance.post(`/app/partyOrganization/queryAllChildren`).then((res) => {
        if (res.code == 0) {
          this.partyOrgList = res.data
          this.partyOrgList.unshift({
            id: this.user.partyOrgId,
            name: this.user.partyOrgName
          })
        }
      });
    },
    confirmSelect(e) {
      this.partyOrgId = e[0].value
      this.partyOrgName = e[0].label
      this.getListInit()
    }
  },
  onReachBottom() {
    this.current ++
    this.getList()
  }
};
</script>
<style lang="scss" scope>
@import "~dvcp-wui/common";
.fixed{
  position: fixed;
  top: 0;
  left: 0;
  background-color: #fff;
  width: 100%;
  .header {
    width: 100%;
    background: #FFF;
    padding: 28px 32px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    height: 120px;
    .location-icon{
      width: 36px;
      height: 36px;
      vertical-align: bottom;
      margin-right: 8px;
    }
  }
  .ai-area__wrapper{
    width: calc(100% - 200px);
  }
  .right{
    width: 150px;
    text-align: center;
    padding: 0 24px;
    height: 56px;
    line-height: 56px;
    background: #EEE;
    border-radius: 28px;
    font-size: 26px;
    font-family: PingFangSC-Regular, PingFang SC;
    color: #333;
  }
  .tab-list{
    display: flex;
    .tab-item{
      flex: 1;
      text-align: center;
      height: 100px;
      line-height: 100px;
      font-size: 32px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #666;
      position: relative;
      .active-line{
        position: absolute;
        bottom: 0;
        left: 50%;
        margin-left: -24px;
        width: 48px;
        height: 6px;
        background: #E05953;
        border-radius: 4px;
      }
    }
  }
}
.list{
  padding-top: 236px;
  .item{
    padding: 32px 32px 32px 36px;
    border-bottom: 1px solid #ddd;
    display: flex;
    justify-content: space-between;
    background-color: #fff;
    .left{
      font-size: 32px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #333;
      line-height: 44px;
      .index{
        display: inline-block;
        width: 50px;
        color: #666;
      }
      img{
        width: 80px;
        height: 80px;
        border-radius: 50%;
        margin-right: 24px;
        vertical-align: middle;
      }
    }
    .right{
      font-size: 32px;
      font-family: PingFangSC-Semibold, PingFang SC;
      font-weight: 600;
      color: #E6736E;
      line-height: 44px;
      margin-top: 16px;
    }
  }
}
  
</style>
