<template>
  <div class="event-list__container">
    <van-nav-bar title="积分明细" left-text="返回" @click-left="$route_back" left-arrow></van-nav-bar>
    <div class="flex-box header flex-v-ce">
      <div class="flex-1 flex-box flex-v-ce">
        <userImage class="about-me__avatar" :img_url="userData.employee_img_url" :user_name="userData.employee_name" fontSize="0.28rem" width="1rem" height="1rem"></userImage>
        <span style="margin-left: 0.2rem;"><Wxopendata type="userName" :openid="userData.employee_name"></Wxopendata></span>
      </div>
      <div>
        <span v-if="userData.point >= 0" class="color_red">+{{ userData.point }}</span>
        <span v-if="userData.point < 0" class="color_green">{{ userData.point }}</span>
      </div>
    </div>
    <div class="text" v-if="userData_tow.site_config.base_point">基础分：{{userData_tow.site_config.base_point}}   工龄分：{{userData_tow.point_config.service_point}}</div>
    <div v-else class="text">该明细不包含基础分与工龄分</div>
    <van-dropdown-menu active-color="#26a2ff"><van-dropdown-item :options="sxArr" v-model="pointType" @change="onChangePointType" /></van-dropdown-menu>
    <div class="event-list__content">
      <scroller ref="scroller" :on-refresh="refresh" :on-infinite="infinite" noDataText="我也是有底线的" :list="eventData">
        <van-cell-group>
          <div class="event-list__item" v-for="(item, index) in eventData" :key="index">
            <div class="flex-box">
              <div class="flex-1 event-content__text overflow_two">{{ item.remark }}</div>
              <div>
                <span class="color_red" v-if="item.point >= 0">+{{ item.point }}{{ types_list[item.pt_id] }}</span>
                <span class="color_green" v-else>{{ item.point }}{{ types_list[item.pt_id] }}</span>
              </div>
            </div>
            <div class="event-time__text">{{ item.event_time }}</div>
          </div>
        </van-cell-group>

        <noData :list="eventData" />
      </scroller>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import request from '@/utils/request'
import { DropdownMenu, DropdownItem, Empty, Search } from 'vant'
import userImage from '@/components/common/user_image'
Vue.use(Empty)
  .use(DropdownMenu)
  .use(DropdownItem)
  .use(Search)

export default {
  name: 'integralDetail',
  data () {
    return {
      loading: true,
      eventData: [],
      userData: {},
      searchParams: {
        employee_ids: null,
        page: 1,
        page_size: 10,
        rule_id: '',
        item_id: '',
        year: '',
        month: '',
        quarter: '',
        pt_id: '3'
      },
      types_list: {},
      sxArr: [{ text: '按事件时间排序', value: 1 }, { text: '按积分从高到底排序', value: 2 }, { text: '按积分从低到高排序', value: 3 }],
      pointType: 1,
      novice: true,
      userData_tow: {
        site_config: {
          base_point: 0
        },
        point_config: {
          service_point: 0
        }
      }
    }
  },
  components: { userImage },
  created () {
    if (this.$route.query.item) {
      var item = JSON.parse(this.$route.query.item)
      this.userData = item
      this.searchParams.employee_ids = item.employee_id
      this.searchParams.rule_id = item.rule_id
      this.searchParams.item_id = item.item_id
      this.searchParams.year = item.year
      this.searchParams.month = item.month
      this.searchParams.quarter = item.quarter
      if (item.employee_id && this.$route.query.isLj) {
        this.getUserPoint()
      }
    }
    this.get_point_types()
  },
  methods: {
    getUserPoint () {
      request('get', '/api/employee/detail', {target: this.searchParams.employee_ids}).then(res => {
        if (res.data.code === 1) {
          this.userData_tow = res.data.data.user
        }
      })
    },
    get_point_types () {
      let self = this
      for (let i in this.$store.getters.point_types) {
        self.types_list[this.$store.getters.point_types[i].id] = this.$store.getters.point_types[i].name
      }
    },
    refresh (done) {
      this.novice = true
      this.searchParams.page = 1
      this.getEventData(function () {
        self.eventData = []
        done()
      })
    },
    infinite (done) {
      if (this.novice) {
        this.searchParams.page++
        this.getEventData(done)
      }
    },
    onChangePointType () {
      this.novice = true
      this.searchParams.page = 1
      if (this.pointType !== 1) {
        this.searchParams.point_sort = this.pointType == 2 ? 'desc' : 'asc'
      } else {
        this.searchParams.point_sort = undefined
      }
      this.$refs.scroller.triggerPullToRefresh()
    },
    getEventData (callback = function () {}) {
      this.loading = true
      request('get', '/api/integral/statistics/integral', this.searchParams)
        .then(res => {
          callback()
          const { scroller } = this.$refs
          if (res.data.code === 1) {
            scroller.finishInfinite(res.data.data.list.length != this.searchParams.page_size) // 停止上滚加载下一页，由于服务器端没有统一空数据和正常数据的格式，所以通过total字段来判定数据是否存在下一页
            const { list } = res.data.data
            if (list.length == 0) {
              this.novice = false
            }
            if (this.searchParams.page === 1) {
              this.eventData = list
            } else {
              this.eventData = this.eventData.concat(list)
            }
          } else {
            scroller.finishInfinite(true)
          }
        })
        .finally(() => {
          this.loading = false
        })
    }
  }
}
</script>

<style scoped lang="less">
.text {
  padding: 0.1rem 0.32rem;
  color: #909399;
  font-size: 0.28rem;
}
.header {
  background-color: #fff;
  padding: 0.24rem 0.32rem;
}
.event-list__container {
  position: relative;

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

  /deep/ .van-dropdown-menu__title--active {
    color: #26a2ff;
  }

  & /deep/ .van-dropdown-menu__item {
    &:nth-child(1),
    &:nth-child(2) {
      flex-grow: 0.9;
    }

    &:nth-child(4) {
      flex-grow: 1.25;

      & /deep/ .van-dropdown-menu__title {
        right: 0.06rem;
      }
    }
  }

  & .event-list__content {
    position: relative;
    height: calc(100% - 3.9rem);
    & .van-hairline--top-bottom:after,
    .body_com .van-hairline-unset--top-bottom:after {
      border: none;
    }
  }

  & .event-list__item {
    position: relative;
    display: flex;
    padding: 0.24rem 0.32rem;
    font-size: 0.32rem;
    color: #333;
    background: #fff;
    box-sizing: border-box;
    flex-direction: column;
    touch-action: none;

    &:not(:last-child)::after {
      position: absolute;
      box-sizing: border-box;
      content: ' ';
      pointer-events: none;
      right: 0;
      bottom: 0;
      left: 0rem;
      border-bottom: 1px solid #ebedf0;
      -webkit-transform: scaleY(0.5);
      transform: scaleY(0.5);
    }
    & .event-content__text {
      position: relative;
      color: #333;
    }

    & .event-time__text {
      position: relative;
      padding-top: 0.08rem;
      font-size: 0.28rem;
      color: #909399;
    }
  }
}
</style>
