<template>
  <div class="container">
    <van-loading size="80px"  color="#1989fa" class="ld" v-if="overlayShow">正在处理...</van-loading>
    <van-overlay :show="overlayShow" />
    <van-radio-group v-model="radio" class="nobr"  @change="handleStoreChange">
      <van-cell-group >
        <van-cell   v-for="(item,index) in list" :key="index"   >
          <div class="list">
            <div class="left">
              <img :src="item.logo" alt="" />
            </div>
            <div class="right">
              <h3>{{ item.name }}</h3>
              <p>{{ item.summaryTitle }}</p>
            </div>
          </div>
          <van-radio slot="right-icon" :name="item.oyStallCode" />
        </van-cell>
      </van-cell-group>
    </van-radio-group>
  </div>
</template>

<script>
import { getStoreList,getMemberListByCode } from "@/api/sidebar/voucher"
import axios from "axios";
import { log } from 'util';
export default {
  data() {
    return {
      radio: '',
      userId: '',
      list: [],
      overlayShow: false,
      oyStallCode: ''
    }
  },
  created() {  
    if(this.$route.query.userId !== '') {
        this.userId = this.$route.query.userId
      }else {
        this.userId = sessionStorage.getItem('userId')
      }
    this.radio = sessionStorage.getItem('oyStallCode')
    // this.radio = 111
    this.getList()  
    // this.userId = '10000190'
  },
  methods: {
    getList() {               
      let params = {
        user_id: this.userId
      }
      getStoreList(params).then(res => {
        console.log(res.data)    
        this.list = res.data        
      })
    },
    ///
    // 获取店铺的所有店员
    getAllNewFans(oyStallCode) {
      let params = {
        oyStallCode: oyStallCode
      }
        getMemberListByCode(params).then(res => {          
          this.getAllInfo(res.data)
        }).catch(err => {
          console.log(err);          
        })      
    },
     // 获取个人粉丝
    getFans() {
      let headerData = {
        agentId: "1000033",
        corpId: "ww4df265003b43fa0d"
      }
      return new Promise((resolve, reject) => {
        axios({
          url:
            "http://139.155.48.151:8085/workWx/auth/contact/listExternalContacts?userId=" + this.userId,
            method: "get",
            headers: headerData
           })
          .then(res => {            
            if(res.data.data != null) {
              sessionStorage.setItem("personalFan", res.data.data.length)
              resolve(res.data.data.length)
            }else {
              console.log('zheli');              
              if(res.errorCode == "84061") {
                sessionStorage.setItem("personalFan", 0)
                resolve(0)
              }else {
                sessionStorage.setItem("personalFan", '')
                resolve('')
              }
              
            }         
          })
          .catch(err => {
            reject(err)
          })
      })
      
    },
    // 获取店铺所有粉丝
    getAllFans() {
      let headerData = {
        agentId: "1000033",
        corpId: "ww4df265003b43fa0d"
      }    
      return new Promise((resolve, reject) => {
        axios({
          url: "http://139.155.48.151:8085/workWx/auth/contact/getCustomerInfoByOyStallCode?oyStallCode=" + this.oyStallCode,
          method: "get",
          headers: headerData
        })
        .then(res => {          
          let newList = []
          res.data.data.forEach(item => {
            if(item != null) {
              newList.push(item)
            }
          })
          sessionStorage.setItem("allFans", newList.length)
          resolve(newList.length)
        })
        .catch(err => {
          reject(err)
        })
      })
      
    },   
    getAddFans(flag, userId) {
      let JsonStr = {}
      if (flag == 1) {
        JsonStr.userid = [userId];
      } else {                  
        JsonStr.userid = userId
      }
      let timeNow = Math.round(new Date() / 1000);
      JsonStr.start_time = timeNow - 24 * 60 * 60;
      JsonStr.end_time = timeNow;
      return new Promise((resolve, reject) => {
        axios({
        url:
          "http://139.155.48.151:8085/workWx/auth/contact/getUserBehaviorData",
        method: "post",
        headers: {
          "Content-Type": "application/json",
          agentId: "1000033",
          corpId: "ww4df265003b43fa0d"
        },
        data: JsonStr
      }).then(res => {
          let data = JSON.parse(res.data.data);
          if (flag == 1) {
            // 获取个人新增粉丝
            console.log(data,'个人新增粉丝')
            if(data != null) {
              let newFans = data.behavior_data[0].new_contact_cnt + data.behavior_data[1].new_contact_cnt;
              sessionStorage.setItem("newFans", newFans)
              resolve(newFans)
            }else {
              sessionStorage.setItem("newFans", '')
              resolve('')
            }                       
          } else {
            console.log(data,'全部新增粉丝');   
            if(data != null) {
              let allNewFans = data.behavior_data[0].new_contact_cnt + data.behavior_data[1].new_contact_cnt;
              sessionStorage.setItem("allNewFans", allNewFans);
              resolve(allNewFans)
            }else {
              sessionStorage.setItem("allNewFans", '')
              resolve('')
            }                 
          }
        })
        .catch(err => {
          reject(err)
        })
      })
      
    },
    getAllInfo(list) {
      console.log('getAllInfo');     
      Promise.all([this.getFans(), this.getAllFans(),this.getAddFans(1,this.userId),this.getAddFans(2,list)]).then(res => {
        console.log('进来了');        
        console.log(res,'promise修改');
        this.overlayShow = false  
        this.$toast("切换成功")
      })
    },
    ///
    handleStoreChange(val) {
      this.overlayShow = true
      console.log(val,'修改后的oyStallCode')   
      this.oyStallCode = val  
      sessionStorage.setItem('oyStallCode',val)    
        let obj = this.list.find(function (obj) {
          return obj.oyStallCode == val
        })   
        sessionStorage.setItem("barName", obj.name)
        this.getAllNewFans(val)
      }
  }
}
</script>

<style scoped>
.ld >>> .van-loading__text {
  color: #fff;
}
.ld {
  z-index: 1000;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);  
}
.container {
  height: auto;
  background-color: #fff;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}
.title {
  height: 44px;
  background: rgba(248, 248, 248, 1);
  line-height: 44px;
  padding: 0 16px;
  font-size: 12px;
  color: rgba(45, 71, 106, 1);
}
.fg {
  padding: 0 16px;
  font-size: 12px;
  color: rgba(45, 71, 106, 1);
  height: 44px;
  line-height: 44px;
}
.icon {
  font-weight: 800;
}
.active {
  background-color: #fff;
  padding: 12px 12px 0 12px;
  box-shadow: 0px 2px 4px 0px rgba(221, 221, 221, 1);
}
.list {
  height: 94px;
  width: 100%;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid rgba(238, 238, 238, 1);
  margin-bottom: 14px;
}
.list3 {
  border-bottom: none;
  margin-bottom: 0;
}
.left,
.right {
  height: 80px;
}
.left {
  width: 80px;
  background: rgba(248, 248, 248, 1);
}
.left img {
  width: 100%;
  height: 80px;
}
.right {
  width: 76%;
  padding: 10px;
  background-color: #fff;
}
h3 {
  font-size: 14px;
  font-weight: bold;
  color: rgba(45, 71, 106, 1);
}
p {
  margin-top: 10px;
  font-size: 12px;
  color: rgba(45, 71, 106, 0.8);
}
.nobr >>> [class*=van-hairline]::after {
  display: none;
}
.nobr >>> .van-cell:not(:last-child)::after {
  display: none;
}
</style>
