// pages/ordermain/ordermain.js
var Jsonutil = require('../../utils/JSONUtil.js')
var BASEURL = require('../../utils/wxconstant.js')
var baseUrl = BASEURL.BaseUrl()
var baseutils = require('../../utils/baseUtils.js')
var netUtil = require('../../utils/network.js')
var isAddType = true //默认是加分申请规则

Page({
  /**
   * 页面的初始数据
   */
  data: {

    navLeftItems: [],
    navRightItems: [],
    curName: '',
    curIndex: 0,

    NormData:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.getcategoryId
    isAddType = options.getAddType
    this.getNormByCategoryId(id)

    console.log('isAddType', isAddType)
  },

  getNormByCategoryId: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 rightnorm = []
        if (isAddType == 'true'){
          rightnorm = res.data.data[0].typedata.addnorm
        }else{
          rightnorm = res.data.data[0].typedata.minus
        }
        console.log('rightnorm=', rightnorm)
        that.setData({
          NormData: res.data.data,
          curName: res.data.data[0].typeName,
          navRightItems: rightnorm
        })
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })
  },
  switchRightTab:function(e){
    var that = this
    let itemname = e.target.dataset.itemname
    var typedata = e.target.dataset.typedata
    var rightnorm = new Array
    if (isAddType == 'true') {
      rightnorm = typedata.addnorm
    } else {
      rightnorm = typedata.minus
    }
    that.setData({
      curName:itemname,
      navRightItems: rightnorm
    })
    console.log('typedata', typedata)
    console.log('itemname', itemname)
    console.log('rightnorm', rightnorm)
  },
  itemClick:function(e){
    var that = this
    let item = e.target.dataset.item

    var pages = getCurrentPages()
    var prevPage = pages[pages.length - 1]  //当前界面
    var prevPage = pages[pages.length - 2]  //上一个页面

    prevPage.setData({
      itemNorm: item,
    })
    console.log('item', item)

    wx.navigateBack({
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.setData({
      wxavatarUrl: wx.getStorageSync('wxavatarUrl'),
      wxnickName: wx.getStorageSync('wxnickName')
    })

  }
})