// pages/find/mall/mall.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ['Point', 'Sales', 'New'],
    current: 0,
    orderBy: 1,
    jifenlists: '',//积分
    jifen: '',
    showaddress: false,
    region: ['广东省', '广州市', '海珠区'],
    customItem: 'all',
    jf: '',
    img: '',
    title: '',
    name: '',
    phone: '',
    address: '',
    id: '',
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    swipers: '',
  },
  // 搜索框
  bindinput: function (e) {
    var that = this;
    console.log(e.detail.value, "搜索值")
    that.setData({
      searchs: e.detail.value,
    })
  },
  // 跳转到搜索页
  search: function () {
    wx.navigateTo({
      url: '../search/search',
    })
  },
  // 跳转到详情页
  // storedetails:function(e){
  //   var id = e.currentTarget.dataset.id;
  //   var title = e.currentTarget.dataset.title;
  //   wx.navigateTo({
  //     url: '../storedetails/storedetails?id='+id+'&title='+title,
  //   })

  // },
  // 切换
  tabIndex: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var orderBy = that.data.current != index ? 1 : 1 - that.data.orderBy;
    that.setData({
      current: index,
      orderBy: orderBy
    })
    var sort;
    if (index == 0 && orderBy == 0) {
      sort = 'DESC';
    }
    else if (index == 1 && orderBy == 0) {
      sort = 'DESC';
    } else if (index == 1 && orderBy == 1) {
      sort = 'ASC';
    } else if (index == 2 && orderBy == 1) {
      sort = 'ASC';
    } else if (index == 2 && orderBy == 0) {
      sort = 'DESC';
    }
    else if (index == 0 && orderBy == 1) {
      sort = 'ASC';
    }
    else {
      sort = 'DESC'
    }
    console.log(index, "index")
    console.log(orderBy, 'orderbuy');
    wx.request({
      url: app.globalData.hostname + 'Int/get_int_list',//积分商城列表
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        business_id: app.globalData.business_id,
        sort: sort,
        page: 1,
        status: Number(index + 1),
      },
      success: function (res) {
        console.log(res.data.data)
        if (res.data.status == 1) {
          console.log(res.data.data)
          that.setData({
            jifenlists: res.data.data
          })
        }
        console.log(res);
      }
    })
  },

  //换购
  redemption: function (e) {
    var that = this;
    var jf = e.currentTarget.dataset.jf;
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.name;
    var img = e.currentTarget.dataset.img;
    // 判断我的积分是否够
    wx.request({
      url: app.globalData.hostname + 'Int/get_my_int',
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        business_id: app.globalData.business_id,
        openid: wx.getStorageSync('openId'),
      },
      success: function (res) {
        let nowjf = res.data.data;

        if (res.data.status == 1 && Number(nowjf) >= Number(jf)) {
          console.log(res.data)
          that.setData({
            showaddress: true,
            jf: jf,
            img: img,
            title: title,
            id: id,
          })

          // 通讯地址
          wx.chooseAddress({
            success: function (res) {
              var shen = [];
              shen.push(res.provinceName);
              shen.push(res.cityName);
              shen.push(res.countyName);
              that.setData({
                region: shen,
                name: res.userName,
                phone: res.telNumber,
                address: res.detailInfo,
              })
              console.log(res)
            }
          })


          // 确认下单

        } else {
          wx.showModal({
            title: 'Tips',
            content: 'Lack of integral',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      }
    })
  },
  // 点击遮罩层隐藏
  showplay: function () {
    console.log("1111111111111")
    var that = this;
    that.setData({
      showaddress: false
    })
  },
  // 地址
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  // 姓名
  name: function (e) {
    var that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    that.setData({
      name: e.detail.value
    })
  },
  // 电话
  phone: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      phone: e.detail.value
    })
  },
  // 详情地址
  address: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      address: e.detail.value
    })
  },
  // 提交按钮
  order_tj: function () {
    var that = this;
    if (that.data.name.length == 0) {
      wx.showToast({
        title: 'Enter your name',
        duration: 1000,
        image: '../../images/icons/warn.png'
      })
    } else if (!(((/^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/).test(that.data.phone)) || ((/^1[3|4|5|7|8][0-9]{9}$/).test(that.data.phone)))) {
      console.log(that.data.phone)
      wx.showToast({
        title: 'Enter your phone',
        duration: 1000,
        image: '../../images/icons/warn.png'
      })
    }
    else if (that.data.region.length == 0) {
      wx.showToast({
        title: 'Please select area',
        duration: 1000,
        image: '../../images/icons/warn.png'
      })
    } else if (that.data.address.length == 0) {
      wx.showToast({
        title: 'Enter your address',
        duration: 1000,
        image: '../../images/icons/warn.png'
      })
    } else {
      // 提交接口
      // 参数：openid, admin_id, username(用户名0), phone(联系电话），address（收货地址），goods_num(商品数量)
      wx.request({
        url: app.globalData.hostname + 'Int/exchange',
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        data: {
          openid: wx.getStorageSync('openId'),
          business_id: app.globalData.business_id,
          int_goods_id: that.data.id,
          // goods_num: 1,
          name: that.data.name,
          mobile: that.data.phone,
          address: that.data.region,
        },
        success: function (res) {
          if (res.data.status == 1) {
            console.log(res.data)
            wx.showToast({
              title: 'Exchange success',
              duration: 1000
            })
            that.setData({
              showaddress: false
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取积分商城列表
    var that = this;
    var u_openid = options.u_openid;
    app.getUserInfo(function (cb, userInfo) {
      that.setData({
        userInfo: userInfo,
      })
    }, u_openid)
    wx.showLoading({
      title: 'loading',
    })
    wx.request({
      url: app.globalData.hostname + '/Int/get_int_list',//
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        business_id: app.globalData.business_id,
        sort: 1,
        page: 1,
        status: 1,
        sort: 'ASC'

      },
      success: function (res) {
        console.log(res.data.data)
        if (res.data.status == 1) {
          console.log(res.data.data)
          that.setData({
            jifenlists: res.data.data
          })
        }
        console.log(res);
      }
    })
    //获取我的轮播图
    wx.request({
      url: app.globalData.hostname + '/Int/chart_list',
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        business_id: app.globalData.business_id,
      },
      success: function (res) {
        console.log(res.data.data)
        that.setData({
          swipers: res.data.data,
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function (res) {
    var userid = wx.getStorageSync('openId')
    // var userid = 1

    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: app.globalData.share_title,
      path: '/pages/index/index?u_openid=' + userid,
      success: function (res) {
        var pages = getCurrentPages()    //获取加载的页面
        var currentPage = pages[pages.length - 1]    //获取当前页面的对象
        //点击分享传进userid 截取userid 从onload拿到
        var url = currentPage.route + '?u_openid=' + userid;  //当前页面url
        console.log(url, '/pages/index/index?u_openid=' + userid);
      },
      fail: function (res) {
        // 转发失败
      },
    }
  },
})