<template>
<view id="brand-list">
    <view class="banner" v-if="brand && brand.brand_img" @tap="jumpBrandDetail">
        <image mode="widthFix" :src="brand.brand_img"></image>
    </view>
    <view class="hot-sale" v-if="commend_goods && commend_goods.length">
        <view class="title mx-1px-bottom">明星热卖</view>
        <view class="ul-content">
            <view class="li-item" v-for="(item, index) in commend_goods" :key="index" :data-id="item.id" @tap="jump" >
                <view class="img-box">
                    <image mode="widthFix" :src="item.img"></image>
                </view>
                <view class="name">
                    {{item.name}}
                </view>
                <view class="money">
                    新品促销价 ￥{{item.sell_price}}
                </view>
                <view class="origin-sale mx-1px-bottom">
                    原价 ￥{{item.market_price}}
                </view>
                <view class="buy-btn">
                    <span class="iconfont icon-gouwuche"></span>
                    立即购买
                </view>
            </view>
        </view>
    </view>
    <view class="all-products" v-if="list && list[0] && list[0].length">
        <view class="title mx-1px-bottom">所有产品</view>
        <view class="ul-content">
            <block v-for="(items, idx) in list" :key="idx"  >
                <view class="li-item" v-for="(item, index) in items" :key="index" :data-id="item.id" @tap="jump" >
                    <view class="detail-info mx-1px-bottom">
                        <view class="img-box">
                            <image :src="item.img"></image>
                        </view>
                        <view class="goods-right">
                            <view class="name">
                                {{item.name}}
                            </view>
                            <view class="money">新品促销价 ￥{{item.sell_price}}</view>
                            <view class="origin-sale">原价 ￥{{item.market_price}}</view>
                        </view>
                    </view>
                    <view class="buy-btn">
                        <span class="iconfont icon-gouwuche"></span>
                        立即购买
                    </view>
                </view>
            </block>

        </view>
    </view>

</view>
</template>
<script>
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';

export default {
  data() {
    return {
      list: '',
      commend_goods: [],
      id: '',
      more: true,
      page: 1,
      brand: ''
    };
  },

  //下拉刷新
  onReachBottom() {
    if (this.more) {
      let page = this.page + 1;
      this.getBrandGoodsList(this.id, page);
    } else {
      wx.showToast({
        image: '../../../static/error.png',
        title: '再拉也没有啦'
      });
    }
  },

  onLoad(e) {
    if (e.id) {
      this.setData({
        id: e.id
      });
      this.getBrandGoodsList(e.id);
    }
  },

  components: {},
  props: {},
  methods: {
    jump(e) {
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/store/detail/detail?id=' + id
      });
    },

    jumpBrandDetail() {
      wx.navigateTo({
        url: '/pages/brand/detail/detail?id=' + this.brand.id
      });
    },

    getBrandGoodsList(id, page = 1) {
      wx.showLoading({
        title: "加载中",
        mask: true
      });
      this.$http.get({
        api: 'api/brand/goods/list/' + id,
        data: {
          page: page
        }
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;

          if (res.status) {
            var pages = res.meta.pagination;
            var current_page = pages.current_page;
            var total_pages = pages.total_pages;
            this.setData({
              //[`list[${page - 1}]`]: res.data,
              more: current_page < total_pages,
              page: current_page,
              commend_goods: res.meta.commend_goods,
              brand: res.meta.brand
            });
			
			this.$set(this.list, page -1, res.data);
			
            wx.setNavigationBarTitle({
              title: res.meta.brand.name
            });
          } else {
            wx.showModal({
              content: res.message || '请求失败',
              showCancel: false
            });
          }
        } else {
          wx.showModal({
            content: res.message || '请求失败',
            showCancel: false
          });
        }

        this.setData({
          show: false
        });
        wx.hideLoading();
      }).catch(rej => {
        wx.hideLoading();
        wx.showModal({
          content: res.message || '请求失败',
          showCancel: false
        });
      });
    },

    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 "brandList";
</style>