// pages/sign/sign.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 科目类型
    typeAll: [],
    typeList: [],
    typeIdx: 0,
    typeId: '',
    // 报名工种
    signTypeAll: [],
    signTyleList: [],
    signTypeIdx: 0,
    signTypeId: '',
    orgList: []
  },
  // 获取科目类型
  getTypeList: function () {
    var self = this
    app.restAjax.get(app.restAjax.path(app.apis.getTrainTypeList, [app.baseUrls.testUrl]), {
      workTypeParentId: 0
    }, {
      headers: {
        token: wx.getStorageSync('token')
      }
    },
    function (code, data) {
      var result = ['请选择科目类型']
      for (let i = 0; i < data.length; i++) {
        result.push(data[i].workTypeName)
      }
      self.setData({
        typeList: result,
        typeAll: data,
        typeId: data[0].workTypeId
      })
    },
    function (code, err) {
      console.log(err)
    })
  },
  // 获取报名工种
  getSignTypeList: function () {
    var self = this
    app.restAjax.get(app.restAjax.path(app.apis.getTrainTypeList, [app.baseUrls.testUrl]), {
      workTypeParentId: self.data.typeId
    }, {
      headers: {
        token: wx.getStorageSync('token')
      }
    },
    function (code, data) {
      var result = []
      for (let i = 0; i < data.length; i++) {
        result.push(data[i].workTypeName)
      }
      self.setData({
        signTyleList: result,
        signTypeAll: data,
        signTypeId: data[0].workTypeId
      })
      self.getOrgList()
    },
    function (code, err) {
      console.log(err)
    })
  },
  // 选择科目类型
  bindPickerChange: function (e) {
    var self = this
    if (e.detail.value != '0') {
      var id = self.data.typeAll[e.detail.value - 1].workTypeId
      self.setData({
        typeIdx: e.detail.value,
        typeId: id,
        signTypeIdx: 0
      })
      self.getSignTypeList()
    } else {
      self.setData({
        typeIdx: e.detail.value,
        typeId: '',
        signTypeIdx: 0,
        signTyleList: [],
        orgList: []
      })
    }
  },
  // 选择报名工种
  bindSignPickerChange: function (e) {
    var self = this
    var curId = self.data.signTypeAll[e.detail.value].workTypeId
    self.setData({
      signTypeIdx: e.detail.value,
      signTypeId: curId
    })
    self.getOrgList()
  },
  // 获取培训机构列表
  getOrgList: function (id) {
    var self = this
    app.restAjax.get(app.restAjax.path(app.apis.getOrgList, [app.baseUrls.testUrl]), {
      workTypeId: self.data.signTypeId
    }, {
      headers: {
        token: wx.getStorageSync('token')
      }
    },
    function (code, data) {
      self.setData({
        orgList: data
      })
    },
    function (code, err) {
      console.log(err)
    })
  },
  doSign: function (e) {
    wx.navigateTo({
      url: '../signOrg/signOrg?applyWorkTypeId=' + this.data.signTypeId + '&applyInstitutionId=' + e.currentTarget.dataset.org,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTypeList()
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})