// pages/stock/goodstype/goodstype.js
let util = require('../../../utils/util.js');
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: util.imgurl,
    isshowadd: false,
    addTypeName: '',
    fatherID: '0',
    ftypes: [],
    ftypess: 0,
    isedite: false,
    ptypes: [],
    ptypess: 0
  },
  settype() {
    if (this.data.ptypess) {
      let pages = getCurrentPages()
      let prevPage = pages[pages.length - 2]
      var goodtypename = ''
      var goodtypeonlyid = ''
      for (var i in this.data.ptypes) {
        if (this.data.ptypes[i].id === this.data.ptypess) {
          goodtypename = this.data.ptypes[i].name
          goodtypeonlyid = this.data.ptypes[i].onlyID
        }
      }
      prevPage.setData({
        goodtypeid: this.data.ptypess,
        goodtypename: goodtypename,
        goodtypeonlyid: goodtypeonlyid
      })
      wx.navigateBack({
        delta: 1
      })
    } else {
      util.showToast('请选择商品分类')
    }
  },
  hideedite() {
    this.setData({
      isedite: false
    })
  },
  setedite() {
    this.setData({
      isedite: true
    })
  },
  getptypes(e) {
    var id = e.currentTarget.dataset.id
    this.setData({
      ftypess: id,
      ptypess: 0
    })
    this.getPTypeData()
  },
  setsel(e) {
    this.setData({
      ptypess: e.currentTarget.dataset.id,
      ptypessonlyid: e.currentTarget.dataset.onlyid
    })
  },
  getFTypeData() {
    util.query('goodscategory/wxapi/listpage', {
      categoryLevel: 1,
      pageNo: 1,
      pageSize: 9999
    }, function(res) {
      if (res.code === 10000) {
        that.setData({
          ftypes: res.data.content
        })
        if (that.data.ftypess === 0 && res.data.content[0]) {
          that.setData({
            ftypess: res.data.content[0].id
          })
        }
        that.getPTypeData()
      } else {
        util.showToast('获取一级分类失败')
      }
    })
  },
  getPTypeData() {
    util.query('goodscategory/wxapi/listpage', {
      categoryLevel: 2,
      fatherId: this.data.ftypess
    }, function(res) {
      if (res.code === 10000) {
        that.setData({
          ptypes: res.data.content
        })
      } else {
        that.setData({
          ptypes: []
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    this.getFTypeData()
    //this.showAddF()
  },
  del(e) {
    wx.showModal({
      title: '确认',
      content: '是否确定删除该分类',
      success(res) {
        if (res.confirm) {
          util.query("goodscategory/wxapi/delete", {
            id: e.currentTarget.dataset.id
          }, function(res) {
            if (res.code == 10000) {
              if (that.data.ftypess === e.currentTarget.dataset.id) {
                that.setData({
                  ftypess: 0
                })
              }
              util.showToast('删除成功')
              that.getFTypeData()
            } else {
              util.showToast(res.message)
            }
          }, "POST")
        } else if (res.cancel) {

        }
      }
    })
  },
  showAddF() {
    this.setData({
      isshowadd: true,
      addTypeName: '',
      fatherID: '0'
    })
  },
  showAdd(e) {
    this.setData({
      isshowadd: true,
      addTypeName: '',
      fatherID: this.data.ftypess
    })
  },
  hideAdd() {
    this.setData({
      isshowadd: false,
      addTypeName: '',
      fatherID: '0'
    })
  },
  setname(e) {
    this.setData({
      addTypeName: e.detail.value
    })
  },
  savetype() {
    if (!this.data.addTypeName) {
      util.showToast('请填写分类名称')
      return
    }
    util.query('goodscategory/wxapi/create', {
      name: this.data.addTypeName,
      fatherID: this.data.fatherID
    }, function(res) {
      if (res.code === 10000) {
        util.showToast('保存成功')
        that.hideAdd()
        if (that.data.fatherID === '0') {
          that.getFTypeData()
        } else {
          that.getPTypeData()
        }
      } else {
        util.showToast('保存失败')
      }
    }, 'POST')
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})