const util = require('../../../utils/util.js');
const config = require('../../../utils/config')
Page({
  data: {
    imgPath:config.imgPath,
    BB_coin:config.coin_level,
    stat:{},
    initialSwipe:0,
    levelName:'普通',//会员等级
    avatar:wx.getStorageSync("WXuserInfo").avatar,
    defaultLevel:1,
    percent:'25%',
    levelList:{
      activite:[],
      goods:[]
    },
    taskList:{
      recommend_task:[],
      newUser_task:[]
    }
  },
  onLoad: function (options) {
    this.stat();
    this.getActList('goods')
  },
  onShow: function () {
    this.getTaskList()

  },
  jumpToDetail(e){
    let type=e.currentTarget.dataset.type,aid=e.currentTarget.dataset.aid;
    wx.navigateTo({
      url: (type=='act'?'/pages/product/activity/index?aid=':'/pages/product/goods/detail?aid=')+aid,
    })
  },
  // 等级特权选项
  checkOptions(e){
    let index= e.currentTarget.dataset.index,percent='';
    switch (index) {
      case '1':
        percent='25%';
        if (this.data.levelList.goods.length<=0) {
          this.getActList('goods');
        }
        break;
      case '2':
        percent='50%';
        if (this.data.levelList.activite.length<=0) {
          this.getActList('act');
        }
        break;
      case '3':
        percent='75%';
        break;
      case '4':
        percent='100%';
        break;
    }
    this.setData({defaultLevel:index,percent})
  },
  // 获取活动抵扣/商品抵扣
  getActList(type){
    let that=this;
    util.ajax({
      func:type=='act'?"v2/article/recom_list":'ebusiness/list',
      data:{
        useUserCoin:1,
        pageIndex: 1,
        pageSize:type=='act'?3:4
      },
      load:false
    }, function(res) {
      if(res.code == 0){
        let data=type=='act'?res.data.list:res.data.goods;
        data.forEach(el => {
          el.finalPrice=parseInt((Number(el.price)*100-el.maxUserCoin*10)/100) ;
        });
        if (type=='act') {
          that.setData({'levelList.activite':data})
        } else {
          that.setData({'levelList.goods':data})
        }
      }else
        util.showTips(res.reason);
    })
  },
  // 获取宝贝币任务列表
  getTaskList(){
    let that=this;
    util.ajax({
      func:'v2/user_coin/task/list',
      load:false
    }, function(res) {
      if(res.code == 0){
        let arr=[],arr1=[];
        res.data.forEach(el => {
          if (el.type==0) {
            arr.push(el)
          }else{
            arr1.push(el)
          }
        });
        that.setData({'taskList.recommend_task':arr,'taskList.newUser_task':arr1})
        console.log(that.data.taskList)
      }
    })
  },
  // 获取购买信息
  getBuyInfo(){
    let that=this;
    return new Promise(function(resolve, reject) {
      util.ajax({
        func:'v2/user/buy/info',
        load:false
      }, function(res) {
        if(res.code == 0){
          let haveEmpty=true,info=res.data;
            for (const key in info) {
              if (Object.hasOwnProperty.call(info, key) && util.isEmpty(info[key])) {
                haveEmpty=false;
              }
            }
          resolve(haveEmpty)
        }else{
          resolve(false)
        }
      })
    })
  },
  // 获取出行人信息列表
  getChildList(){
    let that=this;
    return new Promise(function(resolve, reject) {
      util.ajax({
        func:'user/idcardinfo_list',
        data:{type:0},
        load:false
      }, function(res) {
        if(res.code == 0 && res.data.length > 0){
          resolve(true)
        }else{
          resolve(false)
        }
      })
    })
  },
  // 任务选择
  checkTask(e){
    console.log(e)
    let code = e.currentTarget.dataset.code,url='',
    taskId= e.currentTarget.dataset.id,
    type=false,that=this;
    switch (code) {
      // 新人任务
      case "saveCardInfo": //出行人信息
        this.getChildList().then(res=>{
          console.log(res)
          if (res) {
            util.finishCoinTask(taskId).then(res=>{
              util.showTips(res);
              that.getTaskList()
            })
          } else {
            url="/pages/account/cards/index?taskId="+taskId+"&defaultType=0&makeTask=true";
            that.urlToPage(type,url)
          }
        })
        break;
      case "saveUserInfo": //购买信息
        this.getBuyInfo().then(res=>{
          console.log(res)
          if (res) {
            util.finishCoinTask(taskId).then(res=>{
              util.showTips(res);
              that.getTaskList()
            })
          } else {
            url="/pages/account/cards/index?taskId="+taskId+"&defaultType=1&makeTask=true";
            that.urlToPage(type,url)
          }
        })
        break;
      case "companyVideo": //品牌视频
        url='/pages/public/swiper/web?url=https://mp.weixin.qq.com/s/E3QNVDwO8PongUk0Rr5w1w&title=微电影';
        that.urlToPage(type,url)
        util.finishCoinTask(taskId).then(res=>{
          // util.showTips(res);
        })
        break;
      case "companyDesc": //品牌介绍
        url='/pages/public/swiper/web?url=https://mp.weixin.qq.com/s/7TqtMO62AtHrLaWM93OhRA&title=公司资质';
        that.urlToPage(type,url)
        util.finishCoinTask(taskId).then(res=>{
          // util.showTips(res);
        })
        break;
      // 推荐任务
      case "buyArticle": //消费活动
        url="/pages/home/index";
        type=true;
        that.urlToPage(type,url)
        break;
      case "share"://海报发圈
        url="/pages/account/monetaryCenter/posterTask"
        that.urlToPage(type,url)
        break;
      case "comment"://评论
        url="/pages/order/index/order?otype=3"
        that.urlToPage(type,url)
        break;
      case "browseArticle"://浏览活动
        url="/pages/home/index";
        type=true;
        let data={};
        data.date=util.getNowFormatDate();
        data.view_act=1;
        console.log(data)
        wx.setStorageSync("task_viewAct", data);
        that.urlToPage(type,url)
        break;
    }
  },
  urlToPage(type,url){
    if (type && !util.isEmpty(url)) {
      wx.switchTab({
        url: url,
      })
    } else {
      wx.navigateTo({
        url: url,
      })
    }
  },
  jumpPage(e){
    let url=e.currentTarget.dataset.url;
    if (url=='/pages/home/index') {
      wx.switchTab({
        url: url,
      })
    }else{
      wx.navigateTo({
        url: url,
      })
    }
  },
  // 获取宝贝币相关参数
  stat(){
    let that = this;
    util.stat().then(res=>{
      that.setData({ stat: res });
      let data=util.getCoinLevel(res.allCoin)
      that.setData({initialSwipe:data.index,levelName:data.levelName})
    })
  },
  onShareAppMessage: function () {

  }
})