const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
const util = require("../../utils/util.js");
const Http = require("../../utils/HttpBasics");
const config = require("../../config/config");
const imgurl = require("../../utils/imgurl");
const {
  alipay
} = require("../../utils/imgurl");
let app = getApp();
Page({
  data: {
    navLineHeight: ((app.statusBarHeight + app.statusBarHeight) + 50) + "px",
    navigationBarHeight,
    noDataFlag: false,
    businePageNum: 1,
    businessData: null,
    Lsit: [],
    mallTenantId: '',
  },
  goOrder() {
    tt.navigateTo({
      url: '/pages/order/index/index',
      success: (res) => {

      },
      fail: (res) => {

      },
    });
  },
  getCouponChannelList(key) {
    if (app.globalData.token) {
      if (this.data.noDataFlag) {
        return
      }
      tt.showLoading({
        title: "加载中"
      });
      let param = {}
      if (key == 0) {
        param = {
          pageNum: this.data.businePageNum,
          pageSize: 6,
          targetAd: 101,
          mallTenantId: this.data.mallTenantId
        }
      } else {
        param = {
          pageNum: this.data.businePageNum,
          pageSize: 6,
          business: key,
          targetAd: 101,
          mallTenantId: this.data.mallTenantId
        }
      }
      Http.get({
        url: config.api.couponChannelDouyinLiveList,
        data: param
      }).then(res => {

        if (res.data.list && res.data.list.length != 0) {
          let tempArr = []
          let tempLsit = this.data.businessData
          let data = res.data.list
          data.map(item => {
            tempArr.push(item.id)

            item.discount = (((item.salePriceStr * 1) / (item.priceStr * 1)) * 10).toFixed(1)
          })
          if (this.data.businePageNum > 1) {
            this.setData({
              businessData: [...tempLsit, ...data]
            })
          } else {
            this.setData({
              businessData: data
            })
          }
          this.getPriceAndStock(tempArr.join())

        } else {
          this.setData({
            noDataFlag: true
          })
        }
        tt.hideLoading();
      }).catch(err => {
        tt.hideLoading();
        tt.showModal({
          title: '提示',
          content: err.message ? err.message : err.data,
          showCancel: false
        })
      })
    } else {
      app.tokenCallback = token => {
        if (this.data.noDataFlag) {
          return
        }
        tt.showLoading({
          title: "加载中"
        });
        let param = {}
        if (key == 0) {
          param = {
            pageNum: this.data.businePageNum,
            pageSize: 6,
            targetAd: 101,
            mallTenantId: this.data.mallTenantId
          }
        } else {
          param = {
            pageNum: this.data.businePageNum,
            pageSize: 6,
            business: key,
            targetAd: 101,
            mallTenantId: this.data.mallTenantId
          }
        }
        Http.get({
          url: config.api.couponChannelDouyinLiveList,
          data: param
        }).then(res => {
          if (res.data.list && res.data.list.length != 0) {
            let tempArr = []
            let tempLsit = this.data.businessData
            let data = res.data.list
            data.map(item => {
              tempArr.push(item.id)
              item.discount = (((item.salePriceStr * 1) / (item.priceStr * 1)) * 10).toFixed(1)
            })
            if (this.data.businePageNum > 1) {
              this.setData({
                businessData: [...tempLsit, ...data]
              })
            } else {
              this.setData({
                businessData: data
              })
            }
            this.getPriceAndStock(tempArr.join())

          } else {
            this.setData({
              noDataFlag: true
            })
          }
          tt.hideLoading();
        }).catch(err => {
          tt.hideLoading();
          tt.showModal({
            title: '提示',
            content: err.message ? err.message : err.data,
            showCancel: false
          })
        })
      }
    }



  },

  upDataLsit() {
    this.setData({
      businePageNum: 1,
      noDataFlag: false,
      businessData: []

    });
    this.getCouponChannelList(0)
  },

  //获取券列表的价格和库存(列表接口有缓存通过这个来获取) str 列表数组里面的所有id 
  getPriceAndStock(str) {

    console.log(str);
    Http.get({
      url: config.api.batchCouponPriceAndStock,
      data: {
        couponChannelIds: str,
        mallTenantId: this.data.mallTenantId
      }
    }).then(res => {
      console.log(JSON.parse(res.data));
      let tempObj = JSON.parse(res.data)
      let tempData = this.data.businessData
      tempData.map(item1 => {
        tempObj.map(item2 => {
          if (item2.id == item1.id) {
            item1.sale = item2.inventory - item2.remainInventory
            item1.percent = ((item1.sale / item2.inventory).toFixed(3)) * 100 + '%'
            item1.salePriceStr = item2.salePrice / 100
            item1.priceStr = item2.price / 100
          }
        })
      })
      this.setData({
        Lsit: tempData
      })

    }).catch(err => {
      tt.hideLoading();
      tt.showModal({
        title: '提示',
        content: err.message ? err.message : err.data,
        showCancel: false
      })
    })
  },
  goDetail(e) {
    console.log(e, 'e');
    let couponChannelId = e.currentTarget.dataset.id
    let couponId = e.currentTarget.dataset.couponid
    let mallTenantId = this.data.mallTenantId
    tt.navigateTo({
      url: `/pages/coupon/detail/index?couponChannelId=${couponChannelId}&couponId=${couponId}&mallTenantId=${mallTenantId}`,
    })
  },
  onLoad: function (options) {
    //固定写法 放在指定要手机号授权框出现
    // app.globalData.ifCongPh = 1
    this.getCouponChannelList()

    tt.setStorageSync('mallTenantId', options.tenantId);
    this.setData({
      mallTenantId: options.tenantId
    })
  },
  onReachBottom: function () {
    console.log("我到底了")
    let that = this;
    that.setData({
      businePageNum: that.data.businePageNum + 1
    });
    console.log("加载更多页数" + that.data.page, );
    console.log("加载更多key" + that.data.code);
    this.getCouponChannelList(0)
  },
})