<template>
<!--新数据循环-->
<view id="micro-pages">
    <!--<view>
        <customNavbar navbar-data="{{nvabarData}}"></customNavbar>
    </view>-->
    <block v-if="microData && microData.pages && microData.pages.length">
        <view v-for="(item, index) in microData.pages" :key="index" >
            <view v-if="item.name == 'micro_page_componet_search'">
                <!--搜索按钮-->
                <view class="banner">
                    <view class="search">
                        <div class="menu-box" @tap="jumpMenu">
                            <i class="iconfont icon-fenzu">

                            </i>
                        </div>
                        <view class="search_box">
                            <input type="text" value @input="search" disabled confirm-type="search" @tap="jumpToSearch"></input>
                            <icon type="search" size="20"></icon>

                        </view>
                    </view>
                </view>
            </view>
            <!--轮播图组件-->
            <view v-if="item.name == 'micro_page_componet_slide' && item.value && item.value.length">
                <indexBanner :banner-data="item.value" :banner-title="item.title" :screen-width="screenWidth" :show="item.is_show_title" :meta="item.meta"></indexBanner>
            </view>
            <!--优惠券组件-->
            <view v-if="item.name == 'micro_page_componet_coupon' && item.value && item.value.length">
                <indexCoupon :index-coupon="item.value" :config="config" bindmyGetCoupon="getCoupon" :show="item.is_show_title" :meta="item.meta"></indexCoupon>
            </view>
            <!--套餐-->
            <view v-if="item.name == 'micro_page_componet_suit' && item.value && item.value.length">
                <indexSuit :suit-data="item.value" :index-data="indexData" :show="item.is_show_title" :meta="item.meta"></indexSuit>
            </view>
            <!--大咖推荐-->
            <view v-if="item.name == 'micro_page_componet_article_-' && item.value && item.value.length">
                <bigCoffee :coffee-data="item.value" :coffee-title="item.title" :show="item.is_show_title" :meta="item.meta"></bigCoffee>
            </view>
            <!--专属方案-->
            <view v-if="item.name == 'micro_page_componet_article_|' && item.value && item.value.length">
                <exclusivePlan :plan-data="item.value" :plan-title="item.title" :show="item.is_show_title" :meta="item.meta"></exclusivePlan>
            </view>
            <!--健康美丽Tips-->
            <view v-if="item.name == 'micro_page_componet_goods_group_change' && item.value && item.value.length">
                <healthTips :health-data="item.value" :title="item.title" :show="item.is_show_title" :meta="item.meta"></healthTips>
            </view>
            <!--猜你喜欢-->
            <view v-if="item.name == 'micro_page_componet_guess_like' && item.value && item.value.length">
                <waterfallsFlow :water-data="item.value" :title="item.title" :show="item.is_show_title" :meta="item.meta"></waterfallsFlow>
            </view>
            <!--拼团-->
            <view v-if="item.name == 'micro_page_componet_groupon' && item.value && item.value.length">
                <indexGroupon :index-data="item.value" :config="config" :server="microData.server_time" typename="拼团" :meta="item.meta"></indexGroupon>
            </view>
            <!--秒杀-->
            <view v-if="item.name == 'micro_page_componet_seckill' && item.value && item.value.length">
                <indexSeckill :index-data="item.value" :config="config" :server="microData.server_time" :big-index="index" bind:startevent="isStarts" bind:endevent="isEnd" :meta="item.meta"></indexSeckill>
            </view>

            <!--打call indexCall-->
            <view v-if="item.name == 'micro_page_componet_free_event' && item.value && item.value.length">
                <indexCall :call-data="item.value" :config="config" :is-login="isLogin" :server="microData.server_time" :meta="item.meta"></indexCall>
            </view>
            <!--快捷导航-->
            <view v-if="item.name == 'micro_page_componet_nav' && item.value && item.value.length">
                <indexNav :nav-data="item.value" :meta="item.meta"></indexNav>
            </view>
            <!--图片魔方-->
            <view v-if="item.name == 'micro_page_componet_cube' && item.value && item.value.length">
                <indexCube :cube-data="item.value" :show-type="item.type" :cube-title="item.title" :show="item.is_show_title" :meta="item.meta"></indexCube>
            </view>
            <!--分类商品-->
            <view v-if="item.name == 'micro_page_componet_category' && item.value && item.value.length">
                <indexCategory :category-data="item.value" :meta="item.meta"></indexCategory>
            </view>
            <!--商品分组-->
            <view v-if="item.name == 'micro_page_componet_goods_group' && item.value && item.value.length">
                <indexGrouping :grouping-data="item.value" :meta="item.meta"></indexGrouping>
            </view>
        </view>
    </block>
</view>
</template>
<script>
/**
 * Created by admin on 2017/9/26.
 */
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';
const app = getApp();

import preview from "@/components/preview/preview";
import seckill from "@/components/seckill/seckill";
import group from "@/components/group/group";
import indexBanner from '@/components/index-banner/index-banner';
import indexCube from '@/components/index-cube/index-cube';
import indexCoupon from '@/components/index-coupon/index-coupon';
import indexSuit from '@/components/index-suit/index-suit';
import bigCoffee from '@/components/big-coffee/big-coffee';
import exclusivePlan from "@/components/exclusive-plan/exclusive-plan";
import healthTips from "@/components/health-tips/health-tips";
import waterfallsFlow from "@/components/waterfalls-flow/waterfalls-flow";
import indexGroupon from "@/components/index-groupon/index-groupon";
import indexSeckill from "@/components/index-seckill/index-seckill";
import indexCall from "@/components/index-call/index-call";
import indexNav from "@/components/index-nav/index-nav";
import indexCategory from "@/components/index-category/index-category";
import indexGrouping from "@/components/index-grouping/index-grouping";


export default {
  data() {
    return {
      banner: [],
      indexData: {},
      goods_arr: [],
      topImage: [],
      bestSalesGoods: [],
      suit: [],
      imgHeight: '',
      screenWidth: 0,
      currentDesc: 'news',
      is_newGiftLogin: false,
      isLogin: '',
      noLoginGift: '',
      // 未登录状态下的新人礼信息
      loginGift: '',
      // 登录状态下的新人礼信息
      showNoGift: false,
      // 用户是否关闭弹窗
      showGift: false,
      // 登录状态下是否弹窗
      tapIndex: 0,
      isShow: false,
      goodsIndex: 0,
      goodsList: [],
      author: '',
      scroll: true,
      config: '',
      wyBanner: '',
      wyGoodsList: '',
      microData: '',
      // 组件所需的参数
      nvabarData: {
        showCapsule: 1,
        //是否显示左上角图标   1表示显示    0表示不显示
        title: '我的主页' //导航栏 中间的标题

      },
      id: '',
      // 此页面 页面内容距最顶部的距离
      height: app.globalData.height * 2 + 20,
	  margin_bottom:''
    };
  },

  onShow(e) {
    this.setData({
      isLogin: !!this.$cookieStorage.get('user_token')
    }); // 万有的接口
    // this.getStoreData();
    // !!this.$cookieStorage.get('user_token')
    // console.log(app.globalData.giftLogin)

    this.setData({
      is_newGiftLogin: app.globalData.giftLogin
    });
    let is_info = this.$cookieStorage.get('gift_info');

    if (!this.isLogin) {
      this.newPeopleGift();
    }

    if (this.isLogin && !is_info) {
      this.getPeopleGift();
    }
  },

  onLoad(e) {
    console.log(this.height); // 第三方平台配置颜色

    var gbConfig = this.$cookieStorage.get('globalConfig') || '';
    // this.setData({
    //   config: gbConfig,
    //   id: e.id,
    //   name: e.name || ''
    // });
	
	  this.config=gbConfig;
	  this.id=e.id;
	  this.name=e.name || '';

    if (!gbConfig) {
      let extConfig = {};

      if (extConfig) {
        this.setData({
          config: extConfig
        });
      }
    }

    wx.showLoading();
    this.queryMicroData(e.id, e.name || '');
    wx.getSystemInfo({
      success: res => {
        this.setData({
          screenWidth: res.screenWidth
        });
      }
    });
    this.init(e);
  },

  onPullDownRefresh(e) {
    wx.showLoading();
    this.queryData(this.id);
  },

  onShareAppMessage(res) {
    var info = this.$cookieStorage.get('init_info');
    return {
      title: info.title,
      path: `/${this.route}?id=${this.id}&name=${this.name}`,
      imageUrl: info.imgUrl
    };
  },

  components: {
    preview,
    seckill,
    group,
    indexBanner,
    indexCoupon,
    indexSuit,
    indexGroupon,
    indexSeckill,
    indexCall,
    indexNav,
    indexCategory,
    indexCube,
    indexGrouping,
    bigCoffee,
    healthTips,
    exclusivePlan,
    waterfallsFlow
  },
  methods: {
	  shareapp(){
	  		  var info = this.$cookieStorage.get('init_info');
				var title = info.title;
				var id = this.id;
				var name = this.name;
	  		  uni.getProvider({
	  			  service:'share',
	  			  success:function(res){
	  				  uni.share({
	  				       provider: "weixin",
	  				       scene: "WXSceneSession",
	  				       type: 5,
	  				       title:title,
	  				       imageUrl:'https://cdn.guojiang.club/guojiang_logo.png',
	  				  			  miniProgram:{
	  				  				  id:'gh_72e65196278a',
	  				  				  path:`pages/index/microPages/microPages?id=${id}&name=${name}`,
	  				  				  type:0,
	  				  				  webUrl: 'https://guojiang.club'
	  				  			  },
	  				       success: function (res) {
	  				           console.log("success:" + JSON.stringify(res));
	  				       },
	  				       fail: function (err) {
	  				           console.log("fail:" + JSON.stringify(err));
	  				       }
	  				   });
	  			  }
	  		  })
	  		  
	  		 
	  },
    // 获取初始化数据
    init(e) {
      this.$http.get({
        api: 'api/system/init'
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;

          if (res.status) {
            if (res.data && res.data.other_technical_support) {
              this.setData({
                author: res.data.other_technical_support
              });
            }
            /*wx.setNavigationBarTitle({
             title: res.data.mini_home_title
             })*/


            this.$cookieStorage.set('init_info', res.data.h5_share);
            this.$cookieStorage.set('service_info', res.data.online_service_data);
            this.$cookieStorage.set('distribution_valid_time', res.data.distribution_valid_time);
            this.$cookieStorage.set('init', res.data);
            this.setCode(e);
          } else {
            this.setCode(e);
          }
        } else {
          this.setCode(e);
        }
      });
    },

    setCode(e) {
      const timeMap = {
        y: 31536000000,
        m: 2592000000,
        d: 86400000,
        h: 3600000,
        n: 60000,
        s: 1000
      }; // 默认有效期为7天

      var valid_time = "";
      var clerk_id = e.clerk_id || "";
      var shop_id = e.shop_id || "";
      var agent_code = e.agent_code || '';

      if (e.scene) {
        var scene = decodeURIComponent(e.scene);
        var sceneArr = scene.split(',');

        if (sceneArr.length > 0) {
          agent_code = sceneArr[0];
        }
      }

      var cook_shop_id = this.$cookieStorage.get('shop_id');

      if (!this.$cookieStorage.get('distribution_valid_time')) {
        valid_time = 10080;
      } else {
        valid_time = this.$cookieStorage.get('distribution_valid_time');
      }

      console.log('这个是时间', valid_time);
      let timeStamp = new Date().getTime();
      timeStamp += timeMap.n * valid_time; // 当url上shop_id与缓存中shop_id不一致时，需要清除clerk_id。以此保证shop_id与clerk_id对应

      var cook_clerk_id = this.$cookieStorage.get('clerk_id');

      if (cook_shop_id != shop_id && cook_clerk_id) {
        this.$cookieStorage.clear('clerk_id');
      }

      if (agent_code) {
        this.$cookieStorage.set('agent_code', agent_code, valid_time + 'n'); // 如果有agent_code就将这次进入的时间缓存

        this.$cookieStorage.set('agent_code_time', timeStamp, valid_time + 'n'); // 如果有agent_code并且有coupon_agent_code就将coupon_agent_code清除，保证agent_code为第一位

        if (this.$cookieStorage.get('coupon_agent_code')) {
          this.$cookieStorage.clear('coupon_agent_code');
        }
      }

      if (clerk_id) {
        this.$cookieStorage.set('clerk_id', clerk_id, valid_time + 'n');
      }

      if (shop_id) {
        this.$cookieStorage.set('shop_id', shop_id, valid_time + 'n'); // 如果有shop_id就将这次进入的时间缓存

        this.$cookieStorage.set('shop_id_time', timeStamp, valid_time + 'n');
      }

      const code = agent_code || this.$cookieStorage.get('agent_code');

      if (code) {// this.getCodeUser(code);
      }
    },

    // 根据agent_code获取用户信息
    getCodeUser(agent_code) {
      this.$http.get({
        api: '/api/distribution/getAgentInfo',
        data: {
          agent_code: agent_code
        }
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;

          if (res.status) {
            const title = res.data.name + ' X ' + res.data.home_title;
            wx.setNavigationBarTitle({
              title: title
            });
          }
        }
      });
    },

    jumpAuthor() {
      wx.navigateTo({
        url: '/pages/index/author/author'
      });
    },

    imgLoad(e) {
      var height = e.detail.height;
      var width = e.detail.width;
      var ratio = width / height;
      var screenWidth = this.screenWidth;
      this.setData({
        imgHeight: screenWidth / ratio
      });
    },

    changeItem(e) {
      var index = e.currentTarget.dataset.index;
      if (index == this.currentDesc) return;
      this.setData({
        goods_arr: this.indexData.Mini_H5TabData[index],
        tapIndex: index
      });
    },

    changeGoodsItem(e) {
      var index = e.currentTarget.dataset.index;
      if (index == this.currentDesc) return;
      this.setData({
        goodsIndex: index
      });
    },

    closeGift() {
      this.$cookieStorage.set('gift_info', true);
      this.setData({
        showGift: false
      });
    },

    showModal() {
      this.setData({
        showNoGift: true
      });
    },

    showPreview(e) {
      var items = this.indexData.H5GoodsRecommend[this.goodsIndex].items;
      var index = e.currentTarget.dataset.index;
      this.setData({
        goodsList: items[index].items,
        isShow: true
      });
    },

    jumpMeal(e) {
      // console.log(e);
      var id = e.currentTarget.dataset.suitid;
      wx.navigateTo({
        url: `/pages/store/meal/meal?id=${id}`
      });
    },

    // 关闭弹窗
    isClone() {
      this.setData({
        isShow: false
      });
    },

    // 新人进店有礼
    newPeopleGift() {
      this.$http.get({
        api: 'api/home/gift_new_user'
      }).then(res => {
        res = res.data;

        if (res.status && res.data) {
          var cache_no_gift = this.$cookieStorage.get('new_gift');
          console.log(res.data);
          res.data.gift.forEach(function (val) {
            val.coupon.usestart_at = val.coupon.usestart_at.replace(/\s.+$/, '');
            val.coupon.useend_at = val.coupon.useend_at.replace(/\s.+$/, '');
          });
          this.setData({
            noLoginGift: res.data
          });

          if (!cache_no_gift) {
            this.setData({
              showNoGift: true
            });
          }
        } // else{
        //     wx.showModal({
        //         title: '提示',
        //         content: '请求失败',
        //         success: res=>{
        //             if (res.confirm) {
        //
        //             }
        //         }
        //     })
        // }

      });
    },

    // 新人进店有礼(已登录)
    getPeopleGift() {
      this.$http.post({
        api: 'api/home/gift_new_user',
        header: {
          Authorization: this.$cookieStorage.get('user_token')
        }
      }).then(res => {
        res = res.data;
        var cache_info = this.$cookieStorage.get('gift_info');

        if (res.status && res.data && !cache_info) {
          // 判断是不是老用户
          if (!res.data.activity.is_new_user) {
            console.log(res.data);

            if (this.is_newGiftLogin) {
              res.data.activity.gift.forEach(function (val) {
                val.coupon.usestart_at = val.coupon.usestart_at.replace(/\s.+$/, '');
                val.coupon.useend_at = val.coupon.useend_at.replace(/\s.+$/, '');
              });
              this.setData({
                showGift: true,
                loginGift: res.data
              });
            } else {
              this.closeGift();
            }
          } else {
            res.data.activity.gift.forEach(function (val) {
              val.coupon.usestart_at = val.coupon.usestart_at.replace(/\s.+$/, '');
              val.coupon.useend_at = val.coupon.useend_at.replace(/\s.+$/, '');
            });
            this.setData({
              showGift: true,
              loginGift: res.data
            });
          }
        } else {}
      });
    },

    // 秒杀开始
    isStarts(e) {
      var idx = e.detail.idx;
      var index = e.detail.index;

      if (this.microData.pages[idx].value[index].associate.init_status != 1) {
        this.setData({
          [`microData.pages[${idx}].value[${index}].associate.init_status`]: 1
        });
      }
    },

    // 秒杀结束
    isEnd(e) {
      var idx = e.detail.idx;
      var index = e.detail.index;
      var newData = this.microData;
      newData.pages[idx].value.splice(index, 1);
      this.setData({
        microData: newData
      });
    },

    login() {
      pageLogin(getUrl());
      /*wx.redirectTo({
       url: '/pages/user/register/register?url='+getUrl()+encodeURIComponent('?giftLogin=true')
       })*/
    },

    close() {
      this.setData({
        showNoGift: false
      }); // this.showNoGift = false;

      var time = new Date(new Date().setHours(0, 0, 0, 0)).getTime() + 86400000;
      this.$cookieStorage.set('new_gift', true, time);
    },

    jumpToSearch() {
      wx.navigateTo({
        url: '/pages/store/search/search'
      });
    },

    jumpToDetail(e) {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: `/pages/store/detail/detail?id=${id}`
      });
    },

    jumpCall(e) {
      if (this.isLogin) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/pages/store/call/call?id=' + id
        });
      } else {
        wx.showModal({
          content: '请先登录',
          success: res => {
            if (res.confirm || !res.cancel && !res.confirm) {
              pageLogin(getUrl());
            }
          }
        });
      }
    },

    jumpImg(e) {
      /*wx.scanCode({
       success: res => {
       console.log(res);
       }
       })
       return*/
      var src = e.currentTarget.dataset.src;
      if (!src || src == 'uto_miniprogram') return;
      wx.navigateTo({
        url: src
      });
    },

    //跳到搜索页面
    jumpSearch() {
      wx.navigateTo({
        url: '/pages/store/search/search'
      });
    },

    jumpMenu(e) {
      if (config.PACKAGES.isTab) {
        wx.switchTab({
          url: '/pages/index/classification/classification'
        });
      } else {
        wx.navigateTo({
          url: '/pages/index/classify/classify'
        });
      }
    },

    jumpStore(e) {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/store/detail/detail?id=' + id
      });
    },

    //请求新数据
    queryMicroData(id, name = '') {
      this.$http.get({
        api: 'api/micro/page/' + id,
        data: {
          name: name
        }
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;

          if (res.status) {
            this.setData({
              microData: res.data
            });

            if (res.data && res.data.micro_page) {
              wx.setNavigationBarTitle({
                title: res.data.micro_page.name == 'brand' ? '品牌馆' : res.data.micro_page.name
              });
            }
          } else {
            wx.showModal({
              content: res.message || '请求失败',
              showCancel: false
            });
          }
        } else {
          wx.showModal({
            content: '请求失败',
            showCancel: false
          });
        }

        wx.stopPullDownRefresh();
        wx.hideLoading();
      }).catch(() => {
        wx.stopPullDownRefresh();
        wx.hideLoading();
        wx.showModal({
          content: '请求失败',
          showCancel: false
        });
      });
    },

    go(e) {
      var link = e.currentTarget.dataset.link;

      if (link) {
        if (link.indexOf('c_id') != -1) {
          wx.navigateTo({
            url: '/pages/store/list/list?' + link
          });
        } else if (link.indexOf('id') != -1) {
          wx.navigateTo({
            url: '/pages/store/detail/detail?' + link
          });
        }
      }
    },

    // 领取优惠券
    getCoupon(e) {
      var token = this.$cookieStorage.get('user_token');
      var code = e.detail.code;
      var index = e.detail.index;

      if (token) {
        this.convertCoupon(code, index);
      } else {
        wx.showModal({
          content: '请先登录',
          showCancel: false,
          success: res => {
            if (res.confirm || !res.cancel && !res.confirm) {
              pageLogin(getUrl());
            }
          }
        });
      }
    },

    // 领取优惠券接口
    convertCoupon(code, index) {
      var token = this.$cookieStorage.get('user_token');
      wx.showLoading({
        title: "正在领取",
        mask: true
      });
      this.$http.post({
        api: 'api/coupon/convert',
        header: {
          Authorization: token
        },
        data: {
          coupon_code: code
        }
      }).then(res => {
        wx.hideLoading();

        if (res.statusCode == 200) {
          res = res.data;

          if (res.status) {
            var pages = this.microData.pages;
            pages.forEach((val, idx) => {
              if (val.name == 'micro_page_componet_coupon') {
                this.setData({
                  [`microData.pages[${idx}].value[${index}].associate.has_get`]: true
                });
              }
            });
            wx.showToast({
              title: '领取成功'
            });
          } else {
            wx.showToast({
              title: res.message || '领取失败',
              image: '../../../static/error.png'
            });
          }
        } else {
          wx.showToast({
            title: '领取失败',
            image: '../../../static/error.png'
          });
        }
      }).catch(rej => {
        wx.showToast({
          title: '领取失败',
          image: '../../../static/error.png'
        });
        wx.hideLoading();
      });
    },

    jumpList(e) {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/coupon/goods/goods?id=' + id
      });
    },

    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  },
  computed: {},
  watch: {}
};
</script>
<style rel="stylesheet/less" lang="less">
    @import "microPages";
</style>