<template>
  <div class="mrd-container">
   <van-nav-bar left-text="返回" @click-left="$route_back" left-arrow>
      <div class="department" slot="right">
        <DeptSelectorBtn title="选择部门" v-model="dept" :multi="false"></DeptSelectorBtn>
      </div>
    </van-nav-bar>
    <van-search v-model="keyword" placeholder="输入姓名搜索" />
    <YearMonthPicker v-model="month"/>
    <div class="mrd-filter__wrap">
      <van-checkbox v-model="checkedAllStandard" icon-size="0.32rem" checked-color="#07c160">只看全部达标的</van-checkbox>
    </div>
    <div class="mrd-content">
      <scroller ref="scroller" :on-refresh="onRefresh" :on-infinite="onInfinite">
        <div class="mrd-list__item" v-for="(item, i) in mrdList" :key="i" @click="viewDetail(item)">
          <div class="mrd-item__header">
            <!-- <span class="mrd-item__icon">{{ getIconText(item.name) }}</span> -->
            
            <userImage :img_url="item.img_url" :user_name="item.name" width="0.72rem" height="0.72rem" fontSize="0.15" style="margin-right: 0.1rem"></userImage>
            <span><Wxopendata type="userName" :openid="item.name"></Wxopendata></span>
            <span class="mrd-item__standard" v-if="item.status === 1">全部达标</span>
          </div>
          <div class="mrd-item__content">
            <div class="mrd-item">
              <span class="mrd-item__value">{{ item.reward.point }}</span>
              <span class="mrd-item__label">奖分
               <!-- <icon name="doubt" height="0.24rem" width="0.24rem"/> -->
              </span>
              <span class="mrd-item__target">目标{{ item.reward.target }}</span>
            </div>
            <div class="mrd-item">
              <span class="mrd-item__value">{{ item.deduction.point }}</span>
              <span class="mrd-item__label">扣分
               <!-- <icon name="doubt" height="0.24rem" width="0.24rem"/> -->
              </span>
              <span class="mrd-item__target">目标{{ item.deduction.target }}</span>
            </div>
            <div class="mrd-item">
              <span class="mrd-item__value black-color">{{ getRewardRatio(item) }}</span>
              <span class="mrd-item__label">奖扣比例
               <!-- <icon name="doubt" height="0.24rem" width="0.24rem"/> -->
              </span>
              <span
                class="mrd-item__target">目标>{{ getExecuteTargetRatio(item) }}</span>
            </div>
            <div class="mrd-item">
              <span class="mrd-item__value black-color">{{ item.exec.count }}</span>
              <span class="mrd-item__label">奖扣人次
               <!-- <icon name="doubt" height="0.24rem" width="0.24rem"/> -->
              </span>
              <span class="mrd-item__target">目标{{ item.exec.target }}</span>
            </div>
          </div>
          <div class="mrd-standard__wrap" v-if="item.status === 0">
            <div class="mrd-standard__item">
              <span class="mrd-standard__text" v-if="item.reward.status === 1">达标</span>
            </div>
            <div class="mrd-standard__item">
              <span class="mrd-standard__text" v-if="item.deduction.status === 1">达标</span>
            </div>
            <div class="mrd-standard__item">
              <span class="mrd-standard__text" v-if="item.ratio.status === 1">达标</span>
            </div>
            <div class="mrd-standard__item">
              <span class="mrd-standard__text" v-if="item.exec.status === 1">达标</span>
            </div>
          </div>
        </div>
         <noData :list="mrdList" />
      </scroller>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import moment from 'moment'
import {Search, Checkbox, Empty} from 'vant'
import request from '@/utils/request'
import YearMonthPicker from '@/components/common/YearMonthPicker'
import DeptSelectorBtn from '@/components/common/DeptSelectorBtn'
import {_debounce } from '@/utils/auth'
import userImage from '@/components/common/user_image'
Vue.use(Search).use(Checkbox).use(Empty)
export default {
  name: 'manager_reward_deduction',
  components: {YearMonthPicker, userImage, DeptSelectorBtn},
  data () {
    return {
      data: null,
      dept: [],
      keyword: null,
      loading: true,
      checkedAllStandard: false,
      month: moment().format('YYYY-MM')
    }
  },
  watch: {
    month () {
      this.onMonthPickerChange()
    },
    dept (newVal) {
      if (Array.isArray(newVal) && newVal.length > 0) {
        this.searchParams.dept_id = newVal[0].dept_id
      } else {
        this.searchParams.dept_id = null
      }
      this.onMonthPickerChange()
    },
    keyword: {
      deep: true,
      handler: _debounce(function () {
        this.onMonthPickerChange()
      })
    }
  },
  computed: {
    mrdList () {
      if (Array.isArray(this.data) && this.checkedAllStandard) {
        return this.data.filter(item => item.status === 1)
      }
      return this.data
    },
    hasData () {
      if (this.loading) {
        return false
      }
      return !this.mrdList || this.mrdList.length === 0
    }
  },
  created () {
    this.searchParams = {
      dept_id: null,
      page: 1,
      page_size: 10,
      employee_id: this.$store.getters.user_info.id,
      deptId: null
    }
  },
  methods: {
    getRewardRatio (item) {
      if (item.ratio.status === 0) {
        return '-'
      }
      return `${item.ratio.reward_ratio}:1`
    },
    getExecuteTargetRatio (item) {
      return item.ratio.target <= 0 ? '0:0' : `${item.ratio.target}:1`
    },
    getIconText (employeeName) {
      if (employeeName && employeeName.length > 2) {
        const len = employeeName.length
        return employeeName[len - 2] + employeeName[len - 1]
      }
      return employeeName
    },
    onMonthPickerChange () {
      this.searchParams.page = 1
      this.getData()
    },
    viewDetail (item) {
      this.$router.push({name: 'reward_deduction_statistics', query: {employee_id: item.id, month: this.month}})
    },
    onRefresh (done) {
      var self = this
      this.searchParams.page = 1
      this.getData(function () {
        self.data = []
        done()
      })
    },
    onInfinite (done) {
      this.searchParams.page++
      this.getData(done)
    },
    getData (callBack) {
      this.loading = true
      this.$toast.loading({
        // mask: true,
        // type: 'spinner',
        message: '加载中...'
      })
      var self = this
      const params = {...this.searchParams, month: this.month, keyword: this.keyword}
      request('get', '/api/integral/statistics/prize/list', params, 'v4').then(res => {
        callBack && callBack()
        if (res.data.code === 1) {
          const list = res.data.data.list
          this.$refs.scroller.finishInfinite(list.length != 10) // 停止上滚加载下一页，由于服务器端没有统一空数据和正常数据的格式，所以通过total字段来判定数据是否存在下一页
          if (this.searchParams.page === 1) {
            this.data = list
          } else {
            this.data = this.data.concat(list)
          }
        } else {
          self.$refs.scroller.finishInfinite(true)
        }
      }).finally(() => {
        self.$toast.clear()
        this.loading = false
      })
    }
  }
}
</script>

<style scoped lang="less">
  .mrd-container {
    & /deep/ .department .van-button {
      background-color: transparent;
      border: none;
      color: #fff;
    }

    & .mrd-filter__wrap {
      position: relative;
      padding: 0.15rem 0.32rem;

      & /deep/ .van-checkbox__label {
        font-size: 0.28rem;
        color: #909399;
      }

      & /deep/ .van-checkbox__icon {
        position: relative;
        top: -0.05em;
      }
    }

    & .mrd-content {
      position: relative;
      height: calc(100% - 3.72rem);
    }

    & .mrd-list__item {
      margin-bottom: 0.24rem;
      padding: 0 0.32rem 0.32rem 0.32rem;
      font-size: 0.32rem;
      color: #303133;
      background: #fff;
      touch-action: none;

      & .mrd-item__header {
        display: flex;
        padding: 0.24rem 0;
        align-items: center;

        & .mrd-item__icon {
          display: flex;
          margin-right: 0.16rem;
          width: 0.56rem;
          height: 0.56rem;
          font-size: 0.24rem;
          color: #fff;
          background: #26a2ff;
          align-items: center;
          justify-content: center;
          border-radius: 100%;
        }

        & .mrd-item__standard {
          margin-left: 0.2rem;
          padding: 0.02rem 0.08rem;
          font-size: 0.24rem;
          color: #fff;
          background: #53B87F;
          border-radius: 0.06rem;
        }
      }

      & .mrd-item__content {
        display: flex;
        flex-direction: row;

        & .mrd-item {
          display: flex;
          flex: 1;
          align-items: center;
          justify-content: center;
          flex-direction: column;

          & /deep/ .icon-doubt {
            position: absolute;
            top: 0.13rem;
            right: -0.3rem;
          }

          & .mrd-item__value {
            color: #26a2ff;
            font-size: 0.44rem;
            font-weight: 600;

            &.black-color {
              color: #444444;
            }
          }

          & .mrd-item__label {
            position: relative;
            display: inline-block;
            padding-top: 0.04rem;
            font-size: 0.28rem;
            color: #303133;
            font-weight: 400;
          }

          & .mrd-item__target {
            padding-top: 0.08rem;
            font-size: 0.24rem;
            color: #909399;
            font-weight: 400;
          }
        }
      }

      .mrd-standard__wrap {
        display: flex;
        padding-top: 0.1rem;
        flex-direction: row;

        .mrd-standard__item {
          display: flex;
          flex: 1;
          justify-content: center;

          & .mrd-standard__text {
            padding: 0.02rem 0.08rem;
            font-size: 0.24rem;
            color: #fff;
            background: #53B87F;
            border-radius: 0.06rem;
          }
        }
      }
    }
  }
</style>
