<template>
  <div class="juniorPartner-wrap">
    <!-- Tab栏部分 -->
    <van-tabs
      v-model="active"
      animated
      sticky
      @click="handleTabClick"
    >
      <van-tab :title='`医生 (${doctorListTotal})`'>
        <partnerList
          @emitOnLoad="reachBottom"
          @handleClick="handleClick"
          :loading="loading"
          :finished="finished"
          :list="dataList"
          text="销售"
        ></partnerList>
      </van-tab>

      <van-tab :title='`合伙人 (${partnerListTotal})`'>
        <partnerList
          @emitOnLoad="reachBottom"
          @handleClick="handleClick"
          :loading="loading"
          :finished="finished"
          :list="dataList"
          text="收益"
        ></partnerList>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import Card from '../../../components/common/Card'
import partnerList from '../../../components/partner/partnerList'
import { getSubordinateList, getPartnerInfo } from '../../../api/Partner/Partner'
export default {
  components: {
    Card,
    partnerList
  },
  data() {
    return {
      active: 0,    // tab栏索引
      loading: false,
      finished: false,
      dataList: [],     // 数据列表
      reqParams: {
        type: 1,    // 1：医生  2：合伙人
        pageIndex: 1,
        pageSize: 10,
        name: ""
     },
     doctorListTotal: 0,     // 医生数据总数
     partnerListTotal: 0,    // 合伙人数据总数
    }
  },
  methods: {
    handleClick(AccountId) {
      this.$router.push({name: "SubordinateMyProfitIndex", query: {AccountId}})
    },

    // 页面触底
    reachBottom() {
      this.getList();
    },

    // tab切换时的处理函数
    handleTabClick(index, title) {
      console.log("this.active:" ,this.active)
      this.finished = false;
      this.dataList.length = 0;
      this.reqParams.pageIndex = 1;      
      if (index === 0) {
        // 获取医生数据
        this.reqParams.type = 1;
      } else if (index === 1) {
        // 获取合伙人数据
        this.reqParams.type = 2;
      }
      this.getList();
    },

    // 获取数据列表
    async getList() {
      this.loading = true;
      let response = await getSubordinateList(this.reqParams);
      if(response.data.IsSuccess) {
        const res = response.data;
        if(this.dataList.length < res.Pager.TotalItemCount) {
          this.dataList = [...this.dataList, ...res.ResultObject];
          this.reqParams.pageIndex++;
        }else {
          this.finished = true;
        }
      }else {
        this.$toast(response.data.ResultMsg)
      }
      this.loading = false;
    }
  },
  mounted() {
    this.active = Number(this.$route.query.index);
    if (this.active === 0) {
      // 获取医生信息
      this.reqParams.type = 1;
    } else if (this.active === 1) {
      // 获取合伙人数据
      this.reqParams.type = 2
    } else {
      return;
    }
    this.getList();
    getPartnerInfo()
    .then(res=>{
      this.doctorListTotal = res.data.ResultObject.DoctorCount
      this.partnerListTotal = res.data.ResultObject.PartnerCount
    })
    .catch(err=>{
      console.log(err)
    })
  }
}
</script>

<style lang="less">
#app {
  background-color: #f7f7f7;
}

.van-tab--active {
  color: #478bfb;
}

.van-tabs__line {
  background-color: #478bfb;
}
</style>