<template>
  <div class="ticket-stats__container">
    <van-nav-bar title="奖票统计" left-text="返回" @click-left="$route_back" left-arrow/>
    <van-search v-model="keyword" placeholder="搜索" />
    <van-dropdown-menu>
      <van-dropdown-item :title="dateTitle" ref="dateDropdownItem">
        <DateSelectorDropdown :title.sync="dateTitle" @onConfirm="onConfirmDate"/>
      </van-dropdown-item>
      <van-dropdown-item title="全公司" ref="deptDropdownItem">
        <DeptSelectorDropdown @onConfirm="onConfirmDept"/>
      </van-dropdown-item>
      <div class="dropdown-menu__item">
        <span>降序</span>
      </div>
    </van-dropdown-menu>
    <div class="ticket-list__content">
      <scroller ref="scroller" :on-refresh="onRefresh">
        <van-cell-group>
          <van-cell v-for="(item, index) in data" :key="index">
            <div slot="icon" class="ticket-item__icon-wrap">
              <icon :name="mapRankIconName[index]" v-if="index < 3" class="ticket-item__icon"></icon>
              <span v-else class="ticket-item__icon-label">{{ index + 1 }}</span>
            </div>
            <div class="ticket-item__name-wrap" slot="title">
              <span class="ticket-item__name-icon">{{ getIconText(item.name) }}</span>
              <span class="ticket-item__name">{{ item.name }}</span>
            </div>
            <div class="ticket-item__value" slot="right-icon">
              <span>{{ item.count }}</span>
              <span class="ticket-item__unit">张</span>
            </div>
          </van-cell>
        </van-cell-group>
      </scroller>
    </div>
  </div>
</template>

<script>
  import Vue from 'vue'
  import request from '@/utils/request'
  import {Cell, CellGroup, Search, DropdownMenu, DropdownItem} from 'vant'
  import DateSelectorDropdown from '@/components/common/DateSelectorDropdown'
  import DeptSelectorDropdown from '@/components/common/DeptSelectorDropdown'

  Vue.use(Cell).use(CellGroup).use(Search).use(DropdownMenu).use(DropdownItem)

  export default {
    name: 'ticket_statistics',
    data () {
      return {
        data: [{name: '武钰', count: 100}, {name: '魏岱', count: 90}, {name: '郭婷婷', count: 80}],
        dateTitle: '本月',
        keyword: null,
        mapRankIconName: {0: 'rank-first', 1: 'rank-second', 2: 'rank-third'}
      }
    },
    components: {DateSelectorDropdown, DeptSelectorDropdown},
    created () {
      /** 为了优化性能，在 searchParams 的设值时触发diff 比较. 所以searchParams没有放在data 里定义*/
      this.searchParams = {page_size: 10}
    },
    methods: {
      getIconText (employeeName) {
        if (employeeName && employeeName.length > 2) {
          const len = employeeName.length
          return employeeName[len - 2] + employeeName[len - 1]
        }
        return employeeName
      },
      onConfirmDate (item) {
        if (item.dateMode === 1) {
          this.searchParams.month = item.startDate
          this.searchParams.end_day = null
          this.searchParams.start_day = null
        } else {
          this.searchParams.month = null
          this.searchParams.end_day = item.startDate
          this.searchParams.start_day = item.endDate
        }
        this.$refs.dateDropdownItem.toggle()
        this.$refs.scroller.triggerPullToRefresh()
      },
      onConfirmDept (item) {
        this.searchParams.dept_ids = item ? item.id : null
        this.$refs.deptDropdownItem.toggle()
        this.$refs.scroller.triggerPullToRefresh()
      },
      onRefresh (doneCallback) {
        this.searchParams.page = 1
        this.getData(doneCallback)
      },
      getData (callback=function(){}) {
        request('get','/api/integral/statistics/integral', this.searchParams).then((res) => {
			callback()
          const {scroller} = this.$refs
          if (res.data.code === 1) {
            const {list} = res.data.data

            this.searchParams.page++
            this.eventData = [].concat(this.eventData, list)
            scroller.finishInfinite(list.length < this.searchParams.page_size)
          } else {
            scroller.finishInfinite(true)
          }
        })
      }
    }
  }
</script>

<style scoped lang="less">
  .ticket-stats__container {
    position: relative;

    & /deep/ .van-nav-bar {
      background-color: #26a2ff;
    }

    & .dropdown-menu__item {
      display: flex;
      flex: 1;
      min-width: 0;
      font-size: 0.3rem;
      color: #323233;
      align-items: center;
      justify-content: center;
      cursor: pointer;
    }

    & .ticket-list__content {
      position: relative;
      height: calc(100% - 3rem);
    }

    & .ticket-item__icon-wrap {
      display: flex;
      padding-right: 0.32rem;
      width: 0.64rem;
      align-items: center;
      justify-content: center;

      & .ticket-item__icon {
        width: 0.64rem;
      }

      & .ticket-item__icon-label {
        position: relative;
      }
    }

    .ticket-item__name-wrap {
      display: flex;
      width: 100%;
      height: 100%;
      align-items: center;

      & .ticket-item__name-icon {
        position: relative;
        display: flex;
        width: 0.96rem;
        height: 0.96rem;
        font-size: 0.3rem;
        color: #fff;
        background: #26a2ff;
        align-items: center;
        justify-content: center;
        border-radius: 100%;
      }

      & .ticket-item__name {
        padding-left: 0.24rem;
      }
    }

    & .ticket-item__value {
      display: flex;
      color: #303133;
      align-items: center;
      text-align: right;
    }

    & .ticket-item__unit {
      padding-left: 0.1rem;
      font-size: 0.28rem;
      color: #909399;
    }
  }
</style>
