<template>
  <div style=" background-color: #fff; padding:15px; padding-top:0; overflow:hidden; height: 370px;">




    <el-table
      v-loading="loading"
      ref="rankTable"
      :data="rankList"
      tooltip-effect="dark"
      style="width: 100%"
      :header-cell-style="{ textAlign : 'left' }"  :row-class-name="tableRowClassName">


      <el-table-column label="姓名" prop="target_name" width="120px"><template slot-scope="scope">
        <div class="rank_head"><img v-if="scope.row.target_img != null" v-bind:src="scope.row.target_img" width="42px" height="42px"/>
          <img v-if="scope.row.target_img == null" src="static/images/head_default.png" width="42px" height="42px"/>
          {{scope.row.target_name}}</div>
      </template></el-table-column>

      <el-table-column label="排行">
        <template slot-scope="scope">
          <div class="table-font-number" v-if="scope.row.index <= 3">
            <img src="static/images/rank_01.png" width="20" v-if="scope.row.index == 1">
            <img src="static/images/rank_02.png" width="20" v-if="scope.row.index == 2">
            <img src="static/images/rank_03.png" width="20" v-if="scope.row.index == 3">
          </div>


          <div class="table-font-number" v-if="scope.row.index > 3">
            No.{{ (currentPage - 1) * 10 + scope.row.index }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="总分">
        <template slot-scope="scope">
          <div style="color:#409EFF; font-size: 18px;"
            v-bind:class="'table-font-xl table-font-number ' + (scope.row.p_sum < 0 ? 'point-negative' : 'point-positive')">
            {{ scope.row.p_sum }}
          </div>
        </template>
      </el-table-column>

    </el-table>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        profile: this.$store.getters.user_info,
        loading:false,
        filter: {
          keywords: '',
          target_id_set: [],
          category_id: [],
          dept_id: [],
          point_type: 0,
          time_range: '',
          include_manager: false
        },
        totalCount: 0,
        currentPage: 1,
        rankList: null,
        categoryTree: [],
        deptTree: [],
        employeeOptions: [],
        employeeList:[],
        pointTypes: [],
        instantPickerOptions: {
          shortcuts: [{
            text: '本周',
            onClick(picker) {
              const now = new Date(new Date().toLocaleDateString())
              let start = now.getTime() - (now.getDay() - 1) * 24 * 60 * 60 * 1000
              let end = start + 7 * 24 * 60 * 60 * 1000 - 1000
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '上周',
            onClick(picker) {
              const now = new Date(new Date().toLocaleDateString())
              let start = now.getTime() - (now.getDay() + 6) * 24 * 60 * 60 * 1000
              let end = start + 7 * 24 * 60 * 60 * 1000 - 1000
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '本月',
            onClick(picker) {
              const now = new Date()
              let startDate = new Date(now.getFullYear(), now.getMonth(), 1)
              let endDate = new Date(now.getFullYear(), now.getMonth() + 1, 0)
              picker.$emit('pick', [startDate.getTime(), endDate.getTime()])
            }
          }, {
            text: '上月',
            onClick(picker) {
              const now = new Date()
              let startDate = new Date(now.getFullYear() - (now.getMonth() > 0 ? 0 : 1), (now.getMonth() + 11) % 12, 1)
              let endDate = new Date(now.getFullYear(), now.getMonth(), 0)
              picker.$emit('pick', [startDate.getTime(), endDate.getTime()])
            }
          }, {
            text: '本年',
            onClick(picker) {
              const now = new Date()
              let startDate = new Date(now.getFullYear(), 0, 1)
              let endDate = new Date(now.getFullYear() + 1, 0, 0)
              picker.$emit('pick', [startDate.getTime(), endDate.getTime()])
            }
          }]
        }
      }
    },
    methods: {
      onFilterChanged: function () {
        this.currentPage = 1
        this.loadRankList()
      },
      changePage: function (current) {
        if (isNaN(current) || current < 1) {
          return false
        }
        this.loadRankList()
      },


      tableRowClassName({row, rowIndex}) {

        if (row.target_id == this.$store.getters.user_info.id) {
          return 'rank-mine';
        }
        return '';
      },

      updateEmployeeRange: function (currentVal) {
        if (currentVal) {
          this.loadTargetList()
          this.onFilterChanged()
        } else {
          var self = this
          this.loadTargetList(() => {
            let idArr = self.employeeOptions.map((item) => {
              return item.id
            })
            self.filter.target_id_set = self.filter.target_id_set.filter((item) => idArr.indexOf(item) > -1);
            self.onFilterChanged()
          })
        }
      },
      //获取控件所需数据
      loadWidgetData: function () {
        var self = this
        this.$http('get','/integral.php/ajax_request_common/prepare_integral_options',{id: this.profile.id,category_tree: 1,dept_tree: 1}).then(function (response) {
          if (response.status == 200) {
            var jsonData = response.data
            try {
              self.categoryTree = jsonData.category_tree
              self.deptTree = jsonData.dept_tree
            } catch (err) {
              console.log(err)
            }
          }
        }).catch(function (error) {
          console.log(error)
        })
      },
      //获取可排名的员工列表
      loadTargetList: function (callback) {
        var self = this
        self.loading = true
          this.$http('get','/integral.php/point_data/get_employee_by_superior_id').then(function (response) {
          if (response.status == 200) {
            var jsonData = response.data
            try {


              //得到下级后再获得排名
              self.employeeOptions = jsonData.data
              self.loadRankList();
              if (callback) {
                callback.call()
              }
            } catch (err) {
              console.log(err)
            }
          }
        }).catch(function (error) {
          console.log(error)
        });
      },
      loadPointType: function () {
        var self = this
        this.$http('get','/integral.php/ajax_request_common/get_point_types').then(function (response) {
          if (response.status == 200) {
            let jsonData = response.data
            try {
              self.pointTypes = jsonData
            } catch (err) {
              console.log(err)
            }
          }
        }).catch(function (error) {
          console.log(error)
        })
      },
      loadRankList: function () {
        const target_id_set = []
        for(let item in this.employeeOptions){
          target_id_set.push( this.employeeOptions[item].id)
        }

        console.log(target_id_set.toString())

        let params = {
          site_id: this.profile.site_id,
          page: this.currentPage,
          dept_id: this.profile.dept_id,
          interval:6
        }
        for (let item in this.filter) {
          let value = this.filter[item]
          if (item == 'target_id_set') {
            if (!value || value.length == 0) {
              continue
            }
            params[item] = value.join(",")
          } else if (item == 'category_id' || item == 'dept_id') {
            if (!value || value.length == 0) {
              continue
            }
            params[item] = value[value.length - 1]
          } else if (item == 'time_range') {
            if (!value || value.length < 2) {
              continue
            }
            let timeRangeArr = value.map(x => parseInt(x / 1000))
            timeRangeArr[1] += 60 * 60 * 24 - 1
            params[item] = timeRangeArr.join(",")
          } else if (item == 'include_manager') {
            params['exclude_manager'] = value ? 0 : 1
          } else {
            params[item] = value
          }
        }
        var self = this
        self.loading = true
        this.$http('get','/integral.php/point_data/rank_by_employee',params).then(function (response) {
          if (response.status == 200) {
            self.loading = false
            let jsonData = response.data
            try {
              self.totalCount = jsonData.total_count
              self.rankList = jsonData.list_data
            } catch (err) {
              console.log(err)
            }
          }
        }).catch(function (error) {
          console.log(error)
        })
      }
    },
    created() {
     // this.loadWidgetData()
      //this.loadTargetList()
      //this.loadPointType()
      this.loadRankList()
    }
  }
</script>


<style>
  .rank_head img{width:30px;
    height: 30px;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px; margin-right:5px;}
  .rank_head *{
    vertical-align: middle;}
</style>
