<template>
  <div style="height: 100%;" :class="{ bg_fff: skeletonLoad }">
    <van-nav-bar title="待办事项" left-text="" left-arrow @click-left="routerBak" />
    <VanSkeleton :skeLoad="skeletonLoad">
      <template>
        <van-tabs v-model="type">
          <van-tab title="待处理" name="noEntering"></van-tab>
          <van-tab title="已处理" name="entering"></van-tab>
        </van-tabs>
        <header class="performanceList flex-box-ce" v-show="type=='noEntering'">
          <div class="flex-1">
            当前待办：
            <span v-if="commission > 0" style="color: #e42000;font-size: .29rem;">{{ commission }}条</span>
          </div>
          <div class="selector" @click="selectPfTIme">
            <span>{{ selectPftiText }}</span>
            <van-icon name="arrow-down" />
          </div>
        </header>

        <scroller v-show="type=='noEntering'" ref="work_bench_scroller" class="all" style="position: relative" :on-refresh="refresh" :on-infinite="infinite" noDataText="我也是有底线的" :list="backlogList">

         <template v-if="plcList.length>0&&doSthForData.node_type==0||doSthForData.node_type==10">
            <div class="backlog_list_tit">绩效确认</div>
            <div v-for="(arr, keys) in plcList" :key="arr.id" :style="'z-index:' + (plcList.length - keys)" class="performanceList backlog_list" @click="openAffirm(arr)">
              <van-row class="flex-box">
                <van-col span="3">
                  <userImage  class="about-me__avatar" :id="userInfo.id" :img_url="userInfo.img_url"  :user_name="userInfo.name"  fontSize="0.24"  width="0.65rem"  height="0.65rem"></userImage>
                </van-col>
                <van-col span="20"><span>{{ arr.package_name }}【{{ arr.level_name }}】的绩效评分和等级，需要你检查确认</span></van-col>
                <van-col span="1"><div class="flex-box" style="align-items: center; height: 100%; color: #b1b1b1;"><van-icon name="arrow" /></div></van-col>
              </van-row>
            </div>
         </template>

         <van-empty description="暂无绩效考核数据" v-if="backlogList.length == 0&&plcList.length==0" />
         <template v-if="backlogList.length > 0">
            <div v-for="(item, index) in backlogList" :key="index">
              <div class="backlog_list_tit">{{ item.time }}</div>
              <div v-for="(arr, keys) in item.list" :key="keys" :style="'z-index:' + (item.list.length - keys)" class="performanceList backlog_list" @click="clickPush(arr)">
                <van-row class="flex-box">
                  <van-col span="3">
                    <userImage  class="about-me__avatar" :id="arr.userInfo.id" :img_url="arr.userInfo.img_url"  :user_name="arr.userInfo.name" fontSize="0.24"  width="0.65rem"  height="0.65rem"></userImage>
                  </van-col>
                  <van-col span="20"><span>{{ arr.content }}</span></van-col>
                  <van-col span="1"><div class="flex-box" style="align-items: center; height: 100%; color: #b1b1b1;"><van-icon name="arrow" /></div></van-col>
                </van-row>
              </div>
            </div>
          </template>
          <div style="height: 0.5rem;"></div>
        </scroller>
        <scroller v-show="type=='entering'" ref="work_bench_scroller2" class="all2" :on-refresh="refresh2" :on-infinite="infinite2" noDataText="我也是有底线的" :list="enteringList">
            <div>
              <div style="height: 0.2rem;background-color: rgb(245, 247, 250);"></div>
              <div v-for="(item, index) in enteringList" :key="index"  class="performanceList backlog_list" @click="clickPush2(item)">
                <van-row class="flex-box">
                  <van-col span="3">
                    <userImage  class="about-me__avatar" :id="item.userInfo.id" :img_url="item.userInfo.img_url"  :user_name="item.userInfo.name" fontSize="0.24"  width="0.65rem"  height="0.65rem"></userImage>
                  </van-col>
                  <van-col span="20">
                    <div><span>{{ item.content }}</span></div>
                    <div class="fontColorC" style="margin-top: 0.1rem;">{{item.update_time}}</div>
                  </van-col>
                  <van-col span="1"><div class="flex-box" style="align-items: center; height: 100%; color: #b1b1b1;"><van-icon name="arrow" /></div></van-col>
                </van-row>
              </div>
              <van-empty description="暂无绩效考核数据" v-if="enteringList.length == 0" />
              <div v-else style="height: 0.5rem;"></div>
            </div>
        </scroller>
      </template>
    </VanSkeleton>

    <van-dialog v-model="selectpfdlg" title="" width="300" :show-confirm-button="false" closeOnClickOverlay>
      <van-radio-group v-model="doSthForData.node_type">
        <van-radio
          v-for="(item, index) in selectpfList"
          :key="index"
          :name="item.value"
          @click="clickpfTime(item)"
          style="margin:.3rem 0 .3rem .4rem;font-size:.3rem"
          icon-size="16px"
        >
          <span style="margin-left:.3rem">{{ item.label }}</span>
        </van-radio>
      </van-radio-group>
    </van-dialog>
  </div>
</template>

<script>
import Vue from 'vue';
import moment from 'moment';
import VanSkeleton from '@/performance/components/public/VanSkeleton';
import {Tab, Tabs, Search} from 'vant'
Vue.use(Tab).use(Tabs).use(Search)
export default {
  data() {
    return {
      skeletonLoad: false, // 过度骨架屏
      backlogList: [],
      commission: 0,
      doSthForData: {
        status: 0, // 周期类型
        page: 1, // 当期页
        page_size: 10, // 一页多少数据
        node_type: 0
      },
      theBackupList: [],
      // 分类相关
      selectpfList: [
        // 选择时间选项
        { label: '全部待办', value: 0 },
        { label: '目标制定', value: 1 },
        { label: '指标确认', value: 2 },
        { label: '结果录入', value: 4 },
        { label: '评分', value: 5 },
        { label: '审批', value: 9 },
        { label: '绩效确认', value: 10 }
      ],
      selectpfdlg: false,
      selectPftiText: '全部待办', // 默认值

      pendingList: [] ,//提供给考核详情上下切换人员列表
      plcList:[],
      userInfo:this.$userInfo(),
      type:'noEntering',
      enteringList:[],
      enteringList2:[],
      page:1,
    };
  },
  components: { VanSkeleton },
  watch:{
    type(val){
      this.page=1;
      this.doSthForData.page=1;
      if(val=='noEntering'){
        setTimeout(() => {
          this.$refs.work_bench_scroller.triggerPullToRefresh();
        }, 50);
      }else{
        setTimeout(() => {
          this.$refs.work_bench_scroller2.triggerPullToRefresh();
        }, 50);
      }
    },
  },
  methods: {
    openAffirm(item){
      this.$router.push({ path: '/affirm', query: { pl_id: item.id,level_name:item.level_name,package_id: item.package_id } });
    },
    clickPush2(arr) {
      let query={};
      if(arr.node_type==4){
        let name=arr.content.split('，')[0];
        query={
          pe_ids:JSON.stringify(arr.pe_ids.concat(arr.done_pe_ids)),
          isUpdate:true,
          Tit:name,
          pendingList: JSON.stringify(this.enteringList2),
          package_id:arr.package_id,
          page:this.doSthForData.page,
          activeName:this.doSthForData.node_type,
        }
      }else{
        query={
          id: arr.remark.packageEmployee_id,
          Tit: arr.remark.package_name,
          pendingList: JSON.stringify(this.enteringList2),
          page:this.doSthForData.page,
          activeName:this.doSthForData.node_type,
        }
      }
      this.$router.push({ name:'performanceDetails',query:query});
    },
    clickPush(arr) {
      let query={};
      if(arr.node_type==4){
        let name=arr.content.split('，')[0];
        query={
          pe_ids:JSON.stringify(arr.pe_ids.concat(arr.done_pe_ids)),
          Tit:name,
          pendingList: JSON.stringify(this.pendingList),
          package_id:arr.package_id,
          page:this.doSthForData.page,
          activeName:this.doSthForData.node_type,
        }
      }else{
        query={
          id: arr.remark.packageEmployee_id,
          Tit: arr.remark.package_name,
          pendingList: JSON.stringify(this.pendingList),
          page:this.doSthForData.page,
          activeName:this.doSthForData.node_type,
        }
      }
      this.$router.push({ name:'performanceDetails',query:query});
    },
    clickpfTime(item) {
      this.selectPftiText = item.label;
      this.doSthForData.page = 1;
      setTimeout(() => {
        this.$refs.work_bench_scroller.triggerPullToRefresh();
      }, 50);
      this.selectpfdlg = false;
    },
    selectPfTIme() {
      this.selectpfdlg = true;
    },
    getPlc() {
      this.$axiosUser('get', '/api/pro/per/package/plc/list', {status: 0, page: 1, page_size: 1000 }).then(res => {
        this.commission =this.commission+res.data.data.total;
        this.plcList= res.data.data.list
      })
    },
    doSthForSb(callback) {
      // 代办数量
      let hasMore = false;
      let params = this.doSthForData;
      this.commission=0;
      this.plcList=[];
      if(params.node_type==0||params.node_type==10){
        this.getPlc()
        if(params.node_type==10){
          this.backlogList=[];
          callback && callback(true);
          return false
        }
      }
      this.$axiosUser('get', '/api/pro/per/package/msg/agency', params,'v2').then(res => {
          if (res.data.code == 1) {
            let list = res.data.data.list;
            let pendingList = [];
            list.forEach(item => {
              if(item.node_type==4){
                let userInfo = this.$getEmployeeMapItem(item.first_employee_id)
                item.userInfo = userInfo;
                let name=item.content.split('，')[0]
                pendingList.push({ name: userInfo.name, employeeID: '结果',pe_ids:item.pe_ids.concat(item.done_pe_ids),package_name: name,package_id:item.package_id });
                return false
              }
              if (item.remark.employee_id) { // 被考核人
                let userInfo = this.$getEmployeeMapItem(item.remark.employee_id)
                item.userInfo = userInfo;
                pendingList.push({ name: userInfo.name, employeeID: item.remark.packageEmployee_id, package_name: item.remark.package_name });
              }
            });
            this.pendingList = pendingList;

            this.commission =this.commission+res.data.data.total;
            if (this.doSthForData.page === 1) {
              this.theBackupList = list;
            } else {
              this.theBackupList = this.theBackupList.concat(list);
            }
            hasMore = list.length !== 10;
            callback && callback(hasMore);
            let data = this.theBackupList; //数据源
            data.forEach(item => {
              // 转换展示的时间格式
              if (/\d{4}/g.exec(item.update_time)[0] == moment().format('YYYY')) {
                item.tineKind = moment(new Date(item.update_time.replace(/-/g, '/')).getTime()).format('MM月DD日');
              } else {
                item.tineKind = moment(new Date(item.update_time.replace(/-/g, '/')).getTime()).format('YYYY年MM月DD日');
              }
            });
            // 转换成可用格式
            let bbt = [];
            data.forEach(item => {
              if (bbt[item.tineKind]) {
                bbt[item.tineKind].list.push(item);
              } else {
                bbt[item.tineKind] = true;
                bbt[item.tineKind] = {
                  time: item.tineKind,
                  list: []
                };
                bbt[item.tineKind].list.push(item);
              }
            });
            let listdata = [];
            for (let i in bbt) {
              listdata.push(bbt[i]);
            }
            this.$nextTick(() => {
              this.backlogList = listdata;
            });
            this.skeletonLoad = false;
          }
      })
    },
    getEnteringList(callback) {
      // 代办数量
      let hasMore = false;
      let params = {
        node_type: 0,
        status: 1,
        page: this.page,
        page_size: 10
      }
      this.$axiosUser('get', '/api/pro/per/package/msg/agency', params,'v2').then(res => {
            let list = res.data.data.list;
            let pendingList=[]
            list.forEach(item => {
              if(item.node_type==4){
                let userInfo = this.$getEmployeeMapItem(item.first_employee_id)
                item.userInfo = userInfo;
                let name=item.content.split('，')[0]
                pendingList.push({ name: userInfo.name, employeeID: '结果',pe_ids:item.pe_ids.concat(item.done_pe_ids),package_name: name,package_id:item.package_id });
                return false
              }
              if (item.remark.employee_id) { // 被考核人
                let userInfo = this.$getEmployeeMapItem(item.remark.employee_id)
                item.userInfo = userInfo;
                pendingList.push({ name: userInfo.name, employeeID: item.remark.packageEmployee_id, package_name: item.remark.package_name });
              }
            });
            this.enteringList2=pendingList
            if (this.page === 1) {
              this.enteringList = list;
            } else {
              this.enteringList = this.enteringList.concat(list);
            }
            hasMore = list.length !== 10;
            callback && callback(hasMore);
      })
    },
    // 上拉刷新
    refresh(done) {
      this.doSthForData.page = 1;
      this.doSthForSb(done);
    },
    // 下拉加载
    infinite(done) {
      this.doSthForData.page++;
      this.doSthForSb(done);
    },
    // 上拉刷新
    refresh2(done) {
      this.page = 1;
      this.getEnteringList(done);
    },
    // 下拉加载
    infinite2(done) {
      this.page++;
      this.getEnteringList(done);
    },
    // 返回上一页
    routerBak() {
      this.$route_back();
    },
  },
};
</script>

<style scoped lang="less">
.all {
  height: calc(100% - 2.64rem) !important;
  position: relative !important;
  background-color: #f5f7fa;
}
.all2{
  height: calc(100% - 1.84rem) !important;
  position: relative !important;
  background-color: #f5f7fa;
}
header {
  padding: 0.2rem;
  background-color: #fff;
  padding-left: 0.2rem;
  font-size: 0.3rem;
  z-index: 1;
}
.backlog_list_tit {
  background-color: #f5f7fa;
  padding: 0.2rem 0 0.2rem 0.2rem;
  font-size: 0.28rem;
}
.backlog_list {
  padding: 0.32rem 0.3rem 0.32rem 0.45rem;

  span {
    font-size: 0.29rem;
  }
}
</style>
