// pages/pointRank/pointRank.js
var BASEURL = require('../../utils/wxconstant.js')
var baseUrl = BASEURL.BaseUrl()
var baseutils = require('../../utils/baseUtils.js')
var netUtil = require('../../utils/network.js')
var pageSize = 1

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lastTypeId:'',
    firstType:'请选择标准',
    secondType:'请选择规范',
    pointData:[],
    CotegoryArray: [],
    Cotegory: [],
    CotegoryArray2:[],
    Cotegory2:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    // that.getRankData()
    // return
    that.getCotegory()//获得分类列表
  },
  getCotegory: function () {
    var that = this
    var data = { parentId: 0 }
    var url = baseUrl + BASEURL.getUrl.getCotegory
    netUtil.requestLoading(url, data, '正在加载数据...', 'GET', function (res) {
      if (res.data.success && res.data.data) {
        that.setData({
          Cotegory: res.data.data
        })
        var array = []
        for (var i = 0; i < res.data.data.length; i++) {
          array[i] = res.data.data[i].categoryName
          if (i == res.data.data.length - 1) {
            console.log('array', array)
            that.setData({
              CotegoryArray: array
            })
          }
        }
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })
  },

  bindPickerChange: function (e) {
    var that = this
    console.log('picker发送选择改变，携带值为', e.detail)
    var categoryId = that.data.Cotegory[e.detail.value].categoryId
    var categoryName = that.data.Cotegory[e.detail.value].categoryName
    if (categoryName && categoryId) {
      that.setData({
        firstType: categoryName,
        CotegoryArray2: [],
        Cotegory2: [],
        secondType:'请选择规范',
        lastTypeId: categoryId,
        pointData: [],
      })
      that.getSecondType(categoryId)
      that.getRankData(categoryId)
    }
  },
  bindPickerChange2: function (e) {
    var that = this
    console.log('picker发送选择改变，携带值为', e.detail)
    var lastTypeId
    var secondTypeItem
    if (that.data.Cotegory2[e.detail.value].typedata.addnorm){
      secondTypeItem = that.data.Cotegory2[e.detail.value].typedata.addnorm[0]
    } else if (that.data.Cotegory2[e.detail.value].typedata.minus){
      secondTypeItem = that.data.Cotegory2[e.detail.value].typedata.minus[0]
    }else {
      return
    }
    lastTypeId = secondTypeItem.categoryId
    var categoryName = that.data.Cotegory[e.detail.value].categoryName
    if (categoryName && lastTypeId) {
      that.setData({
        lastTypeId: lastTypeId,
        secondType: categoryName,
        pointData: [],
      })
      that.getRankData(lastTypeId)
    }
  },


  getSecondType: function (id){
    var that = this
    var data = { parentId: id }
    var url = baseUrl + BASEURL.getUrl.getNormByCategoryId
    netUtil.requestLoading(url, data, '正在加载数据...', 'GET', function (res) {
      // console.log('res='+JSON.stringify(res))
      if (res.data.success && res.data.data) {
       
        var array = []
        for (var i = 0; i < res.data.data.length; i++) {
          array[i] = res.data.data[i].typeName
          if (i == res.data.data.length - 1) {
            console.log('array', array)
            that.setData({
              CotegoryArray2: array,
              Cotegory2:res.data.data
            })
          }
        }

      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })
  },

  getRankData:function(id){
    var that = this
    var data = { categoryId: id, pageCount: 20, pageSize: pageSize}
    var url = baseUrl + BASEURL.getUrl.getByNormRank
    netUtil.requestLoading(url, data, '正在加载数据...', 'GET', function (res) {
      if (res.data.success && res.data.data) {
        that.setData({
          pointData: res.data.data
        })
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})