// pages/home/used/used.js
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    used_inglist: '',
    keywords: '',
    page: 1,
    num: 4,
    productList: [],
    expState: true,
    provincearray: ['全国','北京', '安徽', "福建", "甘肃", "广东", "广西", "贵州", "海南", "河北", "河南", "黑龙江", "湖北", "湖南", "吉林", "江苏", "江西", "辽宁", "内蒙古", "宁夏", "青海", "山东", "山西", "陕西", "上海", "四川", "天津", "西藏", "新疆", "云南", "浙江", "重庆", "香港", "澳门", "台湾"],
    provincename:"",
    cityname:'',
    dot:[],
    userIsBlack:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.listProducts();
    this.getClassify();
    this.getdot()
  },

  // 获取分类信息
  getClassify: function(e) {
    let that = this;
    let url = "/api/portal/goods/getCate";
    let params = {}
    app.post(url, params).then((res) => {
      that.setData({
        used_inglist: res
      })

      console.log(that.data.used_inglist)
    }).catch((errMsg) => {})
  },
  // 获取列表信息
  listProducts() {
    let that = this;
    var url = '/api/portal/goods/getGoodsList';
    var params = {
      token: wx.getStorageSync('token'),
      page: this.data.page,
      num: this.data.num,
      keyword: this.data.keywords,
      province: this.data.provincename,
      city:this.data.cityname
    }
    app.post(url, params).then((res) => {
        console.log(res)
        that.setData({
          productList: that.data.productList.concat(res.list),
          userIsBlack: res.userIsBlack
        })
      
      
    }).catch((err) => {})
  },
  //我的发布小红点
  getdot(){
    let that = this;
    var url = '/api/portal/goods/getMyReplyTips';
    var params = {
      token: wx.getStorageSync('token'),

    }
    app.post(url, params).then((res) => {
      console.log(res);
      that.setData({
        dot:res
      })



    }).catch((err) => {

    })
  },

  
  // 获取搜索条件
  getKeywords(e) {
    this.setData({
      keywords: e.detail.value,
      page:1
    })
    // this.searchProducts()
    // 条件筛选
    if (e.detail.value == '' || e.detail.value == null) {
      this.data.page = 1;
      this.searchProducts()
    }
  },

  // 点击搜索
  searchProducts: function () {

    this.setData({
      page:1,
      productList:[]
    })

    this.listProducts();
    // this.data.page = 1;
    // let that = this;
    // let url = "/api/portal/goods/getGoodsList";
    // let params = {
    //   page: this.data.page,
    //   num: this.data.num,
    //   keyword: this.data.keywords
    // }
    // app.post(url, params).then((res) => {
    //   if (res == '') {
    //     that.setData({
    //       productList: []
    //     })
    //   } else {
    //     that.setData({
    //       productList: res
    //     })
    //   }
    // })
  },
  // 显示说明弹层
  explTap: function(){
    let that = this;
    that.setData({
      expState: false
    })
  },
  // 关闭弹层
  explClose: function(){
    let that = this;
    that.setData({
      expState: true
    })
  },
  //跳转到 分类页
  usedkind: function(e) {
    console.log(555)
    var used_id = e.currentTarget.dataset.used_id;
    let usedname = e.currentTarget.dataset.usename
    wx.navigateTo({
      url: '../usedkind/usedkind?used_id='+used_id+'&used_name=' + usedname,
    })
  },
  //跳转到 我的发布
  // mypublish: function() {
  //   console.log(666)
  //   wx.navigateTo({
  //     url: '../mypublish/mypublish',
  //   })
  // },
  navgater(e){
    console.log(e)
    if (this.data.userIsBlack == 1 && e.currentTarget.dataset.type == 'sellidel'){
      wx.navigateTo({
        url: '',
      })
    }else{
      let url = '../' + e.currentTarget.dataset.type + '/' + e.currentTarget.dataset.type
      wx.navigateTo({
        url: url,
      })
    }
   
  },
  //跳转到 卖闲置
  // sellidel: function() {
  //   wx.navigateTo({
  //     url: '../sellidel/sellidel',
  //   })
  // },
//商品详情
  skipDetail: function (e) {
    let goods_id = e.currentTarget.dataset.goods_id;
    console.log(goods_id)
    wx.setStorageSync('goods_id', goods_id);
    wx.navigateTo({
      url: '../goodtail/goodtail?goods_id=' + goods_id,
    })

   
  },

  //获取分类
  bindprovinceChange(e) {
    let that=this;
    that.setData({
      page:1,
      productList:[]
    })
    console.log(e)
   let index=e.detail.value
    // console.log('picker发送选择改变,携带值为', e.detail.value)
   
    if (this.data.provincearray[index]=="全国"){
      this.setData({
        provincename: ''
      })
    }else{
      this.setData({
        provincename: that.data.provincearray[index]
      })
    }
    this.listProducts();

    console.log(that.data.provincename)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

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

  },

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

  },

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

  },

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.page++;
    this.listProducts();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
})